Merge pull request #3098 from CachetHQ/test-new-component-status
Test for status changes before firing event
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -50,7 +50,9 @@ class UpdateComponentCommandHandler
|
||||
$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));
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ class ComponentController extends AbstractApiController
|
||||
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)
|
||||
));
|
||||
|
||||
@@ -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;
|
||||
@@ -176,6 +177,43 @@ class ComponentTest extends AbstractApiTestCase
|
||||
$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();
|
||||
|
||||
Reference in New Issue
Block a user