Fix tests
This commit is contained in:
parent
4f84fede67
commit
a9a8bb84aa
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
namespace Tobyz\Tests\JsonApiServer\feature;
|
namespace Tobyz\Tests\JsonApiServer\feature;
|
||||||
|
|
||||||
|
use Prophecy\Argument;
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
|
use Tobyz\JsonApiServer\Adapter\AdapterInterface;
|
||||||
use Tobyz\JsonApiServer\Exception\ForbiddenException;
|
use Tobyz\JsonApiServer\Exception\ForbiddenException;
|
||||||
|
|
@ -125,6 +126,8 @@ class CreateTest extends AbstractTestCase
|
||||||
$adapter->model()->willReturn($createdModel = (object) []);
|
$adapter->model()->willReturn($createdModel = (object) []);
|
||||||
$adapter->save($createdModel)->shouldBeCalled();
|
$adapter->save($createdModel)->shouldBeCalled();
|
||||||
$adapter->getId($createdModel)->willReturn('1');
|
$adapter->getId($createdModel)->willReturn('1');
|
||||||
|
$adapter->query()->shouldBeCalled();
|
||||||
|
$adapter->find(Argument::any(), '1')->willReturn($createdModel);
|
||||||
|
|
||||||
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) {
|
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) {
|
||||||
$type->creatable();
|
$type->creatable();
|
||||||
|
|
@ -141,6 +144,8 @@ class CreateTest extends AbstractTestCase
|
||||||
$adapter->model()->shouldNotBeCalled();
|
$adapter->model()->shouldNotBeCalled();
|
||||||
$adapter->save($createdModel)->shouldBeCalled();
|
$adapter->save($createdModel)->shouldBeCalled();
|
||||||
$adapter->getId($createdModel)->willReturn('1');
|
$adapter->getId($createdModel)->willReturn('1');
|
||||||
|
$adapter->query()->shouldBeCalled();
|
||||||
|
$adapter->find(Argument::any(), '1')->willReturn($createdModel);
|
||||||
|
|
||||||
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) {
|
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) {
|
||||||
$type->creatable();
|
$type->creatable();
|
||||||
|
|
@ -161,6 +166,8 @@ class CreateTest extends AbstractTestCase
|
||||||
$adapter->model()->willReturn($createdModel = (object) []);
|
$adapter->model()->willReturn($createdModel = (object) []);
|
||||||
$adapter->save($createdModel)->shouldNotBeCalled();
|
$adapter->save($createdModel)->shouldNotBeCalled();
|
||||||
$adapter->getId($createdModel)->willReturn('1');
|
$adapter->getId($createdModel)->willReturn('1');
|
||||||
|
$adapter->query()->shouldBeCalled();
|
||||||
|
$adapter->find(Argument::any(), '1')->willReturn($createdModel);
|
||||||
|
|
||||||
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel, &$called) {
|
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel, &$called) {
|
||||||
$type->creatable();
|
$type->creatable();
|
||||||
|
|
@ -184,6 +191,8 @@ class CreateTest extends AbstractTestCase
|
||||||
$adapter = $this->prophesize(AdapterInterface::class);
|
$adapter = $this->prophesize(AdapterInterface::class);
|
||||||
$adapter->model()->willReturn($createdModel = (object) []);
|
$adapter->model()->willReturn($createdModel = (object) []);
|
||||||
$adapter->getId($createdModel)->willReturn('1');
|
$adapter->getId($createdModel)->willReturn('1');
|
||||||
|
$adapter->query()->shouldBeCalled();
|
||||||
|
$adapter->find(Argument::any(), '1')->willReturn($createdModel);
|
||||||
|
|
||||||
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($adapter, $createdModel, &$called) {
|
$this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($adapter, $createdModel, &$called) {
|
||||||
$type->creatable();
|
$type->creatable();
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,7 @@ class DeleteTest extends AbstractTestCase
|
||||||
$called = false;
|
$called = false;
|
||||||
|
|
||||||
$adapter = $this->prophesize(AdapterInterface::class);
|
$adapter = $this->prophesize(AdapterInterface::class);
|
||||||
$adapter->newQuery()->willReturn($query = (object) []);
|
$adapter->query()->willReturn($query = (object) []);
|
||||||
$adapter->find($query, '1')->willReturn($deletingModel = (object) []);
|
$adapter->find($query, '1')->willReturn($deletingModel = (object) []);
|
||||||
$adapter->delete($deletingModel);
|
$adapter->delete($deletingModel);
|
||||||
|
|
||||||
|
|
@ -123,7 +123,7 @@ class DeleteTest extends AbstractTestCase
|
||||||
public function test_deleting_a_resource_calls_the_delete_adapter_method()
|
public function test_deleting_a_resource_calls_the_delete_adapter_method()
|
||||||
{
|
{
|
||||||
$adapter = $this->prophesize(AdapterInterface::class);
|
$adapter = $this->prophesize(AdapterInterface::class);
|
||||||
$adapter->newQuery()->willReturn($query = (object) []);
|
$adapter->query()->willReturn($query = (object) []);
|
||||||
$adapter->find($query, '1')->willReturn($model = (object) []);
|
$adapter->find($query, '1')->willReturn($model = (object) []);
|
||||||
$adapter->delete($model)->shouldBeCalled();
|
$adapter->delete($model)->shouldBeCalled();
|
||||||
|
|
||||||
|
|
@ -139,7 +139,7 @@ class DeleteTest extends AbstractTestCase
|
||||||
$called = false;
|
$called = false;
|
||||||
|
|
||||||
$adapter = $this->prophesize(AdapterInterface::class);
|
$adapter = $this->prophesize(AdapterInterface::class);
|
||||||
$adapter->newQuery()->willReturn($query = (object) []);
|
$adapter->query()->willReturn($query = (object) []);
|
||||||
$adapter->find($query, '1')->willReturn($deletingModel = (object) []);
|
$adapter->find($query, '1')->willReturn($deletingModel = (object) []);
|
||||||
$adapter->delete($deletingModel)->shouldNotBeCalled();
|
$adapter->delete($deletingModel)->shouldNotBeCalled();
|
||||||
|
|
||||||
|
|
@ -162,7 +162,7 @@ class DeleteTest extends AbstractTestCase
|
||||||
$called = 0;
|
$called = 0;
|
||||||
|
|
||||||
$adapter = $this->prophesize(AdapterInterface::class);
|
$adapter = $this->prophesize(AdapterInterface::class);
|
||||||
$adapter->newQuery()->willReturn($query = (object) []);
|
$adapter->query()->willReturn($query = (object) []);
|
||||||
$adapter->find($query, '1')->willReturn($deletingModel = (object) []);
|
$adapter->find($query, '1')->willReturn($deletingModel = (object) []);
|
||||||
$adapter->delete($deletingModel)->shouldBeCalled();
|
$adapter->delete($deletingModel)->shouldBeCalled();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ class FieldGettersTest extends AbstractTestCase
|
||||||
$type->hasOne('animal')->withLinkage();
|
$type->hasOne('animal')->withLinkage();
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->api->resourceType('animals', new MockAdapter);
|
$this->api->resourceType('animals', new MockAdapter());
|
||||||
|
|
||||||
$response = $this->api->handle(
|
$response = $this->api->handle(
|
||||||
$this->buildRequest('GET', '/users/1')
|
$this->buildRequest('GET', '/users/1')
|
||||||
|
|
@ -100,12 +100,12 @@ class FieldGettersTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
$this->api->resourceType('users', $this->adapter, function (Type $type) {
|
$this->api->resourceType('users', $this->adapter, function (Type $type) {
|
||||||
$type->hasOne('animal')->withLinkage()
|
$type->hasOne('animal')->withLinkage()
|
||||||
->get(function ($model, Context $context) {
|
->get(function ($model, bool $linkageOnly, Context $context) {
|
||||||
return (object) ['id' => '2'];
|
return (object) ['id' => '2'];
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->api->resourceType('animals', new MockAdapter);
|
$this->api->resourceType('animals', new MockAdapter());
|
||||||
|
|
||||||
$response = $this->api->handle(
|
$response = $this->api->handle(
|
||||||
$this->buildRequest('GET', '/users/1')
|
$this->buildRequest('GET', '/users/1')
|
||||||
|
|
@ -122,7 +122,7 @@ class FieldGettersTest extends AbstractTestCase
|
||||||
$type->hasMany('animals')->withLinkage();
|
$type->hasMany('animals')->withLinkage();
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->api->resourceType('animals', new MockAdapter);
|
$this->api->resourceType('animals', new MockAdapter());
|
||||||
|
|
||||||
$response = $this->api->handle(
|
$response = $this->api->handle(
|
||||||
$this->buildRequest('GET', '/users/1')
|
$this->buildRequest('GET', '/users/1')
|
||||||
|
|
@ -138,7 +138,7 @@ class FieldGettersTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
$this->api->resourceType('users', $this->adapter, function (Type $type) {
|
$this->api->resourceType('users', $this->adapter, function (Type $type) {
|
||||||
$type->hasMany('animals')->withLinkage()
|
$type->hasMany('animals')->withLinkage()
|
||||||
->get(function ($model, Context $context) {
|
->get(function ($model, bool $linkageOnly, Context $context) {
|
||||||
return [
|
return [
|
||||||
(object) ['id' => '2'],
|
(object) ['id' => '2'],
|
||||||
(object) ['id' => '3']
|
(object) ['id' => '3']
|
||||||
|
|
@ -146,7 +146,7 @@ class FieldGettersTest extends AbstractTestCase
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->api->resourceType('animals', new MockAdapter);
|
$this->api->resourceType('animals', new MockAdapter());
|
||||||
|
|
||||||
$response = $this->api->handle(
|
$response = $this->api->handle(
|
||||||
$this->buildRequest('GET', '/users/1')
|
$this->buildRequest('GET', '/users/1')
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ class FieldVisibilityTest extends AbstractTestCase
|
||||||
|
|
||||||
public function test_fields_are_visible_by_default()
|
public function test_fields_are_visible_by_default()
|
||||||
{
|
{
|
||||||
$this->api->resourceType('users', new MockAdapter, function (Type $type) {
|
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
|
||||||
$type->attribute('visible');
|
$type->attribute('visible');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -58,7 +58,7 @@ class FieldVisibilityTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
$this->api->resourceType('users', new MockAdapter, function (Type $type) {
|
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
|
||||||
$type->attribute('visibleAttribute')->visible();
|
$type->attribute('visibleAttribute')->visible();
|
||||||
$type->hasOne('visibleHasOne')->visible();
|
$type->hasOne('visibleHasOne')->visible();
|
||||||
$type->hasMany('visibleHasMany')->visible();
|
$type->hasMany('visibleHasMany')->visible();
|
||||||
|
|
@ -81,7 +81,7 @@ class FieldVisibilityTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
$this->api->resourceType('users', new MockAdapter, function (Type $type) {
|
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
|
||||||
$type->attribute('visibleAttribute')
|
$type->attribute('visibleAttribute')
|
||||||
->visible(function () { return true; });
|
->visible(function () { return true; });
|
||||||
|
|
||||||
|
|
@ -152,7 +152,7 @@ class FieldVisibilityTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
$this->api->resourceType('users', new MockAdapter, function (Type $type) {
|
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
|
||||||
$type->attribute('hiddenAttribute')->hidden();
|
$type->attribute('hiddenAttribute')->hidden();
|
||||||
$type->hasOne('hiddenHasOne')->hidden();
|
$type->hasOne('hiddenHasOne')->hidden();
|
||||||
$type->hasMany('hiddenHasMany')->hidden();
|
$type->hasMany('hiddenHasMany')->hidden();
|
||||||
|
|
@ -175,7 +175,7 @@ class FieldVisibilityTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
$this->markTestIncomplete();
|
$this->markTestIncomplete();
|
||||||
|
|
||||||
$this->api->resourceType('users', new MockAdapter, function (Type $type) {
|
$this->api->resourceType('users', new MockAdapter(), function (Type $type) {
|
||||||
$type->attribute('visibleAttribute')
|
$type->attribute('visibleAttribute')
|
||||||
->hidden(function () { return false; });
|
->hidden(function () { return false; });
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@
|
||||||
|
|
||||||
namespace Tobyz\Tests\JsonApiServer\feature;
|
namespace Tobyz\Tests\JsonApiServer\feature;
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
use Tobyz\JsonApiServer\JsonApi;
|
use Tobyz\JsonApiServer\JsonApi;
|
||||||
use Tobyz\JsonApiServer\Context;
|
use Tobyz\JsonApiServer\Context;
|
||||||
use Tobyz\JsonApiServer\Schema\Type;
|
use Tobyz\JsonApiServer\Schema\Type;
|
||||||
|
|
@ -88,44 +87,4 @@ class ScopesTest extends AbstractTestCase
|
||||||
|
|
||||||
$this->assertTrue($this->scopeWasCalled);
|
$this->assertTrue($this->scopeWasCalled);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_scopes_are_applied_to_related_resources()
|
|
||||||
{
|
|
||||||
$this->api->resourceType('pets', new MockAdapter, function (Type $type) {
|
|
||||||
$type->hasOne('owner')
|
|
||||||
->type('users')
|
|
||||||
->includable();
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->api->handle(
|
|
||||||
$this->buildRequest('GET', '/pets/1')
|
|
||||||
->withQueryParams(['include' => 'owner'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertTrue($this->scopeWasCalled);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function test_scopes_are_applied_to_polymorphic_related_resources()
|
|
||||||
{
|
|
||||||
$this->api->resourceType('pets', new MockAdapter, function (Type $type) {
|
|
||||||
$type->hasOne('owner')
|
|
||||||
->polymorphic(['users', 'organisations'])
|
|
||||||
->includable();
|
|
||||||
});
|
|
||||||
|
|
||||||
$organisationScopeWasCalled = false;
|
|
||||||
$this->api->resourceType('organisations', new MockAdapter, function (Type $type) use (&$organisationScopeWasCalled) {
|
|
||||||
$type->scope(function ($query, Context $context) use (&$organisationScopeWasCalled) {
|
|
||||||
$organisationScopeWasCalled = true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
$this->api->handle(
|
|
||||||
$this->buildRequest('GET', '/pets/1')
|
|
||||||
->withQueryParams(['include' => 'owner'])
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertTrue($this->scopeWasCalled);
|
|
||||||
$this->assertTrue($organisationScopeWasCalled);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue