-
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.
Add tests to Doctrine Types functionality
- Loading branch information
Showing
18 changed files
with
454 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
composer.lock | ||
src/_generated | ||
var/ | ||
vendor/ |
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
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
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
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,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\Attribute; | ||
|
||
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineType; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(DoctrineType::class)] | ||
class DoctrineTypeTest extends TestCase | ||
{ | ||
#[Test] | ||
public function name_can_be_specified(): void | ||
{ | ||
$sut = new DoctrineType( | ||
name: 'custom_name', | ||
type: 'string', | ||
); | ||
|
||
$this->assertEquals('custom_name', $sut->name); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\Types; | ||
|
||
use Headsnet\DoctrineToolsBundle\HeadsnetDoctrineToolsBundle; | ||
use Headsnet\DoctrineToolsBundle\Types\CandidateType; | ||
use Headsnet\DoctrineToolsBundle\Types\DoctrineTypesCompilerPass; | ||
use Headsnet\DoctrineToolsBundle\Types\StandardTypes\IntegerMappingPrototype; | ||
use Headsnet\DoctrineToolsBundle\Types\StandardTypes\StringMappingPrototype; | ||
use Headsnet\DoctrineToolsBundle\Types\StandardTypes\UuidMappingPrototype; | ||
use Nyholm\BundleTest\TestKernel; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
#[CoversClass(DoctrineTypesCompilerPass::class)] | ||
#[CoversClass(StringMappingPrototype::class)] | ||
#[CoversClass(IntegerMappingPrototype::class)] | ||
#[CoversClass(UuidMappingPrototype::class)] | ||
#[CoversClass(CandidateType::class)] | ||
class DoctrineTypesCompilerPassTest extends KernelTestCase | ||
{ | ||
protected static function getKernelClass(): string | ||
{ | ||
return TestKernel::class; | ||
} | ||
|
||
/** | ||
* @param array{debug?: bool, environment?: string} $options | ||
*/ | ||
protected static function createKernel(array $options = []): KernelInterface | ||
{ | ||
/** @var TestKernel $kernel $kernel */ | ||
$kernel = parent::createKernel($options); | ||
$kernel->addTestBundle(HeadsnetDoctrineToolsBundle::class); | ||
$kernel->addTestConfig(__DIR__ . '/Fixtures/config.yaml'); | ||
$kernel->handleOptions($options); | ||
|
||
return $kernel; | ||
} | ||
|
||
#[Test] | ||
public function can_find_and_register_types(): void | ||
{ | ||
$kernel = self::bootKernel(); | ||
$container = $kernel->getContainer(); | ||
$result = $container->getParameter('doctrine.dbal.connection_factory.types'); | ||
|
||
$expected = [ | ||
'dummy_string' => [ | ||
'class' => 'Headsnet\DoctrineToolsBundle\_generated\HeadsnetDoctrineTools\Types\DummyStringObjectType', | ||
], | ||
'dummy_integer' => [ | ||
'class' => 'Headsnet\DoctrineToolsBundle\_generated\HeadsnetDoctrineTools\Types\DummyIntegerObjectType', | ||
], | ||
'dummy_uuid' => [ | ||
'class' => 'Headsnet\DoctrineToolsBundle\_generated\HeadsnetDoctrineTools\Types\DummyUuidObjectType', | ||
], | ||
]; | ||
$this->assertEquals($expected, $result); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\Types\Fixtures; | ||
|
||
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineType; | ||
|
||
#[DoctrineType(name: 'dummy_integer', type: 'integer')] | ||
class DummyIntegerObject | ||
{ | ||
public function __construct( | ||
private readonly int $value | ||
) { | ||
} | ||
|
||
public static function create(int $value): self | ||
{ | ||
return new self($value); | ||
} | ||
|
||
public function asInteger(): int | ||
{ | ||
return $this->value; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\Types\Fixtures; | ||
|
||
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineType; | ||
|
||
#[DoctrineType(name: 'dummy_string', type: 'string')] | ||
class DummyStringObject | ||
{ | ||
public function __construct( | ||
private readonly string $value | ||
) { | ||
} | ||
|
||
public static function create(string $value): self | ||
{ | ||
return new self($value); | ||
} | ||
|
||
public function asString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\Types\Fixtures; | ||
|
||
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineType; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
#[DoctrineType(name: 'dummy_uuid', type: 'uuid')] | ||
class DummyUuidObject | ||
{ | ||
public function __construct( | ||
private readonly Uuid $value | ||
) { | ||
} | ||
|
||
public static function create(Uuid $value): self | ||
{ | ||
return new self($value); | ||
} | ||
|
||
public static function fromString(string $value): self | ||
{ | ||
return new self(Uuid::fromString($value)); | ||
} | ||
|
||
public function asString(): string | ||
{ | ||
return $this->value->toRfc4122(); | ||
} | ||
} |
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,13 @@ | ||
|
||
headsnet_doctrine_tools: | ||
root_namespace: Headsnet\DoctrineToolsBundle | ||
custom_types: | ||
scan_dirs: | ||
- './tests/Types/Fixtures' | ||
carbon_mappings: | ||
enabled: false | ||
custom_mappings: | ||
scan_dirs: [] | ||
|
||
parameters: | ||
doctrine.dbal.connection_factory.types: [] |
77 changes: 77 additions & 0 deletions
77
tests/Types/StandardTypes/AbstractIntegerMappingTypeTest.php
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,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\DoctrineToolsBundle\Tests\Types\StandardTypes; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Headsnet\DoctrineToolsBundle\Tests\Types\Fixtures\DummyIntegerObject; | ||
use Headsnet\DoctrineToolsBundle\Types\StandardTypes\AbstractIntegerMappingType; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(AbstractIntegerMappingType::class)] | ||
class AbstractIntegerMappingTypeTest extends TestCase | ||
{ | ||
#[Test] | ||
#[DataProvider('phpDataProvider')] | ||
public function converting_to_php_value(int|null $value, object|null $expectedResult): void | ||
{ | ||
$sut = $this->buildSut(); | ||
|
||
$result = $sut->convertToPHPValue( | ||
$value, | ||
$this->createMock(AbstractPlatform::class) | ||
); | ||
|
||
$this->assertEquals($expectedResult, $result); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: int|null, 1: DummyIntegerObject|null}> | ||
*/ | ||
public static function phpDataProvider(): iterable | ||
{ | ||
yield [42, DummyIntegerObject::create(42)]; | ||
yield [null, null]; | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('databaseDataProvider')] | ||
public function converting_to_database_value(object|null $value, int|null $expectedResult): void | ||
{ | ||
$sut = $this->buildSut(); | ||
|
||
$result = $sut->convertToDatabaseValue( | ||
$value, | ||
$this->createMock(AbstractPlatform::class) | ||
); | ||
|
||
$this->assertEquals($expectedResult, $result); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: DummyIntegerObject|null, 1: int|null}> | ||
*/ | ||
public static function databaseDataProvider(): iterable | ||
{ | ||
yield [DummyIntegerObject::create(42), 42]; | ||
yield [null, null]; | ||
} | ||
|
||
private function buildSut(): AbstractIntegerMappingType | ||
{ | ||
return new class() extends AbstractIntegerMappingType { | ||
public function getName(): string | ||
{ | ||
return 'dummy'; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return DummyIntegerObject::class; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.