浏览代码

Add test case.

mscherer 6 年之前
父节点
当前提交
78a88380cf
共有 1 个文件被更改,包括 33 次插入0 次删除
  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']));
 	}
 
 }