From 25cf74580f41c0bef968dc983fd748dbeb977f16 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernaszuk Date: Sun, 6 Oct 2024 21:53:41 +0200 Subject: [PATCH] Add update hook call tests --- tests/Unit/DBTest.php | 97 ++++++++++++++++--- .../Mock/ObjectMock/ObjectMockBuilder.php | 14 ++- 2 files changed, 96 insertions(+), 15 deletions(-) diff --git a/tests/Unit/DBTest.php b/tests/Unit/DBTest.php index 3edc72e30b..4b2c400c12 100644 --- a/tests/Unit/DBTest.php +++ b/tests/Unit/DBTest.php @@ -213,14 +213,14 @@ public function test_save_data(): void $this->assertCount(1, $this->contentOfTable($object->getTableName())); } - public static function hookProvider(): iterable + public static function saveInsertHookProvider(): iterable { yield from self::hookCalledWithDirectFlagProvider(); - yield from self::hookSkippedWithDirectFlagProvider(); + yield from self::saveInsertHookSkippedWithDirectFlagProvider(); } /** - * @dataProvider hookProvider + * @dataProvider saveInsertHookProvider */ public function test_save_call_hooks(string $hook): void { @@ -228,10 +228,10 @@ public function test_save_call_hooks(string $hook): void \DB::save($object); - $this->assertTrue($object->isMethodCalled($hook)); + $this->assertMethodCalledOnObject($object, $hook); } - public static function hookSkippedWithDirectFlagProvider(): iterable + public static function saveInsertHookSkippedWithDirectFlagProvider(): iterable { yield ['preSave']; yield ['preInsert']; @@ -240,7 +240,7 @@ public static function hookSkippedWithDirectFlagProvider(): iterable } /** - * @dataProvider hookSkippedWithDirectFlagProvider + * @dataProvider saveInsertHookSkippedWithDirectFlagProvider */ public function test_save_skip_hook_with_direct_flag(string $hook): void { @@ -248,7 +248,7 @@ public function test_save_skip_hook_with_direct_flag(string $hook): void \DB::save($object, true); - $this->assertFalse($object->isMethodCalled($hook)); + $this->assertMethodNotCalledOnObject($object, $hook); } public function test_save_set_private_id(): void @@ -261,15 +261,15 @@ public function test_save_set_private_id(): void } /** - * @dataProvider hookProvider + * @dataProvider saveInsertHookProvider */ public function test_save_skip_call_hook_on_object_without_method(string $hook): void { - $object = $this->thereIsAnObject()->withoutMethod($hook)->object(); + $object = $this->thereIsAnObject()->withoutHook($hook)->object(); \DB::save($object); - $this->assertFalse($object->isMethodCalled($hook)); + $this->assertMethodNotCalledOnObject($object, $hook); } public static function hookCalledWithDirectFlagProvider(): iterable @@ -287,7 +287,7 @@ public function test_save_insert_call_hook_with_direct_flag(string $hook): void \DB::save($object, true); - $this->assertTrue($object->isMethodCalled($hook)); + $this->assertMethodCalledOnObject($object, $hook); } public function test_insert_with_duplicate_unique_field_fail(): void @@ -381,6 +381,71 @@ public function test_save_object_identified_update_row(): void $this->assertSame('update', $contentOfTable[0]['publicVar']); } + public static function saveUpdateHookProvider(): iterable + { + yield from self::hookCalledWithDirectFlagProvider(); + yield from self::saveUpdateHookSkippedWithDirectFlagProvider(); + } + + /** + * @dataProvider saveUpdateHookProvider + */ + public function test_save_update_call_hooks(string $hook): void + { + $object = $this->thereIsAnObject() + ->identifiable() + ->withHook($hook) + ->persisted() + ->object(); + + \DB::save($object); + + $this->assertMethodCalledOnObject($object, $hook); + } + + public static function saveUpdateHookSkippedWithDirectFlagProvider(): iterable + { + yield ['preSave']; + yield ['preUpdate']; + yield ['postUpdate']; + yield ['postSave']; + } + + /** + * @dataProvider saveUpdateHookSkippedWithDirectFlagProvider + */ + public function test_save_update_skip_hook_with_direct_flag(string $hook): void + { + $object = $this->thereIsAnObject() + ->identifiable() + ->withHook($hook) + ->persisted() + ->object(); + + \DB::save($object, true); + + $this->assertMethodNotCalledOnObject($object, $hook); + } + + /** + * @dataProvider hookCalledWithDirectFlagProvider + */ + public function test_save_update_call_hook_with_direct_flag(string $hook): void + { + $object = $this->thereIsAnObject() + ->identifiable() + ->withHook($hook) + ->persisted() + ->object(); + + \DB::save($object, true); + + $this->assertMethodCalledOnObject($object, $hook); + } + + // TODO: test setChanged parts + // TODO: test returns + /** * @return ObjectMockBuilder */ @@ -477,4 +542,14 @@ private function randomPositiveInt(): int { return random_int(1, 2**30); } + + private function assertMethodCalledOnObject(object $object, string $hook): void + { + $this->assertTrue($object->isMethodCalled($hook), sprintf('Method %s expects to be called on object %s.', $hook, get_class($object))); + } + + private function assertMethodNotCalledOnObject(object $object, string $hook): void + { + $this->assertFalse($object->isMethodCalled($hook), sprintf('Method %s expects to be NOT called on object %s.', $hook, get_class($object))); + } } \ No newline at end of file diff --git a/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php b/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php index 9297fd2b9f..fdbd2787f4 100644 --- a/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php +++ b/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php @@ -71,7 +71,7 @@ public function isIdSet(): bool } - public function withoutMethod(string $method): self + public function withoutHook(string $method): self { return $this; } @@ -167,6 +167,8 @@ public function object(): object \DB::save($object); } + $object->clearMethodsCalled(); + return $object; } @@ -186,9 +188,8 @@ private function createObjectTable(): void private static function getBaseClassProperties(): string { return <<methodsCalled; } + + public function clearMethodsCalled(): void + { + \$this->methodsCalled = []; + } PHP; }