Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Codeception/Module/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
16 changes: 12 additions & 4 deletions tests/unit/Codeception/Module/RestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
Expand All @@ -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}');
Expand Down Expand Up @@ -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);
Expand All @@ -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"}');
Expand All @@ -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
*/
Expand Down