Skip to content

Add after save callback #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\(\)#'
Expand Down
22 changes: 21 additions & 1 deletion src/Model/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->getResult();
}

if (($resource->isNew()) && ($result instanceof EntityInterface)) {
return $result;
}
Expand All @@ -1127,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;
}

/**
Expand Down
119 changes: 119 additions & 0 deletions tests/TestCase/Model/EndpointAfterSaveCallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Muffin\Webservice\Test\TestCase\Model;

use Cake\TestSuite\TestCase;
use Muffin\Webservice\Connection;
use Muffin\Webservice\Model\Resource;
use Muffin\Webservice\Test\test_app\Model\Endpoint\CallbackEndpoint;

class EndpointAfterSaveCallbackTest extends TestCase
{
/**
* @var \Muffin\Webservice\Connection
*/
public $connection;

/**
* @var Endpoint
*/
public $endpoint;

/**
* @inheritDoc
*/
public function setUp()
{
parent::setUp();

$this->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());
}

/**
* 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());
}
}
60 changes: 60 additions & 0 deletions tests/test_app/Model/Endpoint/CallbackEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Muffin\Webservice\Test\test_app\Model\Endpoint;

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
use Muffin\Webservice\Connection;
use Muffin\Webservice\Model\Endpoint;

class CallbackEndpoint extends Endpoint
{

/**
* Test afterSave Callback
* @param Event $event
* @param EntityInterface $entity
* @param \ArrayObject $options
* @return bool|EntityInterface
*/
public function afterSave(Event $event, EntityInterface $entity, \ArrayObject $options)
{
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;
}
}