| Total Complexity | 63 |
| Total Lines | 1067 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 2 | 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 |
||
| 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 |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @dataProvider getFcharsConstraint |
||
| 432 | */ |
||
| 433 | public function testItSchemaAttributeFcharsConstraints(string $constraint, ?int $expected): void |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @dataProvider getLinkConstraint |
||
| 451 | * |
||
| 452 | * @param mixed $constraint |
||
| 453 | */ |
||
| 454 | public function testItSchemaAttributeLinkConstraints($constraint, ?bool $expected): void |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @dataProvider getFkCheckConstraint |
||
| 468 | * |
||
| 469 | * @param mixed $constraint |
||
| 470 | */ |
||
| 471 | public function testItSchemaAttributeFkCheckConstraints($constraint, ?bool $expected): void |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @dataProvider getShowConstraint |
||
| 485 | */ |
||
| 486 | public function testItSchemaAttributeShowConstraints(string $dataType, string $constraint, string $expected): void |
||
| 487 | { |
||
| 488 | $name = 'foo'; |
||
| 489 | |||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @dataProvider getDefaultConstraint |
||
| 499 | * |
||
| 500 | * @param mixed $constraint |
||
| 501 | * @param mixed $expected |
||
| 502 | */ |
||
| 503 | public function testItSchemaAttributeDefaultConstraints(string $dataType, $constraint, $expected): void |
||
| 504 | { |
||
| 505 | $name = 'foo'; |
||
| 506 | |||
| 507 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 508 | |||
| 509 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 510 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 511 | $this->assertSame($expected, $schemaAttribute->default()); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @dataProvider getHideConstraint |
||
| 516 | */ |
||
| 517 | public function testItSchemaAttributeHideConstraints( |
||
| 518 | string $dataType, |
||
| 519 | string $constraint, |
||
| 520 | string $action, |
||
| 521 | bool $expected |
||
| 522 | ): void { |
||
| 523 | $name = 'foo'; |
||
| 524 | |||
| 525 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 526 | |||
| 527 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 528 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 529 | $this->assertSame($expected, $schemaAttribute->usedIn($action)); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @dataProvider getFkConstraint |
||
| 534 | * |
||
| 535 | * @param mixed $constraint |
||
| 536 | * @param mixed $fkTable |
||
| 537 | * @param mixed $fkId |
||
| 538 | * @param mixed $fkName |
||
| 539 | */ |
||
| 540 | public function testItSchemaAttributeFkConstraints($constraint, $fkTable, $fkId, $fkName): void |
||
| 541 | { |
||
| 542 | $name = 'foreing'; |
||
| 543 | $dataType = 'integer'; |
||
| 544 | |||
| 545 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraint); |
||
| 546 | |||
| 547 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 548 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 549 | $this->assertEquals('int', $schemaAttribute->typeHint()); |
||
| 550 | $this->assertTrue($schemaAttribute->isFk()); |
||
| 551 | $this->assertSame($fkTable, $schemaAttribute->fkTable()); |
||
| 552 | $this->assertSame($fkId, $schemaAttribute->fkId()); |
||
| 553 | $this->assertSame($fkName, $schemaAttribute->fkName()); |
||
| 554 | } |
||
| 555 | |||
| 556 | public function testItSchemaAttributeConstraintsAsString(): void |
||
| 557 | { |
||
| 558 | $name = 'foo'; |
||
| 559 | $dataType = 'string'; |
||
| 560 | $constraints = 'required|minlength:8|maxlength:10|mincheck:3|maxcheck:4|equalto:#bar|type:number'; |
||
| 561 | |||
| 562 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
||
| 563 | |||
| 564 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 565 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 566 | $this->assertEquals('string', $schemaAttribute->typeHint()); |
||
| 567 | $this->assertIsArray($schemaAttribute->constraints()); |
||
| 568 | $this->assertSame(true, $schemaAttribute->isRequired()); |
||
| 569 | $this->assertNull($schemaAttribute->min()); |
||
| 570 | $this->assertSame(8, $schemaAttribute->minLength()); |
||
| 571 | $this->assertSame(3, $schemaAttribute->minCheck()); |
||
| 572 | $this->assertNull($schemaAttribute->max()); |
||
| 573 | $this->assertSame(10, $schemaAttribute->maxLength()); |
||
| 574 | $this->assertSame(4, $schemaAttribute->maxCheck()); |
||
| 575 | $this->assertSame('#bar', $schemaAttribute->equalTo()); |
||
| 576 | $this->assertSame('number', $schemaAttribute->type()); |
||
| 577 | $this->assertFalse($schemaAttribute->isPk()); |
||
| 578 | $this->assertFalse($schemaAttribute->isAi()); |
||
| 579 | $this->assertFalse($schemaAttribute->isFk()); |
||
| 580 | $this->assertNull($schemaAttribute->fkTable()); |
||
| 581 | $this->assertNull($schemaAttribute->fkId()); |
||
| 582 | $this->assertNull($schemaAttribute->fkName()); |
||
| 583 | } |
||
| 584 | |||
| 585 | public function testItSchemaAttributeConstraintsAsArray(): void |
||
| 586 | { |
||
| 587 | $name = 'foo'; |
||
| 588 | $dataType = 'integer'; |
||
| 589 | $constraints = [ |
||
| 590 | 'required', |
||
| 591 | 'equalto' => '#id', |
||
| 592 | 'type' => 'text', |
||
| 593 | 'pk' => true, |
||
| 594 | 'ai' => true, |
||
| 595 | ]; |
||
| 596 | |||
| 597 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
||
| 598 | |||
| 599 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 600 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 601 | $this->assertEquals('int', $schemaAttribute->typeHint()); |
||
| 602 | $this->assertIsArray($schemaAttribute->constraints()); |
||
| 603 | $this->assertSame(true, $schemaAttribute->isRequired()); |
||
| 604 | $this->assertNull($schemaAttribute->min()); |
||
| 605 | $this->assertNull($schemaAttribute->minLength()); |
||
| 606 | $this->assertNull($schemaAttribute->minCheck()); |
||
| 607 | $this->assertNull($schemaAttribute->max()); |
||
| 608 | $this->assertNull($schemaAttribute->maxLength()); |
||
| 609 | $this->assertNull($schemaAttribute->maxCheck()); |
||
| 610 | $this->assertSame('#id', $schemaAttribute->equalTo()); |
||
| 611 | $this->assertSame('text', $schemaAttribute->type()); |
||
| 612 | $this->assertTrue($schemaAttribute->isPk()); |
||
| 613 | $this->assertTrue($schemaAttribute->isAi()); |
||
| 614 | $this->assertFalse($schemaAttribute->isFk()); |
||
| 615 | } |
||
| 616 | |||
| 617 | public function testItSchemaAttributeConstraintsAsArrayCast(): void |
||
| 618 | { |
||
| 619 | $name = 'foo'; |
||
| 620 | $dataType = 'datetime'; |
||
| 621 | $constraints = [ |
||
| 622 | 'required', |
||
| 623 | 'equalto' => '#bar', |
||
| 624 | 'type' => 'number', |
||
| 625 | ]; |
||
| 626 | |||
| 627 | $schemaAttribute = new SchemaAttribute($name, $dataType, $constraints); |
||
| 628 | |||
| 629 | $this->assertEquals($name, $schemaAttribute->name()); |
||
| 630 | $this->assertEquals($dataType, $schemaAttribute->dataType()); |
||
| 631 | $this->assertEquals('\DateTime', $schemaAttribute->typeHint()); |
||
| 632 | $this->assertIsArray($schemaAttribute->constraints()); |
||
| 633 | $this->assertSame(true, $schemaAttribute->isRequired()); |
||
| 634 | $this->assertNull($schemaAttribute->min()); |
||
| 635 | $this->assertNull($schemaAttribute->minLength()); |
||
| 636 | $this->assertNull($schemaAttribute->minCheck()); |
||
| 637 | $this->assertNull($schemaAttribute->max()); |
||
| 638 | $this->assertNull($schemaAttribute->maxLength()); |
||
| 639 | $this->assertNull($schemaAttribute->maxCheck()); |
||
| 640 | $this->assertSame('#bar', $schemaAttribute->equalTo()); |
||
| 641 | $this->assertSame('number', $schemaAttribute->type()); |
||
| 642 | } |
||
| 643 | |||
| 644 | public function getRequiredConstraint(): array |
||
| 645 | { |
||
| 646 | return [ |
||
| 647 | ['', false], |
||
| 648 | ['required', true], |
||
| 649 | ['required:true', true], |
||
| 650 | ['required:false', false], |
||
| 651 | [['required'], true], |
||
| 652 | [['required' => true], true], |
||
| 653 | [['required' => false], false], |
||
| 654 | ]; |
||
| 655 | } |
||
| 656 | |||
| 657 | public function getMinLengthConstraint(): array |
||
| 658 | { |
||
| 659 | return [ |
||
| 660 | ['', null], |
||
| 661 | ['minlength:8', 8], |
||
| 662 | [['minlength' => 8], 8], |
||
| 663 | [['minlength' => '8'], 8], |
||
| 664 | ]; |
||
| 665 | } |
||
| 666 | |||
| 667 | public function getMinConstraint(): array |
||
| 668 | { |
||
| 669 | return [ |
||
| 670 | ['', null], |
||
| 671 | ['min:8', 8], |
||
| 672 | [['min' => 8], 8], |
||
| 673 | [['min' => '8'], 8], |
||
| 674 | ]; |
||
| 675 | } |
||
| 676 | |||
| 677 | public function getMaxLengthConstraint(): array |
||
| 678 | { |
||
| 679 | return [ |
||
| 680 | ['', null], |
||
| 681 | ['maxlength:10', 10], |
||
| 682 | [['maxlength' => 10], 10], |
||
| 683 | [['maxlength' => '10'], 10], |
||
| 684 | ]; |
||
| 685 | } |
||
| 686 | |||
| 687 | public function getMaxConstraint(): array |
||
| 688 | { |
||
| 689 | return [ |
||
| 690 | ['', null], |
||
| 691 | ['max:100', 100], |
||
| 692 | [['max' => 100], 100], |
||
| 693 | [['max' => '100'], 100], |
||
| 694 | ]; |
||
| 695 | } |
||
| 696 | |||
| 697 | public function getMinCheckConstraint(): array |
||
| 698 | { |
||
| 699 | return [ |
||
| 700 | ['', null], |
||
| 701 | ['mincheck:3', 3], |
||
| 702 | [['mincheck' => 3], 3], |
||
| 703 | [['mincheck' => '3'], 3], |
||
| 704 | ]; |
||
| 705 | } |
||
| 706 | |||
| 707 | public function getMaxCheckConstraint(): array |
||
| 708 | { |
||
| 709 | return [ |
||
| 710 | ['', null], |
||
| 711 | ['maxcheck:4', 4], |
||
| 712 | [['maxcheck' => 4], 4], |
||
| 713 | [['maxcheck' => '4'], 4], |
||
| 714 | ]; |
||
| 715 | } |
||
| 716 | |||
| 717 | public function getCheckConstraint(): array |
||
| 726 | ]; |
||
| 727 | } |
||
| 728 | |||
| 729 | public function getLengthConstraint(): array |
||
| 730 | { |
||
| 731 | return [ |
||
| 732 | ['', null, null], |
||
| 733 | ['length:30,40', 30, 40], |
||
| 734 | [['length' => [ |
||
| 735 | 'min' => 5, |
||
| 736 | 'max' => 60, |
||
| 737 | ]], 5, 60], |
||
| 738 | ]; |
||
| 739 | } |
||
| 740 | |||
| 741 | public function getEqualToConstraint(): array |
||
| 742 | { |
||
| 743 | return [ |
||
| 744 | ['', null], |
||
| 745 | ['equalto:#foo', '#foo'], |
||
| 746 | [['equalto' => '.foo'], '.foo'], |
||
| 747 | ]; |
||
| 748 | } |
||
| 749 | |||
| 750 | public function getTypeConstraint(): array |
||
| 751 | { |
||
| 752 | return [ |
||
| 753 | ['', null], |
||
| 754 | ['type:number', 'number'], |
||
| 755 | [['type' => 'text'], 'text'], |
||
| 756 | ]; |
||
| 757 | } |
||
| 758 | |||
| 759 | public function getPkConstraint(): array |
||
| 760 | { |
||
| 761 | return [ |
||
| 762 | ['', false], |
||
| 763 | ['required|pk', true], |
||
| 764 | ['required|pk:true', true], |
||
| 765 | ['required|pk:false', false], |
||
| 766 | [['required', 'pk'], true], |
||
| 767 | [['required', 'pk' => true], true], |
||
| 768 | [['required', 'pk' => false], false], |
||
| 769 | ]; |
||
| 770 | } |
||
| 771 | |||
| 772 | public function getAiConstraint(): array |
||
| 773 | { |
||
| 774 | return [ |
||
| 775 | ['', false], |
||
| 776 | ['required|pk|ai', true], |
||
| 777 | ['required|pk|ai:true', true], |
||
| 778 | [['required', 'pk', 'ai'], true], |
||
| 779 | [['required', 'pk', 'ai' => true], true], |
||
| 780 | ]; |
||
| 781 | } |
||
| 782 | |||
| 783 | public function getCaConstraint(): array |
||
| 793 | ]; |
||
| 794 | } |
||
| 795 | |||
| 796 | public function getUaConstraint(): array |
||
| 797 | { |
||
| 798 | return [ |
||
| 799 | ['', false], |
||
| 800 | ['ua', true], |
||
| 801 | ['ua:true', true], |
||
| 802 | ['ua:false', false], |
||
| 803 | [['ua'], true], |
||
| 804 | [['ua' => true], true], |
||
| 805 | [['ua' => false], false], |
||
| 806 | ]; |
||
| 807 | } |
||
| 808 | |||
| 809 | public function getFkConstraint(): array |
||
| 810 | { |
||
| 811 | return [ |
||
| 812 | ['fk:table', 'table', 'id', 'name'], |
||
| 813 | ['fk:table2,username', 'table2', 'id', 'username'], |
||
| 814 | ['fk:table3,description,uuid', 'table3', 'uuid', 'description'], |
||
| 815 | [['fk' => 'table5'], 'table5', 'id', 'name'], |
||
| 816 | [['fk' => 'table6,username'], 'table6', 'id', 'username'], |
||
| 817 | [['fk' => 'table7,description,uuid'], 'table7', 'uuid', 'description'], |
||
| 818 | ]; |
||
| 819 | } |
||
| 820 | |||
| 821 | public function getCbConstraint(): array |
||
| 822 | { |
||
| 823 | return [ |
||
| 824 | ['', false], |
||
| 825 | ['cb', true], |
||
| 826 | ['cb:true', true], |
||
| 827 | ['cb:false', false], |
||
| 828 | [['cb'], true], |
||
| 829 | [['cb' => true], true], |
||
| 830 | [['cb' => false], false], |
||
| 831 | ]; |
||
| 832 | } |
||
| 833 | |||
| 834 | public function getUbConstraint(): array |
||
| 835 | { |
||
| 836 | return [ |
||
| 837 | ['', false], |
||
| 838 | ['ub', true], |
||
| 839 | ['ub:true', true], |
||
| 840 | ['ub:false', false], |
||
| 841 | [['ub'], true], |
||
| 842 | [['ub' => true], true], |
||
| 843 | [['ub' => false], false], |
||
| 844 | ]; |
||
| 845 | } |
||
| 846 | |||
| 847 | public function getFilterConstraint(): array |
||
| 848 | { |
||
| 849 | return [ |
||
| 850 | ['', null], |
||
| 851 | ['filter:eq', 'eq'], |
||
| 852 | ['filter:nn', 'nn'], |
||
| 853 | ]; |
||
| 854 | } |
||
| 855 | |||
| 856 | public function getFormatConstraint(): array |
||
| 857 | { |
||
| 858 | return [ |
||
| 859 | ['string', '', null], |
||
| 860 | ['datetime', 'format:timeago', 'timeago'], |
||
| 861 | ['datetime', 'format:datetime', 'datetime'], |
||
| 862 | ['integer', 'format:money', 'money'], |
||
| 863 | ]; |
||
| 864 | } |
||
| 865 | |||
| 866 | public function getTrimConstraint(): array |
||
| 867 | { |
||
| 868 | return [ |
||
| 869 | ['', false], |
||
| 870 | ['trim', true], |
||
| 871 | ['trim:true', true], |
||
| 872 | ['trim:false', false], |
||
| 873 | [['trim'], true], |
||
| 874 | [['trim' => true], true], |
||
| 875 | [['trim' => false], false], |
||
| 876 | ]; |
||
| 877 | } |
||
| 878 | |||
| 879 | public function getFcharsConstraint(): array |
||
| 880 | { |
||
| 881 | return [ |
||
| 882 | ['', null], |
||
| 883 | ['fchars:0', 0], |
||
| 884 | ['fchars:1', 1], |
||
| 885 | ['fchars:2', 2], |
||
| 886 | ['fchars:90', 90], |
||
| 887 | ]; |
||
| 888 | } |
||
| 889 | |||
| 890 | public function getLinkConstraint(): array |
||
| 891 | { |
||
| 892 | return [ |
||
| 893 | ['', false], |
||
| 894 | ['link', true], |
||
| 895 | ['link:true', true], |
||
| 896 | ['link:false', false], |
||
| 897 | [['link'], true], |
||
| 898 | [['link' => true], true], |
||
| 899 | [['link' => false], false], |
||
| 900 | ]; |
||
| 901 | } |
||
| 902 | |||
| 903 | public function getFkCheckConstraint(): array |
||
| 913 | ]; |
||
| 914 | } |
||
| 915 | |||
| 916 | public function getShowConstraint(): array |
||
| 917 | { |
||
| 918 | return [ |
||
| 919 | ['string', '', Action::ALL], |
||
| 920 | ['datetime', 'ca', Action::READ], |
||
| 921 | ['integer', 'cb', Action::READ], |
||
| 922 | ['datetime', 'ua', Action::READ], |
||
| 923 | ['integer', 'ub', Action::READ], |
||
| 924 | ['string', 'show:a', Action::ALL], |
||
| 925 | ['string', 'show:i', Action::INDEX], |
||
| 926 | ['string', 'show:c', Action::CREATE], |
||
| 927 | ['string', 'show:r', Action::READ], |
||
| 928 | ['string', 'show:u', Action::UPDATE], |
||
| 929 | ['string', 'show:p', Action::PATCH], |
||
| 930 | ['string', 'show:d', Action::DELETE], |
||
| 931 | ['datetime', 'ca|show:a', Action::ALL], |
||
| 932 | ['integer', 'cb|show:i', Action::INDEX], |
||
| 933 | ['datetime', 'ua|show:c', Action::CREATE], |
||
| 934 | ['integer', 'ub|show:r', Action::READ], |
||
| 935 | ]; |
||
| 936 | } |
||
| 937 | |||
| 938 | public function getHideConstraint(): array |
||
| 939 | { |
||
| 940 | return [ |
||
| 941 | ['string', '', '', false], |
||
| 942 | ['datetime', 'ca', Action::ALL, false], |
||
| 943 | ['datetime', 'ca', Action::INDEX, false], |
||
| 944 | ['datetime', 'ca', Action::CREATE, false], |
||
| 945 | ['datetime', 'ca', Action::READ, true], |
||
| 946 | ['datetime', 'ca', Action::UPDATE, false], |
||
| 947 | ['datetime', 'ca', Action::PATCH, false], |
||
| 948 | ['datetime', 'ca', Action::DELETE, false], |
||
| 949 | ['integer', 'cb', Action::ALL, false], |
||
| 950 | ['integer', 'cb', Action::INDEX, false], |
||
| 951 | ['integer', 'cb', Action::CREATE, false], |
||
| 952 | ['integer', 'cb', Action::READ, true], |
||
| 953 | ['integer', 'cb', Action::UPDATE, false], |
||
| 954 | ['integer', 'cb', Action::PATCH, false], |
||
| 955 | ['integer', 'cb', Action::DELETE, false], |
||
| 956 | ['string', 'hide:a', Action::ALL, false], |
||
| 957 | ['string', 'hide:i', Action::INDEX, false], |
||
| 958 | ['string', 'hide:c', Action::CREATE, false], |
||
| 959 | ['string', 'hide:r', Action::READ, false], |
||
| 960 | ['string', 'hide:u', Action::UPDATE, false], |
||
| 961 | ['string', 'hide:p', Action::PATCH, false], |
||
| 962 | ['string', 'hide:d', Action::DELETE, false], |
||
| 963 | ['datetime', 'ca|hide:r', Action::READ, false], |
||
| 964 | ['integer', 'cb|hide:r', Action::READ, false], |
||
| 965 | ['datetime', 'ua|hide:r', Action::READ, false], |
||
| 966 | ['integer', 'ub|hide:r', Action::READ, false], |
||
| 967 | ]; |
||
| 968 | } |
||
| 969 | |||
| 970 | public function getDefaultConstraint(): array |
||
| 971 | { |
||
| 972 | return [ |
||
| 973 | ['string', '', null], |
||
| 974 | ['string', ['default' => null], null], |
||
| 975 | ['string', 'default:', ''], |
||
| 976 | ['string', 'default:A', 'A'], |
||
| 977 | ['integer', 'default:-1', -1], |
||
| 978 | ['double', 'default:0.0', 0.0], |
||
| 979 | ['float', 'default:0.1', 0.1], |
||
| 980 | ['datetime', 'default:NOW', 'NOW'], |
||
| 981 | ]; |
||
| 982 | } |
||
| 983 | |||
| 984 | public function getConstraintLogicError(): array |
||
| 1083 | ]; |
||
| 1084 | } |
||
| 1085 | } |
||
| 1086 |