-
-
Notifications
You must be signed in to change notification settings - Fork 56
chore: test the BaseType and BaseFunction abstractions
#309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types; | ||
|
|
||
| use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
| use MartinGeorgiev\Doctrine\DBAL\Types\BaseType; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class BaseTypeTest extends TestCase | ||
| { | ||
| /** | ||
| * @var AbstractPlatform&MockObject | ||
| */ | ||
| private AbstractPlatform $platform; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->platform = $this->createMock(AbstractPlatform::class); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function throws_exception_when_type_name_not_configured(): void | ||
| { | ||
| $type = new class extends BaseType { | ||
| protected const TYPE_NAME = ''; | ||
| }; | ||
|
|
||
| $this->expectException(\LogicException::class); | ||
| $this->expectExceptionMessage('no meaningful value for TYPE_NAME constant'); | ||
|
|
||
| $type->getName(); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function throws_exception_when_getting_sql_declaration_with_no_type_name(): void | ||
| { | ||
| $type = new class extends BaseType { | ||
| protected const TYPE_NAME = ''; | ||
| }; | ||
|
|
||
| $this->expectException(\LogicException::class); | ||
| $this->expectExceptionMessage('Doctrine type defined in class'); | ||
|
|
||
| $type->getSQLDeclaration([], $this->platform); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function returns_correct_type_name(): void | ||
| { | ||
| $type = new class extends BaseType { | ||
| protected const TYPE_NAME = 'custom_type'; | ||
| }; | ||
|
|
||
| self::assertEquals('custom_type', $type->getName()); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function gets_correct_sql_declaration(): void | ||
| { | ||
| $type = new class extends BaseType { | ||
| protected const TYPE_NAME = 'custom_type'; | ||
| }; | ||
|
|
||
| $this->platform | ||
| ->expects(self::once()) | ||
| ->method('getDoctrineTypeMapping') | ||
| ->with('custom_type') | ||
| ->willReturn('CUSTOM_SQL_TYPE'); | ||
|
|
||
| $result = $type->getSQLDeclaration([], $this->platform); | ||
| self::assertEquals('CUSTOM_SQL_TYPE', $result); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function requires_sql_comment_hint_returns_false(): void | ||
| { | ||
| $type = new class extends BaseType { | ||
| protected const TYPE_NAME = 'custom_type'; | ||
| }; | ||
|
|
||
| self::assertFalse($type->requiresSQLCommentHint($this->platform)); | ||
|
Check failure on line 94 in tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseTypeTest.php
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; | ||
|
|
||
| use Doctrine\ORM\Query\AST\Node; | ||
| use Doctrine\ORM\Query\SqlWalker; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseFunction; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class BaseFunctionTest extends TestCase | ||
| { | ||
| /** | ||
| * @test | ||
| */ | ||
| public function get_sql_returns_formatted_function_call(): void | ||
| { | ||
| $function = new class('test_function') extends BaseFunction { | ||
| public function __construct(string $name) | ||
|
Check failure on line 20 in tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseFunctionTest.php
|
||
| { | ||
| parent::__construct($name); | ||
| $this->customizeFunction(); | ||
| } | ||
|
|
||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('TEST(%s, %s)'); | ||
| } | ||
|
|
||
| /** | ||
| * @param list<Node|null> $nodes | ||
| */ | ||
| public function setNodes(array $nodes): void | ||
| { | ||
| $this->nodes = $nodes; | ||
| } | ||
| }; | ||
|
|
||
| $node1 = $this->createMock(Node::class); | ||
| $node1->expects($this->once()) | ||
| ->method('dispatch') | ||
| ->willReturn('arg1'); | ||
|
|
||
| $node2 = $this->createMock(Node::class); | ||
| $node2->expects($this->once()) | ||
| ->method('dispatch') | ||
| ->willReturn('arg2'); | ||
|
|
||
| /** @var list<Node|null> $nodes */ | ||
| $nodes = [$node1, $node2]; | ||
| $function->setNodes($nodes); | ||
|
|
||
| $sqlWalker = $this->createMock(SqlWalker::class); | ||
|
|
||
| $result = $function->getSql($sqlWalker); | ||
| $this->assertEquals('TEST(arg1, arg2)', $result); | ||
| } | ||
martin-georgiev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function get_sql_handles_null_nodes(): void | ||
| { | ||
| $function = new class('test_function') extends BaseFunction { | ||
| public function __construct(string $name) | ||
|
Check failure on line 66 in tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseFunctionTest.php
|
||
| { | ||
| parent::__construct($name); | ||
| $this->customizeFunction(); | ||
| } | ||
|
|
||
| protected function customizeFunction(): void | ||
| { | ||
| $this->setFunctionPrototype('TEST(%s)'); | ||
| } | ||
|
|
||
| /** | ||
| * @param list<Node|null> $nodes | ||
| */ | ||
| public function setNodes(array $nodes): void | ||
| { | ||
| $this->nodes = $nodes; | ||
| } | ||
| }; | ||
|
|
||
| /** @var list<Node|null> $nodes */ | ||
| $nodes = [null]; | ||
| $function->setNodes($nodes); | ||
|
|
||
| $sqlWalker = $this->createMock(SqlWalker::class); | ||
|
|
||
| $result = $function->getSql($sqlWalker); | ||
| $this->assertEquals('TEST(null)', $result); | ||
| } | ||
martin-georgiev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.