|
19 | 19 | use Codeception\Util\JsonType; |
20 | 20 | use Codeception\Util\XmlStructure; |
21 | 21 | use Codeception\Util\Soap as XmlUtils; |
| 22 | +use JsonSchema\Validator as JsonSchemaValidator; |
| 23 | +use JsonSchema\Constraints\Constraint as JsonContraint; |
22 | 24 |
|
23 | 25 | /** |
24 | 26 | * Module for testing REST WebService. |
@@ -771,18 +773,7 @@ public function seeResponseIsJson() |
771 | 773 | { |
772 | 774 | $responseContent = $this->connectionModule->_getResponseContent(); |
773 | 775 | \PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty'); |
774 | | - json_decode($responseContent); |
775 | | - $errorCode = json_last_error(); |
776 | | - $errorMessage = json_last_error_msg(); |
777 | | - \PHPUnit\Framework\Assert::assertEquals( |
778 | | - JSON_ERROR_NONE, |
779 | | - $errorCode, |
780 | | - sprintf( |
781 | | - "Invalid json: %s. System message: %s.", |
782 | | - $responseContent, |
783 | | - $errorMessage |
784 | | - ) |
785 | | - ); |
| 776 | + $this->decodeAndValidateJson($responseContent); |
786 | 777 | } |
787 | 778 |
|
788 | 779 | /** |
@@ -841,6 +832,58 @@ public function seeResponseContainsJson($json = []) |
841 | 832 | ); |
842 | 833 | } |
843 | 834 |
|
| 835 | + /** |
| 836 | + * Checks whether last response matches the supplied json schema |
| 837 | + * Supply schema as json string |
| 838 | + * |
| 839 | + * @part json |
| 840 | + */ |
| 841 | + public function seeResponseIsValidOnJsonSchema($schema) |
| 842 | + { |
| 843 | + $responseContent = $this->connectionModule->_getResponseContent(); |
| 844 | + \PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty'); |
| 845 | + $responseObject = $this->decodeAndValidateJson($responseContent); |
| 846 | + |
| 847 | + \PHPUnit\Framework\Assert::assertNotEquals('', $schema, 'schema is empty'); |
| 848 | + $schemaObject = $this->decodeAndValidateJson($schema, "Invalid schema json: %s. System message: %s."); |
| 849 | + |
| 850 | + $validator = new JsonSchemaValidator(); |
| 851 | + $validator->validate($responseObject, $schemaObject, JsonContraint::CHECK_MODE_VALIDATE_SCHEMA); |
| 852 | + $outcome = $validator->isValid(); |
| 853 | + $error = ""; |
| 854 | + if (!$outcome) { |
| 855 | + $errors = $validator->getErrors(); |
| 856 | + $error = array_shift($errors)["message"]; |
| 857 | + } |
| 858 | + \PHPUnit\Framework\Assert::assertTrue( |
| 859 | + $outcome, |
| 860 | + $error |
| 861 | + ); |
| 862 | + } |
| 863 | + |
| 864 | + /** |
| 865 | + * Converts string to json and asserts that no error occured while decoding. |
| 866 | + * |
| 867 | + * @param string $jsonString the json encoded string |
| 868 | + * @param string $errorFormat optional string for custom sprintf format |
| 869 | + */ |
| 870 | + protected function decodeAndValidateJson($jsonString, $errorFormat="Invalid json: %s. System message: %s.") |
| 871 | + { |
| 872 | + $json = json_decode($jsonString); |
| 873 | + $errorCode = json_last_error(); |
| 874 | + $errorMessage = json_last_error_msg(); |
| 875 | + \PHPUnit\Framework\Assert::assertEquals( |
| 876 | + JSON_ERROR_NONE, |
| 877 | + $errorCode, |
| 878 | + sprintf( |
| 879 | + $errorFormat, |
| 880 | + $jsonString, |
| 881 | + $errorMessage |
| 882 | + ) |
| 883 | + ); |
| 884 | + return $json; |
| 885 | + } |
| 886 | + |
844 | 887 | /** |
845 | 888 | * Returns current response so that it can be used in next scenario steps. |
846 | 889 | * |
|
0 commit comments