From 540d82b672b421385c8afcb983f4bc5ea8e2eb39 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 31 Aug 2021 16:43:02 +1000 Subject: [PATCH] Rename Type::newModel for consistency with Adapter --- docs/create.md | 4 ++-- src/Endpoint/Create.php | 2 +- src/Schema/Type.php | 10 +++++----- tests/feature/CreateTest.php | 11 +++++------ tests/unit/Schema/TypeTest.php | 4 ++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/docs/create.md b/docs/create.md index 861ea84..a2b54e4 100644 --- a/docs/create.md +++ b/docs/create.md @@ -14,10 +14,10 @@ $type->creatable(function (Context $context) { ## Customizing the Model -When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the `newModel` method: +When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the `model` method: ```php -$type->newModel(function (Context $context) { +$type->model(function (Context $context) { return new CustomModel; }); ``` diff --git a/src/Endpoint/Create.php b/src/Endpoint/Create.php index 0ffcf43..66c493d 100644 --- a/src/Endpoint/Create.php +++ b/src/Endpoint/Create.php @@ -62,7 +62,7 @@ class Create private function newModel(ResourceType $resourceType, Context $context) { - $newModel = $resourceType->getSchema()->getNewModelCallback(); + $newModel = $resourceType->getSchema()->getModelCallback(); return $newModel ? $newModel($context) diff --git a/src/Schema/Type.php b/src/Schema/Type.php index c18a56c..a88ea06 100644 --- a/src/Schema/Type.php +++ b/src/Schema/Type.php @@ -31,7 +31,7 @@ final class Type private $listable = true; private $defaultSort; private $saveCallback; - private $newModelCallback; + private $modelCallback; private $creatable = false; private $updatable = false; private $deletable = false; @@ -264,17 +264,17 @@ final class Type * * If null, the adapter will be used to create new model instances. */ - public function newModel(?callable $callback): void + public function model(?callable $callback): void { - $this->newModelCallback = $callback; + $this->modelCallback = $callback; } /** * Get the callback to create a new model instance. */ - public function getNewModelCallback(): ?callable + public function getModelCallback(): ?callable { - return $this->newModelCallback; + return $this->modelCallback; } /** diff --git a/tests/feature/CreateTest.php b/tests/feature/CreateTest.php index 4100fd6..64c0de6 100644 --- a/tests/feature/CreateTest.php +++ b/tests/feature/CreateTest.php @@ -41,7 +41,6 @@ class CreateTest extends AbstractTestCase ->withParsedBody([ 'data' => array_merge([ 'type' => 'users', - 'id' => '1', ], $data) ]) ); @@ -123,7 +122,7 @@ class CreateTest extends AbstractTestCase public function test_new_models_are_supplied_and_saved_by_the_adapter() { $adapter = $this->prophesize(AdapterInterface::class); - $adapter->newModel()->willReturn($createdModel = (object) []); + $adapter->model()->willReturn($createdModel = (object) []); $adapter->save($createdModel)->shouldBeCalled(); $adapter->getId($createdModel)->willReturn('1'); @@ -139,13 +138,13 @@ class CreateTest extends AbstractTestCase $createdModel = (object) []; $adapter = $this->prophesize(AdapterInterface::class); - $adapter->newModel()->shouldNotBeCalled(); + $adapter->model()->shouldNotBeCalled(); $adapter->save($createdModel)->shouldBeCalled(); $adapter->getId($createdModel)->willReturn('1'); $this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($createdModel) { $type->creatable(); - $type->newModel(function ($context) use ($createdModel) { + $type->model(function ($context) use ($createdModel) { $this->assertInstanceOf(Context::class, $context); return $createdModel; }); @@ -159,7 +158,7 @@ class CreateTest extends AbstractTestCase $called = false; $adapter = $this->prophesize(AdapterInterface::class); - $adapter->newModel()->willReturn($createdModel = (object) []); + $adapter->model()->willReturn($createdModel = (object) []); $adapter->save($createdModel)->shouldNotBeCalled(); $adapter->getId($createdModel)->willReturn('1'); @@ -183,7 +182,7 @@ class CreateTest extends AbstractTestCase $called = 0; $adapter = $this->prophesize(AdapterInterface::class); - $adapter->newModel()->willReturn($createdModel = (object) []); + $adapter->model()->willReturn($createdModel = (object) []); $adapter->getId($createdModel)->willReturn('1'); $this->api->resourceType('users', $adapter->reveal(), function (Type $type) use ($adapter, $createdModel, &$called) { diff --git a/tests/unit/Schema/TypeTest.php b/tests/unit/Schema/TypeTest.php index 8d84db3..85a8213 100644 --- a/tests/unit/Schema/TypeTest.php +++ b/tests/unit/Schema/TypeTest.php @@ -18,7 +18,7 @@ class TypeTest extends TestCase { public function test_returns_an_existing_field_with_the_same_name_of_the_same_type() { - $type = new Type; + $type = new Type(); $attribute = $type->attribute('dogs'); $attributeAgain = $type->attribute('dogs'); @@ -30,7 +30,7 @@ class TypeTest extends TestCase public function test_overwrites_an_existing_field_with_the_same_name_of_a_different_type() { - $type = new Type; + $type = new Type(); $attribute = $type->attribute('dogs'); $hasOne = $type->hasOne('dogs');