1 | <?php |
||
36 | class ProxyQueryTest extends TestCase |
||
37 | { |
||
38 | /** |
||
39 | * @var EntityManager |
||
40 | */ |
||
41 | private $em; |
||
42 | |||
43 | public static function setUpBeforeClass(): void |
||
44 | { |
||
45 | if (!Type::hasType('uuid')) { |
||
46 | Type::addType('uuid', UuidType::class); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | protected function setUp(): void |
||
51 | { |
||
52 | $this->em = DoctrineTestHelper::createTestEntityManager(); |
||
53 | |||
54 | $schemaTool = new SchemaTool($this->em); |
||
55 | $classes = [ |
||
56 | $this->em->getClassMetadata(DoubleNameEntity::class), |
||
57 | ]; |
||
58 | |||
59 | try { |
||
60 | $schemaTool->dropSchema($classes); |
||
61 | } catch (\Exception $e) { |
||
|
|||
62 | } |
||
63 | |||
64 | try { |
||
65 | $schemaTool->createSchema($classes); |
||
66 | } catch (\Exception $e) { |
||
67 | } |
||
68 | } |
||
69 | |||
70 | protected function tearDown(): void |
||
71 | { |
||
72 | $this->em = null; |
||
73 | } |
||
74 | |||
75 | public function dataGetFixedQueryBuilder() |
||
76 | { |
||
77 | return [ |
||
78 | ['aaa', 'bbb', 'id', 'id_idx', 33, Type::INTEGER, true], |
||
79 | ['aaa', 'bbb', 'associatedId', 'associatedId_idx', 33, null, true], |
||
80 | ['aaa', 'bbb', 'id.value', 'id_value_idx', 33, Type::INTEGER, false], |
||
81 | ['aaa', 'bbb', 'id.uuid', 'id_uuid_idx', new NonIntegerIdentifierTestClass('80fb6f91-bba1-4d35-b3d4-e06b24494e85'), UuidType::NAME, false], |
||
82 | ]; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @dataProvider dataGetFixedQueryBuilder |
||
87 | */ |
||
88 | public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType, $distinct): void |
||
89 | { |
||
90 | $meta = $this->createMock(ClassMetadata::class); |
||
91 | $meta->expects($this->any()) |
||
92 | ->method('getIdentifierFieldNames') |
||
93 | ->willReturn([$id]); |
||
94 | $meta->expects($this->any()) |
||
95 | ->method('getTypeOfField') |
||
96 | ->willReturn($identifierType); |
||
97 | |||
98 | $mf = $this->createMock(ClassMetadataFactory::class); |
||
99 | $mf->expects($this->any()) |
||
100 | ->method('getMetadataFor') |
||
101 | ->with($this->equalTo($class)) |
||
102 | ->willReturn($meta); |
||
103 | |||
104 | $platform = $this->createMock(PostgreSqlPlatform::class); |
||
105 | |||
106 | $conn = $this->createMock(Connection::class); |
||
107 | $conn->expects($this->any()) |
||
108 | ->method('getDatabasePlatform') |
||
109 | ->willReturn($platform); |
||
110 | |||
111 | $em = $this->createMock(EntityManager::class); |
||
112 | $em->expects($this->any()) |
||
113 | ->method('getMetadataFactory') |
||
114 | ->willReturn($mf); |
||
115 | $em->expects($this->any()) |
||
116 | ->method('getConnection') |
||
117 | ->willReturn($conn); |
||
118 | |||
119 | $q = $this->getMockBuilder(AbstractQuery::class) |
||
120 | ->disableOriginalConstructor() |
||
121 | ->setMethods(['setHint', 'execute']) |
||
122 | ->getMockForAbstractClass(); |
||
123 | $q->expects($this->once()) |
||
124 | ->method('setHint') |
||
125 | ->willReturn($q); |
||
126 | $q->expects($this->any()) |
||
127 | ->method('execute') |
||
128 | ->willReturn([[$id => $value]]); |
||
129 | |||
130 | $qb = $this->getMockBuilder(QueryBuilder::class) |
||
131 | ->setConstructorArgs([$em]) |
||
132 | ->getMock(); |
||
133 | $qb->expects($this->once()) |
||
134 | ->method('distinct') |
||
135 | ->with($this->equalTo($distinct)); |
||
136 | $qb->expects($this->any()) |
||
137 | ->method('getEntityManager') |
||
138 | ->willReturn($em); |
||
139 | $qb->expects($this->any()) |
||
140 | ->method('getQuery') |
||
141 | ->willReturn($q); |
||
142 | $qb->expects($this->once()) |
||
143 | ->method('setParameter') |
||
144 | ->with($this->equalTo($expectedId), $this->equalTo([$value])); |
||
145 | $qb->expects($this->any()) |
||
146 | ->method('getDQLPart') |
||
147 | ->willReturnCallback(static function ($part) use ($class, $alias) { |
||
148 | $parts = [ |
||
149 | 'from' => [new From($class, $alias)], |
||
150 | 'orderBy' => [new OrderBy('whatever', 'DESC')], |
||
151 | ]; |
||
152 | |||
153 | return $parts[$part]; |
||
154 | }); |
||
155 | $qb->expects($this->once()) |
||
156 | ->method('addOrderBy') |
||
157 | ->with("$alias.$id", null); |
||
158 | $qb->expects($this->once()) |
||
159 | ->method('getRootEntities') |
||
160 | ->willReturn([$class]); |
||
161 | $qb->expects($this->exactly(2)) |
||
162 | ->method('getRootAliases') |
||
163 | ->willReturn([$alias]); |
||
164 | |||
165 | $pq = $this->getMockBuilder(ProxyQuery::class) |
||
166 | ->setConstructorArgs([$qb]) |
||
167 | ->setMethods(['a']) |
||
168 | ->getMock(); |
||
169 | |||
170 | $pq->setDistinct($distinct); |
||
171 | |||
172 | /* Work */ |
||
173 | $pq->execute(); |
||
174 | } |
||
175 | |||
176 | public function testSetHint(): void |
||
200 | |||
201 | public function testSortOrderValidatesItsInput(): void |
||
210 | |||
211 | public function validSortOrders() |
||
222 | |||
223 | /** |
||
224 | * @dataProvider validSortOrders |
||
225 | */ |
||
226 | public function testItAllowsSortOrdersWithStrangeCase($validValue): void |
||
232 | |||
233 | public function testExecuteWithOrderBy(): void |
||
234 | { |
||
235 | $entity1 = new DoubleNameEntity(1, 'Foo', 'Bar'); |
||
236 | $entity2 = new DoubleNameEntity(2, 'Bar', 'Bar'); |
||
237 | $entity3 = new DoubleNameEntity(3, 'Bar', 'Foo'); |
||
272 | } |
||
273 |