|
13 | 13 | use Tempest\Database\Migrations\CreateMigrationsTable; |
14 | 14 | use Tempest\Mapper\CasterFactory; |
15 | 15 | use Tempest\Mapper\SerializerFactory; |
| 16 | +use Tempest\Validation\Exceptions\ValidationException; |
16 | 17 | use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable; |
17 | 18 | use Tests\Tempest\Fixtures\Migrations\CreateBookTable; |
18 | 19 | use Tests\Tempest\Fixtures\Models\A; |
|
42 | 43 | use Tests\Tempest\Integration\ORM\Models\CasterEnum; |
43 | 44 | use Tests\Tempest\Integration\ORM\Models\CasterModel; |
44 | 45 | use Tests\Tempest\Integration\ORM\Models\ChildModel; |
| 46 | +use Tests\Tempest\Integration\ORM\Models\ModelWithValidation; |
45 | 47 | use Tests\Tempest\Integration\ORM\Models\ParentModel; |
46 | 48 | use Tests\Tempest\Integration\ORM\Models\StaticMethodTableNameModel; |
47 | 49 | use Tests\Tempest\Integration\ORM\Models\ThroughModel; |
48 | 50 |
|
| 51 | +use function Tempest\Database\model; |
49 | 52 | use function Tempest\map; |
50 | 53 |
|
51 | 54 | /** |
@@ -540,4 +543,54 @@ public function test_table_name_overrides(): void |
540 | 543 | $this->assertEquals('custom_attribute_table_name', new ModelDefinition(AttributeTableNameModel::class)->getTableDefinition()->name); |
541 | 544 | $this->assertEquals('custom_static_method_table_name', new ModelDefinition(StaticMethodTableNameModel::class)->getTableDefinition()->name); |
542 | 545 | } |
| 546 | + |
| 547 | + public function test_validation_on_create(): void |
| 548 | + { |
| 549 | + $this->expectException(ValidationException::class); |
| 550 | + |
| 551 | + ModelWithValidation::create( |
| 552 | + index: -1, |
| 553 | + ); |
| 554 | + } |
| 555 | + |
| 556 | + public function test_validation_on_update(): void |
| 557 | + { |
| 558 | + $model = ModelWithValidation::new( |
| 559 | + id: new Id(1), |
| 560 | + index: 1, |
| 561 | + ); |
| 562 | + |
| 563 | + $this->expectException(ValidationException::class); |
| 564 | + |
| 565 | + $model->update( |
| 566 | + index: -1, |
| 567 | + ); |
| 568 | + } |
| 569 | + |
| 570 | + public function test_validation_on_new(): void |
| 571 | + { |
| 572 | + $model = ModelWithValidation::new( |
| 573 | + index: 1, |
| 574 | + ); |
| 575 | + |
| 576 | + $model->index = -1; |
| 577 | + |
| 578 | + $this->expectException(ValidationException::class); |
| 579 | + |
| 580 | + $model->save(); |
| 581 | + } |
| 582 | + |
| 583 | + public function test_skipped_validation(): void |
| 584 | + { |
| 585 | + try { |
| 586 | + model(ModelWithValidation::class)->validate( |
| 587 | + index: -1, |
| 588 | + skip: -1, |
| 589 | + ); |
| 590 | + } catch (ValidationException $validationException) { |
| 591 | + $this->assertStringContainsString('index', $validationException->getMessage()); |
| 592 | + $this->assertStringContainsString(ModelWithValidation::class, $validationException->getMessage()); |
| 593 | + $this->assertStringNotContainsString('skip', $validationException->getMessage()); |
| 594 | + } |
| 595 | + } |
543 | 596 | } |
0 commit comments