diff --git a/tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseTypeTest.php b/tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseTypeTest.php new file mode 100644 index 00000000..0ef5af3a --- /dev/null +++ b/tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseTypeTest.php @@ -0,0 +1,97 @@ +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'; + }; + + // @phpstan-ignore-next-line Not all Doctrine version like this method as it's deprecated. For now, we ignore the deprecation. + self::assertFalse($type->requiresSQLCommentHint($this->platform)); + } +} diff --git a/tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseFunctionTest.php b/tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseFunctionTest.php new file mode 100644 index 00000000..e9de6fd7 --- /dev/null +++ b/tests/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseFunctionTest.php @@ -0,0 +1,97 @@ +customizeFunction(); + } + + protected function customizeFunction(): void + { + $this->setFunctionPrototype('TEST(%s, %s)'); + } + + /** + * @param list $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 $nodes */ + $nodes = [$node1, $node2]; + $function->setNodes($nodes); + + $sqlWalker = $this->createMock(SqlWalker::class); + + $result = $function->getSql($sqlWalker); + $this->assertEquals('TEST(arg1, arg2)', $result); + } + + /** + * @test + */ + public function get_sql_handles_null_nodes(): void + { + $function = new class('test_function') extends BaseFunction { + // @phpstan-ignore-next-line The type for $name is not string in older Doctrine versions + public function __construct(string $name) + { + parent::__construct($name); + $this->customizeFunction(); + } + + protected function customizeFunction(): void + { + $this->setFunctionPrototype('TEST(%s)'); + } + + /** + * @param list $nodes + */ + public function setNodes(array $nodes): void + { + $this->nodes = $nodes; + } + }; + + /** @var list $nodes */ + $nodes = [null]; + $function->setNodes($nodes); + + $sqlWalker = $this->createMock(SqlWalker::class); + + $result = $function->getSql($sqlWalker); + $this->assertEquals('TEST(null)', $result); + } +}