From edfbb2384f80e00fdca3343f1f7fa478d0853a8a Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 5 Dec 2016 19:03:27 +0000 Subject: [PATCH] Components can now be supplied meta data --- .../Component/AddComponentCommand.php | 26 +++++++--- .../Component/UpdateComponentCommand.php | 12 ++++- .../Component/AddComponentCommandHandler.php | 1 + .../UpdateComponentCommandHandler.php | 1 + .../Incident/ReportIncidentCommandHandler.php | 1 + .../Controllers/Api/ComponentController.php | 6 ++- .../Dashboard/ComponentController.php | 6 ++- app/Models/Component.php | 3 ++ ...5045_AlterTableComponentsAddMetaColumn.php | 41 +++++++++++++++ tests/Api/ComponentTest.php | 50 +++++++++++++++++++ .../Component/AddComponentCommandTest.php | 4 +- .../Component/UpdateComponentCommandTest.php | 4 +- 12 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 database/migrations/2016_12_05_185045_AlterTableComponentsAddMetaColumn.php diff --git a/app/Bus/Commands/Component/AddComponentCommand.php b/app/Bus/Commands/Component/AddComponentCommand.php index b564300aa9cb..1332fe4a657a 100644 --- a/app/Bus/Commands/Component/AddComponentCommand.php +++ b/app/Bus/Commands/Component/AddComponentCommand.php @@ -62,6 +62,13 @@ final class AddComponentCommand */ public $enabled; + /** + * JSON meta data for the component. + * + * @var string|null + */ + public $meta; + /** * The validation rules. * @@ -75,22 +82,24 @@ final class AddComponentCommand 'order' => 'nullable|int', 'group_id' => 'nullable|int', 'enabled' => 'nullable|bool', + 'meta' => 'nullable|string', ]; /** * Create a new add component command instance. * - * @param string $name - * @param string $description - * @param int $status - * @param string $link - * @param int $order - * @param int $group_id - * @param bool $enabled + * @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 $meta * * @return void */ - public function __construct($name, $description, $status, $link, $order, $group_id, $enabled) + public function __construct($name, $description, $status, $link, $order, $group_id, $enabled, $meta) { $this->name = $name; $this->description = $description; @@ -99,5 +108,6 @@ public function __construct($name, $description, $status, $link, $order, $group_ $this->order = $order; $this->group_id = $group_id; $this->enabled = $enabled; + $this->meta = $meta; } } diff --git a/app/Bus/Commands/Component/UpdateComponentCommand.php b/app/Bus/Commands/Component/UpdateComponentCommand.php index d01458668851..7905d7cd1de0 100644 --- a/app/Bus/Commands/Component/UpdateComponentCommand.php +++ b/app/Bus/Commands/Component/UpdateComponentCommand.php @@ -71,6 +71,13 @@ final class UpdateComponentCommand */ public $enabled; + /** + * JSON meta data for the component. + * + * @var string|null + */ + public $meta; + /** * The validation rules. * @@ -84,6 +91,7 @@ final class UpdateComponentCommand 'order' => 'nullable|int', 'group_id' => 'nullable|int', 'enabled' => 'nullable|bool', + 'meta' => 'nullable|string', ]; /** @@ -97,10 +105,11 @@ final class UpdateComponentCommand * @param int $order * @param int $group_id * @param bool $enabled + * @param string|null $meta * * @return void */ - public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled) + public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled, $meta) { $this->component = $component; $this->name = $name; @@ -110,5 +119,6 @@ public function __construct(Component $component, $name, $description, $status, $this->order = $order; $this->group_id = $group_id; $this->enabled = $enabled; + $this->meta = $meta; } } diff --git a/app/Bus/Handlers/Commands/Component/AddComponentCommandHandler.php b/app/Bus/Handlers/Commands/Component/AddComponentCommandHandler.php index 538d6391912e..ffe506104ef2 100644 --- a/app/Bus/Handlers/Commands/Component/AddComponentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Component/AddComponentCommandHandler.php @@ -50,6 +50,7 @@ protected function filter(AddComponentCommand $command) 'enabled' => $command->enabled, 'order' => $command->order, 'group_id' => $command->group_id, + 'meta' => $command->meta, ]; return array_filter($params, function ($val) { diff --git a/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php index cf45d8bc5ebf..d7789f754c76 100644 --- a/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -56,6 +56,7 @@ protected function filter(UpdateComponentCommand $command) 'enabled' => $command->enabled, 'order' => $command->order, 'group_id' => $command->group_id, + 'meta' => $command->meta, ]; return array_filter($params, function ($val) { diff --git a/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php index f612343f681d..0e46f78dcab1 100644 --- a/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -100,6 +100,7 @@ public function handle(ReportIncidentCommand $command) null, null, null, + null, null )); } diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 35c43a8ad13e..2692965fcbcd 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -77,7 +77,8 @@ public function postComponents() Binput::get('link'), Binput::get('order'), Binput::get('group_id'), - (bool) Binput::get('enabled', true) + (bool) Binput::get('enabled', true), + Binput::get('meta', null) )); } catch (QueryException $e) { throw new BadRequestHttpException(); @@ -118,7 +119,8 @@ public function putComponent(Component $component) Binput::get('link'), Binput::get('order'), Binput::get('group_id'), - (bool) Binput::get('enabled', true) + (bool) Binput::get('enabled', true), + Binput::get('meta', null) )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index c8bc20fd25db..d5554fdf3a3e 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -134,7 +134,8 @@ public function updateComponentAction(Component $component) $componentData['link'], $componentData['order'], $componentData['group_id'], - $componentData['enabled'] + $componentData['enabled'], + null // Meta data cannot be supplied through the dashboard yet. )); } catch (ValidationException $e) { return cachet_redirect('dashboard.components.edit', [$component->id]) @@ -187,7 +188,8 @@ public function createComponentAction() $componentData['link'], $componentData['order'], $componentData['group_id'], - $componentData['enabled'] + $componentData['enabled'], + null // Meta data cannot be supplied through the dashboard yet. )); } catch (ValidationException $e) { return cachet_redirect('dashboard.components.create') diff --git a/app/Models/Component.php b/app/Models/Component.php index d488f9c324fc..783d426d70b5 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -35,6 +35,7 @@ class Component extends Model implements HasPresenter 'description' => '', 'link' => '', 'enabled' => true, + 'meta' => null, ]; /** @@ -50,6 +51,7 @@ class Component extends Model implements HasPresenter 'link' => 'string', 'group_id' => 'int', 'enabled' => 'bool', + 'meta' => 'json', 'deleted_at' => 'date', ]; @@ -67,6 +69,7 @@ class Component extends Model implements HasPresenter 'order', 'group_id', 'enabled', + 'meta', ]; /** diff --git a/database/migrations/2016_12_05_185045_AlterTableComponentsAddMetaColumn.php b/database/migrations/2016_12_05_185045_AlterTableComponentsAddMetaColumn.php new file mode 100644 index 000000000000..fdfe6974e5f5 --- /dev/null +++ b/database/migrations/2016_12_05_185045_AlterTableComponentsAddMetaColumn.php @@ -0,0 +1,41 @@ +longText('meta')->nullable()->default(null)->after('enabled'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('components', function (Blueprint $table) { + $table->dropColumn('meta'); + }); + } +} diff --git a/tests/Api/ComponentTest.php b/tests/Api/ComponentTest.php index b03b70256897..e7ae01c55619 100644 --- a/tests/Api/ComponentTest.php +++ b/tests/Api/ComponentTest.php @@ -84,6 +84,31 @@ public function testPostComponentWithoutEnabledField() $this->assertResponseOk(); } + public function testPostComponentWithMetaData() + { + $this->beUser(); + + $this->post('/api/v1/components', [ + 'name' => 'Foo', + 'description' => 'Bar', + 'status' => 1, + 'link' => 'http://example.com', + 'order' => 1, + 'group_id' => 1, + 'enabled' => true, + 'meta' => [ + 'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0', + ], + ]); + + $this->seeJson([ + 'meta' => [ + 'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0', + ], + ]); + $this->assertResponseOk(); + } + public function testPostDisabledComponent() { $this->beUser(); @@ -122,6 +147,31 @@ public function testPutComponent() $this->assertResponseOk(); } + public function testPutComponentWithMetaData() + { + $this->beUser(); + $component = factory('CachetHQ\Cachet\Models\Component')->create([ + 'meta' => [ + 'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0', + ], + ]); + + $this->put('/api/v1/components/1', [ + 'meta' => [ + 'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0', + 'foo' => 'bar', + ], + ]); + + $this->seeJson([ + 'meta' => [ + 'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0', + 'foo' => 'bar', + ], + ]); + $this->assertResponseOk(); + } + public function testDeleteComponent() { $this->beUser(); diff --git a/tests/Bus/Commands/Component/AddComponentCommandTest.php b/tests/Bus/Commands/Component/AddComponentCommandTest.php index 9adb22bdc5c5..851b96885115 100644 --- a/tests/Bus/Commands/Component/AddComponentCommandTest.php +++ b/tests/Bus/Commands/Component/AddComponentCommandTest.php @@ -36,6 +36,7 @@ protected function getObjectAndParams() 'order' => 0, 'group_id' => 0, 'enabled' => true, + 'meta' => null, ]; $object = new AddComponentCommand( $params['name'], @@ -44,7 +45,8 @@ protected function getObjectAndParams() $params['link'], $params['order'], $params['group_id'], - $params['enabled'] + $params['enabled'], + $params['meta'] ); return compact('params', 'object'); diff --git a/tests/Bus/Commands/Component/UpdateComponentCommandTest.php b/tests/Bus/Commands/Component/UpdateComponentCommandTest.php index edcb432c3e18..9d7bd3ad126f 100644 --- a/tests/Bus/Commands/Component/UpdateComponentCommandTest.php +++ b/tests/Bus/Commands/Component/UpdateComponentCommandTest.php @@ -38,6 +38,7 @@ protected function getObjectAndParams() 'order' => 0, 'group_id' => 0, 'enabled' => true, + 'meta' => null, ]; $object = new UpdateComponentCommand( @@ -48,7 +49,8 @@ protected function getObjectAndParams() $params['link'], $params['order'], $params['group_id'], - $params['enabled'] + $params['enabled'], + $params['meta'] ); return compact('params', 'object');