Skip to content

Improved support for Codeception 5/PHP 8 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 12, 2022
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
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/Robofile.php export-ignore
/*.md export-ignore
/*.yml export-ignore
36 changes: 22 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
{
"name":"codeception/module-rest",
"description":"REST module for Codeception",
"keywords":["codeception", "rest"],
"homepage":"https://codeception.com/",
"type":"library",
"license":"MIT",
"authors":[
"name": "codeception/module-rest",
"description": "REST module for Codeception",
"license": "MIT",
"type": "library",
"keywords": [
"codeception",
"rest"
],
"authors": [
{
"name":"Gintautas Miselis"
"name": "Gintautas Miselis"
}
],
"minimum-stability": "dev",
"homepage": "https://codeception.com/",
"require": {
"php": "^8.0",
"ext-dom": "*",
"ext-json": "*",
"codeception/codeception": "^5.0.0-alpha1",
"codeception/codeception": "dev-5.0-interfaces as 5.0.0",
"justinrainbow/json-schema": "~5.2.9",
"softcreatr/jsonpath": "^0.8"
},
"require-dev": {
"ext-libxml": "*",
"ext-simplexml": "*",
"codeception/lib-innerbrowser": "^3.0",
"codeception/stub": "^4.0",
"codeception/util-universalframework": "^1.0",
"codeception/lib-innerbrowser": "^2.0 | *@dev"
"codeception/util-universalframework": "^1.0"
},
"conflict": {
"codeception/codeception": "<5.0"
},
"suggest": {
"aws/aws-sdk-php": "For using AWS Auth"
},
"autoload":{
"classmap": ["src/"]
"minimum-stability": "dev",
"autoload": {
"classmap": [
"src/"
]
},
"config": {
"classmap-authoritative": true
Expand Down
21 changes: 8 additions & 13 deletions src/Codeception/Module/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class REST extends Module implements DependsOnModule, PartedModule, API, Conflic
/**
* @var array<string, string>
*/
protected $config = [
protected array $config = [
'url' => '',
'aws' => ''
];
Expand Down Expand Up @@ -245,7 +245,6 @@ public function deleteHeader(string $name): void
* Checks over the given HTTP header and (optionally)
* its value, asserting that are there
*
* @param string $name
* @param $value
* @part json
* @part xml
Expand All @@ -267,7 +266,6 @@ public function seeHttpHeader(string $name, $value = null): void
* Checks over the given HTTP header and (optionally)
* its value, asserting that are not there
*
* @param string $name
* @param $value
* @part json
* @part xml
Expand Down Expand Up @@ -295,7 +293,6 @@ public function dontSeeHttpHeader(string $name, $value = null): void
* $I->seeHttpHeaderOnce('Cache-Control');
* ```
*
* @param string $name
* @part json
* @part xml
*/
Expand All @@ -313,7 +310,7 @@ public function seeHttpHeaderOnce(string $name): void
* @part json
* @part xml
*/
public function grabHttpHeader(string $name, bool $first = true)
public function grabHttpHeader(string $name, bool $first = true): string|array
{
return $this->getRunningClient()->getInternalResponse()->getHeader($name, $first);
}
Expand Down Expand Up @@ -408,7 +405,6 @@ public function amNTLMAuthenticated(string $username, string $password): void
* <?php
* $I->amAWSAuthenticated();
* ```
* @param array $additionalAWSConfig
* @throws ConfigurationException
*/
public function amAWSAuthenticated(array $additionalAWSConfig = []): void
Expand Down Expand Up @@ -528,7 +524,7 @@ public function sendGet(string $url, array $params = [])
* $response = $I->sendPut('/message/1', ['subject' => 'Read this!']);
* ```
*
* @param array|string|\JsonSerializable $params
* @param array|string|JsonSerializable $params
* @part json
* @part xml
*/
Expand All @@ -545,7 +541,7 @@ public function sendPut(string $url, $params = [], array $files = [])
* $response = $I->sendPatch('/message/1', ['subject' => 'Read this!']);
* ```
*
* @param array|string|\JsonSerializable $params
* @param array|string|JsonSerializable $params
* @part json
* @part xml
*/
Expand Down Expand Up @@ -657,7 +653,7 @@ protected function execute($method, $url, $parameters = [], $files = [])
$url = $this->config['url'];
} elseif (!is_string($url)) {
throw new ModuleException(__CLASS__, 'URL must be string');
} elseif (strpos($url, '://') === false && $this->config['url']) {
} elseif (!str_contains($url, '://') && $this->config['url']) {
$url = rtrim($this->config['url'], '/') . '/' . ltrim($url, '/');
}

Expand All @@ -676,7 +672,7 @@ protected function execute($method, $url, $parameters = [], $files = [])
if (is_array($parameters) || $isQueryParamsAwareMethod) {
if ($isQueryParamsAwareMethod) {
if (!empty($parameters)) {
if (strpos($url, '?') !== false) {
if (str_contains($url, '?')) {
$url .= '&';
} else {
$url .= '?';
Expand Down Expand Up @@ -706,7 +702,7 @@ protected function execute($method, $url, $parameters = [], $files = [])
}

$printedResponse = $this->response;
if ($this->isBinaryData((string) $printedResponse)) {
if ($this->isBinaryData((string)$printedResponse)) {
$printedResponse = $this->binaryToDebugString($printedResponse);
}

Expand Down Expand Up @@ -980,7 +976,6 @@ public function seeResponseIsValidOnJsonSchemaString(string $schema): void
* 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
*
* @param string $schemaFilename
* @part json
* @see codecept_absolute_path()
*/
Expand All @@ -1000,7 +995,7 @@ public function seeResponseIsValidOnJsonSchema(string $schemaFilename): void
* @param string $jsonString the json encoded string
* @param string $errorFormat optional string for custom sprintf format
*/
protected function decodeAndValidateJson(string $jsonString, string $errorFormat="Invalid json: %s. System message: %s.")
protected function decodeAndValidateJson(string $jsonString, string $errorFormat = "Invalid json: %s. System message: %s.")
{
$json = json_decode($jsonString);
$errorCode = json_last_error();
Expand Down
4 changes: 2 additions & 2 deletions src/Codeception/Step/AsJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public function run(ModuleContainer $container = null)
$container->getModule('REST')->haveHttpHeader('Content-Type', 'application/json');
$resp = parent::run($container);
$container->getModule('REST')->seeResponseIsJson();
return json_decode($resp, true);
return json_decode($resp, true, 512, JSON_THROW_ON_ERROR);
}

public static function getTemplate(Template $template): ?Template
{
$action = $template->getVar('action');

// should only be applied to send* methods
if (strpos($action, 'send') !== 0) return;
if (!str_starts_with($action, 'send')) return null;

$conditionalDoc = "* JSON response will be automatically decoded \n " . $template->getVar('doc');

Expand Down
12 changes: 3 additions & 9 deletions src/Codeception/Util/JsonArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ public function toArray(): array
return $this->jsonArray;
}

/**
* @return DOMNodeList|bool
*/
public function filterByXPath(string $xPath)
public function filterByXPath(string $xPath): DOMNodeList|false
{
$path = new DOMXPath($this->toXml());
return $path->query($xPath);
Expand All @@ -88,10 +85,7 @@ public function filterByJsonPath(string $jsonPath): array
return (new JSONPath($this->jsonArray))->find($jsonPath)->getData();
}

/**
* @return string|false
*/
public function getXmlString()
public function getXmlString(): string|bool
{
return $this->toXml()->saveXML();
}
Expand All @@ -110,7 +104,7 @@ private function arrayToXml(DOMDocument $doc, DOMNode $node, array $array): void
} else {
try {
$subNode = $doc->createElement($key);
} catch (Exception $exception) {
} catch (Exception) {
$key = $this->getValidTagNameForInvalidKey($key);
$subNode = $doc->createElement($key);
}
Expand Down
21 changes: 8 additions & 13 deletions src/Codeception/Util/JsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ public static function cleanCustomFilters(): void
* Checks data against passed JsonType.
* If matching fails function returns a string with a message describing failure.
* On success returns `true`.
*
* @return string|bool
*/
public function matches(array $jsonType)
public function matches(array $jsonType): string|bool
{
if (array_key_exists(0, $this->jsonArray) && is_array($this->jsonArray[0])) {
// a list of items
Expand All @@ -116,10 +114,7 @@ public function matches(array $jsonType)
return $this->typeComparison($this->jsonArray, $jsonType);
}

/**
* @return string|bool
*/
protected function typeComparison(array $data, array $jsonType)
protected function typeComparison(array $data, array $jsonType): string|bool
{
foreach ($jsonType as $key => $type) {
if (!array_key_exists($key, $data)) {
Expand Down Expand Up @@ -149,8 +144,8 @@ protected function typeComparison(array $data, array $jsonType)
return ':regex($$' . $count++ . ')';
}, $type);

$matchTypes = preg_split("#(?![^]\(]*\))\|#", $filterType);
$matched = false;
$matchTypes = preg_split("#(?![^]\(]*\))\|#", $filterType);
$matched = false;
$currentType = strtolower(gettype($data[$key]));

if ($currentType === 'double') {
Expand All @@ -175,7 +170,7 @@ protected function typeComparison(array $data, array $jsonType)
return $regexes[1][$pos];
}, $filter);

$matched = $matched && $this->matchFilter($filter, (string) $data[$key]);
$matched = $matched && $this->matchFilter($filter, (string)$data[$key]);
}

if ($matched) {
Expand All @@ -194,13 +189,13 @@ protected function typeComparison(array $data, array $jsonType)
protected function matchFilter(string $filter, string $value)
{
$filter = trim($filter);
if (strpos($filter, '!') === 0) {
if (str_starts_with($filter, '!')) {
return !$this->matchFilter(substr($filter, 1), $value);
}

// apply custom filters
foreach (static::$customFilters as $customFilter => $callable) {
if (strpos($customFilter, '/') === 0 && preg_match($customFilter, $filter, $matches)) {
if (str_starts_with($customFilter, '/') && preg_match($customFilter, $filter, $matches)) {
array_shift($matches);
return call_user_func_array($callable, array_merge([$value], $matches));
}
Expand All @@ -210,7 +205,7 @@ protected function matchFilter(string $filter, string $value)
}
}

if (strpos($filter, '=') === 0) {
if (str_starts_with($filter, '=')) {
return $value === substr($filter, 1);
}

Expand Down
Loading