Skip to content

Commit abb4f25

Browse files
Show property name on failing JSON schema assertions (#105)
* Show property name on failing JSON schema assertions * Add validating test
1 parent 35adf73 commit abb4f25

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Codeception/Module/REST.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -967,15 +967,19 @@ public function seeResponseIsValidOnJsonSchemaString(string $schema): void
967967
$validator->validate($responseObject, $schemaObject, JsonConstraint::CHECK_MODE_VALIDATE_SCHEMA);
968968

969969
$outcome = $validator->isValid();
970-
$error = '';
970+
$message = '';
971971
if (!$outcome) {
972-
$errors = $validator->getErrors();
973-
$error = array_shift($errors)["message"];
972+
foreach ($validator->getErrors() as $error) {
973+
if ($message !== '') {
974+
$message .= ', ';
975+
}
976+
$message .= sprintf("[Property: '%s'] %s", $error['property'], $error['message']);
977+
}
974978
}
975979

976980
Assert::assertTrue(
977981
$outcome,
978-
$error
982+
$message
979983
);
980984
}
981985

tests/unit/Codeception/Module/RestTest.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public function testSeeResponseJsonXpathEvaluatesToBoolean()
575575
$this->setStubResponse('{"success": 1}');
576576
$this->module->seeResponseJsonXpathEvaluatesTo('count(//success) > 0', true);
577577
}
578-
578+
579579
public function testSeeResponseJsonXpathEvaluatesToNumber()
580580
{
581581
$this->setStubResponse('{"success": 1}');
@@ -587,7 +587,7 @@ public function testDontSeeResponseJsonXpathEvaluatesToBoolean()
587587
$this->setStubResponse('{"success": 1}');
588588
$this->module->dontSeeResponseJsonXpathEvaluatesTo('count(//success) > 0', false);
589589
}
590-
590+
591591
public function testDontSeeResponseJsonXpathEvaluatesToNumber()
592592
{
593593
$this->setStubResponse('{"success": 1}');
@@ -649,7 +649,7 @@ public function testHaveServerParameter()
649649
* @dataProvider schemaAndResponse
650650
*/
651651

652-
public function testSeeResponseIsValidOnJsonSchemachesJsonSchema(string $schema, string $response, bool $outcome, string $error)
652+
public function testSeeResponseIsValidOnJsonSchemaMatchesJsonSchema(string $schema, string $response, bool $outcome, string $error)
653653
{
654654
$response = file_get_contents(codecept_data_dir($response));
655655
$this->setStubResponse($response);
@@ -662,7 +662,7 @@ public function testSeeResponseIsValidOnJsonSchemachesJsonSchema(string $schema,
662662
$this->module->seeResponseIsValidOnJsonSchema(codecept_data_dir($schema));
663663
}
664664

665-
public function testSeeResponseIsValidOnJsonSchemachesJsonSchemaString()
665+
public function testSeeResponseIsValidOnJsonSchemaMatchesJsonSchemaString()
666666
{
667667
$this->setStubResponse('{"name": "john", "age": 20}');
668668
$this->module->seeResponseIsValidOnJsonSchemaString('{"type": "object"}');
@@ -678,6 +678,14 @@ public function testSeeResponseIsValidOnJsonSchemachesJsonSchemaString()
678678
$this->module->seeResponseIsValidOnJsonSchemaString(json_encode($schema, JSON_THROW_ON_ERROR));
679679
}
680680

681+
public function testSeeResponseIsInvalidOnJsonSchemaMatchesJsonSchemaString()
682+
{
683+
$this->setStubResponse('{"name": null, "age": 20}');
684+
$this->expectExceptionMessage("[Property: 'name'] NULL value found, but a string is required");
685+
$this->shouldFail();
686+
$this->module->seeResponseIsValidOnJsonSchemaString('{"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}}}');
687+
}
688+
681689
/**
682690
* @dataProvider configAndRequestUrls
683691
*/

0 commit comments

Comments
 (0)