-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAbstractDoctrineDatabaseTest.php
More file actions
176 lines (156 loc) · 5.61 KB
/
Copy pathAbstractDoctrineDatabaseTest.php
File metadata and controls
176 lines (156 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Tests\CorePersistence\Gateway;
use DateTimeImmutable;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Types\Types;
use Ibexa\Contracts\CorePersistence\Gateway\AbstractDoctrineDatabase;
use Ibexa\Contracts\CorePersistence\Gateway\DoctrineSchemaMetadata;
use Ibexa\Contracts\CorePersistence\Gateway\DoctrineSchemaMetadataInterface;
use Ibexa\Contracts\CorePersistence\Gateway\DoctrineSchemaMetadataRegistryInterface;
use PHPUnit\Framework\TestCase;
/**
* @covers \Ibexa\Contracts\CorePersistence\Gateway\AbstractDoctrineDatabase
*/
final class AbstractDoctrineDatabaseTest extends TestCase
{
/**
* @dataProvider provideForTestDateTimeQueries
*
* @param array<mixed|\Doctrine\Common\Collections\Expr\Comparison> $query
* @param array<array-key, string> $expectedParameters
*/
public function testDateTimeQueries(
array $query,
string $sql,
array $expectedParameters
): void {
$connection = $this->createConnectionMock();
$database = $this->createDatabaseGateway($connection);
$result = $this->createMock(ResultStatement::class);
$result->expects(self::once())
->method('fetch')
->willReturn(false);
$connection->expects(self::once())
->method('executeQuery')
->with(
self::identicalTo($sql),
self::identicalTo($expectedParameters),
)
->willReturn($result);
$database->findBy($query);
}
/**
* @return iterable<array{
* array<mixed|\Doctrine\Common\Collections\Expr\Comparison>,
* non-empty-string,
* array<array-key, string>,
* }>
*/
public static function provideForTestDateTimeQueries(): iterable
{
yield [
[
new Comparison(
'date_time_immutable_column',
Comparison::LT,
new DateTimeImmutable('2024-01-01 00:00:00'),
),
],
str_replace(
"\n",
' ',
<<<SQL
SELECT __foo_table__.id, __foo_table__.date_time_immutable_column
FROM __foo_table__ __foo_table__
WHERE __foo_table__.date_time_immutable_column < :date_time_immutable_column_0
SQL,
),
[
'date_time_immutable_column_0' => '2024-01-01 00:00:00',
],
];
yield [
[
'date_time_immutable_column' => new DateTimeImmutable('2024-01-01 00:00:00'),
],
str_replace(
"\n",
' ',
<<<SQL
SELECT __foo_table__.id, __foo_table__.date_time_immutable_column
FROM __foo_table__ __foo_table__ WHERE __foo_table__.date_time_immutable_column = ?
SQL,
),
[
1 => '2024-01-01 00:00:00',
],
];
}
/**
* @return \Ibexa\Contracts\CorePersistence\Gateway\AbstractDoctrineDatabase<array<mixed>>
*/
private function createDatabaseGateway(Connection $connection): AbstractDoctrineDatabase
{
$registry = $this->createMock(DoctrineSchemaMetadataRegistryInterface::class);
$metadata = new DoctrineSchemaMetadata(
$connection,
null,
'__foo_table__',
[
'id' => Types::INTEGER,
'date_time_immutable_column' => Types::DATETIME_IMMUTABLE,
],
['id'],
);
$registry->expects(self::atLeastOnce())
->method('getMetadataForTable')
->with(self::identicalTo('__foo_table__'))
->willReturn($metadata);
return new class($connection, $registry, $metadata) extends AbstractDoctrineDatabase {
private DoctrineSchemaMetadata $metadata;
public function __construct(
Connection $connection,
DoctrineSchemaMetadataRegistryInterface $registry,
DoctrineSchemaMetadata $metadata
) {
parent::__construct($connection, $registry);
$this->metadata = $metadata;
}
protected function getTableName(): string
{
return '__foo_table__';
}
protected function buildMetadata(): DoctrineSchemaMetadataInterface
{
return $this->metadata;
}
};
}
/**
* @return \Doctrine\DBAL\Connection&\PHPUnit\Framework\MockObject\MockObject
*/
private function createConnectionMock(): Connection
{
$connection = $this->createMock(Connection::class);
$connection->expects(self::once())
->method('createQueryBuilder')
->willReturn(new QueryBuilder($connection));
$connection->expects(self::atLeastOnce())
->method('getDatabasePlatform')
->willReturn(new PostgreSQL94Platform());
$connection->expects(self::atLeastOnce())
->method('getExpressionBuilder')
->willReturn(new ExpressionBuilder($connection));
return $connection;
}
}