Skip to content

Commit 73f5c9b

Browse files
Improved support for Codeception 5/PHP 8 (#72)
Co-authored-by: TavoNiievez <[email protected]>
1 parent 393b71e commit 73f5c9b

File tree

8 files changed

+77
-80
lines changed

8 files changed

+77
-80
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
/tests export-ignore
33
/.gitattributes export-ignore
44
/.gitignore export-ignore
5-
/Robofile.php export-ignore
65
/*.md export-ignore
76
/*.yml export-ignore

composer.json

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
{
2-
"name":"codeception/module-rest",
3-
"description":"REST module for Codeception",
4-
"keywords":["codeception", "rest"],
5-
"homepage":"https://codeception.com/",
6-
"type":"library",
7-
"license":"MIT",
8-
"authors":[
2+
"name": "codeception/module-rest",
3+
"description": "REST module for Codeception",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": [
7+
"codeception",
8+
"rest"
9+
],
10+
"authors": [
911
{
10-
"name":"Gintautas Miselis"
12+
"name": "Gintautas Miselis"
1113
}
1214
],
13-
"minimum-stability": "dev",
15+
"homepage": "https://codeception.com/",
1416
"require": {
1517
"php": "^8.0",
1618
"ext-dom": "*",
1719
"ext-json": "*",
18-
"codeception/codeception": "^5.0.0-alpha1",
20+
"codeception/codeception": "dev-5.0-interfaces as 5.0.0",
1921
"justinrainbow/json-schema": "~5.2.9",
2022
"softcreatr/jsonpath": "^0.8"
2123
},
2224
"require-dev": {
2325
"ext-libxml": "*",
2426
"ext-simplexml": "*",
27+
"codeception/lib-innerbrowser": "^3.0",
2528
"codeception/stub": "^4.0",
26-
"codeception/util-universalframework": "^1.0",
27-
"codeception/lib-innerbrowser": "^2.0 | *@dev"
29+
"codeception/util-universalframework": "^1.0"
30+
},
31+
"conflict": {
32+
"codeception/codeception": "<5.0"
2833
},
2934
"suggest": {
3035
"aws/aws-sdk-php": "For using AWS Auth"
3136
},
32-
"autoload":{
33-
"classmap": ["src/"]
37+
"minimum-stability": "dev",
38+
"autoload": {
39+
"classmap": [
40+
"src/"
41+
]
3442
},
3543
"config": {
3644
"classmap-authoritative": true

src/Codeception/Module/REST.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class REST extends Module implements DependsOnModule, PartedModule, API, Conflic
101101
/**
102102
* @var array<string, string>
103103
*/
104-
protected $config = [
104+
protected array $config = [
105105
'url' => '',
106106
'aws' => ''
107107
];
@@ -245,7 +245,6 @@ public function deleteHeader(string $name): void
245245
* Checks over the given HTTP header and (optionally)
246246
* its value, asserting that are there
247247
*
248-
* @param string $name
249248
* @param $value
250249
* @part json
251250
* @part xml
@@ -267,7 +266,6 @@ public function seeHttpHeader(string $name, $value = null): void
267266
* Checks over the given HTTP header and (optionally)
268267
* its value, asserting that are not there
269268
*
270-
* @param string $name
271269
* @param $value
272270
* @part json
273271
* @part xml
@@ -295,7 +293,6 @@ public function dontSeeHttpHeader(string $name, $value = null): void
295293
* $I->seeHttpHeaderOnce('Cache-Control');
296294
* ```
297295
*
298-
* @param string $name
299296
* @part json
300297
* @part xml
301298
*/
@@ -313,7 +310,7 @@ public function seeHttpHeaderOnce(string $name): void
313310
* @part json
314311
* @part xml
315312
*/
316-
public function grabHttpHeader(string $name, bool $first = true)
313+
public function grabHttpHeader(string $name, bool $first = true): string|array
317314
{
318315
return $this->getRunningClient()->getInternalResponse()->getHeader($name, $first);
319316
}
@@ -408,7 +405,6 @@ public function amNTLMAuthenticated(string $username, string $password): void
408405
* <?php
409406
* $I->amAWSAuthenticated();
410407
* ```
411-
* @param array $additionalAWSConfig
412408
* @throws ConfigurationException
413409
*/
414410
public function amAWSAuthenticated(array $additionalAWSConfig = []): void
@@ -528,7 +524,7 @@ public function sendGet(string $url, array $params = [])
528524
* $response = $I->sendPut('/message/1', ['subject' => 'Read this!']);
529525
* ```
530526
*
531-
* @param array|string|\JsonSerializable $params
527+
* @param array|string|JsonSerializable $params
532528
* @part json
533529
* @part xml
534530
*/
@@ -545,7 +541,7 @@ public function sendPut(string $url, $params = [], array $files = [])
545541
* $response = $I->sendPatch('/message/1', ['subject' => 'Read this!']);
546542
* ```
547543
*
548-
* @param array|string|\JsonSerializable $params
544+
* @param array|string|JsonSerializable $params
549545
* @part json
550546
* @part xml
551547
*/
@@ -657,7 +653,7 @@ protected function execute($method, $url, $parameters = [], $files = [])
657653
$url = $this->config['url'];
658654
} elseif (!is_string($url)) {
659655
throw new ModuleException(__CLASS__, 'URL must be string');
660-
} elseif (strpos($url, '://') === false && $this->config['url']) {
656+
} elseif (!str_contains($url, '://') && $this->config['url']) {
661657
$url = rtrim($this->config['url'], '/') . '/' . ltrim($url, '/');
662658
}
663659

@@ -676,7 +672,7 @@ protected function execute($method, $url, $parameters = [], $files = [])
676672
if (is_array($parameters) || $isQueryParamsAwareMethod) {
677673
if ($isQueryParamsAwareMethod) {
678674
if (!empty($parameters)) {
679-
if (strpos($url, '?') !== false) {
675+
if (str_contains($url, '?')) {
680676
$url .= '&';
681677
} else {
682678
$url .= '?';
@@ -706,7 +702,7 @@ protected function execute($method, $url, $parameters = [], $files = [])
706702
}
707703

708704
$printedResponse = $this->response;
709-
if ($this->isBinaryData((string) $printedResponse)) {
705+
if ($this->isBinaryData((string)$printedResponse)) {
710706
$printedResponse = $this->binaryToDebugString($printedResponse);
711707
}
712708

@@ -980,7 +976,6 @@ public function seeResponseIsValidOnJsonSchemaString(string $schema): void
980976
* Checks whether last response matches the supplied json schema (https://json-schema.org/)
981977
* Supply schema as relative file path in your project directory or an absolute path
982978
*
983-
* @param string $schemaFilename
984979
* @part json
985980
* @see codecept_absolute_path()
986981
*/
@@ -1000,7 +995,7 @@ public function seeResponseIsValidOnJsonSchema(string $schemaFilename): void
1000995
* @param string $jsonString the json encoded string
1001996
* @param string $errorFormat optional string for custom sprintf format
1002997
*/
1003-
protected function decodeAndValidateJson(string $jsonString, string $errorFormat="Invalid json: %s. System message: %s.")
998+
protected function decodeAndValidateJson(string $jsonString, string $errorFormat = "Invalid json: %s. System message: %s.")
1004999
{
10051000
$json = json_decode($jsonString);
10061001
$errorCode = json_last_error();

src/Codeception/Step/AsJson.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public function run(ModuleContainer $container = null)
1212
$container->getModule('REST')->haveHttpHeader('Content-Type', 'application/json');
1313
$resp = parent::run($container);
1414
$container->getModule('REST')->seeResponseIsJson();
15-
return json_decode($resp, true);
15+
return json_decode($resp, true, 512, JSON_THROW_ON_ERROR);
1616
}
1717

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

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

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

src/Codeception/Util/JsonArray.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ public function toArray(): array
7070
return $this->jsonArray;
7171
}
7272

73-
/**
74-
* @return DOMNodeList|bool
75-
*/
76-
public function filterByXPath(string $xPath)
73+
public function filterByXPath(string $xPath): DOMNodeList|false
7774
{
7875
$path = new DOMXPath($this->toXml());
7976
return $path->query($xPath);
@@ -88,10 +85,7 @@ public function filterByJsonPath(string $jsonPath): array
8885
return (new JSONPath($this->jsonArray))->find($jsonPath)->getData();
8986
}
9087

91-
/**
92-
* @return string|false
93-
*/
94-
public function getXmlString()
88+
public function getXmlString(): string|bool
9589
{
9690
return $this->toXml()->saveXML();
9791
}
@@ -110,7 +104,7 @@ private function arrayToXml(DOMDocument $doc, DOMNode $node, array $array): void
110104
} else {
111105
try {
112106
$subNode = $doc->createElement($key);
113-
} catch (Exception $exception) {
107+
} catch (Exception) {
114108
$key = $this->getValidTagNameForInvalidKey($key);
115109
$subNode = $doc->createElement($key);
116110
}

src/Codeception/Util/JsonType.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,8 @@ public static function cleanCustomFilters(): void
9191
* Checks data against passed JsonType.
9292
* If matching fails function returns a string with a message describing failure.
9393
* On success returns `true`.
94-
*
95-
* @return string|bool
9694
*/
97-
public function matches(array $jsonType)
95+
public function matches(array $jsonType): string|bool
9896
{
9997
if (array_key_exists(0, $this->jsonArray) && is_array($this->jsonArray[0])) {
10098
// a list of items
@@ -116,10 +114,7 @@ public function matches(array $jsonType)
116114
return $this->typeComparison($this->jsonArray, $jsonType);
117115
}
118116

119-
/**
120-
* @return string|bool
121-
*/
122-
protected function typeComparison(array $data, array $jsonType)
117+
protected function typeComparison(array $data, array $jsonType): string|bool
123118
{
124119
foreach ($jsonType as $key => $type) {
125120
if (!array_key_exists($key, $data)) {
@@ -149,8 +144,8 @@ protected function typeComparison(array $data, array $jsonType)
149144
return ':regex($$' . $count++ . ')';
150145
}, $type);
151146

152-
$matchTypes = preg_split("#(?![^]\(]*\))\|#", $filterType);
153-
$matched = false;
147+
$matchTypes = preg_split("#(?![^]\(]*\))\|#", $filterType);
148+
$matched = false;
154149
$currentType = strtolower(gettype($data[$key]));
155150

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

178-
$matched = $matched && $this->matchFilter($filter, (string) $data[$key]);
173+
$matched = $matched && $this->matchFilter($filter, (string)$data[$key]);
179174
}
180175

181176
if ($matched) {
@@ -194,13 +189,13 @@ protected function typeComparison(array $data, array $jsonType)
194189
protected function matchFilter(string $filter, string $value)
195190
{
196191
$filter = trim($filter);
197-
if (strpos($filter, '!') === 0) {
192+
if (str_starts_with($filter, '!')) {
198193
return !$this->matchFilter(substr($filter, 1), $value);
199194
}
200195

201196
// apply custom filters
202197
foreach (static::$customFilters as $customFilter => $callable) {
203-
if (strpos($customFilter, '/') === 0 && preg_match($customFilter, $filter, $matches)) {
198+
if (str_starts_with($customFilter, '/') && preg_match($customFilter, $filter, $matches)) {
204199
array_shift($matches);
205200
return call_user_func_array($callable, array_merge([$value], $matches));
206201
}
@@ -210,7 +205,7 @@ protected function matchFilter(string $filter, string $value)
210205
}
211206
}
212207

213-
if (strpos($filter, '=') === 0) {
208+
if (str_starts_with($filter, '=')) {
214209
return $value === substr($filter, 1);
215210
}
216211

0 commit comments

Comments
 (0)