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 4 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
6 changes: 6 additions & 0 deletions 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->result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to $event->getResult().

}

if (($resource->isNew()) && ($result instanceof EntityInterface)) {
return $result;
}
Expand Down
72 changes: 72 additions & 0 deletions tests/TestCase/Model/EndpointAfterSaveCallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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());
}
}
28 changes: 28 additions & 0 deletions tests/test_app/Model/Endpoint/CallbackEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

use Cake\Datasource\EntityInterface;
use Cake\Event\Event;
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)
{
$event->stopPropagation();
if ($entity->get('title')) {
$entity->set('title', 'Loads of sun');
}

return $entity;
}
}