Browse Source

Add test case.

mscherer 6 years ago
parent
commit
78a88380cf
1 changed files with 33 additions and 0 deletions
  1. 33 0
      tests/TestCase/Model/Behavior/AfterSaveBehaviorTest.php

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

@@ -43,11 +43,44 @@ class AfterSaveBehaviorTest extends TestCase {
 
 		// The saved entity is resetted
 		$this->assertFalse($entityAfter->isDirty('body'));
+		$this->assertFalse($entityAfter->isNew());
+		$this->assertSame(['id' => 4, 'body' => 'test save'], $entityAfter->extractOriginal(['id', 'body']));
 
 		$entityBefore = $this->table->getEntityBeforeSave();
 
 		// The stored one from before the save contains the info we want
 		$this->assertTrue($entityBefore->isDirty('body'));
+		$this->assertTrue($entityBefore->isNew());
+		$this->assertSame(['id' => null, 'body' => 'test save'], $entityBefore->extractOriginal(['id', 'body']));
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testSaveExisting() {
+		$data = [
+			'body' => 'test save',
+		];
+
+		$entity = $this->table->newEntity($data);
+		$this->table->saveOrFail($entity);
+
+		$entity = $this->table->get($entity->id);
+		$entity = $this->table->patchEntity($entity, ['body' => 'modified']);
+		$this->assertEmpty($entity->getErrors());
+
+		$entityAfter = $this->table->save($entity);
+		$this->assertTrue((bool)$entityAfter);
+
+		// The saved entity is resetted
+		$this->assertFalse($entityAfter->isDirty('body'));
+		$this->assertSame(['id' => 4, 'body' => 'modified'], $entityAfter->extractOriginal(['id', 'body']));
+
+		$entityBefore = $this->table->getEntityBeforeSave();
+
+		// The stored one from before the save contains the info we want
+		$this->assertTrue($entityBefore->isDirty('body'));
+		$this->assertSame(['id' => 4, 'body' => 'test save'], $entityBefore->extractOriginal(['id', 'body']));
 	}
 
 }