diff --git a/src/Codeception/Module/REST.php b/src/Codeception/Module/REST.php index 5fcbdad..b7b6c33 100644 --- a/src/Codeception/Module/REST.php +++ b/src/Codeception/Module/REST.php @@ -967,15 +967,19 @@ public function seeResponseIsValidOnJsonSchemaString(string $schema): void $validator->validate($responseObject, $schemaObject, JsonConstraint::CHECK_MODE_VALIDATE_SCHEMA); $outcome = $validator->isValid(); - $error = ''; + $message = ''; if (!$outcome) { - $errors = $validator->getErrors(); - $error = array_shift($errors)["message"]; + foreach ($validator->getErrors() as $error) { + if ($message !== '') { + $message .= ', '; + } + $message .= sprintf("[Property: '%s'] %s", $error['property'], $error['message']); + } } Assert::assertTrue( $outcome, - $error + $message ); } diff --git a/tests/unit/Codeception/Module/RestTest.php b/tests/unit/Codeception/Module/RestTest.php index 563d345..cd1c4e7 100644 --- a/tests/unit/Codeception/Module/RestTest.php +++ b/tests/unit/Codeception/Module/RestTest.php @@ -575,7 +575,7 @@ public function testSeeResponseJsonXpathEvaluatesToBoolean() $this->setStubResponse('{"success": 1}'); $this->module->seeResponseJsonXpathEvaluatesTo('count(//success) > 0', true); } - + public function testSeeResponseJsonXpathEvaluatesToNumber() { $this->setStubResponse('{"success": 1}'); @@ -587,7 +587,7 @@ public function testDontSeeResponseJsonXpathEvaluatesToBoolean() $this->setStubResponse('{"success": 1}'); $this->module->dontSeeResponseJsonXpathEvaluatesTo('count(//success) > 0', false); } - + public function testDontSeeResponseJsonXpathEvaluatesToNumber() { $this->setStubResponse('{"success": 1}'); @@ -649,7 +649,7 @@ public function testHaveServerParameter() * @dataProvider schemaAndResponse */ - public function testSeeResponseIsValidOnJsonSchemachesJsonSchema(string $schema, string $response, bool $outcome, string $error) + public function testSeeResponseIsValidOnJsonSchemaMatchesJsonSchema(string $schema, string $response, bool $outcome, string $error) { $response = file_get_contents(codecept_data_dir($response)); $this->setStubResponse($response); @@ -662,7 +662,7 @@ public function testSeeResponseIsValidOnJsonSchemachesJsonSchema(string $schema, $this->module->seeResponseIsValidOnJsonSchema(codecept_data_dir($schema)); } - public function testSeeResponseIsValidOnJsonSchemachesJsonSchemaString() + public function testSeeResponseIsValidOnJsonSchemaMatchesJsonSchemaString() { $this->setStubResponse('{"name": "john", "age": 20}'); $this->module->seeResponseIsValidOnJsonSchemaString('{"type": "object"}'); @@ -678,6 +678,14 @@ public function testSeeResponseIsValidOnJsonSchemachesJsonSchemaString() $this->module->seeResponseIsValidOnJsonSchemaString(json_encode($schema, JSON_THROW_ON_ERROR)); } + public function testSeeResponseIsInvalidOnJsonSchemaMatchesJsonSchemaString() + { + $this->setStubResponse('{"name": null, "age": 20}'); + $this->expectExceptionMessage("[Property: 'name'] NULL value found, but a string is required"); + $this->shouldFail(); + $this->module->seeResponseIsValidOnJsonSchemaString('{"type": "object", "properties": {"name": {"type": "string"}, "age": {"type": "integer"}}}'); + } + /** * @dataProvider configAndRequestUrls */