Skip to content

Commit 17d8993

Browse files
committed
test: add maxErrors behavior tests for OpenApiResponseValidator
1 parent 874cbdb commit 17d8993

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

tests/Unit/OpenApiResponseValidatorTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Studio\OpenApiContractTesting\OpenApiResponseValidator;
1010
use Studio\OpenApiContractTesting\OpenApiSpecLoader;
1111

12+
use function count;
13+
1214
class OpenApiResponseValidatorTest extends TestCase
1315
{
1416
private OpenApiResponseValidator $validator;
@@ -584,6 +586,76 @@ public function v31_no_content_response_passes(): void
584586
$this->assertTrue($result->isValid());
585587
}
586588

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+
587659
#[Test]
588660
public function v30_strip_prefixes_applied(): void
589661
{

0 commit comments

Comments
 (0)