Ecodev /
graphql-doctrine
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace GraphQLTests\Doctrine; |
||
| 6 | |||
| 7 | use GraphQL\Doctrine\Definition\Operator\AbstractOperator; |
||
| 8 | use GraphQL\Doctrine\Definition\Operator\BetweenOperatorType; |
||
| 9 | use GraphQL\Doctrine\Definition\Operator\ContainOperatorType; |
||
| 10 | use GraphQL\Doctrine\Definition\Operator\EmptyOperatorType; |
||
| 11 | use GraphQL\Doctrine\Definition\Operator\EqualOperatorType; |
||
| 12 | use GraphQL\Doctrine\Definition\Operator\GreaterOperatorType; |
||
| 13 | use GraphQL\Doctrine\Definition\Operator\GreaterOrEqualOperatorType; |
||
| 14 | use GraphQL\Doctrine\Definition\Operator\InOperatorType; |
||
| 15 | use GraphQL\Doctrine\Definition\Operator\LessOperatorType; |
||
| 16 | use GraphQL\Doctrine\Definition\Operator\LessOrEqualOperatorType; |
||
| 17 | use GraphQL\Doctrine\Definition\Operator\LikeOperatorType; |
||
| 18 | use GraphQL\Doctrine\Definition\Operator\NullOperatorType; |
||
| 19 | use GraphQL\Doctrine\Factory\UniqueNameFactory; |
||
| 20 | use GraphQL\Type\Definition\Type; |
||
| 21 | use GraphQLTests\Doctrine\Blog\Model\Post; |
||
| 22 | |||
| 23 | class OperatorsTest extends \PHPUnit\Framework\TestCase |
||
| 24 | { |
||
| 25 | use TypesTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @dataProvider providerOperator |
||
| 29 | * |
||
| 30 | * @param string $expected |
||
| 31 | * @param string $className |
||
| 32 | * @param array $args |
||
| 33 | */ |
||
| 34 | public function testOperator(string $expected, string $className, array $args): void |
||
| 35 | { |
||
| 36 | /** @var AbstractOperator $operator */ |
||
| 37 | $operator = new $className($this->types, Type::string()); |
||
| 38 | $uniqueNameFactory = new UniqueNameFactory(); |
||
| 39 | $metadata = $this->entityManager->getClassMetadata(Post::class); |
||
| 40 | $queryBuilder = $this->entityManager->createQueryBuilder(); |
||
| 41 | $alias = 'alias'; |
||
| 42 | $field = 'field'; |
||
| 43 | |||
| 44 | $actual = $operator->getDqlCondition($metadata, $queryBuilder, $alias, $field, $args, $uniqueNameFactory); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 45 | |||
| 46 | self::assertEquals($expected, $actual, 'DQL condition should match'); |
||
| 47 | self::assertSame(mb_substr_count($expected, ':'), $queryBuilder->getParameters()->count(), 'should declare the same number of parameters that are actually used'); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function providerOperator(): array |
||
| 51 | { |
||
| 52 | return [ |
||
| 53 | [ |
||
| 54 | 'alias.field BETWEEN :filter1 AND :filter2', |
||
| 55 | BetweenOperatorType::class, |
||
| 56 | [ |
||
| 57 | 'from' => 123, |
||
| 58 | 'to' => 456, |
||
| 59 | 'not' => false, |
||
| 60 | ], |
||
| 61 | ], |
||
| 62 | [ |
||
| 63 | 'alias.field NOT BETWEEN :filter1 AND :filter2', |
||
| 64 | BetweenOperatorType::class, |
||
| 65 | [ |
||
| 66 | 'from' => 123, |
||
| 67 | 'to' => 456, |
||
| 68 | 'not' => true, |
||
| 69 | ], |
||
| 70 | ], |
||
| 71 | [ |
||
| 72 | ':filter1 MEMBER OF alias.field', |
||
| 73 | ContainOperatorType::class, |
||
| 74 | [ |
||
| 75 | 'values' => [123, 456], |
||
| 76 | 'not' => false, |
||
| 77 | ], |
||
| 78 | ], |
||
| 79 | [ |
||
| 80 | ':filter1 NOT MEMBER OF alias.field', |
||
| 81 | ContainOperatorType::class, |
||
| 82 | [ |
||
| 83 | 'values' => [123, 456], |
||
| 84 | 'not' => true, |
||
| 85 | ], |
||
| 86 | ], |
||
| 87 | [ |
||
| 88 | 'alias.field IS EMPTY', |
||
| 89 | EmptyOperatorType::class, |
||
| 90 | [ |
||
| 91 | 'not' => false, |
||
| 92 | ], |
||
| 93 | ], |
||
| 94 | [ |
||
| 95 | 'alias.field IS NOT EMPTY', |
||
| 96 | EmptyOperatorType::class, |
||
| 97 | [ |
||
| 98 | 'not' => true, |
||
| 99 | ], |
||
| 100 | ], |
||
| 101 | [ |
||
| 102 | 'alias.field = :filter1', |
||
| 103 | EqualOperatorType::class, |
||
| 104 | [ |
||
| 105 | 'value' => 123, |
||
| 106 | 'not' => false, |
||
| 107 | ], |
||
| 108 | ], |
||
| 109 | [ |
||
| 110 | 'alias.field != :filter1', |
||
| 111 | EqualOperatorType::class, |
||
| 112 | [ |
||
| 113 | 'value' => 123, |
||
| 114 | 'not' => true, |
||
| 115 | ], |
||
| 116 | ], |
||
| 117 | [ |
||
| 118 | 'alias.field > :filter1', |
||
| 119 | GreaterOperatorType::class, |
||
| 120 | [ |
||
| 121 | 'value' => 123, |
||
| 122 | 'not' => false, |
||
| 123 | ], |
||
| 124 | ], |
||
| 125 | [ |
||
| 126 | 'alias.field <= :filter1', |
||
| 127 | GreaterOperatorType::class, |
||
| 128 | [ |
||
| 129 | 'value' => 123, |
||
| 130 | 'not' => true, |
||
| 131 | ], |
||
| 132 | ], |
||
| 133 | [ |
||
| 134 | 'alias.field >= :filter1', |
||
| 135 | GreaterOrEqualOperatorType::class, |
||
| 136 | [ |
||
| 137 | 'value' => 123, |
||
| 138 | 'not' => false, |
||
| 139 | ], |
||
| 140 | ], |
||
| 141 | [ |
||
| 142 | 'alias.field < :filter1', |
||
| 143 | GreaterOrEqualOperatorType::class, |
||
| 144 | [ |
||
| 145 | 'value' => 123, |
||
| 146 | 'not' => true, |
||
| 147 | ], |
||
| 148 | ], |
||
| 149 | [ |
||
| 150 | 'alias.field IN (:filter1)', |
||
| 151 | InOperatorType::class, |
||
| 152 | [ |
||
| 153 | 'values' => [123, 456], |
||
| 154 | 'not' => false, |
||
| 155 | ], |
||
| 156 | ], |
||
| 157 | [ |
||
| 158 | 'alias.field NOT IN (:filter1)', |
||
| 159 | InOperatorType::class, |
||
| 160 | [ |
||
| 161 | 'values' => [123, 456], |
||
| 162 | 'not' => true, |
||
| 163 | ], |
||
| 164 | ], |
||
| 165 | [ |
||
| 166 | 'alias.field < :filter1', |
||
| 167 | LessOperatorType::class, |
||
| 168 | [ |
||
| 169 | 'value' => 123, |
||
| 170 | 'not' => false, |
||
| 171 | ], |
||
| 172 | ], |
||
| 173 | [ |
||
| 174 | 'alias.field >= :filter1', |
||
| 175 | LessOperatorType::class, |
||
| 176 | [ |
||
| 177 | 'value' => 123, |
||
| 178 | 'not' => true, |
||
| 179 | ], |
||
| 180 | ], |
||
| 181 | [ |
||
| 182 | 'alias.field <= :filter1', |
||
| 183 | LessOrEqualOperatorType::class, |
||
| 184 | [ |
||
| 185 | 'value' => 123, |
||
| 186 | 'not' => false, |
||
| 187 | ], |
||
| 188 | ], |
||
| 189 | [ |
||
| 190 | 'alias.field > :filter1', |
||
| 191 | LessOrEqualOperatorType::class, |
||
| 192 | [ |
||
| 193 | 'value' => 123, |
||
| 194 | 'not' => true, |
||
| 195 | ], |
||
| 196 | ], |
||
| 197 | [ |
||
| 198 | 'alias.field LIKE :filter1', |
||
| 199 | LikeOperatorType::class, |
||
| 200 | [ |
||
| 201 | 'value' => 123, |
||
| 202 | 'not' => false, |
||
| 203 | ], |
||
| 204 | ], |
||
| 205 | [ |
||
| 206 | 'alias.field NOT LIKE :filter1', |
||
| 207 | LikeOperatorType::class, |
||
| 208 | [ |
||
| 209 | 'value' => 123, |
||
| 210 | 'not' => true, |
||
| 211 | ], |
||
| 212 | ], |
||
| 213 | [ |
||
| 214 | 'alias.field IS NULL', |
||
| 215 | NullOperatorType::class, |
||
| 216 | [ |
||
| 217 | 'value' => 123, |
||
| 218 | 'not' => false, |
||
| 219 | ], |
||
| 220 | ], |
||
| 221 | [ |
||
| 222 | 'alias.field IS NOT NULL', |
||
| 223 | NullOperatorType::class, |
||
| 224 | [ |
||
| 225 | 'value' => 123, |
||
| 226 | 'not' => true, |
||
| 227 | ], |
||
| 228 | ], |
||
| 229 | ]; |
||
| 230 | } |
||
| 231 | } |
||
| 232 |