Skip to content

Commit

Permalink
Add seeResponseIsValidOnJsonSchemaString and examples (#8)
Browse files Browse the repository at this point in the history
* Added examples

* Use file for schema by default
Added `seeResponseIsValidOnJsonSchemaString` for checks based on string

* Use codecept_absolute_path instead of codecept_data_dir
  • Loading branch information
ruudboon committed Feb 1, 2020
1 parent fc95e98 commit c86417a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
46 changes: 43 additions & 3 deletions src/Codeception/Module/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,12 +833,34 @@ public function seeResponseContainsJson($json = [])
}

/**
* Checks whether last response matches the supplied json schema
* Supply schema as json string
* Checks whether last response matches the supplied json schema (https://json-schema.org/)
* Supply schema as json string.
*
* Examples:
*
* ``` php
* <?php
* // response: {"name": "john", "age": 20}
* $I->seeResponseIsValidOnJsonSchemaString('{"type": "object"}');
*
* // response {"name": "john", "age": 20}
* $schema = [
* "properties" => [
* "age" => [
* "type" => "integer",
* "minimum" => 18
* ]
* ]
* ];
* $I->seeResponseIsValidOnJsonSchemaString(json_encode($schema));
*
* ?>
* ```
*
* @param string $schema
* @part json
*/
public function seeResponseIsValidOnJsonSchema($schema)
public function seeResponseIsValidOnJsonSchemaString($schema)
{
$responseContent = $this->connectionModule->_getResponseContent();
\PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty');
Expand All @@ -861,6 +883,24 @@ public function seeResponseIsValidOnJsonSchema($schema)
);
}

/**
* Checks whether last response matches the supplied json schema (https://json-schema.org/)
* Supply schema as relative file path in your project directory or an absolute path
*
* @see codecept_absolute_path()
*
* @param string $schemaFilename
* @part json
*/
public function seeResponseIsValidOnJsonSchema($schemaFilename)
{
$file = codecept_absolute_path($schemaFilename);
if (!file_exists($file)) {
throw new ModuleException(__CLASS__, "File $file does not exist");
}
$this->seeResponseIsValidOnJsonSchemaString(file_get_contents($file));
}

/**
* Converts string to json and asserts that no error occured while decoding.
*
Expand Down
19 changes: 16 additions & 3 deletions tests/unit/Codeception/Module/RestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,26 @@ public function testSeeResponseIsValidOnJsonSchemachesJsonSchema($schema, $respo
$response = file_get_contents(codecept_data_dir($response));
$this->setStubResponse($response);

$validSchema = file_get_contents(codecept_data_dir($schema));

if (!$outcome) {
$this->expectExceptionMessage($error);
$this->shouldFail();
}
$this->module->seeResponseIsValidOnJsonSchema($validSchema);
$this->module->seeResponseIsValidOnJsonSchema(codecept_data_dir($schema));
}

public function testSeeResponseIsValidOnJsonSchemachesJsonSchemaString() {
$this->setStubResponse('{"name": "john", "age": 20}');
$this->module->seeResponseIsValidOnJsonSchemaString('{"type": "object"}');

$schema = [
"properties" => [
"age" => [
"type" => "integer",
"minimum" => 18
]
]
];
$this->module->seeResponseIsValidOnJsonSchemaString(json_encode($schema));
}

/**
Expand Down

0 comments on commit c86417a

Please sign in to comment.