Skip to content

Commit

Permalink
added hooks to the custom type converter, so you can for example atta…
Browse files Browse the repository at this point in the history
…ch injections with DI...
  • Loading branch information
Zrnik committed Nov 15, 2021
1 parent 8990cdc commit 6158bec
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Repository/CustomTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ abstract class CustomTypeConverter
* @param callable $onCreate
* @return string
*/
public function addOnCreate(callable $onCreate): string
public static function addOnCreate(callable $onCreate): string
{
$hookKey = null;
while (!array_key_exists((string)$hookKey, self::$onCreate) || $hookKey === null) {
while (array_key_exists((string)$hookKey, self::$onCreate) || $hookKey === null) {
$hookKey = Random::generate(12);
}

self::$onCreate[$hookKey] = $onCreate;

return $hookKey;
}

Expand All @@ -53,7 +55,7 @@ public function addOnCreate(callable $onCreate): string
* @param string $hookKey
* @return bool
*/
public function removeOnCreate(string $hookKey): bool
public static function removeOnCreate(string $hookKey): bool
{
if (array_key_exists($hookKey, self::$onCreate)) {
unset(self::$onCreate[$hookKey]);
Expand Down
10 changes: 10 additions & 0 deletions tests/Mock/Repository/ConfigurationNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Tests\Mock\Repository;

use Exception;

class ConfigurationNotFoundException extends Exception
{

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

namespace Tests\Mock\Repository;

use Exception;
use stdClass;
use Zrnik\MkSQL\Repository\CustomTypeConverter;

class HookExampleConverter extends CustomTypeConverter
{
public ?stdClass $configuration = null;

public function serialize(mixed $value): string|bool
{
/** @noinspection JsonEncodingApiUsageInspection */
$encoded = json_encode($value);

if($this->configuration === null) {
throw new ConfigurationNotFoundException('Configuration not found!');
}


if($encoded === false) {
throw new Exception('Should not be false!');
}

return $encoded;
}

public function deserialize(mixed $value): mixed
{
/** @noinspection JsonEncodingApiUsageInspection */
$decoded = json_decode($value, true);

if($this->configuration === null) {
throw new ConfigurationNotFoundException('Configuration not found!');
}

return $decoded;
}

public function getDatabaseType(): string
{
return 'longtext';
}
}
63 changes: 63 additions & 0 deletions tests/Repository/CustomTypeConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace Tests\Repository;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Tests\Mock\Repository\ConfigurationNotFoundException;
use Tests\Mock\Repository\HookExampleConverter;
use Zrnik\MkSQL\Repository\CustomTypeConverter;
use Zrnik\PHPUnit\Exceptions;

class CustomTypeConverterTest extends TestCase
{
use Exceptions;

public string $testProperty;

public function testHook(): void
{
$thisReflection = new ReflectionClass($this);

/** @var HookExampleConverter $converter */
$converter = CustomTypeConverter::initialize(
HookExampleConverter::class, $thisReflection->getProperty('testProperty')
);

$exampleArray = ['hello', 'world'];

$this->assertExceptionThrown(
ConfigurationNotFoundException::class,
function () use ($converter, $exampleArray) {
$converter->serialize($exampleArray);
}
);

CustomTypeConverter::addOnCreate(
static function (CustomTypeConverter $converter) {
if($converter instanceof HookExampleConverter) {
$converter->configuration = new stdClass();
}
}
);

//Re-create the converter, so the hook has a chance to get applied:

/** @var HookExampleConverter $converter */
$converter = CustomTypeConverter::initialize(
HookExampleConverter::class, $thisReflection->getProperty('testProperty')
);


$this->assertNoExceptionThrown(
function () use ($converter, $exampleArray) {
$converter->serialize($exampleArray);
}
);


}


}

0 comments on commit 6158bec

Please sign in to comment.