Skip to content

Commit

Permalink
Merge pull request #3098 from CachetHQ/test-new-component-status
Browse files Browse the repository at this point in the history
Test for status changes before firing event
  • Loading branch information
jbrooksuk committed Jun 17, 2018
2 parents 648af9f + 9accf90 commit bb87b42
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
32 changes: 16 additions & 16 deletions app/Bus/Commands/Component/UpdateComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,49 @@ final class UpdateComponentCommand
/**
* The component name.
*
* @var string
* @var string|null
*/
public $name;

/**
* The component description.
*
* @var string
* @var string|null
*/
public $description;

/**
* The component status.
*
* @var int
* @var int|null
*/
public $status;

/**
* The component link.
*
* @var string
* @var string|null
*/
public $link;

/**
* The component order.
*
* @var int
* @var int|null
*/
public $order;

/**
* The component group.
*
* @var int
* @var int|null
*/
public $group_id;

/**
* Is the component enabled?
*
* @var bool
* @var bool|null
*/
public $enabled;

Expand Down Expand Up @@ -106,24 +106,24 @@ final class UpdateComponentCommand
* Create a new update component command instance.
*
* @param \CachetHQ\Cachet\Models\Component $component
* @param string $name
* @param string $description
* @param int $status
* @param string $link
* @param int $order
* @param int $group_id
* @param bool $enabled
* @param string|null $name
* @param string|null $description
* @param int|null $status
* @param string|null $link
* @param int|null $order
* @param int|null $group_id
* @param bool|null $enabled
* @param array|null $meta
* @param bool $silent
*
* @return void
*/
public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled, $meta, $silent)
public function __construct(Component $component, $name = null, $description = null, $status = null, $link = null, $order = null, $group_id = null, $enabled = null, $meta = null, $silent = null)
{
$this->component = $component;
$this->name = $name;
$this->description = $description;
$this->status = (int) $status;
$this->status = $status;
$this->link = $link;
$this->order = $order;
$this->group_id = $group_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function handle(UpdateComponentCommand $command)
$component = $command->component;
$originalStatus = $component->status;

event(new ComponentStatusWasChangedEvent($this->auth->user(), $component, $originalStatus, $command->status, $command->silent));
if ($command->status && (int) $originalStatus !== (int) $command->status) {
event(new ComponentStatusWasChangedEvent($this->auth->user(), $component, $originalStatus, $command->status, $command->silent));
}

$component->update($this->filter($command));

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/ComponentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function update(Component $component)
Binput::get('link'),
Binput::get('order'),
Binput::get('group_id'),
(bool) Binput::get('enabled', true),
(bool) Binput::get('enabled'),
Binput::get('meta', null),
(bool) Binput::get('silent', false)
));
Expand Down
38 changes: 38 additions & 0 deletions tests/Api/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace CachetHQ\Tests\Cachet\Api;

use CachetHQ\Cachet\Bus\Events\Component\ComponentStatusWasChangedEvent;
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasCreatedEvent;
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasRemovedEvent;
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasUpdatedEvent;
Expand Down Expand Up @@ -176,6 +177,43 @@ public function test_can_update_component()
$response->assertJsonFragment(['name' => 'Foo']);
}

public function test_can_update_component_without_status_change()
{
$this->beUser();
$component = factory(Component::class)->create();

$this->expectsEvents(ComponentWasUpdatedEvent::class);
$this->doesntExpectEvents(ComponentStatusWasChangedEvent::class);

$response = $this->json('PUT', '/api/v1/components/1', [
'name' => 'Foo',
]);

$response->assertStatus(200);
$response->assertJsonFragment(['name' => 'Foo']);
}

public function test_can_update_component_with_status_change()
{
$this->beUser();
$component = factory(Component::class)->create([
'status' => 1,
]);

$this->expectsEvents([
ComponentWasUpdatedEvent::class,
ComponentStatusWasChangedEvent::class,
]);

$response = $this->json('PUT', '/api/v1/components/1', [
'name' => 'Foo',
'status' => 2,
]);

$response->assertStatus(200);
$response->assertJsonFragment(['name' => 'Foo', 'status' => 2]);
}

public function test_can_update_component_with_meta_data()
{
$this->beUser();
Expand Down

0 comments on commit bb87b42

Please sign in to comment.