|
15 | 15 | use ShipMonk\Doctrine\Walker\Handlers\CommentWholeSqlHintHandler; |
16 | 16 | use ShipMonk\Doctrine\Walker\Handlers\LowercaseSelectHintHandler; |
17 | 17 | use Symfony\Component\Cache\Adapter\ArrayAdapter; |
18 | | -use function sprintf; |
19 | 18 |
|
20 | 19 | class HintDrivenSqlWalkerTest extends TestCase |
21 | 20 | { |
@@ -77,47 +76,74 @@ public function testPagination(): void |
77 | 76 | */ |
78 | 77 | public static function walksProvider(): iterable |
79 | 78 | { |
80 | | - $selectDql = sprintf('SELECT w FROM %s w', DummyEntity::class); |
| 79 | + $selectQueryCallback = static function (EntityManager $entityManager): Query { |
| 80 | + return $entityManager->createQueryBuilder() |
| 81 | + ->select('w') |
| 82 | + ->from(DummyEntity::class, 'w') |
| 83 | + ->getQuery(); |
| 84 | + }; |
| 85 | + |
| 86 | + $selectWithLimitQueryCallback = static function (EntityManager $entityManager): Query { |
| 87 | + return $entityManager->createQueryBuilder() |
| 88 | + ->select('w') |
| 89 | + ->from(DummyEntity::class, 'w') |
| 90 | + ->setMaxResults(1) |
| 91 | + ->getQuery(); |
| 92 | + }; |
| 93 | + |
| 94 | + $updateQueryCallback = static function (EntityManager $entityManager): Query { |
| 95 | + return $entityManager->createQueryBuilder() |
| 96 | + ->update(DummyEntity::class, 'w') |
| 97 | + ->set('w.id', 1) |
| 98 | + ->getQuery(); |
| 99 | + }; |
| 100 | + |
| 101 | + $deleteQueryCallback = static function (EntityManager $entityManager): Query { |
| 102 | + return $entityManager->createQueryBuilder() |
| 103 | + ->delete(DummyEntity::class, 'w') |
| 104 | + ->getQuery(); |
| 105 | + }; |
81 | 106 |
|
82 | 107 | yield 'Lowercase select' => [ |
83 | | - static fn(EntityManager $entityManager): Query => $entityManager->createQuery($selectDql), |
| 108 | + $selectQueryCallback, |
84 | 109 | LowercaseSelectHintHandler::class, |
85 | 110 | null, |
86 | 111 | 'select d0_.id AS id_0 FROM dummy_entity d0_', |
87 | 112 | ]; |
88 | 113 |
|
| 114 | + yield 'Lowercase select with LIMIT' => [ |
| 115 | + $selectWithLimitQueryCallback, |
| 116 | + LowercaseSelectHintHandler::class, |
| 117 | + null, |
| 118 | + 'select d0_.id AS id_0 FROM dummy_entity d0_ LIMIT 1', |
| 119 | + ]; |
| 120 | + |
89 | 121 | yield 'Comment whole sql - select' => [ |
90 | | - static fn(EntityManager $entityManager): Query => $entityManager->createQuery($selectDql), |
| 122 | + $selectQueryCallback, |
91 | 123 | CommentWholeSqlHintHandler::class, |
92 | 124 | 'custom comment', |
93 | 125 | 'SELECT d0_.id AS id_0 FROM dummy_entity d0_ -- custom comment', |
94 | 126 | ]; |
95 | 127 |
|
96 | | - yield 'Comment whole sql - update' => [ |
97 | | - static fn(EntityManager $entityManager): Query => $entityManager->createQuery(sprintf('UPDATE %s w SET w.id = 1', DummyEntity::class)), |
| 128 | + yield 'Comment whole sql - select with LIMIT' => [ |
| 129 | + $selectWithLimitQueryCallback, |
98 | 130 | CommentWholeSqlHintHandler::class, |
99 | 131 | 'custom comment', |
100 | | - 'UPDATE dummy_entity SET id = 1 -- custom comment', |
| 132 | + 'SELECT d0_.id AS id_0 FROM dummy_entity d0_ -- custom comment LIMIT 1', // see readme limitations |
101 | 133 | ]; |
102 | 134 |
|
103 | | - yield 'Comment whole sql - delete' => [ |
104 | | - static fn(EntityManager $entityManager): Query => $entityManager->createQuery(sprintf('DELETE FROM %s w', DummyEntity::class)), |
| 135 | + yield 'Comment whole sql - update' => [ |
| 136 | + $updateQueryCallback, |
105 | 137 | CommentWholeSqlHintHandler::class, |
106 | 138 | 'custom comment', |
107 | | - 'DELETE FROM dummy_entity -- custom comment', |
| 139 | + 'UPDATE dummy_entity SET id = 1 -- custom comment', |
108 | 140 | ]; |
109 | 141 |
|
110 | | - yield 'Comment whole sql with LIMIT' => [ |
111 | | - static function (EntityManager $entityManager): Query { |
112 | | - return $entityManager->createQueryBuilder() |
113 | | - ->select('w') |
114 | | - ->from(DummyEntity::class, 'w') |
115 | | - ->setMaxResults(1) |
116 | | - ->getQuery(); |
117 | | - }, |
| 142 | + yield 'Comment whole sql - delete' => [ |
| 143 | + $deleteQueryCallback, |
118 | 144 | CommentWholeSqlHintHandler::class, |
119 | 145 | 'custom comment', |
120 | | - 'SELECT d0_.id AS id_0 FROM dummy_entity d0_ -- custom comment LIMIT 1', // see readme limitations |
| 146 | + 'DELETE FROM dummy_entity -- custom comment', |
121 | 147 | ]; |
122 | 148 | } |
123 | 149 |
|
|
0 commit comments