From b3379d4fe214c4a989adeb4a81edf93211e9593a Mon Sep 17 00:00:00 2001 From: Louis Hadrien Date: Mon, 14 Oct 2019 15:45:23 +0200 Subject: [PATCH 1/6] Add after save callback --- src/Model/Endpoint.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Model/Endpoint.php b/src/Model/Endpoint.php index 48fc896..65f08d7 100644 --- a/src/Model/Endpoint.php +++ b/src/Model/Endpoint.php @@ -1106,6 +1106,12 @@ public function save(EntityInterface $resource, $options = []) return false; } + $event = $this->dispatchEvent('Model.afterSave', compact('resource', 'options')); + + if ($event->isStopped()) { + return $event->result; + } + if (($resource->isNew()) && ($result instanceof EntityInterface)) { return $result; } From cf25aa2d99b3e1d12a407413938fdaf3bc6fca53 Mon Sep 17 00:00:00 2001 From: Hadry Date: Tue, 15 Oct 2019 11:05:55 +0200 Subject: [PATCH 2/6] [DONE] added test case --- .../Model/EndpointAfterSaveCallbackTest.php | 74 +++++++++++++++++++ .../Model/Endpoint/CallbackEndpoint.php | 30 ++++++++ 2 files changed, 104 insertions(+) create mode 100644 tests/TestCase/Model/EndpointAfterSaveCallbackTest.php create mode 100644 tests/test_app/Model/Endpoint/CallbackEndpoint.php diff --git a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php new file mode 100644 index 0000000..c7e99f7 --- /dev/null +++ b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php @@ -0,0 +1,74 @@ +connection = new Connection([ + 'name' => 'test', + 'service' => 'Test' + ]); + $this->endpoint = new CallbackEndpoint([ + 'connection' => $this->connection, + 'primaryKey' => 'id', + 'displayField' => 'title', + 'schema' => [ + 'id' => ['type' => 'int'], + 'title' => ['type' => 'string'], + 'body' => ['type' => 'string'], + ] + ]); + } + + /** + * Test afterSave return altered data + */ + public function testAfterSave() + { + $resource = new Resource([ + 'id' => 4, + 'title' => 'Loads of fun', + 'body' => 'Woot' + ]); + + $savedResource = $this->endpoint->save($resource); + $this->assertInstanceOf('\Muffin\Webservice\Model\Resource', $savedResource); + $this->assertEquals([ + 'id' => 4, + 'title' => 'Loads of sun', + 'body' => 'Woot' + ], $savedResource->toArray()); + + $newResource = $this->endpoint->get(4); + $this->assertInstanceOf('\Muffin\Webservice\Model\Resource', $newResource); + $this->assertEquals([ + 'id' => 4, + 'title' => 'Loads of fun', + 'body' => 'Woot' + ], $newResource->toArray()); + + } + +} diff --git a/tests/test_app/Model/Endpoint/CallbackEndpoint.php b/tests/test_app/Model/Endpoint/CallbackEndpoint.php new file mode 100644 index 0000000..5e99e72 --- /dev/null +++ b/tests/test_app/Model/Endpoint/CallbackEndpoint.php @@ -0,0 +1,30 @@ +stopPropagation(); + if($entity->get('title')) { + $entity->set('title', 'Loads of sun'); + } + + return $entity; + } +} From b37cb1b317df5cb7551ae2a1ecee0e594fa643ed Mon Sep 17 00:00:00 2001 From: Hadry Date: Tue, 15 Oct 2019 11:13:22 +0200 Subject: [PATCH 3/6] [DONE] fix PHPCS --- tests/TestCase/Model/EndpointAfterSaveCallbackTest.php | 2 -- tests/test_app/Model/Endpoint/CallbackEndpoint.php | 8 +++----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php index c7e99f7..9e5882a 100644 --- a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php +++ b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php @@ -68,7 +68,5 @@ public function testAfterSave() 'title' => 'Loads of fun', 'body' => 'Woot' ], $newResource->toArray()); - } - } diff --git a/tests/test_app/Model/Endpoint/CallbackEndpoint.php b/tests/test_app/Model/Endpoint/CallbackEndpoint.php index 5e99e72..99d8968 100644 --- a/tests/test_app/Model/Endpoint/CallbackEndpoint.php +++ b/tests/test_app/Model/Endpoint/CallbackEndpoint.php @@ -1,9 +1,7 @@ stopPropagation(); - if($entity->get('title')) { - $entity->set('title', 'Loads of sun'); - } + if ($entity->get('title')) { + $entity->set('title', 'Loads of sun'); + } return $entity; } From 8d733b8b57094f84c9e9a2d0b05a3d78ef2df748 Mon Sep 17 00:00:00 2001 From: Hadry Date: Fri, 18 Oct 2019 10:19:44 +0200 Subject: [PATCH 4/6] [DONE] fix PHPStan --- phpstan.neon | 1 - 1 file changed, 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 0678015..819a577 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,6 @@ parameters: - '#Method Muffin\\Webservice\\Query::endpoint\(\) should return \$this\(Muffin\\Webservice\\Query\)|Muffin\\Webservice\\Model\\Endpoint but returns Cake\Datasource\RepositoryInterface#' - '#Call to an undefined method Cake\\Datasource\\RepositoryInterface::callFinder\(\)#' - '#Call to an undefined method Cake\\Datasource\\RepositoryInterface::dispatchEvent\(\)#' - - '#Method Muffin\\Webservice\\Query::offset\(\) should return \$this\(Cake\\Datasource\\QueryInterface\) but returns \$this\(Muffin\\Webservice\\Query\)#' - '#Return type \(array\) of method Muffin\\Webservice\\Query::aliasField\(\) should be compatible with return type \(string\) of method Cake\\Datasource\\QueryInterface::aliasField\(\)#' - '#Call to an undefined method Cake\\Datasource\\RepositoryInterface::getName\(\)#' - '#Call to an undefined method Traversable::count\(\)#' From e0b08034783158940d46a1dda51cc49fd1c4bd3f Mon Sep 17 00:00:00 2001 From: Hadry Date: Wed, 18 Dec 2019 15:02:41 +0100 Subject: [PATCH 5/6] [DONE] add beforeDelete & afterDelete callbacks --- src/Model/Endpoint.php | 18 ++++++- .../Model/EndpointAfterSaveCallbackTest.php | 48 +++++++++++++++++++ .../Model/Endpoint/CallbackEndpoint.php | 35 +++++++++++++- 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/Model/Endpoint.php b/src/Model/Endpoint.php index 65f08d7..820eea2 100644 --- a/src/Model/Endpoint.php +++ b/src/Model/Endpoint.php @@ -1109,7 +1109,7 @@ public function save(EntityInterface $resource, $options = []) $event = $this->dispatchEvent('Model.afterSave', compact('resource', 'options')); if ($event->isStopped()) { - return $event->result; + return $event->getResult(); } if (($resource->isNew()) && ($result instanceof EntityInterface)) { @@ -1133,9 +1133,23 @@ public function save(EntityInterface $resource, $options = []) */ public function delete(EntityInterface $resource, $options = []) { - return (bool)$this->query()->delete()->where([ + $event = $this->dispatchEvent('Model.beforeDelete', compact('resource', 'options')); + + if ($event->isStopped()) { + return $event->getResult(); + } + + $result = (bool)$this->query()->delete()->where([ $this->getPrimaryKey() => $resource->get($this->getPrimaryKey()) ])->execute(); + + $event = $this->dispatchEvent('Model.afterDelete', compact('resource', 'options')); + + if ($event->isStopped()) { + return $event->getResult(); + } + + return $result; } /** diff --git a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php index 9e5882a..a22db5a 100644 --- a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php +++ b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php @@ -69,4 +69,52 @@ public function testAfterSave() 'body' => 'Woot' ], $newResource->toArray()); } + + /** + * Test beforeDelete and afterDelete return altered data + * + */ + public function testBeforeAndAfterDelete() + { + $resource1 = new Resource([ + 'id' => 4, + 'title' => 'Loads of sun', + 'body' => 'Woot' + ]); + $resource2 = new Resource([ + 'id' => 5, + 'title' => 'I love sun', + 'body' => 'Woot' + ]); + $resource3 = new Resource([ + 'id' => 6, + 'title' => 'I need sun', + 'body' => 'Woot' + ]); + $savedResource = $this->endpoint->save($resource1); + $this->assertInstanceOf('\Muffin\Webservice\Model\Resource', $savedResource); + $savedResource = $this->endpoint->save($resource2); + $this->assertInstanceOf('\Muffin\Webservice\Model\Resource', $savedResource); + $savedResource = $this->endpoint->save($resource3); + $this->assertInstanceOf('\Muffin\Webservice\Model\Resource', $savedResource); + + $resource = $this->endpoint->get(4); + $deletedResource = $this->endpoint->delete($resource); + $this->assertTrue($deletedResource); + + $editedResource = $this->endpoint->get(5); + $this->assertEquals([ + 'id' => 5, + 'title' => 'I love fun', + 'body' => 'Woot' + ], $editedResource->toArray()); + + $editedResource2 = $this->endpoint->get(6); + $this->assertEquals([ + 'id' => 6, + 'title' => 'I need fun', + 'body' => 'Woot' + ], $editedResource2->toArray()); + } + } diff --git a/tests/test_app/Model/Endpoint/CallbackEndpoint.php b/tests/test_app/Model/Endpoint/CallbackEndpoint.php index 99d8968..8a05887 100644 --- a/tests/test_app/Model/Endpoint/CallbackEndpoint.php +++ b/tests/test_app/Model/Endpoint/CallbackEndpoint.php @@ -4,6 +4,7 @@ use Cake\Datasource\EntityInterface; use Cake\Event\Event; +use Muffin\Webservice\Connection; use Muffin\Webservice\Model\Endpoint; class CallbackEndpoint extends Endpoint @@ -18,11 +19,43 @@ class CallbackEndpoint extends Endpoint */ public function afterSave(Event $event, EntityInterface $entity, \ArrayObject $options) { - $event->stopPropagation(); if ($entity->get('title')) { $entity->set('title', 'Loads of sun'); } return $entity; } + + /** + * Test beforeDelete Callback + * @param Event $event + * @param EntityInterface $entity + * @return EntityInterface + */ + public function beforeDelete(Event $event, EntityInterface $entity) + { + $entity = $this->get(5); + if ($entity->get('title')) { + $entity->set('title', 'I love fun'); + } + + return $entity; + } + + /** + * Test afterDelete Callback + * @param Event $event + * @param EntityInterface $entity + * @return EntityInterface + */ + public function afterDelete(Event $event, EntityInterface $entity) + { + $entity = $this->get(6); + if ($entity->get('title')) { + $entity->set('title', 'I need fun'); + } + + return $entity; + } + } From 03b28517cef73c863710caa8412253f88dfec0cf Mon Sep 17 00:00:00 2001 From: Hadry Date: Wed, 18 Dec 2019 15:22:54 +0100 Subject: [PATCH 6/6] [DONE] fix phpcs --- tests/TestCase/Model/EndpointAfterSaveCallbackTest.php | 1 - tests/test_app/Model/Endpoint/CallbackEndpoint.php | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php index a22db5a..4e4aa0e 100644 --- a/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php +++ b/tests/TestCase/Model/EndpointAfterSaveCallbackTest.php @@ -116,5 +116,4 @@ public function testBeforeAndAfterDelete() 'body' => 'Woot' ], $editedResource2->toArray()); } - } diff --git a/tests/test_app/Model/Endpoint/CallbackEndpoint.php b/tests/test_app/Model/Endpoint/CallbackEndpoint.php index 8a05887..3f73503 100644 --- a/tests/test_app/Model/Endpoint/CallbackEndpoint.php +++ b/tests/test_app/Model/Endpoint/CallbackEndpoint.php @@ -47,7 +47,7 @@ public function beforeDelete(Event $event, EntityInterface $entity) * @param Event $event * @param EntityInterface $entity * @return EntityInterface - */ + */ public function afterDelete(Event $event, EntityInterface $entity) { $entity = $this->get(6); @@ -57,5 +57,4 @@ public function afterDelete(Event $event, EntityInterface $entity) return $entity; } - }