| Total Complexity | 61 |
| Total Lines | 1017 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SchemaAttributeTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaAttributeTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 16 | class SchemaAttributeTest extends TestCase |
||
| 17 | { |
||
| 18 | public function testItSchemaAttributeWithInvalidNamePropertiesThrowException(): void |
||
| 19 | { |
||
| 20 | $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
||
| 21 | $this->expectExceptionMessage('Name:'); |
||
| 22 | |||
| 23 | new SchemaAttribute('', 'string'); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function testItSchemaAttributeWithInvalidDataTypePropertiesThrowException(): void |
||
| 27 | { |
||
| 28 | $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
||
| 29 | $this->expectExceptionMessage('DataType:'); |
||
| 30 | |||
| 31 | new SchemaAttribute('foo', 'bar'); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @dataProvider getConstraintLogicError |
||
| 36 | */ |
||
| 37 | public function testItSchemaAttributeConstraintLogicError(string $dataType, string $constraints): void |
||
| 38 | { |
||
| 39 | $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class); |
||
| 40 | $this->expectExceptionMessage('Logic:'); |
||
| 41 | |||
| 42 | new SchemaAttribute('foo', $dataType, $constraints); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function testItSchemaAttributeWithRequiredPropertiesSetValues(): void |
||
| 46 | { |
||
| 47 | $name = 'foo'; |
||
| 48 | $dataType = 'string'; |
||
| 49 | |||
| 50 | $schemaAttribute = new SchemaAttribute($name, $dataType); |
||
| 51 | |||
| 52 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 53 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @dataProvider getRequiredConstraint |
||
| 58 | * |
||
| 59 | * @param mixed $constraint |
||
| 60 | * @param mixed $expected |
||
| 61 | */ |
||
| 62 | public function testItSchemaAttributeRequiredConstraints($constraint, $expected): void |
||
| 63 | { |
||
| 64 | $name = 'foo'; |
||
| 65 | $dataType = 'string'; |
||
| 66 | |||
| 67 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 68 | |||
| 69 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 70 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 71 | $this->assertSame($expected, $schemaAttribute->isRequired()); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @dataProvider getMinLengthConstraint |
||
| 76 | * |
||
| 77 | * @param mixed $constraint |
||
| 78 | * @param mixed $expected |
||
| 79 | */ |
||
| 80 | public function testItSchemaAttributeMinLengthConstraints($constraint, $expected): void |
||
| 81 | { |
||
| 82 | $name = 'foo'; |
||
| 83 | $dataType = 'string'; |
||
| 84 | |||
| 85 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 86 | |||
| 87 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 88 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 89 | $this->assertSame($expected, $schemaAttribute->minLength()); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @dataProvider getMinConstraint |
||
| 94 | * |
||
| 95 | * @param mixed $constraint |
||
| 96 | * @param mixed $expected |
||
| 97 | */ |
||
| 98 | public function testItSchemaAttributeMinConstraints($constraint, $expected): void |
||
| 99 | { |
||
| 100 | $name = 'min'; |
||
| 101 | $dataType = 'integer'; |
||
| 102 | |||
| 103 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 104 | |||
| 105 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 106 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 107 | $this->assertSame($expected, $schemaAttribute->min()); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @dataProvider getMaxLengthConstraint |
||
| 112 | * |
||
| 113 | * @param mixed $constraint |
||
| 114 | * @param mixed $expected |
||
| 115 | */ |
||
| 116 | public function testItSchemaAttributeMaxLengthConstraints($constraint, $expected): void |
||
| 117 | { |
||
| 118 | $name = 'minLength'; |
||
| 119 | $dataType = 'string'; |
||
| 120 | |||
| 121 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 122 | |||
| 123 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 124 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 125 | $this->assertSame($expected, $schemaAttribute->maxLength()); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @dataProvider getMaxConstraint |
||
| 130 | * |
||
| 131 | * @param mixed $constraint |
||
| 132 | * @param mixed $expected |
||
| 133 | */ |
||
| 134 | public function testItSchemaAttributeMaxConstraints($constraint, $expected): void |
||
| 135 | { |
||
| 136 | $name = 'max'; |
||
| 137 | $dataType = 'integer'; |
||
| 138 | |||
| 139 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 140 | |||
| 141 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 142 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 143 | $this->assertSame($expected, $schemaAttribute->max()); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @dataProvider getMinCheckConstraint |
||
| 148 | * |
||
| 149 | * @param mixed $constraint |
||
| 150 | * @param mixed $expected |
||
| 151 | */ |
||
| 152 | public function testItSchemaAttributeMinCheckConstraints($constraint, $expected): void |
||
| 153 | { |
||
| 154 | $name = 'minCheck'; |
||
| 155 | $dataType = 'integer'; |
||
| 156 | |||
| 157 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 158 | |||
| 159 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 160 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 161 | $this->assertSame($expected, $schemaAttribute->minCheck()); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @dataProvider getMaxCheckConstraint |
||
| 166 | * |
||
| 167 | * @param mixed $constraint |
||
| 168 | * @param mixed $expected |
||
| 169 | */ |
||
| 170 | public function testItSchemaAttributeMaxCheckConstraints($constraint, $expected): void |
||
| 171 | { |
||
| 172 | $name = 'maxCheck'; |
||
| 173 | $dataType = 'integer'; |
||
| 174 | |||
| 175 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 176 | |||
| 177 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 178 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 179 | $this->assertSame($expected, $schemaAttribute->maxCheck()); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @dataProvider getCheckConstraint |
||
| 184 | * |
||
| 185 | * @param mixed $constraint |
||
| 186 | * @param mixed $expectedMin |
||
| 187 | * @param mixed $expectedMax |
||
| 188 | */ |
||
| 189 | public function testItSchemaAttributeCheckConstraints($constraint, $expectedMin, $expectedMax): void |
||
| 190 | { |
||
| 191 | $name = 'check'; |
||
| 192 | $dataType = 'integer'; |
||
| 193 | |||
| 194 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 195 | |||
| 196 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 197 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 198 | $this->assertSame($expectedMin, $schemaAttribute->minCheck()); |
||
| 199 | $this->assertSame($expectedMax, $schemaAttribute->maxCheck()); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @dataProvider getLengthConstraint |
||
| 204 | * |
||
| 205 | * @param mixed $constraint |
||
| 206 | * @param mixed $expectedMin |
||
| 207 | * @param mixed $expectedMax |
||
| 208 | */ |
||
| 209 | public function testItSchemaAttributeLengthConstraints($constraint, $expectedMin, $expectedMax): void |
||
| 210 | { |
||
| 211 | $name = 'length'; |
||
| 212 | $dataType = 'string'; |
||
| 213 | |||
| 214 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 215 | |||
| 216 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 217 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 218 | $this->assertSame($expectedMin, $schemaAttribute->minLength()); |
||
| 219 | $this->assertSame($expectedMax, $schemaAttribute->maxLength()); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @dataProvider getEqualToConstraint |
||
| 224 | * |
||
| 225 | * @param mixed $constraint |
||
| 226 | * @param mixed $expected |
||
| 227 | */ |
||
| 228 | public function testItSchemaAttributeEqualToConstraints($constraint, $expected): void |
||
| 229 | { |
||
| 230 | $name = 'equalTo'; |
||
| 231 | $dataType = 'string'; |
||
| 232 | |||
| 233 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 234 | |||
| 235 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 236 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 237 | $this->assertSame($expected, $schemaAttribute->equalTo()); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @dataProvider getTypeConstraint |
||
| 242 | * |
||
| 243 | * @param mixed $constraint |
||
| 244 | * @param mixed $expected |
||
| 245 | */ |
||
| 246 | public function testItSchemaAttributeTypeConstraints($constraint, $expected): void |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @dataProvider getPkConstraint |
||
| 260 | * |
||
| 261 | * @param mixed $constraint |
||
| 262 | * @param mixed $expected |
||
| 263 | */ |
||
| 264 | public function testItSchemaAttributePkConstraints($constraint, $expected): void |
||
| 265 | { |
||
| 266 | $name = 'foo'; |
||
| 267 | $dataType = 'string'; |
||
| 268 | |||
| 269 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 270 | |||
| 271 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 272 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 273 | $this->assertSame($expected, $schemaAttribute->isPk()); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @dataProvider getAiConstraint |
||
| 278 | * |
||
| 279 | * @param mixed $constraint |
||
| 280 | * @param mixed $expected |
||
| 281 | */ |
||
| 282 | public function testItSchemaAttributeAiConstraints($constraint, $expected): void |
||
| 283 | { |
||
| 284 | $name = 'foo'; |
||
| 285 | $dataType = 'integer'; |
||
| 286 | |||
| 287 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 288 | |||
| 289 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 290 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 291 | $this->assertSame($expected, $schemaAttribute->isAi()); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @dataProvider getCaConstraint |
||
| 296 | * |
||
| 297 | * @param mixed $constraint |
||
| 298 | * @param mixed $expected |
||
| 299 | */ |
||
| 300 | public function testItSchemaAttributeCaConstraints($constraint, $expected): void |
||
| 301 | { |
||
| 302 | $name = 'foo'; |
||
| 303 | $dataType = 'datetime'; |
||
| 304 | |||
| 305 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 306 | |||
| 307 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 308 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 309 | $this->assertSame($expected, $schemaAttribute->isCa()); |
||
| 310 | $this->assertSame($expected, $schemaAttribute->isBlame()); |
||
| 311 | $this->assertSame($expected, $schemaAttribute->isBlameAt()); |
||
| 312 | $this->assertSame(false, $schemaAttribute->isBlameBy()); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @dataProvider getUaConstraint |
||
| 317 | * |
||
| 318 | * @param mixed $constraint |
||
| 319 | * @param mixed $expected |
||
| 320 | */ |
||
| 321 | public function testItSchemaAttributeUaConstraints($constraint, $expected): void |
||
| 322 | { |
||
| 323 | $name = 'foo'; |
||
| 324 | $dataType = 'datetime'; |
||
| 325 | |||
| 326 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 327 | |||
| 328 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 329 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 330 | $this->assertSame($expected, $schemaAttribute->isUa()); |
||
| 331 | $this->assertSame($expected, $schemaAttribute->isBlame()); |
||
| 332 | $this->assertSame($expected, $schemaAttribute->isBlameAt()); |
||
| 333 | $this->assertSame(false, $schemaAttribute->isBlameBy()); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @dataProvider getCbConstraint |
||
| 338 | * |
||
| 339 | * @param mixed $constraint |
||
| 340 | * @param mixed $expected |
||
| 341 | */ |
||
| 342 | public function testItSchemaAttributeCbConstraints($constraint, $expected): void |
||
| 343 | { |
||
| 344 | $name = 'foo'; |
||
| 345 | $dataType = 'integer'; |
||
| 346 | |||
| 347 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 348 | |||
| 349 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 350 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 351 | $this->assertSame($expected, $schemaAttribute->isCb()); |
||
| 352 | $this->assertSame($expected, $schemaAttribute->isBlame()); |
||
| 353 | $this->assertSame(false, $schemaAttribute->isBlameAt()); |
||
| 354 | $this->assertSame($expected, $schemaAttribute->isBlameBy()); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @dataProvider getUbConstraint |
||
| 359 | * |
||
| 360 | * @param mixed $constraint |
||
| 361 | * @param mixed $expected |
||
| 362 | */ |
||
| 363 | public function testItSchemaAttributeUbConstraints($constraint, $expected): void |
||
| 364 | { |
||
| 365 | $name = 'foo'; |
||
| 366 | $dataType = 'integer'; |
||
| 367 | |||
| 368 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 369 | |||
| 370 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 371 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 372 | $this->assertSame($expected, $schemaAttribute->isUb()); |
||
| 373 | $this->assertSame($expected, $schemaAttribute->isBlame()); |
||
| 374 | $this->assertSame(false, $schemaAttribute->isBlameAt()); |
||
| 375 | $this->assertSame($expected, $schemaAttribute->isBlameBy()); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @dataProvider getFilterConstraint |
||
| 380 | * |
||
| 381 | * @param mixed $constraint |
||
| 382 | * @param mixed $expected |
||
| 383 | */ |
||
| 384 | public function testItSchemaAttributeFilterConstraints($constraint, $expected): void |
||
| 385 | { |
||
| 386 | $name = 'foo'; |
||
| 387 | $dataType = 'integer'; |
||
| 388 | |||
| 389 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 390 | |||
| 391 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 392 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 393 | $this->assertSame($expected, $schemaAttribute->filter()); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @dataProvider getFormatConstraint |
||
| 398 | */ |
||
| 399 | public function testItSchemaAttributeFormatConstraints( |
||
| 400 | string $dataType, |
||
| 401 | string $constraint, |
||
| 402 | ?string $expected |
||
| 403 | ): void { |
||
| 404 | $name = 'foo'; |
||
| 405 | |||
| 406 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 407 | |||
| 408 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 409 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 410 | $this->assertSame($expected, $schemaAttribute->format()); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @dataProvider getTrimConstraint |
||
| 415 | * |
||
| 416 | * @param mixed $constraint |
||
| 417 | */ |
||
| 418 | public function testItSchemaAttributeTrimConstraints($constraint, bool $expected): void |
||
| 419 | { |
||
| 420 | $name = 'foo'; |
||
| 421 | $dataType = 'string'; |
||
| 422 | |||
| 423 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 424 | |||
| 425 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 426 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 427 | $this->assertSame($expected, $schemaAttribute->trim()); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @dataProvider getFcharsConstraint |
||
| 432 | */ |
||
| 433 | public function testItSchemaAttributeFcharsConstraints(string $constraint, ?int $expected): void |
||
| 434 | { |
||
| 435 | $name = 'foo'; |
||
| 436 | $dataType = 'string'; |
||
| 437 | |||
| 438 | if (!empty($constraint)) { |
||
| 439 | $constraint = 'fk:fkTable,fkName|' . $constraint; |
||
| 440 | } |
||
| 441 | |||
| 442 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 443 | |||
| 444 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 445 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 446 | $this->assertSame($expected, $schemaAttribute->fchars()); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @dataProvider getLinkConstraint |
||
| 451 | * |
||
| 452 | * @param mixed $constraint |
||
| 453 | */ |
||
| 454 | public function testItSchemaAttributeLinkConstraints($constraint, ?bool $expected): void |
||
| 455 | { |
||
| 456 | $name = 'foo'; |
||
| 457 | $dataType = 'string'; |
||
| 458 | |||
| 459 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 460 | |||
| 461 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 462 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 463 | $this->assertSame($expected, $schemaAttribute->link()); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @dataProvider getFkCheckConstraint |
||
| 468 | * |
||
| 469 | * @param mixed $constraint |
||
| 470 | */ |
||
| 471 | public function testItSchemaAttributeFkCheckConstraints($constraint, ?bool $expected): void |
||
| 472 | { |
||
| 473 | $name = 'foo'; |
||
| 474 | $dataType = 'string'; |
||
| 475 | |||
| 476 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 477 | |||
| 478 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 479 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 480 | $this->assertSame($expected, $schemaAttribute->fkcheck()); |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @dataProvider getShowConstraint |
||
| 485 | */ |
||
| 486 | public function testItSchemaAttributeShowConstraints(string $dataType, string $constraint, string $expected): void |
||
| 487 | { |
||
| 488 | $name = 'foo'; |
||
| 489 | |||
| 490 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 491 | |||
| 492 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 493 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 494 | $this->assertTrue($schemaAttribute->usedIn($expected)); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @dataProvider getHideConstraint |
||
| 499 | */ |
||
| 500 | public function testItSchemaAttributeHideConstraints( |
||
| 501 | string $dataType, |
||
| 502 | string $constraint, |
||
| 503 | string $action, |
||
| 504 | bool $expected |
||
| 505 | ): void { |
||
| 506 | $name = 'foo'; |
||
| 507 | |||
| 508 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 509 | |||
| 510 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 511 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 512 | $this->assertSame($expected, $schemaAttribute->usedIn($action)); |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @dataProvider getFkConstraint |
||
| 517 | * |
||
| 518 | * @param mixed $constraint |
||
| 519 | * @param mixed $fkTable |
||
| 520 | * @param mixed $fkId |
||
| 521 | * @param mixed $fkName |
||
| 522 | */ |
||
| 523 | public function testItSchemaAttributeFkConstraints($constraint, $fkTable, $fkId, $fkName): void |
||
| 524 | { |
||
| 525 | $name = 'foreing'; |
||
| 526 | $dataType = 'integer'; |
||
| 527 | |||
| 528 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 529 | |||
| 530 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 531 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 532 | $this->assertEquals('int', $schemaAttribute->typeHint()); |
||
| 533 | $this->assertTrue($schemaAttribute->isFk()); |
||
| 534 | $this->assertSame($fkTable, $schemaAttribute->fkTable()); |
||
| 535 | $this->assertSame($fkId, $schemaAttribute->fkId()); |
||
| 536 | $this->assertSame($fkName, $schemaAttribute->fkName()); |
||
| 537 | } |
||
| 538 | |||
| 539 | public function testItSchemaAttributeConstraintsAsString(): void |
||
| 540 | { |
||
| 541 | $name = 'foo'; |
||
| 542 | $dataType = 'string'; |
||
| 543 | $constraints = 'required|minlength:8|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number'; |
||
| 544 | |||
| 545 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
||
| 546 | |||
| 547 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 548 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 549 | $this->assertEquals('string', $schemaAttribute->typeHint()); |
||
| 550 | $this->assertIsArray($schemaAttribute->constraints()); |
||
| 551 | $this->assertSame(true, $schemaAttribute->isRequired()); |
||
| 552 | $this->assertNull($schemaAttribute->min()); |
||
| 553 | $this->assertSame(8, $schemaAttribute->minLength()); |
||
| 554 | $this->assertSame(3, $schemaAttribute->minCheck()); |
||
| 555 | $this->assertNull($schemaAttribute->max()); |
||
| 556 | $this->assertSame(10, $schemaAttribute->maxLength()); |
||
| 557 | $this->assertSame(4, $schemaAttribute->maxCheck()); |
||
| 558 | $this->assertSame('#bar', $schemaAttribute->equalTo()); |
||
| 559 | $this->assertSame('number', $schemaAttribute->type()); |
||
| 560 | $this->assertFalse($schemaAttribute->isPk()); |
||
| 561 | $this->assertFalse($schemaAttribute->isAi()); |
||
| 562 | $this->assertFalse($schemaAttribute->isFk()); |
||
| 563 | $this->assertNull($schemaAttribute->fkTable()); |
||
| 564 | $this->assertNull($schemaAttribute->fkId()); |
||
| 565 | $this->assertNull($schemaAttribute->fkName()); |
||
| 566 | } |
||
| 567 | |||
| 568 | public function testItSchemaAttributeConstraintsAsArray(): void |
||
| 569 | { |
||
| 570 | $name = 'foo'; |
||
| 571 | $dataType = 'integer'; |
||
| 572 | $constraints = [ |
||
| 573 | 'required', |
||
| 574 | 'equalto' => '#id', |
||
| 575 | 'type' => 'text', |
||
| 576 | 'pk' => true, |
||
| 577 | 'ai' => true, |
||
| 578 | ]; |
||
| 579 | |||
| 580 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
||
| 581 | |||
| 582 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 583 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 584 | $this->assertEquals('int', $schemaAttribute->typeHint()); |
||
| 585 | $this->assertIsArray($schemaAttribute->constraints()); |
||
| 586 | $this->assertSame(true, $schemaAttribute->isRequired()); |
||
| 587 | $this->assertNull($schemaAttribute->min()); |
||
| 588 | $this->assertNull($schemaAttribute->minLength()); |
||
| 589 | $this->assertNull($schemaAttribute->minCheck()); |
||
| 590 | $this->assertNull($schemaAttribute->max()); |
||
| 591 | $this->assertNull($schemaAttribute->maxLength()); |
||
| 592 | $this->assertNull($schemaAttribute->maxCheck()); |
||
| 593 | $this->assertSame('#id', $schemaAttribute->equalTo()); |
||
| 594 | $this->assertSame('text', $schemaAttribute->type()); |
||
| 595 | $this->assertTrue($schemaAttribute->isPk()); |
||
| 596 | $this->assertTrue($schemaAttribute->isAi()); |
||
| 597 | $this->assertFalse($schemaAttribute->isFk()); |
||
| 598 | } |
||
| 599 | |||
| 600 | public function testItSchemaAttributeConstraintsAsArrayCast(): void |
||
| 601 | { |
||
| 602 | $name = 'foo'; |
||
| 603 | $dataType = 'datetime'; |
||
| 604 | $constraints = [ |
||
| 605 | 'required', |
||
| 606 | 'equalto' => '#bar', |
||
| 607 | 'type' => 'number', |
||
| 608 | ]; |
||
| 609 | |||
| 610 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
||
| 611 | |||
| 612 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 613 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 614 | $this->assertEquals('\DateTime', $schemaAttribute->typeHint()); |
||
| 615 | $this->assertIsArray($schemaAttribute->constraints()); |
||
| 616 | $this->assertSame(true, $schemaAttribute->isRequired()); |
||
| 617 | $this->assertNull($schemaAttribute->min()); |
||
| 618 | $this->assertNull($schemaAttribute->minLength()); |
||
| 619 | $this->assertNull($schemaAttribute->minCheck()); |
||
| 620 | $this->assertNull($schemaAttribute->max()); |
||
| 621 | $this->assertNull($schemaAttribute->maxLength()); |
||
| 622 | $this->assertNull($schemaAttribute->maxCheck()); |
||
| 623 | $this->assertSame('#bar', $schemaAttribute->equalTo()); |
||
| 624 | $this->assertSame('number', $schemaAttribute->type()); |
||
| 625 | } |
||
| 626 | |||
| 627 | public function getRequiredConstraint(): array |
||
| 628 | { |
||
| 629 | return [ |
||
| 630 | ['', false], |
||
| 631 | ['required', true], |
||
| 632 | ['required:true', true], |
||
| 633 | ['required:false', false], |
||
| 634 | [['required'], true], |
||
| 635 | [['required' => true], true], |
||
| 636 | [['required' => false], false], |
||
| 637 | ]; |
||
| 638 | } |
||
| 639 | |||
| 640 | public function getMinLengthConstraint(): array |
||
| 641 | { |
||
| 642 | return [ |
||
| 643 | ['', null], |
||
| 644 | ['minlength:8', 8], |
||
| 645 | [['minlength' => 8], 8], |
||
| 646 | [['minlength' => '8'], 8], |
||
| 647 | ]; |
||
| 648 | } |
||
| 649 | |||
| 650 | public function getMinConstraint(): array |
||
| 651 | { |
||
| 652 | return [ |
||
| 653 | ['', null], |
||
| 654 | ['min:8', 8], |
||
| 655 | [['min' => 8], 8], |
||
| 656 | [['min' => '8'], 8], |
||
| 657 | ]; |
||
| 658 | } |
||
| 659 | |||
| 660 | public function getMaxLengthConstraint(): array |
||
| 661 | { |
||
| 662 | return [ |
||
| 663 | ['', null], |
||
| 664 | ['maxlength:10', 10], |
||
| 665 | [['maxlength' => 10], 10], |
||
| 666 | [['maxlength' => '10'], 10], |
||
| 667 | ]; |
||
| 668 | } |
||
| 669 | |||
| 670 | public function getMaxConstraint(): array |
||
| 671 | { |
||
| 672 | return [ |
||
| 673 | ['', null], |
||
| 674 | ['max:100', 100], |
||
| 675 | [['max' => 100], 100], |
||
| 676 | [['max' => '100'], 100], |
||
| 677 | ]; |
||
| 678 | } |
||
| 679 | |||
| 680 | public function getMinCheckConstraint(): array |
||
| 681 | { |
||
| 682 | return [ |
||
| 683 | ['', null], |
||
| 684 | ['mincheck:3', 3], |
||
| 685 | [['mincheck' => 3], 3], |
||
| 686 | [['mincheck' => '3'], 3], |
||
| 687 | ]; |
||
| 688 | } |
||
| 689 | |||
| 690 | public function getMaxCheckConstraint(): array |
||
| 691 | { |
||
| 692 | return [ |
||
| 693 | ['', null], |
||
| 694 | ['maxcheck:4', 4], |
||
| 695 | [['maxcheck' => 4], 4], |
||
| 696 | [['maxcheck' => '4'], 4], |
||
| 697 | ]; |
||
| 698 | } |
||
| 699 | |||
| 700 | public function getCheckConstraint(): array |
||
| 709 | ]; |
||
| 710 | } |
||
| 711 | |||
| 712 | public function getLengthConstraint(): array |
||
| 713 | { |
||
| 714 | return [ |
||
| 715 | ['', null, null], |
||
| 716 | ['length:30,40', 30, 40], |
||
| 717 | [['length' => [ |
||
| 718 | 'min' => 5, |
||
| 719 | 'max' => 60, |
||
| 720 | ]], 5, 60], |
||
| 721 | ]; |
||
| 722 | } |
||
| 723 | |||
| 724 | public function getEqualToConstraint(): array |
||
| 725 | { |
||
| 726 | return [ |
||
| 727 | ['', null], |
||
| 728 | ['equalto:#foo', '#foo'], |
||
| 729 | [['equalto' => '.foo'], '.foo'], |
||
| 730 | ]; |
||
| 731 | } |
||
| 732 | |||
| 733 | public function getTypeConstraint(): array |
||
| 734 | { |
||
| 735 | return [ |
||
| 736 | ['', null], |
||
| 737 | ['type:number', 'number'], |
||
| 738 | [['type' => 'text'], 'text'], |
||
| 739 | ]; |
||
| 740 | } |
||
| 741 | |||
| 742 | public function getPkConstraint(): array |
||
| 743 | { |
||
| 744 | return [ |
||
| 745 | ['', false], |
||
| 746 | ['required|pk', true], |
||
| 747 | ['required|pk:true', true], |
||
| 748 | ['required|pk:false', false], |
||
| 749 | [['required', 'pk'], true], |
||
| 750 | [['required', 'pk' => true], true], |
||
| 751 | [['required', 'pk' => false], false], |
||
| 752 | ]; |
||
| 753 | } |
||
| 754 | |||
| 755 | public function getAiConstraint(): array |
||
| 756 | { |
||
| 757 | return [ |
||
| 758 | ['', false], |
||
| 759 | ['required|pk|ai', true], |
||
| 760 | ['required|pk|ai:true', true], |
||
| 761 | [['required', 'pk', 'ai'], true], |
||
| 762 | [['required', 'pk', 'ai' => true], true], |
||
| 763 | ]; |
||
| 764 | } |
||
| 765 | |||
| 766 | public function getCaConstraint(): array |
||
| 776 | ]; |
||
| 777 | } |
||
| 778 | |||
| 779 | public function getUaConstraint(): array |
||
| 780 | { |
||
| 781 | return [ |
||
| 782 | ['', false], |
||
| 783 | ['ua', true], |
||
| 784 | ['ua:true', true], |
||
| 785 | ['ua:false', false], |
||
| 786 | [['ua'], true], |
||
| 787 | [['ua' => true], true], |
||
| 788 | [['ua' => false], false], |
||
| 789 | ]; |
||
| 790 | } |
||
| 791 | |||
| 792 | public function getFkConstraint(): array |
||
| 793 | { |
||
| 794 | return [ |
||
| 795 | ['fk:table', 'table', 'id', 'name'], |
||
| 796 | ['fk:table2,username', 'table2', 'id', 'username'], |
||
| 797 | ['fk:table3,description,uuid', 'table3', 'uuid', 'description'], |
||
| 798 | [['fk' => 'table5'], 'table5', 'id', 'name'], |
||
| 799 | [['fk' => 'table6,username'], 'table6', 'id', 'username'], |
||
| 800 | [['fk' => 'table7,description,uuid'], 'table7', 'uuid', 'description'], |
||
| 801 | ]; |
||
| 802 | } |
||
| 803 | |||
| 804 | public function getCbConstraint(): array |
||
| 805 | { |
||
| 806 | return [ |
||
| 807 | ['', false], |
||
| 808 | ['cb', true], |
||
| 809 | ['cb:true', true], |
||
| 810 | ['cb:false', false], |
||
| 811 | [['cb'], true], |
||
| 812 | [['cb' => true], true], |
||
| 813 | [['cb' => false], false], |
||
| 814 | ]; |
||
| 815 | } |
||
| 816 | |||
| 817 | public function getUbConstraint(): array |
||
| 818 | { |
||
| 819 | return [ |
||
| 820 | ['', false], |
||
| 821 | ['ub', true], |
||
| 822 | ['ub:true', true], |
||
| 823 | ['ub:false', false], |
||
| 824 | [['ub'], true], |
||
| 825 | [['ub' => true], true], |
||
| 826 | [['ub' => false], false], |
||
| 827 | ]; |
||
| 828 | } |
||
| 829 | |||
| 830 | public function getFilterConstraint(): array |
||
| 831 | { |
||
| 832 | return [ |
||
| 833 | ['', null], |
||
| 834 | ['filter:eq', 'eq'], |
||
| 835 | ['filter:nn', 'nn'], |
||
| 836 | ]; |
||
| 837 | } |
||
| 838 | |||
| 839 | public function getFormatConstraint(): array |
||
| 840 | { |
||
| 841 | return [ |
||
| 842 | ['string', '', null], |
||
| 843 | ['datetime', 'format:timeago', 'timeago'], |
||
| 844 | ['datetime', 'format:datetime', 'datetime'], |
||
| 845 | ['integer', 'format:money', 'money'], |
||
| 846 | ]; |
||
| 847 | } |
||
| 848 | |||
| 849 | public function getTrimConstraint(): array |
||
| 850 | { |
||
| 851 | return [ |
||
| 852 | ['', false], |
||
| 853 | ['trim', true], |
||
| 854 | ['trim:true', true], |
||
| 855 | ['trim:false', false], |
||
| 856 | [['trim'], true], |
||
| 857 | [['trim' => true], true], |
||
| 858 | [['trim' => false], false], |
||
| 859 | ]; |
||
| 860 | } |
||
| 861 | |||
| 862 | public function getFcharsConstraint(): array |
||
| 863 | { |
||
| 864 | return [ |
||
| 865 | ['', null], |
||
| 866 | ['fchars:0', 0], |
||
| 867 | ['fchars:1', 1], |
||
| 868 | ['fchars:2', 2], |
||
| 869 | ['fchars:90', 90], |
||
| 870 | ]; |
||
| 871 | } |
||
| 872 | |||
| 873 | public function getLinkConstraint(): array |
||
| 874 | { |
||
| 875 | return [ |
||
| 876 | ['', false], |
||
| 877 | ['link', true], |
||
| 878 | ['link:true', true], |
||
| 879 | ['link:false', false], |
||
| 880 | [['link'], true], |
||
| 881 | [['link' => true], true], |
||
| 882 | [['link' => false], false], |
||
| 883 | ]; |
||
| 884 | } |
||
| 885 | |||
| 886 | public function getFkCheckConstraint(): array |
||
| 896 | ]; |
||
| 897 | } |
||
| 898 | |||
| 899 | public function getShowConstraint(): array |
||
| 900 | { |
||
| 901 | return [ |
||
| 902 | ['string', '', Action::ALL], |
||
| 903 | ['datetime', 'ca', Action::READ], |
||
| 904 | ['integer', 'cb', Action::READ], |
||
| 905 | ['datetime', 'ua', Action::READ], |
||
| 906 | ['integer', 'ub', Action::READ], |
||
| 907 | ['string', 'show:a', Action::ALL], |
||
| 908 | ['string', 'show:i', Action::INDEX], |
||
| 909 | ['string', 'show:c', Action::CREATE], |
||
| 910 | ['string', 'show:r', Action::READ], |
||
| 911 | ['string', 'show:u', Action::UPDATE], |
||
| 912 | ['string', 'show:d', Action::DELETE], |
||
| 913 | ['datetime', 'ca|show:a', Action::ALL], |
||
| 914 | ['integer', 'cb|show:i', Action::INDEX], |
||
| 915 | ['datetime', 'ua|show:c', Action::CREATE], |
||
| 916 | ['integer', 'ub|show:r', Action::READ], |
||
| 917 | ]; |
||
| 918 | } |
||
| 919 | |||
| 920 | public function getHideConstraint(): array |
||
| 921 | { |
||
| 922 | return [ |
||
| 923 | ['string', '', '', false], |
||
| 924 | ['datetime', 'ca', Action::ALL, false], |
||
| 925 | ['datetime', 'ca', Action::INDEX, false], |
||
| 926 | ['datetime', 'ca', Action::CREATE, false], |
||
| 927 | ['datetime', 'ca', Action::READ, true], |
||
| 928 | ['datetime', 'ca', Action::UPDATE, false], |
||
| 929 | ['datetime', 'ca', Action::DELETE, false], |
||
| 930 | ['integer', 'cb', Action::ALL, false], |
||
| 931 | ['integer', 'cb', Action::INDEX, false], |
||
| 932 | ['integer', 'cb', Action::CREATE, false], |
||
| 933 | ['integer', 'cb', Action::READ, true], |
||
| 934 | ['integer', 'cb', Action::UPDATE, false], |
||
| 935 | ['integer', 'cb', Action::DELETE, false], |
||
| 936 | ['string', 'hide:a', Action::ALL, false], |
||
| 937 | ['string', 'hide:i', Action::INDEX, false], |
||
| 938 | ['string', 'hide:c', Action::CREATE, false], |
||
| 939 | ['string', 'hide:r', Action::READ, false], |
||
| 940 | ['string', 'hide:u', Action::UPDATE, false], |
||
| 941 | ['string', 'hide:d', Action::DELETE, false], |
||
| 942 | ['datetime', 'ca|hide:r', Action::READ, false], |
||
| 943 | ['integer', 'cb|hide:r', Action::READ, false], |
||
| 944 | ['datetime', 'ua|hide:r', Action::READ, false], |
||
| 945 | ['integer', 'ub|hide:r', Action::READ, false], |
||
| 946 | ]; |
||
| 947 | } |
||
| 948 | |||
| 949 | public function getConstraintLogicError(): array |
||
| 1033 | ]; |
||
| 1034 | } |
||
| 1035 | } |
||
| 1036 |