-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added hooks to the custom type converter, so you can for example atta…
…ch injections with DI...
- Loading branch information
Showing
4 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
|
||
|
||
} | ||
|
||
|
||
} |