Skip to content

Commit d25a076

Browse files
chore: test the BaseType and BaseFunction abstractions (#309)
1 parent 67c344e commit d25a076

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types;
6+
7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use MartinGeorgiev\Doctrine\DBAL\Types\BaseType;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class BaseTypeTest extends TestCase
13+
{
14+
/**
15+
* @var AbstractPlatform&MockObject
16+
*/
17+
private AbstractPlatform $platform;
18+
19+
protected function setUp(): void
20+
{
21+
$this->platform = $this->createMock(AbstractPlatform::class);
22+
}
23+
24+
/**
25+
* @test
26+
*/
27+
public function throws_exception_when_type_name_not_configured(): void
28+
{
29+
$type = new class extends BaseType {
30+
protected const TYPE_NAME = '';
31+
};
32+
33+
$this->expectException(\LogicException::class);
34+
$this->expectExceptionMessage('no meaningful value for TYPE_NAME constant');
35+
36+
$type->getName();
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function throws_exception_when_getting_sql_declaration_with_no_type_name(): void
43+
{
44+
$type = new class extends BaseType {
45+
protected const TYPE_NAME = '';
46+
};
47+
48+
$this->expectException(\LogicException::class);
49+
$this->expectExceptionMessage('Doctrine type defined in class');
50+
51+
$type->getSQLDeclaration([], $this->platform);
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function returns_correct_type_name(): void
58+
{
59+
$type = new class extends BaseType {
60+
protected const TYPE_NAME = 'custom_type';
61+
};
62+
63+
self::assertEquals('custom_type', $type->getName());
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
public function gets_correct_sql_declaration(): void
70+
{
71+
$type = new class extends BaseType {
72+
protected const TYPE_NAME = 'custom_type';
73+
};
74+
75+
$this->platform
76+
->expects(self::once())
77+
->method('getDoctrineTypeMapping')
78+
->with('custom_type')
79+
->willReturn('CUSTOM_SQL_TYPE');
80+
81+
$result = $type->getSQLDeclaration([], $this->platform);
82+
self::assertEquals('CUSTOM_SQL_TYPE', $result);
83+
}
84+
85+
/**
86+
* @test
87+
*/
88+
public function requires_sql_comment_hint_returns_false(): void
89+
{
90+
$type = new class extends BaseType {
91+
protected const TYPE_NAME = 'custom_type';
92+
};
93+
94+
// @phpstan-ignore-next-line Not all Doctrine version like this method as it's deprecated. For now, we ignore the deprecation.
95+
self::assertFalse($type->requiresSQLCommentHint($this->platform));
96+
}
97+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;
6+
7+
use Doctrine\ORM\Query\AST\Node;
8+
use Doctrine\ORM\Query\SqlWalker;
9+
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseFunction;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class BaseFunctionTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*/
17+
public function get_sql_returns_formatted_function_call(): void
18+
{
19+
$function = new class('test_function') extends BaseFunction {
20+
// @phpstan-ignore-next-line The type for $name is not string in older Doctrine versions
21+
public function __construct(string $name)
22+
{
23+
parent::__construct($name);
24+
$this->customizeFunction();
25+
}
26+
27+
protected function customizeFunction(): void
28+
{
29+
$this->setFunctionPrototype('TEST(%s, %s)');
30+
}
31+
32+
/**
33+
* @param list<Node|null> $nodes
34+
*/
35+
public function setNodes(array $nodes): void
36+
{
37+
$this->nodes = $nodes;
38+
}
39+
};
40+
41+
$node1 = $this->createMock(Node::class);
42+
$node1->expects($this->once())
43+
->method('dispatch')
44+
->willReturn('arg1');
45+
46+
$node2 = $this->createMock(Node::class);
47+
$node2->expects($this->once())
48+
->method('dispatch')
49+
->willReturn('arg2');
50+
51+
/** @var list<Node|null> $nodes */
52+
$nodes = [$node1, $node2];
53+
$function->setNodes($nodes);
54+
55+
$sqlWalker = $this->createMock(SqlWalker::class);
56+
57+
$result = $function->getSql($sqlWalker);
58+
$this->assertEquals('TEST(arg1, arg2)', $result);
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function get_sql_handles_null_nodes(): void
65+
{
66+
$function = new class('test_function') extends BaseFunction {
67+
// @phpstan-ignore-next-line The type for $name is not string in older Doctrine versions
68+
public function __construct(string $name)
69+
{
70+
parent::__construct($name);
71+
$this->customizeFunction();
72+
}
73+
74+
protected function customizeFunction(): void
75+
{
76+
$this->setFunctionPrototype('TEST(%s)');
77+
}
78+
79+
/**
80+
* @param list<Node|null> $nodes
81+
*/
82+
public function setNodes(array $nodes): void
83+
{
84+
$this->nodes = $nodes;
85+
}
86+
};
87+
88+
/** @var list<Node|null> $nodes */
89+
$nodes = [null];
90+
$function->setNodes($nodes);
91+
92+
$sqlWalker = $this->createMock(SqlWalker::class);
93+
94+
$result = $function->getSql($sqlWalker);
95+
$this->assertEquals('TEST(null)', $result);
96+
}
97+
}

0 commit comments

Comments
 (0)