From 987c21bbefd6baecfbef424b167e5b35f3525b90 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J <64917965+TavoNiievez@users.noreply.github.com> Date: Sun, 6 Jun 2021 19:11:38 -0500 Subject: [PATCH] Code standards updated to PHP 7.3 (#39) * Code standards updated to PHP 7.3 * Support for Codeception 5 * AssertSame instead of AssertEquals * temporarily use dev branch --- .github/workflows/main.yml | 2 +- composer.json | 85 ++-- readme.md | 4 + .../Exception/ExternalUrlException.php | 6 +- src/Codeception/Lib/Framework.php | 7 +- src/Codeception/Lib/InnerBrowser.php | 366 ++++++++---------- src/Codeception/Util/HttpCode.php | 7 +- .../Constraints/CrawlerConstraintTest.php | 5 +- .../Constraints/CrawlerNotConstraintTest.php | 4 +- .../Codeception/Module/FrameworksTest.php | 9 +- tests/unit/Codeception/Module/TestsForWeb.php | 177 ++++----- tests/unit/Codeception/Util/HttpCodeTest.php | 15 +- 12 files changed, 342 insertions(+), 345 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5484dae..4691414 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: - php: [5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0] + php: [7.3, 7.4, 8.0] steps: - name: Checkout code diff --git a/composer.json b/composer.json index 32f3fd0..742578d 100644 --- a/composer.json +++ b/composer.json @@ -1,44 +1,45 @@ { - "name":"codeception/lib-innerbrowser", - "description":"Parent library for all Codeception framework modules and PhpBrowser", - "keywords":["codeception"], - "homepage":"https://codeception.com/", - "type":"library", - "license":"MIT", - "authors":[ - { - "name":"Michael Bodnarchuk", - "email":"davert@mail.ua", - "homepage":"http://codegyre.com" - }, - { - "name":"Gintautas Miselis" - } - ], - "minimum-stability": "RC", - - "require": { - "php": ">=5.6.0 <9.0", - "codeception/codeception": "*@dev", - "symfony/browser-kit": ">=2.7 <6.0", - "symfony/dom-crawler": ">=2.7 <6.0", - "ext-mbstring": "*", - "ext-dom": "*", - "ext-json": "*" - }, - "require-dev": { - "codeception/util-universalframework": "dev-master" - }, - "conflict": { - "codeception/codeception": "<4.0" - }, - "autoload":{ - "classmap": ["src/"] - }, - "config": { - "classmap-authoritative": true - }, - "scripts": { - "test": "codecept run --coverage-xml" - } + "name": "codeception/lib-innerbrowser", + "description": "Parent library for all Codeception framework modules and PhpBrowser", + "keywords": [ "codeception" ], + "homepage": "https://codeception.com/", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Michael Bodnarchuk", + "email": "davert@mail.ua", + "homepage": "https://codegyre.com" + }, + { + "name": "Gintautas Miselis" + } + ], + "minimum-stability": "RC", + "require": { + "php": "^7.3 || ^8.0", + "ext-dom": "*", + "ext-json": "*", + "ext-mbstring": "*", + "codeception/codeception": "^5.0 || *@dev", + "symfony/browser-kit": ">=4.4 <6.0", + "symfony/dom-crawler": ">=4.4 <6.0" + }, + "require-dev": { + "codeception/util-universalframework": "dev-master" + }, + "conflict": { + "codeception/codeception": "<5.0" + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "config": { + "classmap-authoritative": true + }, + "scripts": { + "test": "codecept run --coverage-xml" + } } diff --git a/readme.md b/readme.md index cb38f1e..f3a3b59 100644 --- a/readme.md +++ b/readme.md @@ -7,6 +7,10 @@ Parent library for all Codeception framework modules and PhpBrowser. [![Total Downloads](https://poser.pugx.org/codeception/lib-innerbrowser/downloads)](https://packagist.org/packages/codeception/lib-innerbrowser) [![License](https://poser.pugx.org/codeception/lib-innerbrowser/license)](/LICENSE) +## Requirements + +* `PHP 7.3` or higher. + ## Installation ``` diff --git a/src/Codeception/Exception/ExternalUrlException.php b/src/Codeception/Exception/ExternalUrlException.php index bcacbe3..ea32e26 100644 --- a/src/Codeception/Exception/ExternalUrlException.php +++ b/src/Codeception/Exception/ExternalUrlException.php @@ -1,7 +1,11 @@ getHeader('content-type') : ''; - list($responseMimeType) = explode(';', $responseContentType); + [$responseMimeType] = explode(';', $responseContentType); - $extension = isset($extensions[$responseMimeType]) ? $extensions[$responseMimeType] : 'html'; + $extension = $extensions[$responseMimeType] ?? 'html'; $filename = mb_strcut($filename, 0, 244, 'utf-8') . '.fail.' . $extension; $this->_savePageSource($report = codecept_output_dir() . $filename); @@ -120,12 +129,15 @@ public function _after(TestInterface $test) $this->headers = []; } - public function _conflicts() + /** + * @return class-string + */ + public function _conflicts(): string { return \Codeception\Lib\Interfaces\Web::class; } - public function _findElements($locator) + public function _findElements(array $locator) { return $this->match($locator); } @@ -147,22 +159,22 @@ public function _findElements($locator) * Does not load the response into the module so you can't interact with response page (click, fill forms). * To load arbitrary page for interaction, use `_loadPage` method. * - * @api * @param string $method * @param string $uri - * @param string $content + * @param string|null $content * @return string * @throws ExternalUrlException + * @api * @see `_loadPage` */ public function _request( - $method, - $uri, + string $method, + string $uri, array $parameters = [], array $files = [], array $server = [], - $content = null - ) { + string $content = null + ): string { $this->clientRequest($method, $uri, $parameters, $files, $server, $content); return $this->_getResponseContent(); } @@ -184,20 +196,27 @@ public function _request( * @return string * @throws ModuleException */ - public function _getResponseContent() + public function _getResponseContent(): string { - return (string)$this->getRunningClient()->getInternalResponse()->getContent(); + return $this->getRunningClient()->getInternalResponse()->getContent(); } /** * @param string $method * @param string $uri - * @param string $content + * @param string|null $content * @param bool $changeHistory * @return SymfonyCrawler */ - protected function clientRequest($method, $uri, array $parameters = [], array $files = [], array $server = [], $content = null, $changeHistory = true) - { + protected function clientRequest( + string $method, + string $uri, + array $parameters = [], + array $files = [], + array $server = [], + string $content = null, + bool $changeHistory = true + ): SymfonyCrawler { $this->debugSection("Request Headers", $this->headers); foreach ($this->headers as $header => $val) { // moved from REST module @@ -207,7 +226,7 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f } $header = str_replace('-', '_', strtoupper($header)); - $server["HTTP_$header"] = $val; + $server["HTTP_{$header}"] = $val; // Issue #827 - symfony foundation requires 'CONTENT_TYPE' without HTTP_ if ($this instanceof Framework && $header === 'CONTENT_TYPE') { @@ -250,10 +269,7 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f return $this->redirectIfNecessary($result, $maxRedirects, 0); } - /** - * @return bool - */ - protected function isInternalDomain($domain) + protected function isInternalDomain(string $domain): bool { if ($this->internalDomains === null) { $this->internalDomains = $this->getInternalDomains(); @@ -279,18 +295,18 @@ protected function isInternalDomain($domain) * } * ``` * - * @api * @param string $method * @param string $uri - * @param string $content + * @param string|null $content + *@api */ public function _loadPage( - $method, - $uri, + string $method, + string $uri, array $parameters = [], array $files = [], array $server = [], - $content = null + string $content = null ) { $this->crawler = $this->clientRequest($method, $uri, $parameters, $files, $server, $content); $this->baseUrl = $this->retrieveBaseUrl(); @@ -301,7 +317,7 @@ public function _loadPage( * @return SymfonyCrawler * @throws ModuleException */ - private function getCrawler() + private function getCrawler(): SymfonyCrawler { if (!$this->crawler) { throw new ModuleException($this, 'Crawler is null. Perhaps you forgot to call "amOnPage"?'); @@ -309,7 +325,7 @@ private function getCrawler() return $this->crawler; } - private function getRunningClient() + private function getRunningClient(): AbstractBrowser { try { if ($this->client->getInternalRequest() === null) { @@ -328,7 +344,7 @@ private function getRunningClient() return $this->client; } - public function _savePageSource($filename) + public function _savePageSource(string $filename): void { file_put_contents($filename, $this->_getResponseContent()); } @@ -339,7 +355,7 @@ public function _savePageSource($filename) * @param string $username * @param string $password */ - public function amHttpAuthenticated($username, $password) + public function amHttpAuthenticated(string $username, string $password): void { $this->client->setServerParameter('PHP_AUTH_USER', $username); $this->client->setServerParameter('PHP_AUTH_PW', $password); @@ -370,7 +386,7 @@ public function amHttpAuthenticated($username, $password) * @param string $value the value to set it to for subsequent * requests */ - public function haveHttpHeader($name, $value) + public function haveHttpHeader(string $name, string $value): void { $name = implode('-', array_map('ucfirst', explode('-', strtolower(str_replace('_', '-', $name))))); $this->headers[$name] = $value; @@ -392,18 +408,18 @@ public function haveHttpHeader($name, $value) * * @param string $name the name of the header to delete. */ - public function deleteHeader($name) + public function deleteHeader(string $name): void { $name = implode('-', array_map('ucfirst', explode('-', strtolower(str_replace('_', '-', $name))))); unset($this->headers[$name]); } - public function amOnPage($page) + public function amOnPage(string $page): void { $this->_loadPage('GET', $page); } - public function click($link, $context = null) + public function click($link, $context = null): void { if ($context) { $this->crawler = $this->match($context); @@ -441,7 +457,7 @@ public function click($link, $context = null) * @param $link * @return bool */ - protected function clickByLocator($link) + protected function clickByLocator($link): bool { $nodes = $this->match($link); if ($nodes->count() === 0) { @@ -468,7 +484,7 @@ protected function clickByLocator($link) * * @return bool clicked something */ - private function clickButton(DOMNode $node) + private function clickButton(DOMNode $node): bool { /** * First we check if the button is associated to a form. @@ -518,15 +534,12 @@ private function openHrefFromDomNode(DOMNode $node) $this->amOnPage(preg_replace('/#.*/', '', $link->getUri())); } - private function getBaseUrl() + private function getBaseUrl(): ?string { return $this->baseUrl; } - /** - * @return string - */ - private function retrieveBaseUrl() + private function retrieveBaseUrl(): string { $baseUrl = ''; @@ -540,7 +553,7 @@ private function retrieveBaseUrl() return $this->getAbsoluteUrlFor($baseUrl); } - public function see($text, $selector = null) + public function see(string $text, $selector = null): void { if (!$selector) { $this->assertPageContains($text); @@ -551,7 +564,7 @@ public function see($text, $selector = null) $this->assertDomContains($nodes, $this->stringifySelector($selector), $text); } - public function dontSee($text, $selector = null) + public function dontSee(string $text, $selector = null): void { if (!$selector) { $this->assertPageNotContains($text); @@ -562,17 +575,17 @@ public function dontSee($text, $selector = null) $this->assertDomNotContains($nodes, $this->stringifySelector($selector), $text); } - public function seeInSource($raw) + public function seeInSource(string $raw): void { $this->assertPageSourceContains($raw); } - public function dontSeeInSource($raw) + public function dontSeeInSource(string $raw): void { $this->assertPageSourceNotContains($raw); } - public function seeLink($text, $url = null) + public function seeLink(string $text, string $url = null): void { $crawler = $this->getCrawler()->selectLink($text); if ($crawler->count() === 0) { @@ -587,7 +600,7 @@ public function seeLink($text, $url = null) $this->assertTrue(true); } - public function dontSeeLink($text, $url = '') + public function dontSeeLink(string $text, string $url = null): void { $crawler = $this->getCrawler()->selectLink($text); if (!$url && $crawler->count() > 0) { @@ -595,7 +608,7 @@ public function dontSeeLink($text, $url = '') } $crawler = $crawler->filterXPath( sprintf('.//a[substring(@href, string-length(@href) - string-length(%1$s) + 1)=%1$s]', - SymfonyCrawler::xpathLiteral($url)) + SymfonyCrawler::xpathLiteral((string) $url)) ); if ($crawler->count() > 0) { $this->fail("Link containing text '$text' and URL '$url' was found in page " . $this->_getCurrentUri()); @@ -606,42 +619,42 @@ public function dontSeeLink($text, $url = '') * @return string * @throws ModuleException */ - public function _getCurrentUri() + public function _getCurrentUri(): string { return Uri::retrieveUri($this->getRunningClient()->getHistory()->current()->getUri()); } - public function seeInCurrentUrl($uri) + public function seeInCurrentUrl(string $uri): void { $this->assertStringContainsString($uri, $this->_getCurrentUri()); } - public function dontSeeInCurrentUrl($uri) + public function dontSeeInCurrentUrl(string $uri): void { $this->assertStringNotContainsString($uri, $this->_getCurrentUri()); } - public function seeCurrentUrlEquals($uri) + public function seeCurrentUrlEquals(string $uri): void { - $this->assertEquals(rtrim($uri, '/'), rtrim($this->_getCurrentUri(), '/')); + $this->assertSame(rtrim($uri, '/'), rtrim($this->_getCurrentUri(), '/')); } - public function dontSeeCurrentUrlEquals($uri) + public function dontSeeCurrentUrlEquals(string $uri): void { - $this->assertNotEquals(rtrim($uri, '/'), rtrim($this->_getCurrentUri(), '/')); + $this->assertNotSame(rtrim($uri, '/'), rtrim($this->_getCurrentUri(), '/')); } - public function seeCurrentUrlMatches($uri) + public function seeCurrentUrlMatches(string $uri): void { $this->assertRegExp($uri, $this->_getCurrentUri()); } - public function dontSeeCurrentUrlMatches($uri) + public function dontSeeCurrentUrlMatches(string $uri): void { $this->assertNotRegExp($uri, $this->_getCurrentUri()); } - public function grabFromCurrentUrl($uri = null) + public function grabFromCurrentUrl(string $uri = null) { if (!$uri) { return $this->_getCurrentUri(); @@ -657,36 +670,36 @@ public function grabFromCurrentUrl($uri = null) return $matches[1]; } - public function seeCheckboxIsChecked($checkbox) + public function seeCheckboxIsChecked($checkbox): void { $checkboxes = $this->getFieldsByLabelOrCss($checkbox); $this->assertDomContains($checkboxes->filter('input[checked=checked]'), 'checkbox'); } - public function dontSeeCheckboxIsChecked($checkbox) + public function dontSeeCheckboxIsChecked($checkbox): void { $checkboxes = $this->getFieldsByLabelOrCss($checkbox); - $this->assertEquals(0, $checkboxes->filter('input[checked=checked]')->count()); + $this->assertSame(0, $checkboxes->filter('input[checked=checked]')->count()); } - public function seeInField($field, $value) + public function seeInField($field, $value): void { $nodes = $this->getFieldsByLabelOrCss($field); $this->assert($this->proceedSeeInField($nodes, $value)); } - public function dontSeeInField($field, $value) + public function dontSeeInField($field, $value): void { $nodes = $this->getFieldsByLabelOrCss($field); $this->assertNot($this->proceedSeeInField($nodes, $value)); } - public function seeInFormFields($formSelector, array $params) + public function seeInFormFields($formSelector, array $params): void { $this->proceedSeeInFormFields($formSelector, $params, false); } - public function dontSeeInFormFields($formSelector, array $params) + public function dontSeeInFormFields($formSelector, array $params): void { $this->proceedSeeInFormFields($formSelector, $params, true); } @@ -703,7 +716,7 @@ protected function proceedSeeInFormFields($formSelector, array $params, $assertN $this->pushFormField($fields, $form, $name, $values); } - foreach ($fields as list($field, $values)) { + foreach ($fields as [$field, $values]) { if (!is_array($values)) { $values = [$values]; } @@ -729,7 +742,7 @@ protected function proceedSeeInFormFields($formSelector, array $params, $assertN * @param mixed $values * @return void */ - protected function pushFormField(&$fields, $form, $name, $values) + protected function pushFormField(array &$fields, SymfonyCrawler $form, string $name, $values): void { $field = $form->filterXPath(sprintf('.//*[@name=%s]', SymfonyCrawler::xpathLiteral($name))); @@ -747,7 +760,7 @@ protected function pushFormField(&$fields, $form, $name, $values) } } - protected function proceedSeeInField(Crawler $fields, $value) + protected function proceedSeeInField(Crawler $fields, $value): array { $testValues = $this->getValueAndTextFromField($fields); if (!is_array($testValues)) { @@ -800,7 +813,7 @@ protected function getValueAndTextFromField(Crawler $nodes) return $values; } - $this->fail("Element $nodes is not a form field or does not contain a form field"); + $this->fail("Element {$nodes} is not a form field or does not contain a form field"); } /** @@ -809,7 +822,7 @@ protected function getValueAndTextFromField(Crawler $nodes) * @param SymfonyCrawler $input * @return array|string */ - protected function getInputValue($input) + protected function getInputValue(SymfonyCrawler $input) { $inputType = $input->attr('type'); if ($inputType === 'checkbox' || $inputType === 'radio') { @@ -832,7 +845,7 @@ protected function getInputValue($input) * @param string $name the field name * @return string the name after stripping trailing square brackets */ - protected function getSubmissionFormFieldName($name) + protected function getSubmissionFormFieldName(string $name): string { if (substr($name, -2) === '[]') { return substr($name, 0, -2); @@ -854,7 +867,7 @@ protected function getSubmissionFormFieldName($name) * @param array $params the parameters to be submitted * @return array the $params array after replacing bool values */ - protected function setCheckboxBoolValues(Crawler $form, array $params) + protected function setCheckboxBoolValues(Crawler $form, array $params): array { $checkboxes = $form->filter('input[type=checkbox]'); $chFoundByName = []; @@ -896,9 +909,9 @@ protected function setCheckboxBoolValues(Crawler $form, array $params) * @param SymfonyCrawler $frmCrawl the form to submit * @param array $params additional parameter values to set on the * form - * @param string $button the name of a submit button in the form + * @param string|null $button the name of a submit button in the form */ - protected function proceedSubmitForm(Crawler $frmCrawl, array $params, $button = null) + protected function proceedSubmitForm(Crawler $frmCrawl, array $params, string $button = null): void { $url = null; $form = $this->getFormFor($frmCrawl); @@ -945,7 +958,7 @@ protected function proceedSubmitForm(Crawler $frmCrawl, array $params, $button = $this->forms = []; } - public function submitForm($selector, array $params, $button = null) + public function submitForm($selector, array $params, $button = null): void { $form = $this->match($selector)->first(); if (count($form) === 0) { @@ -963,7 +976,7 @@ public function submitForm($selector, array $params, $button = null) * @throws \Codeception\Exception\TestRuntimeException if either the current * URL or the passed URI can't be parsed */ - protected function getAbsoluteUrlFor($uri) + protected function getAbsoluteUrlFor(string $uri): string { $currentUrl = $this->getRunningClient()->getHistory()->current()->getUri(); if (empty($uri) || strpos($uri, '#') === 0) { @@ -979,7 +992,7 @@ protected function getAbsoluteUrlFor($uri) * @throws \Codeception\Exception\TestRuntimeException if either the current * URL or the URI of the form's action can't be parsed */ - protected function getFormUrl(Crawler $form) + protected function getFormUrl(Crawler $form): string { $action = $form->form()->getUri(); return $this->getAbsoluteUrlFor($action); @@ -1005,7 +1018,7 @@ protected function getFormUrl(Crawler $form) * @param SymfonyCrawler $form the form * @return SymfonyForm */ - private function getFormFromCrawler(Crawler $form) + private function getFormFromCrawler(Crawler $form): SymfonyForm { $fakeDom = new DOMDocument(); $fakeDom->appendChild($fakeDom->importNode($form->getNode(0), true)); @@ -1039,7 +1052,7 @@ private function getFormFromCrawler(Crawler $form) * * @return SymfonyForm */ - protected function getFormFor(Crawler $node) + protected function getFormFor(Crawler $node): SymfonyForm { if (strcasecmp($node->first()->getNode(0)->tagName, 'form') === 0) { $form = $node->first(); @@ -1066,7 +1079,7 @@ protected function getFormFor(Crawler $node) * @param SymfonyForm $form the form * @return array an array of name => value pairs */ - protected function getFormValuesFor(SymfonyForm $form) + protected function getFormValuesFor(SymfonyForm $form): array { $formNodeCrawler = new Crawler($form->getFormNode()); $values = []; @@ -1103,8 +1116,9 @@ protected function getFormValuesFor(SymfonyForm $form) return $values; } - public function fillField($field, $value) + public function fillField($field, $value): void { + $value = (string) $value; $input = $this->getFieldByLabelOrCss($field); $form = $this->getFormFor($input); $name = $input->attr('name'); @@ -1121,11 +1135,7 @@ public function fillField($field, $value) } } - /** - * @param $field - * @return SymfonyCrawler - */ - protected function getFieldsByLabelOrCss($field) + protected function getFieldsByLabelOrCss($field): SymfonyCrawler { if (is_array($field)) { $input = $this->strictMatch($field); @@ -1163,16 +1173,13 @@ protected function getFieldsByLabelOrCss($field) return $input; } - /** - * @return SymfonyCrawler - */ - protected function getFieldByLabelOrCss($field) + protected function getFieldByLabelOrCss($field): SymfonyCrawler { $input = $this->getFieldsByLabelOrCss($field); return $input->first(); } - public function selectOption($select, $option) + public function selectOption($select, $option): void { $field = $this->getFieldByLabelOrCss($select); $form = $this->getFormFor($field); @@ -1242,12 +1249,12 @@ protected function matchOption(Crawler $field, $option) return $option; } - public function checkOption($option) + public function checkOption($option): void { $this->proceedCheckOption($option)->tick(); } - public function uncheckOption($option) + public function uncheckOption($option): void { $this->proceedCheckOption($option)->untick(); } @@ -1256,7 +1263,7 @@ public function uncheckOption($option) * @param $option * @return ChoiceFormField */ - protected function proceedCheckOption($option) + protected function proceedCheckOption($option): ChoiceFormField { $form = $this->getFormFor($field = $this->getFieldByLabelOrCss($option)); $name = $field->attr('name'); @@ -1273,7 +1280,7 @@ protected function proceedCheckOption($option) return $formField; } - public function attachFile($field, $filename) + public function attachFile($field, string $filename): void { $form = $this->getFormFor($field = $this->getFieldByLabelOrCss($field)); $filePath = codecept_data_dir() . $filename; @@ -1297,10 +1304,10 @@ public function attachFile($field, $filename) * Sends an ajax GET request with the passed parameters. * See `sendAjaxPostRequest()` * - * @param $uri - * @param $params + * @param string $uri + * @param array $params */ - public function sendAjaxGetRequest($uri, $params = []) + public function sendAjaxGetRequest(string $uri, array $params = []): void { $this->sendAjaxRequest('GET', $uri, $params); } @@ -1328,7 +1335,7 @@ public function sendAjaxGetRequest($uri, $params = []) * @param string $uri * @param array $params */ - public function sendAjaxPostRequest($uri, $params = []) + public function sendAjaxPostRequest(string $uri, array $params = []): void { $this->sendAjaxRequest('POST', $uri, $params); } @@ -1342,11 +1349,11 @@ public function sendAjaxPostRequest($uri, $params = []) * $I->sendAjaxRequest('PUT', '/posts/7', ['title' => 'new title']); * ``` * - * @param $method - * @param $uri + * @param string $method + * @param string $uri * @param array $params */ - public function sendAjaxRequest($method, $uri, $params = []) + public function sendAjaxRequest(string $method, string $uri, array $params = []): void { $this->clientRequest($method, $uri, $params, [], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'], null, false); } @@ -1362,7 +1369,7 @@ protected function debugResponse($url) $this->debugSection('Response Headers', $this->getRunningClient()->getInternalResponse()->getHeaders()); } - public function makeHtmlSnapshot($name = null) + public function makeHtmlSnapshot(string $name = null): void { if (empty($name)) { $name = uniqid(date("Y-m-d_H-i-s_"), true); @@ -1399,7 +1406,7 @@ protected function getResponseStatusCode() * @param $selector * @return SymfonyCrawler */ - protected function match($selector) + protected function match($selector): SymfonyCrawler { if (is_array($selector)) { return $this->strictMatch($selector); @@ -1418,13 +1425,13 @@ protected function match($selector) * @throws TestRuntimeException * @return SymfonyCrawler */ - protected function strictMatch(array $by) + protected function strictMatch(array $by): SymfonyCrawler { $type = key($by); $locator = $by[$type]; switch ($type) { case 'id': - return $this->filterByCSS("#$locator"); + return $this->filterByCSS("#{$locator}"); case 'name': return $this->filterByXPath(sprintf('.//*[@name=%s]', SymfonyCrawler::xpathLiteral($locator))); case 'css': @@ -1434,10 +1441,10 @@ protected function strictMatch(array $by) case 'link': return $this->filterByXPath(sprintf('.//a[.=%s or contains(./@title, %s)]', SymfonyCrawler::xpathLiteral($locator), SymfonyCrawler::xpathLiteral($locator))); case 'class': - return $this->filterByCSS(".$locator"); + return $this->filterByCSS(".{$locator}"); default: throw new TestRuntimeException( - "Locator type '$by' is not defined. Use either: xpath, css, id, link, class, name" + "Locator type '{$by}' is not defined. Use either: xpath, css, id, link, class, name" ); } } @@ -1466,7 +1473,7 @@ public function grabTextFrom($cssOrXPathOrRegex) throw new ElementNotFound($cssOrXPathOrRegex, 'Element that matches CSS or XPath or Regex'); } - public function grabAttributeFrom($cssOrXpath, $attribute) + public function grabAttributeFrom($cssOrXpath, string $attribute) { $nodes = $this->match($cssOrXpath); if ($nodes->count() === 0) { @@ -1475,7 +1482,7 @@ public function grabAttributeFrom($cssOrXpath, $attribute) return $nodes->first()->attr($attribute); } - public function grabMultiple($cssOrXpath, $attribute = null) + public function grabMultiple($cssOrXpath, string $attribute = null): array { $result = []; $nodes = $this->match($cssOrXpath); @@ -1523,22 +1530,22 @@ public function grabValueFrom($field) return $values; } - $this->fail("Element $nodes is not a form field or does not contain a form field"); + $this->fail("Element {$nodes} is not a form field or does not contain a form field"); } - public function setCookie($name, $val, array $params = []) + public function setCookie(string $name, ?string $val, array $params = []) { $cookies = $this->client->getCookieJar(); $params = array_merge($this->defaultCookieParameters, $params); - $expires = isset($params['expiry']) ? $params['expiry'] : null; // WebDriver compatibility + $expires = $params['expiry'] ?? null; // WebDriver compatibility $expires = isset($params['expires']) && !$expires ? $params['expires'] : null; - $path = isset($params['path']) ? $params['path'] : null; - $domain = isset($params['domain']) ? $params['domain'] : ''; - $secure = isset($params['secure']) ? $params['secure'] : false; - $httpOnly = isset($params['httpOnly']) ? $params['httpOnly'] : true; - $encodedValue = isset($params['encodedValue']) ? $params['encodedValue'] : false; + $path = $params['path'] ?? null; + $domain = $params['domain'] ?? ''; + $secure = $params['secure'] ?? false; + $httpOnly = $params['httpOnly'] ?? true; + $encodedValue = $params['encodedValue'] ?? false; @@ -1546,7 +1553,7 @@ public function setCookie($name, $val, array $params = []) $this->debugCookieJar(); } - public function grabCookie($cookie, array $params = []) + public function grabCookie(string $cookie, array $params = []) { $params = array_merge($this->defaultCookieParameters, $params); $this->debugCookieJar(); @@ -1563,73 +1570,73 @@ public function grabCookie($cookie, array $params = []) * @throws ModuleException if no page was opened. * @return string Current page source code. */ - public function grabPageSource() + public function grabPageSource(): string { return $this->_getResponseContent(); } - public function seeCookie($cookie, array $params = []) + public function seeCookie(string $cookie, array $params = []) { $params = array_merge($this->defaultCookieParameters, $params); $this->debugCookieJar(); $this->assertNotNull($this->client->getCookieJar()->get($cookie, $params['path'], $params['domain'])); } - public function dontSeeCookie($cookie, array $params = []) + public function dontSeeCookie(string $cookie, array $params = []) { $params = array_merge($this->defaultCookieParameters, $params); $this->debugCookieJar(); $this->assertNull($this->client->getCookieJar()->get($cookie, $params['path'], $params['domain'])); } - public function resetCookie($cookie, array $params = []) + public function resetCookie(string $cookie, array $params = []) { $params = array_merge($this->defaultCookieParameters, $params); $this->client->getCookieJar()->expire($cookie, $params['path'], $params['domain']); $this->debugCookieJar(); } - private function stringifySelector($selector) + private function stringifySelector($selector): string { if (is_array($selector)) { - return trim(json_encode($selector), '{}'); + return trim(json_encode($selector, JSON_THROW_ON_ERROR), '{}'); } return $selector; } - public function seeElement($selector, $attributes = []) + public function seeElement($selector, array $attributes = []): void { $nodes = $this->match($selector); $selector = $this->stringifySelector($selector); if (!empty($attributes)) { $nodes = $this->filterByAttributes($nodes, $attributes); - $selector .= "' with attribute(s) '" . trim(json_encode($attributes), '{}'); + $selector .= "' with attribute(s) '" . trim(json_encode($attributes, JSON_THROW_ON_ERROR), '{}'); } $this->assertDomContains($nodes, $selector); } - public function dontSeeElement($selector, $attributes = []) + public function dontSeeElement($selector, array $attributes = []): void { $nodes = $this->match($selector); $selector = $this->stringifySelector($selector); if (!empty($attributes)) { $nodes = $this->filterByAttributes($nodes, $attributes); - $selector .= "' with attribute(s) '" . trim(json_encode($attributes), '{}'); + $selector .= "' with attribute(s) '" . trim(json_encode($attributes, JSON_THROW_ON_ERROR), '{}'); } $this->assertDomNotContains($nodes, $selector); } - public function seeNumberOfElements($selector, $expected) + public function seeNumberOfElements($selector, $expected): void { $counted = count($this->match($selector)); if (is_array($expected)) { - list($floor, $ceil) = $expected; + [$floor, $ceil] = $expected; $this->assertTrue( $floor <= $counted && $ceil >= $counted, 'Number of elements counted differs from expected range' ); } else { - $this->assertEquals( + $this->assertSame( $expected, $counted, 'Number of elements counted differs from expected number' @@ -1637,7 +1644,7 @@ public function seeNumberOfElements($selector, $expected) } } - public function seeOptionIsSelected($selector, $optionText) + public function seeOptionIsSelected($selector, string $optionText) { $selected = $this->matchSelectedOption($selector); $this->assertDomContains($selected, 'selected option'); @@ -1645,27 +1652,24 @@ public function seeOptionIsSelected($selector, $optionText) $value = $selected->getNode(0)->tagName === 'option' ? $selected->text() : $selected->getNode(0)->getAttribute('value'); - $this->assertEquals($optionText, $value); + $this->assertSame($optionText, $value); } - public function dontSeeOptionIsSelected($selector, $optionText) + public function dontSeeOptionIsSelected($selector, string $optionText) { $selected = $this->matchSelectedOption($selector); - if (!$selected->count()) { - $this->assertEquals(0, $selected->count()); + if ($selected->count() === 0) { + $this->assertSame(0, $selected->count()); return; } //If element is radio then we need to check value $value = $selected->getNode(0)->tagName === 'option' ? $selected->text() : $selected->getNode(0)->getAttribute('value'); - $this->assertNotEquals($optionText, $value); + $this->assertNotSame($optionText, $value); } - /** - * @return SymfonyCrawler - */ - protected function matchSelectedOption($select) + protected function matchSelectedOption($select): SymfonyCrawler { $nodes = $this->getFieldsByLabelOrCss($select); $selectedOptions = $nodes->filter('option[selected],input:checked'); @@ -1696,14 +1700,14 @@ public function seePageNotFound() * * @param int $code */ - public function seeResponseCodeIs($code) + public function seeResponseCodeIs(int $code): void { $failureMessage = sprintf( 'Expected HTTP Status Code: %s. Actual Status Code: %s', HttpCode::getDescription($code), HttpCode::getDescription($this->getResponseStatusCode()) ); - $this->assertEquals($code, $this->getResponseStatusCode(), $failureMessage); + $this->assertSame($code, $this->getResponseStatusCode(), $failureMessage); } /** @@ -1712,7 +1716,7 @@ public function seeResponseCodeIs($code) * @param int $from * @param int $to */ - public function seeResponseCodeIsBetween($from, $to) + public function seeResponseCodeIsBetween(int $from, int $to): void { $failureMessage = sprintf( 'Expected HTTP Status Code between %s and %s. Actual Status Code: %s', @@ -1736,13 +1740,13 @@ public function seeResponseCodeIsBetween($from, $to) * ``` * @param int $code */ - public function dontSeeResponseCodeIs($code) + public function dontSeeResponseCodeIs(int $code): void { $failureMessage = sprintf( 'Expected HTTP status code other than %s', HttpCode::getDescription($code) ); - $this->assertNotEquals($code, $this->getResponseStatusCode(), $failureMessage); + $this->assertNotSame($code, $this->getResponseStatusCode(), $failureMessage); } /** @@ -1777,7 +1781,7 @@ public function seeResponseCodeIsServerError() $this->seeResponseCodeIsBetween(500, 599); } - public function seeInTitle($title) + public function seeInTitle(string $title) { $nodes = $this->getCrawler()->filter('title'); if ($nodes->count() === 0) { @@ -1786,7 +1790,7 @@ public function seeInTitle($title) $this->assertStringContainsString($title, $nodes->first()->text(), "page title contains $title"); } - public function dontSeeInTitle($title) + public function dontSeeInTitle(string $title) { $nodes = $this->getCrawler()->filter('title'); if ($nodes->count() === 0) { @@ -1796,28 +1800,19 @@ public function dontSeeInTitle($title) $this->assertStringNotContainsString($title, $nodes->first()->text(), "page title contains $title"); } - /** - * @param string $message - */ - protected function assertDomContains($nodes, $message, $text = '') + protected function assertDomContains($nodes, string $message, string $text = ''): void { $constraint = new CrawlerConstraint($text, $this->_getCurrentUri()); $this->assertThat($nodes, $constraint, $message); } - /** - * @param string $message - */ - protected function assertDomNotContains($nodes, $message, $text = '') + protected function assertDomNotContains($nodes, string $message, string $text = ''): void { $constraint = new CrawlerNotConstraint($text, $this->_getCurrentUri()); $this->assertThat($nodes, $constraint, $message); } - /** - * @param string $message - */ - protected function assertPageContains($needle, $message = '') + protected function assertPageContains(string $needle, string $message = ''): void { $constraint = new PageConstraint($needle, $this->_getCurrentUri()); $this->assertThat( @@ -1827,10 +1822,7 @@ protected function assertPageContains($needle, $message = '') ); } - /** - * @param string $message - */ - protected function assertPageNotContains($needle, $message = '') + protected function assertPageNotContains(string $needle, string $message = ''): void { $constraint = new PageConstraint($needle, $this->_getCurrentUri()); $this->assertThatItsNot( @@ -1840,10 +1832,7 @@ protected function assertPageNotContains($needle, $message = '') ); } - /** - * @param string $message - */ - protected function assertPageSourceContains($needle, $message = '') + protected function assertPageSourceContains(string $needle, string $message = ''): void { $constraint = new PageConstraint($needle, $this->_getCurrentUri()); $this->assertThat( @@ -1853,10 +1842,7 @@ protected function assertPageSourceContains($needle, $message = '') ); } - /** - * @param string $message - */ - protected function assertPageSourceNotContains($needle, $message = '') + protected function assertPageSourceNotContains(string $needle, string $message = ''): void { $constraint = new PageConstraint($needle, $this->_getCurrentUri()); $this->assertThatItsNot( @@ -1868,11 +1854,11 @@ protected function assertPageSourceNotContains($needle, $message = '') /** * @param string $name - * @param array $form + * @param array|object $form * @param FormField $dynamicField - * @return FormField + * @return FormField|array */ - protected function matchFormField($name, $form, $dynamicField) + protected function matchFormField(string $name, $form, FormField $dynamicField) { if (substr($name, -2) !== '[]') { return $form[$name]; @@ -1887,11 +1873,7 @@ protected function matchFormField($name, $form, $dynamicField) throw new TestRuntimeException("None of form fields by {$name}[] were not matched"); } - /** - * @param $locator - * @return SymfonyCrawler - */ - protected function filterByCSS($locator) + protected function filterByCSS(string $locator): SymfonyCrawler { if (!Locator::isCSS($locator)) { throw new MalformedLocatorException($locator, 'css'); @@ -1899,11 +1881,7 @@ protected function filterByCSS($locator) return $this->getCrawler()->filter($locator); } - /** - * @param $locator - * @return SymfonyCrawler - */ - protected function filterByXPath($locator) + protected function filterByXPath(string $locator): SymfonyCrawler { if (!Locator::isXPath($locator)) { throw new MalformedLocatorException($locator, 'xpath'); @@ -1915,7 +1893,7 @@ protected function filterByXPath($locator) * @param array $requestParams * @return array */ - protected function getFormPhpValues($requestParams) + protected function getFormPhpValues(array $requestParams): array { foreach ($requestParams as $name => $value) { $qs = http_build_query([$name => $value]); @@ -1941,7 +1919,7 @@ protected function getFormPhpValues($requestParams) * @param int $redirectCount * @return SymfonyCrawler */ - protected function redirectIfNecessary($result, $maxRedirects, $redirectCount) + protected function redirectIfNecessary(SymfonyCrawler $result, int $maxRedirects, int $redirectCount): SymfonyCrawler { $locationHeader = $this->client->getInternalResponse()->getHeader('Location'); $statusCode = $this->getResponseStatusCode(); @@ -1979,8 +1957,7 @@ protected function redirectIfNecessary($result, $maxRedirects, $redirectCount) * * @param string $name */ - - public function switchToIframe($name) + public function switchToIframe(string $name): void { $iframe = $this->match("iframe[name=$name]")->first(); if (count($iframe) === 0) { @@ -1999,7 +1976,7 @@ public function switchToIframe($name) * * @param int $numberOfSteps (default value 1) */ - public function moveBack($numberOfSteps = 1) + public function moveBack(int $numberOfSteps = 1): void { if (!is_int($numberOfSteps) || $numberOfSteps < 1) { throw new InvalidArgumentException('numberOfSteps must be positive integer'); @@ -2072,10 +2049,7 @@ protected function setCookiesFromOptions() } } - /** - * @return string - */ - protected function getNormalizedResponseContent() + protected function getNormalizedResponseContent(): string { $content = $this->_getResponseContent(); // Since strip_tags has problems with JS code that contains @@ -2111,7 +2085,7 @@ public function setServerParameters(array $params) * @param string $name * @param string $value */ - public function haveServerParameter($name, $value) + public function haveServerParameter(string $name, string $value): void { $this->client->setServerParameter($name, $value); } @@ -2168,7 +2142,7 @@ public function followRedirect() * * @param int $maxRedirects */ - public function setMaxRedirects($maxRedirects) + public function setMaxRedirects(int $maxRedirects): void { $this->client->setMaxRedirects($maxRedirects); } diff --git a/src/Codeception/Util/HttpCode.php b/src/Codeception/Util/HttpCode.php index dadb46e..f88bce7 100644 --- a/src/Codeception/Util/HttpCode.php +++ b/src/Codeception/Util/HttpCode.php @@ -1,5 +1,7 @@ seeResponseCodeIs(HttpCode::OK); * $I->dontSeeResponseCodeIs(HttpCode::NOT_FOUND); * ``` - * - * */ class HttpCode { @@ -347,10 +347,9 @@ class HttpCode * HttpCode::getDescription(401); // '401 (Unauthorized)' * ``` * - * @param int $code * @return int|string */ - public static function getDescription($code) + public static function getDescription(int $code) { if (isset(self::$codes[$code])) { return sprintf('%d (%s)', $code, self::$codes[$code]); diff --git a/tests/unit/Codeception/Constraints/CrawlerConstraintTest.php b/tests/unit/Codeception/Constraints/CrawlerConstraintTest.php index 7700edc..b3a762c 100644 --- a/tests/unit/Codeception/Constraints/CrawlerConstraintTest.php +++ b/tests/unit/Codeception/Constraints/CrawlerConstraintTest.php @@ -1,5 +1,8 @@ fail('Expected to get exception here'); } catch (\InvalidArgumentException $e) { codecept_debug('Exception: ' . $e->getMessage()); + } catch (\TypeError $e) { + codecept_debug('Error: ' . $e->getMessage()); } } } @@ -88,7 +93,7 @@ public function testCreateSnapshotOnFail() $container = Stub::make('Codeception\Lib\ModuleContainer'); $module = Stub::construct(get_class($this->module), [$container], [ '_savePageSource' => Stub\Expected::once(function ($filename) { - $this->assertEquals(codecept_log_dir('Codeception.Module.UniversalFramework.looks.like..test.fail.html'), $filename); + $this->assertSame(codecept_log_dir('Codeception.Module.UniversalFramework.looks.like..test.fail.html'), $filename); }), ]); $module->_initialize(); diff --git a/tests/unit/Codeception/Module/TestsForWeb.php b/tests/unit/Codeception/Module/TestsForWeb.php index 2db61b3..9236aa1 100644 --- a/tests/unit/Codeception/Module/TestsForWeb.php +++ b/tests/unit/Codeception/Module/TestsForWeb.php @@ -1,4 +1,7 @@ module->amOnPage('/form/button'); $this->module->click("btn0"); $form = data::get('form'); - $this->assertEquals('val', $form['text']); + $this->assertSame('val', $form['text']); } public function testClickByLinkTitle() @@ -229,7 +232,7 @@ public function testCheckboxByCss() $this->module->checkOption('#checkin'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('agree', $form['terms']); + $this->assertSame('agree', $form['terms']); } public function testCheckboxByName() @@ -238,7 +241,7 @@ public function testCheckboxByName() $this->module->checkOption('terms'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('agree', $form['terms']); + $this->assertSame('agree', $form['terms']); } public function testCheckboxByLabel() @@ -247,7 +250,7 @@ public function testCheckboxByLabel() $this->module->checkOption('I Agree'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('agree', $form['terms']); + $this->assertSame('agree', $form['terms']); } /** @@ -260,7 +263,7 @@ public function testCheckboxArray() $this->module->checkOption('#id2'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('second', reset($form['field'])); + $this->assertSame('second', reset($form['field'])); } public function testSelectByCss() @@ -269,7 +272,7 @@ public function testSelectByCss() $this->module->selectOption('form select[name=age]', 'adult'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('adult', $form['age']); + $this->assertSame('adult', $form['age']); } public function testSelectByName() @@ -278,7 +281,7 @@ public function testSelectByName() $this->module->selectOption('age', 'adult'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('adult', $form['age']); + $this->assertSame('adult', $form['age']); } public function testSelectByLabel() @@ -287,7 +290,7 @@ public function testSelectByLabel() $this->module->selectOption('Select your age', 'dead'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('dead', $form['age']); + $this->assertSame('dead', $form['age']); } public function testSelectByLabelAndOptionText() @@ -296,7 +299,7 @@ public function testSelectByLabelAndOptionText() $this->module->selectOption('Select your age', '21-60'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('adult', $form['age']); + $this->assertSame('adult', $form['age']); } public function testSeeSelectedOption() @@ -322,7 +325,7 @@ public function testSeeSelectedOptionReturnsFirstOptionIfNotSelected() $this->module->seeOptionIsSelected('#age', 'below 13'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('child', $form['age'], 'first option was not submitted'); + $this->assertSame('child', $form['age'], 'first option was not submitted'); } /** @@ -334,7 +337,7 @@ public function testSubmitSeveralSubmitsForm() $this->module->amOnPage('/form/example8'); $this->module->click('form button[value="second"]'); $form = data::get('form'); - $this->assertEquals('second', $form['submit']); + $this->assertSame('second', $form['submit']); } /** @@ -348,7 +351,7 @@ public function testSubmitLotsOfSubmitsForm() $this->module->amOnPage('/form/example11'); $this->module->click('form button[value="fifth"]'); $form = data::get('form'); - $this->assertEquals('fifth', $form['submit']); + $this->assertSame('fifth', $form['submit']); } public function testSelectMultipleOptionsByText() @@ -357,7 +360,7 @@ public function testSelectMultipleOptionsByText() $this->module->selectOption('What do you like the most?', array('Play Video Games', 'Have Sex')); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals(array('play', 'adult'), $form['like']); + $this->assertSame(array('play', 'adult'), $form['like']); } public function testSelectMultipleOptionsByValue() @@ -366,7 +369,7 @@ public function testSelectMultipleOptionsByValue() $this->module->selectOption('What do you like the most?', array('eat', 'adult')); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals(array('eat', 'adult'), $form['like']); + $this->assertSame(array('eat', 'adult'), $form['like']); } public function testHidden() @@ -374,7 +377,7 @@ public function testHidden() $this->module->amOnPage('/form/hidden'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('kill_people', $form['action']); + $this->assertSame('kill_people', $form['action']); } public function testTextareaByCss() @@ -383,7 +386,7 @@ public function testTextareaByCss() $this->module->fillField('textarea', 'Nothing special'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('Nothing special', $form['description']); + $this->assertSame('Nothing special', $form['description']); } public function testTextareaByLabel() @@ -392,7 +395,7 @@ public function testTextareaByLabel() $this->module->fillField('Description', 'Nothing special'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('Nothing special', $form['description']); + $this->assertSame('Nothing special', $form['description']); } public function testTextFieldByCss() @@ -401,7 +404,7 @@ public function testTextFieldByCss() $this->module->fillField('#name', 'Nothing special'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('Nothing special', $form['name']); + $this->assertSame('Nothing special', $form['name']); } public function testTextFieldByName() @@ -411,8 +414,8 @@ public function testTextFieldByName() $this->module->fillField('LoginForm[password]', '123456'); $this->module->click('Login'); $login = data::get('form'); - $this->assertEquals('davert', $login['LoginForm']['username']); - $this->assertEquals('123456', $login['LoginForm']['password']); + $this->assertSame('davert', $login['LoginForm']['username']); + $this->assertSame('123456', $login['LoginForm']['password']); } public function testTextFieldByLabel() @@ -421,7 +424,7 @@ public function testTextFieldByLabel() $this->module->fillField('Name', 'Nothing special'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('Nothing special', $form['name']); + $this->assertSame('Nothing special', $form['name']); } public function testTextFieldByLabelWithoutFor() @@ -430,7 +433,7 @@ public function testTextFieldByLabelWithoutFor() $this->module->fillField('Other label', 'Nothing special'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('Nothing special', $form['othername']); + $this->assertSame('Nothing special', $form['othername']); } public function testFileFieldByCss() @@ -698,26 +701,26 @@ public function testGrabTextFrom() { $this->module->amOnPage('/'); $result = $this->module->grabTextFrom('h1'); - $this->assertEquals("Welcome to test app!", $result); + $this->assertSame("Welcome to test app!", $result); $result = $this->module->grabTextFrom('descendant-or-self::h1'); - $this->assertEquals("Welcome to test app!", $result); + $this->assertSame("Welcome to test app!", $result); $result = $this->module->grabTextFrom('~Welcome to (\w+) app!~'); - $this->assertEquals('test', $result); + $this->assertSame('test', $result); } public function testGrabValueFrom() { $this->module->amOnPage('/form/hidden'); $result = $this->module->grabValueFrom('#action'); - $this->assertEquals("kill_people", $result); + $this->assertSame("kill_people", $result); $result = $this->module->grabValueFrom("descendant-or-self::form/descendant::input[@name='action']"); - $this->assertEquals("kill_people", $result); + $this->assertSame("kill_people", $result); $this->module->amOnPage('/form/textarea'); $result = $this->module->grabValueFrom('#description'); - $this->assertEquals('sunrise', $result); + $this->assertSame('sunrise', $result); $this->module->amOnPage('/form/select'); $result = $this->module->grabValueFrom('#age'); - $this->assertEquals('oldfag', $result); + $this->assertSame('oldfag', $result); } /** @@ -728,19 +731,19 @@ public function testGrabValueFromWithFillField() $this->module->amOnPage('/form/bug3866'); $this->module->fillField('empty', 'new value'); $result = $this->module->grabValueFrom('#empty'); - $this->assertEquals('new value', $result); + $this->assertSame('new value', $result); $this->module->fillField('empty_textarea', 'new value'); $result = $this->module->grabValueFrom('#empty_textarea'); - $this->assertEquals('new value', $result); + $this->assertSame('new value', $result); $this->module->fillField('//textarea[@name="textarea[name][]"]', 'new value'); $result = $this->module->grabValueFrom('#textarea_with_square_bracket'); - $this->assertEquals('new value', $result); + $this->assertSame('new value', $result); } public function testGrabAttributeFrom() { $this->module->amOnPage('/search'); - $this->assertEquals('get', $this->module->grabAttributeFrom('form', 'method')); + $this->assertSame('get', $this->module->grabAttributeFrom('form', 'method')); } public function testLinksWithSimilarNames() @@ -797,7 +800,7 @@ public function testCookies() $this->module->dontSeeCookie('evil_cookie'); $cookie = $this->module->grabCookie($cookie_name); - $this->assertEquals($cookie_value, $cookie); + $this->assertSame($cookie_value, $cookie); $this->module->resetCookie($cookie_name); $this->module->dontSeeCookie($cookie_name); @@ -814,7 +817,7 @@ public function testCookiesWithPath() $this->module->dontSeeCookie('evil_cookie'); $cookie = $this->module->grabCookie($cookie_name, ['path' => '/info']); - $this->assertEquals($cookie_value, $cookie); + $this->assertSame($cookie_value, $cookie); $this->module->resetCookie($cookie_name, ['path' => '/info']); $this->module->dontSeeCookie($cookie_name, ['path' => '/info']); @@ -944,8 +947,8 @@ public function testExample1() $this->module->checkOption('#LoginForm_rememberMe'); $this->module->click('Login'); $login = data::get('form'); - $this->assertEquals('davert', $login['LoginForm']['username']); - $this->assertEquals('123456', $login['LoginForm']['password']); + $this->assertSame('davert', $login['LoginForm']['username']); + $this->assertSame('123456', $login['LoginForm']['password']); $this->assertNotEmpty($login['LoginForm']['rememberMe']); } @@ -956,9 +959,9 @@ public function testExample2() $this->module->fillField('input[name=password]', '123456'); $this->module->click('Log on'); $login = data::get('form'); - $this->assertEquals('davert', $login['username']); - $this->assertEquals('123456', $login['password']); - $this->assertEquals('login', $login['action']); + $this->assertSame('davert', $login['username']); + $this->assertSame('123456', $login['password']); + $this->assertSame('login', $login['action']); } public function testAmpersand() @@ -1041,13 +1044,13 @@ public function testSubmitForm() 'description' => 'Is Codeception maintainer' )); $form = data::get('form'); - $this->assertEquals('Davert', $form['name']); - $this->assertEquals('Is Codeception maintainer', $form['description']); + $this->assertSame('Davert', $form['name']); + $this->assertSame('Is Codeception maintainer', $form['description']); $this->assertArrayNotHasKey('disabled_fieldset', $form); $this->assertArrayNotHasKey('disabled_fieldset_textarea', $form); $this->assertArrayNotHasKey('disabled_fieldset_select', $form); $this->assertArrayNotHasKey('disabled_field', $form); - $this->assertEquals('kill_all', $form['action']); + $this->assertSame('kill_all', $form['action']); } public function testSubmitFormWithFillField() @@ -1059,8 +1062,8 @@ public function testSubmitFormWithFillField() 'description' => 'Is from Iliyum, NY' ]); $form = data::get('form'); - $this->assertEquals('Kilgore Trout', $form['name']); - $this->assertEquals('Is from Iliyum, NY', $form['description']); + $this->assertSame('Kilgore Trout', $form['name']); + $this->assertSame('Is from Iliyum, NY', $form['description']); } public function testSubmitFormWithoutButton() @@ -1070,7 +1073,7 @@ public function testSubmitFormWithoutButton() 'text' => 'Hello!' )); $form = data::get('form'); - $this->assertEquals('Hello!', $form['text']); + $this->assertSame('Hello!', $form['text']); } public function testSubmitFormWithAmpersand() @@ -1078,7 +1081,7 @@ public function testSubmitFormWithAmpersand() $this->module->amOnPage('/form/submitform_ampersands'); $this->module->submitForm('form', []); $form = data::get('form'); - $this->assertEquals('this & that', $form['test']); + $this->assertSame('this & that', $form['test']); } public function testSubmitFormWithArrayField() @@ -1101,8 +1104,8 @@ public function testSubmitFormMultiSelectWithArrayParameter() ]); $form = data::get('form'); $this->assertCount(2, $form['select']); - $this->assertEquals('see test one', $form['select'][0]); - $this->assertEquals('not seen four', $form['select'][1]); + $this->assertSame('see test one', $form['select'][0]); + $this->assertSame('not seen four', $form['select'][1]); } public function testSubmitFormWithMultiSelect() @@ -1111,8 +1114,8 @@ public function testSubmitFormWithMultiSelect() $this->module->submitForm('form', []); $form = data::get('form'); $this->assertCount(2, $form['select']); - $this->assertEquals('see test one', $form['select'][0]); - $this->assertEquals('see test two', $form['select'][1]); + $this->assertSame('see test one', $form['select'][0]); + $this->assertSame('see test two', $form['select'][1]); } public function testSubmitFormCheckboxWithArrayParameter() @@ -1127,9 +1130,9 @@ public function testSubmitFormCheckboxWithArrayParameter() ]); $form = data::get('form'); $this->assertCount(3, $form['checkbox']); - $this->assertEquals('not seen one', $form['checkbox'][0]); - $this->assertEquals('see test two', $form['checkbox'][1]); - $this->assertEquals('not seen three', $form['checkbox'][2]); + $this->assertSame('not seen one', $form['checkbox'][0]); + $this->assertSame('see test two', $form['checkbox'][1]); + $this->assertSame('not seen three', $form['checkbox'][2]); } public function testSubmitFormCheckboxWithBooleanArrayParameter() @@ -1144,8 +1147,8 @@ public function testSubmitFormCheckboxWithBooleanArrayParameter() ]); $form = data::get('form'); $this->assertCount(2, $form['checkbox']); - $this->assertEquals('not seen one', $form['checkbox'][0]); - $this->assertEquals('not seen two', $form['checkbox'][1]); + $this->assertSame('not seen one', $form['checkbox'][0]); + $this->assertSame('not seen two', $form['checkbox'][1]); } /** @@ -1162,7 +1165,7 @@ public function testSubmitFormWithDefaultTextareaValue() $this->module->amOnPage('/form/textarea'); $this->module->submitForm('form', []); $form = data::get('form'); - $this->assertEquals('sunrise', $form['description']); + $this->assertSame('sunrise', $form['description']); } /** @@ -1181,10 +1184,10 @@ public function testClickLinkWithInnerSpan() public function testSelectTwoSubmitsByText() { $this->module->amOnPage('/form/select_two_submits'); - $this->module->selectOption('What kind of sandwich would you like?', 2); + $this->module->selectOption('What kind of sandwich would you like?', '2'); $this->module->click('Save'); $form = data::get('form'); - $this->assertEquals(2, $form['sandwich_select']); + $this->assertSame('2', $form['sandwich_select']); } public function testSelectTwoSubmitsByCSS() @@ -1193,7 +1196,7 @@ public function testSelectTwoSubmitsByCSS() $this->module->selectOption("form select[name='sandwich_select']", '2'); $this->module->click('Save'); $form = data::get('form'); - $this->assertEquals(2, $form['sandwich_select']); + $this->assertSame('2', $form['sandwich_select']); } protected function shouldFail() @@ -1212,8 +1215,8 @@ public function testSubmitFormWithTwoSubmitButtonsSubmitsCorrectValue() $form = data::get('form'); $this->assertArrayHasKey('button2', $form); $this->assertArrayHasKey('username', $form); - $this->assertEquals('value2', $form['button2']); - $this->assertEquals('fred', $form['username']); + $this->assertSame('value2', $form['button2']); + $this->assertSame('fred', $form['username']); } /** @@ -1227,8 +1230,8 @@ public function testSubmitFormWithTwoSubmitButtonsSubmitsCorrectValueAfterFillFi $form = data::get('form'); $this->assertArrayHasKey('button2', $form); $this->assertArrayHasKey('username', $form); - $this->assertEquals('value2', $form['button2']); - $this->assertEquals('bob', $form['username']); + $this->assertSame('value2', $form['button2']); + $this->assertSame('bob', $form['username']); } /* @@ -1271,8 +1274,8 @@ public function testSubmitFormWithDefaultRadioAndCheckboxValues() $form = data::get('form'); $this->assertArrayHasKey('checkbox1', $form, 'Checkbox value not sent'); $this->assertArrayHasKey('radio1', $form, 'Radio button value not sent'); - $this->assertEquals('testing', $form['checkbox1']); - $this->assertEquals('to be sent', $form['radio1']); + $this->assertSame('testing', $form['checkbox1']); + $this->assertSame('to be sent', $form['radio1']); } public function testSubmitFormCheckboxWithBoolean() @@ -1283,7 +1286,7 @@ public function testSubmitFormCheckboxWithBoolean() )); $form = data::get('form'); $this->assertArrayHasKey('checkbox1', $form, 'Checkbox value not sent'); - $this->assertEquals('testing', $form['checkbox1']); + $this->assertSame('testing', $form['checkbox1']); $this->module->amOnPage('/form/example16'); $this->module->submitForm('form', array( @@ -1322,7 +1325,7 @@ public function testSubmitFormWithButtons() 'Button values for buttons 1, 2 and 4 should not be set' ); $this->assertArrayHasKey('button3', $form, 'Button value for button3 should be set'); - $this->assertEquals($form['button3'], 'third', 'Button value for button3 should equal third'); + $this->assertSame($form['button3'], 'third', 'Button value for button3 should equal third'); $this->module->amOnPage('/form/form_with_buttons'); $this->module->submitForm('form', array( @@ -1334,7 +1337,7 @@ public function testSubmitFormWithButtons() 'Button values for buttons 1, 2 and 3 should not be set' ); $this->assertArrayHasKey('button4', $form, 'Button value for button4 should be set'); - $this->assertEquals($form['button4'], 'fourth', 'Button value for button4 should equal fourth'); + $this->assertSame($form['button4'], 'fourth', 'Button value for button4 should equal fourth'); } /** @@ -1404,7 +1407,7 @@ public function testTextFieldByNameFirstNotCss() $this->module->fillField('price', '19.99'); $this->module->click('Create'); $data = data::get('form'); - $this->assertEquals('Special Widget', $data['title']); + $this->assertSame('Special Widget', $data['title']); } /** @@ -1444,7 +1447,7 @@ public function testSubmitAdjacentForms() $data = data::get('form'); $this->assertArrayHasKey('second-field', $data); $this->assertArrayNotHasKey('first-field', $data); - $this->assertEquals('Killgore Trout', $data['second-field']); + $this->assertSame('Killgore Trout', $data['second-field']); } public function testSubmitAdjacentFormsByButton() @@ -1456,7 +1459,7 @@ public function testSubmitAdjacentFormsByButton() $data = data::get('form'); $this->assertArrayHasKey('first-field', $data); $this->assertArrayNotHasKey('second-field', $data); - $this->assertEquals('First', $data['first-field']); + $this->assertSame('First', $data['first-field']); $this->module->amOnPage('/form/submit_adjacentforms'); $this->module->fillField('first-field', 'First'); @@ -1465,7 +1468,7 @@ public function testSubmitAdjacentFormsByButton() $data = data::get('form'); $this->assertArrayNotHasKey('first-field', $data); $this->assertArrayHasKey('second-field', $data); - $this->assertEquals('Second', $data['second-field']); + $this->assertSame('Second', $data['second-field']); } public function testArrayField() @@ -1518,7 +1521,7 @@ public function testFillFieldWithAmpersand() $this->module->fillField('Name', 'this & that'); $this->module->click('Submit'); $form = data::get('form'); - $this->assertEquals('this & that', $form['name']); + $this->assertSame('this & that', $form['name']); } public function testSeeInDeactivatedField() @@ -1542,20 +1545,20 @@ public function testGrabMultiple() $arr = $this->module->grabMultiple('#grab-multiple a:first-child'); $this->assertCount(1, $arr); - $this->assertEquals('First', $arr[0]); + $this->assertSame('First', $arr[0]); $arr = $this->module->grabMultiple('#grab-multiple a'); $this->assertCount(3, $arr); - $this->assertEquals('First', $arr[0]); - $this->assertEquals('Second', $arr[1]); - $this->assertEquals('Third', $arr[2]); + $this->assertSame('First', $arr[0]); + $this->assertSame('Second', $arr[1]); + $this->assertSame('Third', $arr[2]); // href for WebDriver with selenium returns a full link, so testing with ID $arr = $this->module->grabMultiple('#grab-multiple a', 'id'); $this->assertCount(3, $arr); - $this->assertEquals('first-link', $arr[0]); - $this->assertEquals('second-link', $arr[1]); - $this->assertEquals('third-link', $arr[2]); + $this->assertSame('first-link', $arr[0]); + $this->assertSame('second-link', $arr[1]); + $this->assertSame('third-link', $arr[2]); } /** @@ -1592,7 +1595,7 @@ public function testSelectOptionValueSelector() $this->module->selectOption('age', ['value' => '20']); $this->module->click('Submit'); $data = data::get('form'); - $this->assertEquals('20', $data['age']); + $this->assertSame('20', $data['age']); } public function testSelectOptionTextSelector() @@ -1755,7 +1758,7 @@ public function testPasswordArgument() $this->module->fillField('password', new \Codeception\Step\Argument\PasswordArgument('thisissecret')); $this->module->click('Submit'); $data = data::get('form'); - $this->assertEquals('thisissecret', $data['password']); + $this->assertSame('thisissecret', $data['password']); } @@ -1787,12 +1790,12 @@ public function testUncheckHidden() $this->module->uncheckOption('#coffee-id'); $this->module->click("Submit Preference"); $form = data::get('form'); - $this->assertEquals('0', $form['coffee']); + $this->assertSame('0', $form['coffee']); // test all other inputs are submitted as intended - $this->assertEquals('mouse', $form['wireless']); - $this->assertEquals('1', $form['tea']); - $this->assertEquals('on', $form['vanilla']); // 'on' is set internally + $this->assertSame('mouse', $form['wireless']); + $this->assertSame('1', $form['tea']); + $this->assertSame('on', $form['vanilla']); // 'on' is set internally $this->assertFalse(isset($form['butter'])); } diff --git a/tests/unit/Codeception/Util/HttpCodeTest.php b/tests/unit/Codeception/Util/HttpCodeTest.php index 4bb4fa6..517c2b7 100644 --- a/tests/unit/Codeception/Util/HttpCodeTest.php +++ b/tests/unit/Codeception/Util/HttpCodeTest.php @@ -1,18 +1,21 @@ assertEquals(200, HttpCode::OK); - $this->assertEquals(404, HttpCode::NOT_FOUND); + $this->assertSame(200, HttpCode::OK); + $this->assertSame(404, HttpCode::NOT_FOUND); } public function testHttpCodeWithDescription() { - $this->assertEquals('200 (OK)', HttpCode::getDescription(200)); - $this->assertEquals('301 (Moved Permanently)', HttpCode::getDescription(301)); - $this->assertEquals('401 (Unauthorized)', HttpCode::getDescription(401)); + $this->assertSame('200 (OK)', HttpCode::getDescription(200)); + $this->assertSame('301 (Moved Permanently)', HttpCode::getDescription(301)); + $this->assertSame('401 (Unauthorized)', HttpCode::getDescription(401)); } }