AfterSaveBehaviorTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Shim\TestSuite\TestCase;
  4. use TestApp\Model\Table\PostsTable;
  5. class AfterSaveBehaviorTest extends TestCase {
  6. /**
  7. * @var array
  8. */
  9. protected array $fixtures = [
  10. 'plugin.Tools.Articles',
  11. ];
  12. /**
  13. * @var \Tools\Model\Table\Table|\Tools\Model\Behavior\AfterSaveBehavior
  14. */
  15. protected $table;
  16. /**
  17. * @return void
  18. */
  19. public function setUp(): void {
  20. parent::setUp();
  21. $this->table = $this->getTableLocator()->get('Articles');
  22. $this->table->addBehavior('Tools.AfterSave');
  23. }
  24. /**
  25. * @return void
  26. */
  27. public function testSaveBasic() {
  28. $data = [
  29. 'body' => 'test save',
  30. ];
  31. $entity = $this->table->newEntity($data);
  32. $entityAfter = $this->table->save($entity);
  33. $this->assertTrue((bool)$entityAfter);
  34. // The saved entity is resetted
  35. $this->assertFalse($entityAfter->isDirty('body'));
  36. $this->assertFalse($entityAfter->isNew());
  37. $this->assertSame(['id' => 4, 'body' => 'test save'], $entityAfter->extractOriginal(['id', 'body']));
  38. $entityBefore = $this->table->getEntityBeforeSave();
  39. // The stored one from before the save contains the info we want
  40. $this->assertTrue($entityBefore->isDirty('body'));
  41. $this->assertTrue($entityBefore->isNew());
  42. $this->assertSame(['body' => 'test save'], $entityBefore->extractOriginal(['id', 'body']));
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testSaveExisting() {
  48. $data = [
  49. 'body' => 'test save',
  50. ];
  51. $entity = $this->table->newEntity($data);
  52. $this->table->saveOrFail($entity);
  53. $entity = $this->table->get($entity->id);
  54. $entity = $this->table->patchEntity($entity, ['body' => 'modified']);
  55. $this->assertEmpty($entity->getErrors());
  56. $entityAfter = $this->table->save($entity);
  57. $this->assertTrue((bool)$entityAfter);
  58. // The saved entity is resetted
  59. $this->assertFalse($entityAfter->isDirty('body'));
  60. $this->assertSame(['body' => 'modified'], $entityAfter->extractOriginal(['body']));
  61. $entityBefore = $this->table->getEntityBeforeSave();
  62. // The stored one from before the save contains the info we want
  63. $this->assertTrue($entityBefore->isDirty('body'));
  64. $this->assertSame(['body' => 'test save'], $entityBefore->extractOriginal(['body']));
  65. }
  66. /**
  67. * @return void
  68. */
  69. public function testAfterSaveCallbackWihoutBehavior() {
  70. $table = $this->getTableLocator()->get('Posts');
  71. $this->assertInstanceOf(PostsTable::class, $table);
  72. $entity = $table->newEmptyEntity();
  73. $entity = $table->patchEntity($entity, ['author_id' => 1, 'title' => 'Some title']);
  74. $this->assertNotEmpty($entity->getDirty());
  75. $table->saveOrFail($entity);
  76. $dirty = $entity->getDirty();
  77. $this->assertSame([], $dirty);
  78. $dirtyBefore = $table->dirtyFieldsBefore;
  79. $dirtyAfter = $table->dirtyFieldsAfter;
  80. $this->assertSame(['author_id', 'title'], $dirtyBefore);
  81. $this->assertSame(['author_id', 'title', 'id'], $dirtyAfter);
  82. // Now we edit existing entity with only one field
  83. $entity->title = 'New title';
  84. $table->saveOrFail($entity);
  85. $dirtyAfter = $table->dirtyFieldsAfter;
  86. $this->assertSame(['title'], $dirtyAfter);
  87. }
  88. }