From bb545d4f7c261472472da8730267d9df162199cb Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Thu, 9 Feb 2023 20:11:19 +0200 Subject: [PATCH] Revert "Add missing types to method signatures" This reverts commit 9baf90249f5d5614f184a5ea018adbd9cfd6c8cc. --- src/Codeception/Constraint/JsonContains.php | 8 +- src/Codeception/Constraint/JsonType.php | 17 ++- src/Codeception/Module/REST.php | 112 ++++++++---------- src/Codeception/Step/AsJson.php | 9 +- .../Util/ArrayContainsComparator.php | 7 +- src/Codeception/Util/JsonType.php | 17 ++- tests/unit/Codeception/Module/RestTest.php | 3 +- 7 files changed, 92 insertions(+), 81 deletions(-) diff --git a/src/Codeception/Constraint/JsonContains.php b/src/Codeception/Constraint/JsonContains.php index 2bb0102..28b42da 100644 --- a/src/Codeception/Constraint/JsonContains.php +++ b/src/Codeception/Constraint/JsonContains.php @@ -16,8 +16,14 @@ class JsonContains extends Constraint { - public function __construct(protected array $expected) + /** + * @var array + */ + protected $expected; + + public function __construct(array $expected) { + $this->expected = $expected; } /** diff --git a/src/Codeception/Constraint/JsonType.php b/src/Codeception/Constraint/JsonType.php index 744d33d..5a5c950 100644 --- a/src/Codeception/Constraint/JsonType.php +++ b/src/Codeception/Constraint/JsonType.php @@ -13,17 +13,26 @@ class JsonType extends Constraint { - public function __construct( - protected array $jsonType, - private bool $match = true) + /** + * @var array + */ + protected $jsonType; + /** + * @var bool + */ + private $match; + + public function __construct(array $jsonType, bool $match = true) { + $this->jsonType = $jsonType; + $this->match = $match; } /** * Evaluates the constraint for parameter $other. Returns true if the * constraint is met, false otherwise. * - * @param array|JsonArray $jsonArray Value or object to evaluate. + * @param mixed $jsonArray Value or object to evaluate. */ protected function matches($jsonArray): bool { diff --git a/src/Codeception/Module/REST.php b/src/Codeception/Module/REST.php index bd60b2e..4062d08 100644 --- a/src/Codeception/Module/REST.php +++ b/src/Codeception/Module/REST.php @@ -22,10 +22,7 @@ use Codeception\Util\JsonArray; use Codeception\Util\JsonType; use Codeception\Util\Soap as XmlUtils; -use Codeception\Util\XmlBuilder; use Codeception\Util\XmlStructure; -use DOMDocument; -use DOMNode; use Exception; use JsonException; use JsonSchema\Constraints\Constraint as JsonConstraint; @@ -124,13 +121,17 @@ class REST extends Module implements DependsOnModule, PartedModule, API, Conflic protected int $DEFAULT_SHORTEN_VALUE = 150; - public HttpKernelBrowser|AbstractBrowser|null $client; + /** + * @var HttpKernelBrowser|AbstractBrowser + */ + public $client; public bool $isFunctional = false; protected ?InnerBrowser $connectionModule = null; - public array|string|ArrayAccess|JsonSerializable $params = []; + /** @var array */ + public $params = []; public ?string $response = null; @@ -149,7 +150,7 @@ protected function resetVariables(): void public function _conflicts(): string { - return API::class; + return \Codeception\Lib\Interfaces\API::class; } public function _depends(): array @@ -201,7 +202,7 @@ protected function getRunningClient(): AbstractBrowser } /** - * Sets na HTTP header to be used for all subsequent requests. Use [`deleteHeader`](#deleteHeader) to unset it. + * Sets a HTTP header to be used for all subsequent requests. Use [`deleteHeader`](#deleteHeader) to unset it. * * ```php * execute('POST', $url, $params, $files); } @@ -480,7 +481,7 @@ public function sendPost( * @part json * @part xml */ - public function sendHead(string $url, array $params = []): ?string + public function sendHead(string $url, array $params = []) { return $this->execute('HEAD', $url, $params); } @@ -510,7 +511,7 @@ public function sendOptions(string $url, array $params = []): void * @part json * @part xml */ - public function sendGet(string $url, array $params = []): ?string + public function sendGet(string $url, array $params = []) { return $this->execute('GET', $url, $params); } @@ -523,14 +524,11 @@ public function sendGet(string $url, array $params = []): ?string * $response = $I->sendPut('/message/1', ['subject' => 'Read this!']); * ``` * + * @param array|string|JsonSerializable $params * @part json * @part xml */ - public function sendPut( - string $url, - array|string|ArrayAccess|JsonSerializable $params = [], - array $files = [] - ): ?string + public function sendPut(string $url, $params = [], array $files = []) { return $this->execute('PUT', $url, $params, $files); } @@ -543,14 +541,11 @@ public function sendPut( * $response = $I->sendPatch('/message/1', ['subject' => 'Read this!']); * ``` * + * @param array|string|JsonSerializable $params * @part json * @part xml */ - public function sendPatch( - string $url, - array|string|ArrayAccess|JsonSerializable $params = [], - array $files = [] - ): ?string + public function sendPatch(string $url, $params = [], array $files = []) { return $this->execute('PATCH', $url, $params, $files); } @@ -566,11 +561,7 @@ public function sendPatch( * @part json * @part xml */ - public function sendDelete( - string $url, - array|string|ArrayAccess|JsonSerializable $params = [], - array $files = [] - ): ?string + public function sendDelete(string $url, array $params = [], array $files = []) { return $this->execute('DELETE', $url, $params, $files); } @@ -578,14 +569,11 @@ public function sendDelete( /** * Sends a HTTP request. * + * @param array|string|JsonSerializable $params * @part json * @part xml */ - public function send( - string $method, - string $url, - array|string|ArrayAccess|JsonSerializable $params = [], - array $files = []): ?string + public function send(string $method, string $url, $params = [], array $files = []) { return $this->execute(strtoupper($method), $url, $params, $files); } @@ -652,13 +640,13 @@ public function sendUnlink(string $url, array $linkEntries): void } /** + * @param $method + * @param $url + * @param array|string|object $parameters + * @param array $files * @throws ModuleException|ExternalUrlException|JsonException */ - protected function execute( - string $method, - string $url, - array|string|ArrayAccess|JsonSerializable $parameters = [], - array $files = []): ?string + protected function execute($method, $url, $parameters = [], $files = []) { // allow full url to be requested if (!$url) { @@ -704,9 +692,6 @@ protected function execute( $this->response = $this->connectionModule->_request($method, $url, $parameters, $files); } else { - /** - * @var string $parameters - */ $requestData = $parameters; if ($this->isBinaryData($requestData)) { $requestData = $this->binaryToDebugString($requestData); @@ -754,10 +739,7 @@ protected function binaryToDebugString(string $data): string return '[binary-data length:' . strlen($data) . ' md5:' . md5($data) . ']'; } - protected function encodeApplicationJson( - string $method, - array|string|ArrayAccess|JsonSerializable $parameters, - ): array|string + protected function encodeApplicationJson(string $method, $parameters) { if ( array_key_exists('Content-Type', $this->connectionModule->headers) @@ -919,15 +901,15 @@ public function dontSeeResponseContains(string $text): void * * ``` php * seeResponseContainsJson(['name' => 'john']); + * // response: {name: john, email: john@gmail.com} + * $I->seeResponseContainsJson(array('name' => 'john')); * - * // response {"user": "john", "profile": {"email": "john@gmail.com"}} - * $I->seeResponseContainsJson(['email' => 'john@gmail.com']); + * // response {user: john, profile: { email: john@gmail.com }} + * $I->seeResponseContainsJson(array('email' => 'john@gmail.com')); * * ``` * - * This method recursively checks if one array can be found inside another. + * This method recursively checks if one array can be found inside of another. * * @part json */ @@ -1331,13 +1313,13 @@ public function dontSeeResponseContainsJson(array $json = []): void * * ```php * "davert@codeception.com"} + * // {'user_id': 1, 'email' => 'davert@codeception.com'} * $I->seeResponseMatchesJsonType([ * 'user_id' => 'string:>0:<1000', // multiple filters can be used * 'email' => 'string:regex(~\@~)' // we just check that @ char is included * ]); * - * // {"user_id"'": "1"} + * // {'user_id': '1'} * $I->seeResponseMatchesJsonType([ * 'user_id' => 'string:>0', // works with strings as well * ]); @@ -1531,9 +1513,10 @@ public function dontSeeXmlResponseMatchesXpath(string $xPath): void * Finds and returns text contents of element. * Element is matched by either CSS or XPath * + * @param mixed $cssOrXPath * @part xml */ - public function grabTextContentFromXmlElement(string $cssOrXPath): string + public function grabTextContentFromXmlElement($cssOrXPath): string { $el = (new XmlStructure($this->connectionModule->_getResponseContent()))->matchElement($cssOrXPath); return $el->textContent; @@ -1559,9 +1542,12 @@ public function grabAttributeFromXmlElement(string $cssOrXPath, string $attribut * Checks XML response equals provided XML. * Comparison is done by canonicalizing both xml`s. * + * Parameters can be passed either as DOMDocument, DOMNode, XML string, or array (if no attributes). + * + * @param mixed $xml * @part xml */ - public function seeXmlResponseEquals(DOMDocument|string $xml): void + public function seeXmlResponseEquals($xml): void { Assert::assertXmlStringEqualsXmlString($this->connectionModule->_getResponseContent(), $xml); } @@ -1571,10 +1557,12 @@ public function seeXmlResponseEquals(DOMDocument|string $xml): void * Checks XML response does not equal to provided XML. * Comparison is done by canonicalizing both xml`s. * + * Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes). + * * @param mixed $xml * @part xml */ - public function dontSeeXmlResponseEquals(DOMDocument|string $xml): void + public function dontSeeXmlResponseEquals($xml): void { Assert::assertXmlStringNotEqualsXmlString( $this->connectionModule->_getResponseContent(), @@ -1594,9 +1582,10 @@ public function dontSeeXmlResponseEquals(DOMDocument|string $xml): void * $I->seeXmlResponseIncludes("1"); * ``` * + * @param mixed $xml * @part xml */ - public function seeXmlResponseIncludes(DOMNode|XmlBuilder|array|string $xml): void + public function seeXmlResponseIncludes($xml): void { $this->assertStringContainsString( XmlUtils::toXml($xml)->C14N(), @@ -1610,9 +1599,10 @@ public function seeXmlResponseIncludes(DOMNode|XmlBuilder|array|string $xml): vo * Comparison is done by canonicalizing both xml`s. * Parameter can be passed either as XmlBuilder, DOMDocument, DOMNode, XML string, or array (if no attributes). * + * @param mixed $xml * @part xml */ - public function dontSeeXmlResponseIncludes(DOMNode|XmlBuilder|array|string $xml): void + public function dontSeeXmlResponseIncludes($xml): void { $this->assertStringNotContainsString( XmlUtils::toXml($xml)->C14N(), diff --git a/src/Codeception/Step/AsJson.php b/src/Codeception/Step/AsJson.php index 4a935c7..78acdf2 100644 --- a/src/Codeception/Step/AsJson.php +++ b/src/Codeception/Step/AsJson.php @@ -3,20 +3,15 @@ namespace Codeception\Step; use Codeception\Lib\ModuleContainer; -use Codeception\Module\REST; use Codeception\Util\Template; class AsJson extends Action implements GeneratedStep { public function run(ModuleContainer $container = null) { - /** - * @var REST $restModule - */ - $restModule = $container->getModule('REST'); - $restModule->haveHttpHeader('Content-Type', 'application/json'); + $container->getModule('REST')->haveHttpHeader('Content-Type', 'application/json'); $resp = parent::run($container); - $restModule->seeResponseIsJson(); + $container->getModule('REST')->seeResponseIsJson(); return json_decode($resp, true, 512, JSON_THROW_ON_ERROR); } diff --git a/src/Codeception/Util/ArrayContainsComparator.php b/src/Codeception/Util/ArrayContainsComparator.php index 1646489..4d1832c 100644 --- a/src/Codeception/Util/ArrayContainsComparator.php +++ b/src/Codeception/Util/ArrayContainsComparator.php @@ -32,9 +32,11 @@ public function containsArray(array $needle): bool } /** - * @author nleippe@integr8ted.com + * @return array|bool * @author tiger.seo@gmail.com * @link https://www.php.net/manual/en/function.array-intersect-assoc.php#39822 + * + * @author nleippe@integr8ted.com */ private function arrayIntersectRecursive(mixed $arr1, mixed $arr2): bool|array|null { @@ -86,6 +88,9 @@ private function sequentialArrayIntersect(array $arr1, array $arr2): array return $ret; } + /** + * @return array|bool|null + */ private function associativeArrayIntersect(array $arr1, array $arr2): bool|array|null { $commonKeys = array_intersect(array_keys($arr1), array_keys($arr2)); diff --git a/src/Codeception/Util/JsonType.php b/src/Codeception/Util/JsonType.php index c2d50f0..09e4d59 100644 --- a/src/Codeception/Util/JsonType.php +++ b/src/Codeception/Util/JsonType.php @@ -30,7 +30,10 @@ */ class JsonType { - protected array $jsonArray; + /** + * @var array|JsonArray + */ + protected $jsonArray; protected static array $customFilters = []; @@ -38,8 +41,10 @@ class JsonType * Creates instance of JsonType * Pass an array or `\Codeception\Util\JsonArray` with data. * If non-associative array is passed - the very first element of it will be used for matching. + * + * @param $jsonArray array|JsonArray */ - public function __construct(array|JsonArray $jsonArray) + public function __construct($jsonArray) { if ($jsonArray instanceof JsonArray) { $jsonArray = $jsonArray->toArray(); @@ -116,8 +121,8 @@ protected function typeComparison(array $data, array $jsonType): string|bool return sprintf("Key `%s` doesn't exist in ", $key) . json_encode($data, JSON_THROW_ON_ERROR); } - if (is_array($type)) { - $message = $this->typeComparison($data[$key], $type); + if (is_array($jsonType[$key])) { + $message = $this->typeComparison($data[$key], $jsonType[$key]); if (is_string($message)) { return $message; @@ -129,11 +134,11 @@ protected function typeComparison(array $data, array $jsonType): string|bool $regexMatcher = '/:regex\((((\()|(\{)|(\[)|(<)|(.)).*?(?(3)\)|(?(4)\}|(?(5)\]|(?(6)>|\7)))))\)/'; $regexes = []; - // Match the string ':regex(' and any characters until an ending regex delimiter followed by character ')' + // Match the string ':regex(' and any characters until a ending regex delimiter followed by character ')' // Place the 'any character' + delimiter matches in to an array. preg_match_all($regexMatcher, $type, $regexes); - // Do the same match as above, but replace the 'any character' + delimiter with a place holder ($${count}). + // Do the same match as above, but replace the the 'any character' + delimiter with a place holder ($${count}). $filterType = preg_replace_callback($regexMatcher, function (): string { static $count = 0; return ':regex($$' . $count++ . ')'; diff --git a/tests/unit/Codeception/Module/RestTest.php b/tests/unit/Codeception/Module/RestTest.php index 388f5d2..d8de3e9 100644 --- a/tests/unit/Codeception/Module/RestTest.php +++ b/tests/unit/Codeception/Module/RestTest.php @@ -317,11 +317,12 @@ public function testThrowsExceptionIfParametersIsString(string $method) } /** + * @param array|object $parameters * @dataProvider invalidParameterTypes */ public function testThrowsExceptionIfParametersIsOfUnexpectedType($parameters) { - $this->expectException(TypeError::class); + $this->expectExceptionMessage('POST parameters must be array, string or object implementing JsonSerializable interface'); $this->module->sendPOST('/', $parameters); }