Skip to content

Commit d663dd5

Browse files
committed
Refactor seeResponseIsJson and seeResponseIsValidOnJsonSchema to share functionality.
1 parent bc5d5eb commit d663dd5

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

src/Codeception/Module/REST.php

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -773,18 +773,7 @@ public function seeResponseIsJson()
773773
{
774774
$responseContent = $this->connectionModule->_getResponseContent();
775775
\PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty');
776-
json_decode($responseContent);
777-
$errorCode = json_last_error();
778-
$errorMessage = json_last_error_msg();
779-
\PHPUnit\Framework\Assert::assertEquals(
780-
JSON_ERROR_NONE,
781-
$errorCode,
782-
sprintf(
783-
"Invalid json: %s. System message: %s.",
784-
$responseContent,
785-
$errorMessage
786-
)
787-
);
776+
$this->decodeAndValidateJson($responseContent);
788777
}
789778

790779
/**
@@ -853,32 +842,10 @@ public function seeResponseIsValidOnJsonSchema($schema)
853842
{
854843
$responseContent = $this->connectionModule->_getResponseContent();
855844
\PHPUnit\Framework\Assert::assertNotEquals('', $responseContent, 'response is empty');
856-
$responseObject = json_decode($responseContent);
857-
$errorCode = json_last_error();
858-
$errorMessage = json_last_error_msg();
859-
\PHPUnit\Framework\Assert::assertEquals(
860-
JSON_ERROR_NONE,
861-
$errorCode,
862-
sprintf(
863-
"Invalid json: %s. System message: %s.",
864-
$responseContent,
865-
$errorMessage
866-
)
867-
);
845+
$responseObject = $this->decodeAndValidateJson($responseContent);
868846

869847
\PHPUnit\Framework\Assert::assertNotEquals('', $schema, 'schema is empty');
870-
$schemaObject = json_decode($schema, true);
871-
$errorCode = json_last_error();
872-
$errorMessage = json_last_error_msg();
873-
\PHPUnit\Framework\Assert::assertEquals(
874-
JSON_ERROR_NONE,
875-
$errorCode,
876-
sprintf(
877-
"Invalid schema json: %s. System message: %s.",
878-
$responseContent,
879-
$errorMessage
880-
)
881-
);
848+
$schemaObject = $this->decodeAndValidateJson($schema, "Invalid schema json: %s. System message: %s.");
882849

883850
$validator = new JsonSchemaValidator();
884851
$validator->validate($responseObject, $schemaObject, JsonContraint::CHECK_MODE_VALIDATE_SCHEMA);
@@ -894,6 +861,29 @@ public function seeResponseIsValidOnJsonSchema($schema)
894861
);
895862
}
896863

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+
897887
/**
898888
* Returns current response so that it can be used in next scenario steps.
899889
*

0 commit comments

Comments
 (0)