Skip to content

Commit

Permalink
Add update hook call tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kwizer15 committed Oct 6, 2024
1 parent 6613b9c commit 25cf745
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 15 deletions.
97 changes: 86 additions & 11 deletions tests/Unit/DBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,25 @@ 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
{
$object = $this->thereIsAnObject()->withHook($hook)->object();

\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'];
Expand All @@ -240,15 +240,15 @@ public static function hookSkippedWithDirectFlagProvider(): iterable
}

/**
* @dataProvider hookSkippedWithDirectFlagProvider
* @dataProvider saveInsertHookSkippedWithDirectFlagProvider
*/
public function test_save_skip_hook_with_direct_flag(string $hook): void
{
$object = $this->thereIsAnObject()->withHook($hook)->object();

\DB::save($object, true);

$this->assertFalse($object->isMethodCalled($hook));
$this->assertMethodNotCalledOnObject($object, $hook);
}

public function test_save_set_private_id(): void
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)));
}
}
14 changes: 10 additions & 4 deletions tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function isIdSet(): bool

}

public function withoutMethod(string $method): self
public function withoutHook(string $method): self
{
return $this;
}
Expand Down Expand Up @@ -167,6 +167,8 @@ public function object(): object
\DB::save($object);
}

$object->clearMethodsCalled();

return $object;
}

Expand All @@ -186,9 +188,8 @@ private function createObjectTable(): void
private static function getBaseClassProperties(): string
{
return <<<PHP
public \$publicVar = null;
PHP;
;
public \$publicVar = null;
PHP;
}

private function getBaseClassMethods(): string
Expand All @@ -214,6 +215,11 @@ public function getMethodsCalled(): array
{
return \$this->methodsCalled;
}
public function clearMethodsCalled(): void
{
\$this->methodsCalled = [];
}
PHP;
}

Expand Down

0 comments on commit 25cf745

Please sign in to comment.