Skip to content

Commit

Permalink
tests for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Nov 15, 2021
1 parent 6158bec commit 8b259c6
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 7 deletions.
1 change: 1 addition & 0 deletions .idea/MkSQL.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Repository/Fetch/EntityStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public function linkEntities(): void
}
}
}

// Here, we have all entities linked, we can call afterRetrieve hook
foreach ($this->entities as $entity) {
$entity->afterRetrieve();
}

}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Repository/Fetch/ResultService.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ public function getEntities(string $baseEntityClassString, ?string $propertyName
}
}

foreach ($result as $entity) {
$entity->afterRetrieve();
}


return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Repository/Saver/Saver.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,13 @@ function insert(array $entities): void
$entity->setPrimaryKeyValue($firstPk);
$entity->updateRawData();
$entity->indicateSave();
$entity->afterSave();
$firstPk++;
}
//endregion

$this->pdo->commit();


foreach ($entities as $entity) {
$entity->afterSave();
}
Expand Down
69 changes: 68 additions & 1 deletion tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
use Tests\Mock\Bugs\DoubleRetrieve\Reward;
use Tests\Mock\Bugs\TreeSave\RepeatingNode;
use Tests\Mock\Bugs\TreeSave\TreeSaveRepository;
use Tests\Mock\EntitiesWithHooks\AfterRetrieveHookEntity;
use Tests\Mock\EntitiesWithHooks\AfterSaveHookEntity;
use Tests\Mock\EntitiesWithHooks\BeforeSaveHookEntity;
use Tests\Mock\EntitiesWithHooks\EntityHookExceptionType;
use Tests\Mock\EntitiesWithHooks\EntityHookRepository;
use Tests\Mock\EntitiesWithHooks\EntityWithHookException;
use Tracy\Debugger;
use Zrnik\MkSQL\Column;
use Zrnik\MkSQL\Enum\DriverType;
Expand Down Expand Up @@ -198,10 +204,10 @@ function () use ($Updater) {

$this->subTestAllowPrivatePropertiesInEntities($pdo);

$this->subTestBaseEntityHooks($pdo);

echo ']' . PHP_EOL . 'Complete!';


}


Expand Down Expand Up @@ -1207,4 +1213,65 @@ private function subTestAllowPrivatePropertiesInEntities(PDO $pdo): void
static::assertSame($entities[count($entities) - 1]->id, $entity->id);

}

private function subTestBaseEntityHooks(PDO $pdo): void
{
$entityHookRepository = new EntityHookRepository($pdo);

###############################################
###############################################
###############################################

/** @var EntityWithHookException $ex */
$ex = $this->assertExceptionThrown(
EntityWithHookException::class,
function() use ($entityHookRepository) {
$entityHookRepository->save(BeforeSaveHookEntity::create());
}
);

static::assertSame(
EntityHookExceptionType::BEFORE_SAVE,
$ex->hookExceptionType
);

###############################################
###############################################
###############################################

/** @var EntityWithHookException $ex */
$ex = $this->assertExceptionThrown(
EntityWithHookException::class,
function() use ($entityHookRepository) {
$entityHookRepository->save(AfterSaveHookEntity::create());
}
);

static::assertSame(
EntityHookExceptionType::AFTER_SAVE,
$ex->hookExceptionType
);

###############################################
###############################################
###############################################

/** @var EntityWithHookException $ex */
$ex = $this->assertExceptionThrown(
EntityWithHookException::class,
function() use ($entityHookRepository) {
$entityHookRepository->save(AfterRetrieveHookEntity::create());
$entityHookRepository->getAll(
AfterRetrieveHookEntity::class
);
}
);

static::assertSame(
EntityHookExceptionType::AFTER_RETRIEVE,
$ex->hookExceptionType
);

}

}
27 changes: 27 additions & 0 deletions tests/Mock/EntitiesWithHooks/AfterRetrieveHookEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Tests\Mock\EntitiesWithHooks;

use Zrnik\MkSQL\Repository\Attributes\ColumnType;
use Zrnik\MkSQL\Repository\Attributes\PrimaryKey;
use Zrnik\MkSQL\Repository\Attributes\TableName;
use Zrnik\MkSQL\Repository\BaseEntity;

#[TableName('hook_test_AfterRetrieveHookEntity')]
class AfterRetrieveHookEntity extends BaseEntity
{
#[PrimaryKey]
public ?int $id = null;

#[ColumnType('varchar(100)')]
public string $afterSaveException = 'Hello World';

/**
* @throws EntityWithHookException
* @noinspection PhpMissingParentCallCommonInspection
*/
public function afterRetrieve(): void
{
throw new EntityWithHookException(EntityHookExceptionType::AFTER_RETRIEVE);
}
}
27 changes: 27 additions & 0 deletions tests/Mock/EntitiesWithHooks/AfterSaveHookEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace Tests\Mock\EntitiesWithHooks;

use Zrnik\MkSQL\Repository\Attributes\ColumnType;
use Zrnik\MkSQL\Repository\Attributes\PrimaryKey;
use Zrnik\MkSQL\Repository\Attributes\TableName;
use Zrnik\MkSQL\Repository\BaseEntity;

#[TableName('hook_test_AfterSaveHookEntity')]
class AfterSaveHookEntity extends BaseEntity
{
#[PrimaryKey]
public ?int $id = null;

#[ColumnType('varchar(100)')]
public string $afterSaveException = 'Hello World';

/**
* @throws EntityWithHookException
* @noinspection PhpMissingParentCallCommonInspection
*/
public function afterSave(): void
{
throw new EntityWithHookException(EntityHookExceptionType::AFTER_SAVE);
}
}
24 changes: 24 additions & 0 deletions tests/Mock/EntitiesWithHooks/BeforeSaveHookEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Tests\Mock\EntitiesWithHooks;

use Zrnik\MkSQL\Repository\Attributes\PrimaryKey;
use Zrnik\MkSQL\Repository\Attributes\TableName;
use Zrnik\MkSQL\Repository\BaseEntity;

#[TableName('hook_test_BeforeSaveHookEntity')]
class BeforeSaveHookEntity extends BaseEntity
{
#[PrimaryKey]
public ?int $id = null;

/**
* @throws EntityWithHookException
* @noinspection PhpMissingParentCallCommonInspection
*/
public function beforeSave(): void
{
throw new EntityWithHookException(EntityHookExceptionType::BEFORE_SAVE);
}

}
13 changes: 13 additions & 0 deletions tests/Mock/EntitiesWithHooks/EntityHookExceptionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Tests\Mock\EntitiesWithHooks;

use Zrnik\Base\Enum;

class EntityHookExceptionType extends Enum
{
public const BEFORE_SAVE = 0;
public const AFTER_SAVE = 1;

public const AFTER_RETRIEVE = 10;
}
16 changes: 16 additions & 0 deletions tests/Mock/EntitiesWithHooks/EntityHookRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Tests\Mock\EntitiesWithHooks;

use Zrnik\MkSQL\Updater;
use Zrnik\MkSQL\Utilities\Installable;

class EntityHookRepository extends Installable
{
protected function install(Updater $updater): void
{
$updater->use(AfterRetrieveHookEntity::class);
$updater->use(AfterSaveHookEntity::class);
$updater->use(BeforeSaveHookEntity::class);
}
}
22 changes: 22 additions & 0 deletions tests/Mock/EntitiesWithHooks/EntityWithHookException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Tests\Mock\EntitiesWithHooks;

use Exception;

class EntityWithHookException extends Exception
{
public int $hookExceptionType;

public function __construct(int $hookExceptionType)
{
$this->hookExceptionType = $hookExceptionType;

parent::__construct(
sprintf(
'Hook exception "%s" thrown!',
EntityHookExceptionType::getName($hookExceptionType)
)
);
}
}

0 comments on commit 8b259c6

Please sign in to comment.