217 lines
6.1 KiB
PHP
217 lines
6.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Cachet.
|
|
*
|
|
* (c) Alt Three Services Limited
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace CachetHQ\Tests\Cachet\Api;
|
|
|
|
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasCreatedEvent;
|
|
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasRemovedEvent;
|
|
use CachetHQ\Cachet\Bus\Events\Component\ComponentWasUpdatedEvent;
|
|
use CachetHQ\Cachet\Models\Component;
|
|
|
|
/**
|
|
* This is the component test class.
|
|
*
|
|
* @author James Brooks <james@alt-three.com>
|
|
* @author Graham Campbell <graham@alt-three.com>
|
|
*/
|
|
class ComponentTest extends AbstractApiTestCase
|
|
{
|
|
public function test_can_get_all_components()
|
|
{
|
|
$components = factory(Component::class, 3)->create();
|
|
|
|
$response = $this->json('GET', '/api/v1/components');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment(['id' => $components[0]->id]);
|
|
$response->assertJsonFragment(['id' => $components[1]->id]);
|
|
$response->assertJsonFragment(['id' => $components[2]->id]);
|
|
}
|
|
|
|
public function test_cannot_get_invalid_component()
|
|
{
|
|
$response = $this->json('GET', '/api/v1/components/1');
|
|
|
|
$response->assertStatus(404);
|
|
}
|
|
|
|
public function test_cannot_create_component_without_authorization()
|
|
{
|
|
$this->doesntExpectEvents(ComponentWasCreatedEvent::class);
|
|
|
|
$response = $this->json('POST', '/api/v1/components');
|
|
|
|
$response->assertStatus(401);
|
|
}
|
|
|
|
public function test_cannot_create_component_without_data()
|
|
{
|
|
$this->beUser();
|
|
|
|
$this->doesntExpectEvents(ComponentWasCreatedEvent::class);
|
|
|
|
$response = $this->json('POST', '/api/v1/components');
|
|
|
|
$response->assertStatus(400);
|
|
}
|
|
|
|
public function test_can_create_component()
|
|
{
|
|
$this->beUser();
|
|
|
|
$this->expectsEvents(ComponentWasCreatedEvent::class);
|
|
|
|
$response = $this->json('POST', '/api/v1/components', [
|
|
'name' => 'Foo',
|
|
'description' => 'Bar',
|
|
'status' => 1,
|
|
'link' => 'http://example.com',
|
|
'order' => 1,
|
|
'group_id' => 1,
|
|
'enabled' => true,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment(['name' => 'Foo']);
|
|
}
|
|
|
|
public function test_can_create_component_without_enabled_field()
|
|
{
|
|
$this->beUser();
|
|
|
|
$this->expectsEvents(ComponentWasCreatedEvent::class);
|
|
|
|
$response = $this->json('POST', '/api/v1/components', [
|
|
'name' => 'Foo',
|
|
'description' => 'Bar',
|
|
'status' => 1,
|
|
'link' => 'http://example.com',
|
|
'order' => 1,
|
|
'group_id' => 1,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment(['name' => 'Foo', 'enabled' => true]);
|
|
}
|
|
|
|
public function test_can_create_component_with_meta_data()
|
|
{
|
|
$this->beUser();
|
|
|
|
$this->expectsEvents(ComponentWasCreatedEvent::class);
|
|
|
|
$response = $this->json('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',
|
|
],
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment([
|
|
'name' => 'Foo',
|
|
'status' => 1,
|
|
'meta' => [
|
|
'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0',
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function test_can_create_disabled_component()
|
|
{
|
|
$this->beUser();
|
|
|
|
$this->expectsEvents(ComponentWasCreatedEvent::class);
|
|
|
|
$response = $this->json('POST', '/api/v1/components', [
|
|
'name' => 'Foo',
|
|
'description' => 'Bar',
|
|
'status' => 1,
|
|
'link' => 'http://example.com',
|
|
'order' => 1,
|
|
'group_id' => 1,
|
|
'enabled' => 0,
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment(['name' => 'Foo', 'enabled' => false]);
|
|
}
|
|
|
|
public function test_can_get_newly_created_component()
|
|
{
|
|
$component = factory(Component::class)->create();
|
|
|
|
$response = $this->json('GET', '/api/v1/components/1');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment(['name' => $component->name]);
|
|
}
|
|
|
|
public function test_can_update_component()
|
|
{
|
|
$this->beUser();
|
|
$component = factory(Component::class)->create();
|
|
|
|
$this->expectsEvents(ComponentWasUpdatedEvent::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_meta_data()
|
|
{
|
|
$this->beUser();
|
|
$component = factory(Component::class)->create([
|
|
'meta' => [
|
|
'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0',
|
|
],
|
|
]);
|
|
|
|
$this->expectsEvents(ComponentWasUpdatedEvent::class);
|
|
|
|
$response = $this->json('PUT', '/api/v1/components/1', [
|
|
'meta' => [
|
|
'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0',
|
|
'foo' => 'bar',
|
|
],
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonFragment([
|
|
'meta' => [
|
|
'uuid' => '172ff3fb-41f7-49d3-8bcd-f57b53627fa0',
|
|
'foo' => 'bar',
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function test_can_delete_component()
|
|
{
|
|
$this->beUser();
|
|
$component = factory(Component::class)->create();
|
|
|
|
$this->expectsEvents(ComponentWasRemovedEvent::class);
|
|
|
|
$response = $this->json('DELETE', '/api/v1/components/1');
|
|
$response->assertStatus(204);
|
|
}
|
|
}
|