Skip to content

Commit c86417a

Browse files
authored
Add seeResponseIsValidOnJsonSchemaString and examples (#8)
* Added examples * Use file for schema by default Added `seeResponseIsValidOnJsonSchemaString` for checks based on string * Use codecept_absolute_path instead of codecept_data_dir
1 parent fc95e98 commit c86417a

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/Codeception/Module/REST.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,34 @@ public function seeResponseContainsJson($json = [])
833833
}
834834

835835
/**
836-
* Checks whether last response matches the supplied json schema
837-
* Supply schema as json string
836+
* Checks whether last response matches the supplied json schema (https://json-schema.org/)
837+
* Supply schema as json string.
838838
*
839+
* Examples:
840+
*
841+
* ``` php
842+
* <?php
843+
* // response: {"name": "john", "age": 20}
844+
* $I->seeResponseIsValidOnJsonSchemaString('{"type": "object"}');
845+
*
846+
* // response {"name": "john", "age": 20}
847+
* $schema = [
848+
* "properties" => [
849+
* "age" => [
850+
* "type" => "integer",
851+
* "minimum" => 18
852+
* ]
853+
* ]
854+
* ];
855+
* $I->seeResponseIsValidOnJsonSchemaString(json_encode($schema));
856+
*
857+
* ?>
858+
* ```
859+
*
860+
* @param string $schema
839861
* @part json
840862
*/
841-
public function seeResponseIsValidOnJsonSchema($schema)
863+
public function seeResponseIsValidOnJsonSchemaString($schema)
842864
{
843865
$responseContent = $this->connectionModule->_getResponseContent();
844866
\PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty');
@@ -861,6 +883,24 @@ public function seeResponseIsValidOnJsonSchema($schema)
861883
);
862884
}
863885

886+
/**
887+
* Checks whether last response matches the supplied json schema (https://json-schema.org/)
888+
* Supply schema as relative file path in your project directory or an absolute path
889+
*
890+
* @see codecept_absolute_path()
891+
*
892+
* @param string $schemaFilename
893+
* @part json
894+
*/
895+
public function seeResponseIsValidOnJsonSchema($schemaFilename)
896+
{
897+
$file = codecept_absolute_path($schemaFilename);
898+
if (!file_exists($file)) {
899+
throw new ModuleException(__CLASS__, "File $file does not exist");
900+
}
901+
$this->seeResponseIsValidOnJsonSchemaString(file_get_contents($file));
902+
}
903+
864904
/**
865905
* Converts string to json and asserts that no error occured while decoding.
866906
*

tests/unit/Codeception/Module/RestTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,26 @@ public function testSeeResponseIsValidOnJsonSchemachesJsonSchema($schema, $respo
478478
$response = file_get_contents(codecept_data_dir($response));
479479
$this->setStubResponse($response);
480480

481-
$validSchema = file_get_contents(codecept_data_dir($schema));
482-
483481
if (!$outcome) {
484482
$this->expectExceptionMessage($error);
485483
$this->shouldFail();
486484
}
487-
$this->module->seeResponseIsValidOnJsonSchema($validSchema);
485+
$this->module->seeResponseIsValidOnJsonSchema(codecept_data_dir($schema));
486+
}
487+
488+
public function testSeeResponseIsValidOnJsonSchemachesJsonSchemaString() {
489+
$this->setStubResponse('{"name": "john", "age": 20}');
490+
$this->module->seeResponseIsValidOnJsonSchemaString('{"type": "object"}');
491+
492+
$schema = [
493+
"properties" => [
494+
"age" => [
495+
"type" => "integer",
496+
"minimum" => 18
497+
]
498+
]
499+
];
500+
$this->module->seeResponseIsValidOnJsonSchemaString(json_encode($schema));
488501
}
489502

490503
/**

0 commit comments

Comments
 (0)