|
9 | 9 | use Studio\OpenApiContractTesting\OpenApiResponseValidator; |
10 | 10 | use Studio\OpenApiContractTesting\OpenApiSpecLoader; |
11 | 11 |
|
| 12 | +use function count; |
| 13 | + |
12 | 14 | class OpenApiResponseValidatorTest extends TestCase |
13 | 15 | { |
14 | 16 | private OpenApiResponseValidator $validator; |
@@ -584,6 +586,76 @@ public function v31_no_content_response_passes(): void |
584 | 586 | $this->assertTrue($result->isValid()); |
585 | 587 | } |
586 | 588 |
|
| 589 | + // ======================================== |
| 590 | + // maxErrors tests |
| 591 | + // ======================================== |
| 592 | + |
| 593 | + #[Test] |
| 594 | + public function default_max_errors_reports_multiple_errors(): void |
| 595 | + { |
| 596 | + $result = $this->validator->validate( |
| 597 | + 'petstore-3.0', |
| 598 | + 'GET', |
| 599 | + '/v1/pets', |
| 600 | + 200, |
| 601 | + [ |
| 602 | + 'data' => [ |
| 603 | + ['id' => 'not-an-int', 'name' => 123], |
| 604 | + ['id' => 'also-not-an-int', 'name' => 456], |
| 605 | + ], |
| 606 | + ], |
| 607 | + ); |
| 608 | + |
| 609 | + $this->assertFalse($result->isValid()); |
| 610 | + $this->assertGreaterThan(1, count($result->errors())); |
| 611 | + } |
| 612 | + |
| 613 | + #[Test] |
| 614 | + public function max_errors_one_limits_to_single_error(): void |
| 615 | + { |
| 616 | + $validator = new OpenApiResponseValidator(maxErrors: 1); |
| 617 | + $result = $validator->validate( |
| 618 | + 'petstore-3.0', |
| 619 | + 'GET', |
| 620 | + '/v1/pets', |
| 621 | + 200, |
| 622 | + [ |
| 623 | + 'data' => [ |
| 624 | + ['id' => 'not-an-int', 'name' => 123], |
| 625 | + ['id' => 'also-not-an-int', 'name' => 456], |
| 626 | + ], |
| 627 | + ], |
| 628 | + ); |
| 629 | + |
| 630 | + $this->assertFalse($result->isValid()); |
| 631 | + $this->assertCount(1, $result->errors()); |
| 632 | + } |
| 633 | + |
| 634 | + #[Test] |
| 635 | + public function max_errors_zero_reports_all_errors(): void |
| 636 | + { |
| 637 | + $validator = new OpenApiResponseValidator(maxErrors: 0); |
| 638 | + $result = $validator->validate( |
| 639 | + 'petstore-3.0', |
| 640 | + 'GET', |
| 641 | + '/v1/pets', |
| 642 | + 200, |
| 643 | + [ |
| 644 | + 'data' => [ |
| 645 | + ['id' => 'not-an-int', 'name' => 123], |
| 646 | + ['id' => 'also-not-an-int', 'name' => 456], |
| 647 | + ], |
| 648 | + ], |
| 649 | + ); |
| 650 | + |
| 651 | + $this->assertFalse($result->isValid()); |
| 652 | + $this->assertGreaterThan(1, count($result->errors())); |
| 653 | + } |
| 654 | + |
| 655 | + // ======================================== |
| 656 | + // Strip prefix tests |
| 657 | + // ======================================== |
| 658 | + |
587 | 659 | #[Test] |
588 | 660 | public function v30_strip_prefixes_applied(): void |
589 | 661 | { |
|
0 commit comments