Browse Source

AfterSave behavior.

mscherer 6 years ago
parent
commit
de571577fb

+ 55 - 0
src/Model/Behavior/AfterSaveBehavior.php

@@ -0,0 +1,55 @@
+<?php
+/**
+ * @author Mark Scherer
+ * @license http://opensource.org/licenses/mit-license.php MIT
+ */
+
+namespace Tools\Model\Behavior;
+
+use ArrayObject;
+use Cake\Datasource\EntityInterface;
+use Cake\Event\Event;
+use Cake\ORM\Behavior;
+use LogicException;
+
+/**
+ * Allow entity to be available inside afterSave() callback.
+ * It takes a clone of the entity from beforeSave(). This allows all the
+ * info on it to be available after save without resetting (dirty, ...).
+ */
+class AfterSaveBehavior extends Behavior {
+
+	/**
+	 * @var \Cake\Datasource\EntityInterface|null
+	 */
+	protected $_entity;
+
+	/**
+	 * @var array
+	 */
+	protected $_defaultConfig = [
+	];
+
+	/**
+	 * @param \Cake\Event\Event $event
+	 * @param \Cake\Datasource\EntityInterface $entity
+	 * @param \ArrayObject $options
+	 * @return void
+	 */
+	public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
+		$this->_entity = clone $entity;
+	}
+
+	/**
+	 * @return \Cake\Datasource\EntityInterface
+	 * @throws \LogicException
+	 */
+	public function getEntityBeforeSave() {
+		if (!$this->_entity) {
+			throw new LogicException('You need to successfully save first - including beforeSave() callback fired.');
+		}
+
+		return $this->_entity;
+	}
+
+}

+ 1 - 3
src/Model/Behavior/TypographicBehavior.php

@@ -139,14 +139,12 @@ class TypographicBehavior extends Behavior {
 	 * @param \Cake\Event\Event $event
 	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
-	 * @return bool
+	 * @return void
 	 */
 	public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
 		if ($this->_config['before'] === 'save') {
 			$this->process($entity);
 		}
-
-		return true;
 	}
 
 	/**

+ 53 - 0
tests/TestCase/Model/Behavior/AfterSaveBehaviorTest.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace Tools\Test\TestCase\Model\Behavior;
+
+use Cake\ORM\TableRegistry;
+use Tools\TestSuite\TestCase;
+
+class AfterSaveBehaviorTest extends TestCase {
+
+	/**
+	 * @var array
+	 */
+	public $fixtures = [
+		'core.Articles'
+	];
+
+	/**
+	 * @var \Tools\Model\Table\Table|\Tools\Model\Behavior\AfterSaveBehavior
+	 */
+	public $table;
+
+	/**
+	 * @return void
+	 */
+	public function setUp() {
+		parent::setUp();
+
+		$this->table = TableRegistry::getTableLocator()->get('Articles');
+		$this->table->addBehavior('Tools.AfterSave');
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testSaveBasic() {
+		$data = [
+			'body' => 'test save',
+		];
+
+		$entity = $this->table->newEntity($data);
+		$entityAfter = $this->table->save($entity);
+		$this->assertTrue((bool)$entityAfter);
+
+		// The saved entity is resetted
+		$this->assertFalse($entityAfter->isDirty('body'));
+
+		$entityBefore = $this->table->getEntityBeforeSave();
+
+		// The stored one from before the save contains the info we want
+		$this->assertTrue($entityBefore->isDirty('body'));
+	}
+
+}