Skip to content

Commit 88ea36e

Browse files
committed
Improve code style
1 parent 4560d65 commit 88ea36e

30 files changed

+417
-243
lines changed

.github/workflows/static-analysis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Static analysis
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
phpcs:
11+
name: Code style
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
17+
- name: Install PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: '8.1'
21+
ini-values: memory_limit=-1, date.timezone='UTC'
22+
tools: phpcs
23+
24+
- name: Check production code style
25+
run: phpcs src/
26+
27+
- name: Check test code style
28+
run: phpcs tests/ --standard=tests/phpcs.xml

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
"autoload":{
3434
"classmap": ["src/"]
3535
},
36+
"autoload-dev": {
37+
"classmap": [
38+
"tests/data/app/data.php",
39+
"tests/unit/Codeception/Constraints/TestedWebElement.php"
40+
]
41+
},
3642
"config": {
3743
"classmap-authoritative": true
3844
}

phpcs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Codeception">
3+
<description>Codeception code standard</description>
4+
<rule ref="PSR12">
5+
<exclude name="PSR2.Methods.MethodDeclaration.Underscore"/>
6+
</rule>
7+
</ruleset>

src/Codeception/Constraint/WebDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure
5454
}
5555

5656
$output = "Failed asserting that any element by " . Locator::humanReadableString($selector);
57-
$output .= $this->uriMessage('on page');
57+
$output .= ' ' . $this->uriMessage('on page');
5858

5959
if (count($nodes) < 5) {
6060
$output .= "\nElements: ";

src/Codeception/Constraint/WebDriverNot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function fail($nodes, $selector, ComparisonFailure $comparisonFailure
3838
}
3939

4040
$output = "There was {$selector} element";
41-
$output .= $this->uriMessage("on page");
41+
$output .= ' ' . $this->uriMessage('on page');
4242
$output .= $this->nodesList($nodes, $this->string);
4343
$output .= "\ncontaining '{$this->string}'";
4444

src/Codeception/Module/WebDriver.php

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
//phpcs:disable Generic.Files.LineLength.TooLong
34
declare(strict_types=1);
45

56
namespace Codeception\Module;
@@ -65,11 +66,11 @@
6566
*
6667
* * Java is required
6768
* * NodeJS is required
68-
*
69+
*
6970
* The fastest way to get started is to [Install and launch Selenium using selenium-standalone NodeJS package](https://www.npmjs.com/package/selenium-standalone).
70-
*
71+
*
7172
* Launch selenium standalone in separate console window:
72-
*
73+
*
7374
* ```
7475
* selenium-standalone start
7576
* ```
@@ -83,11 +84,11 @@
8384
* url: 'http://localhost/'
8485
* browser: chrome # 'chrome' or 'firefox'
8586
* ```
86-
*
87+
*
8788
* ## Headless Chrome Browser
88-
*
89+
*
8990
* To enable headless mode (launch tests without showing a window) for Chrome browser using Selenium use this config in `acceptance.suite.yml`:
90-
*
91+
*
9192
* ```yaml
9293
* modules:
9394
* enabled:
@@ -98,7 +99,7 @@
9899
* chromeOptions:
99100
* args: ["--headless", "--disable-gpu"]
100101
* ```
101-
*
102+
*
102103
* ## Headless Selenium in Docker
103104
*
104105
* Docker can ship Selenium Server with all its dependencies and browsers inside a single container.
@@ -113,12 +114,12 @@
113114
* ## Local Chrome and/or Firefox
114115
*
115116
* Tests can be executed directly through ChromeDriver or GeckoDriver (for Firefox). Consider using this option if you don't plan to use Selenium.
116-
*
117+
*
117118
* ### ChromeDriver
118-
*
119+
*
119120
* * Download and install [ChromeDriver](https://sites.google.com/chromium.org/driver/downloads?authuser=0)
120121
* * Launch ChromeDriver in a separate console window: `chromedriver --url-base=/wd/hub`.
121-
*
122+
*
122123
* Configuration in `acceptance.suite.yml`:
123124
*
124125
* ```yaml
@@ -139,10 +140,10 @@
139140
*
140141
*
141142
* ### GeckoDriver
142-
*
143+
*
143144
* * [GeckoDriver](https://github.com/mozilla/geckodriver/releases) must be installed
144145
* * Start GeckoDriver in a separate console window: `geckodriver`.
145-
*
146+
*
146147
* Configuration in `acceptance.suite.yml`:
147148
*
148149
* ```yaml
@@ -160,7 +161,7 @@
160161
* intl.accept_languages: "de-AT" # Set HTTP-Header `Accept-Language: de-AT` for requests
161162
* ```
162163
* See here for [Firefox capabilities](https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#List_of_capabilities)
163-
*
164+
*
164165
* ## Cloud Testing
165166
*
166167
* Cloud Testing services can run your WebDriver tests in the cloud.
@@ -460,15 +461,24 @@ public function _requires(): array
460461
protected function getBaseElement(): WebDriverSearchContext
461462
{
462463
if (!$this->baseElement) {
463-
throw new ModuleException($this, "Page not loaded. Use `\$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it");
464+
throw new ModuleException(
465+
$this,
466+
"Page not loaded. Use `\$I->amOnPage` (or hidden API methods `_request` and `_loadPage`) to open it"
467+
);
464468
}
465469

466470
return $this->baseElement;
467471
}
468472

469473
public function _initialize()
470474
{
471-
$this->wdHost = sprintf('%s://%s:%s%s', $this->config['protocol'], $this->config['host'], $this->config['port'], $this->config['path']);
475+
$this->wdHost = sprintf(
476+
'%s://%s:%s%s',
477+
$this->config['protocol'],
478+
$this->config['host'],
479+
$this->config['port'],
480+
$this->config['path']
481+
);
472482
$this->capabilities = $this->config['capabilities'];
473483
$this->capabilities[WebDriverCapabilityType::BROWSER_NAME] = $this->config['browser'];
474484
if ($proxy = $this->getProxy()) {
@@ -674,7 +684,8 @@ public function debugWebDriverLogs(TestInterface $test = null): void
674684

675685
$this->debugSection("Selenium {$logType} Logs", "\n" . $this->formatLogEntries($logEntries));
676686

677-
if ($logType === 'browser' && $this->config['log_js_errors']
687+
if (
688+
$logType === 'browser' && $this->config['log_js_errors']
678689
&& ($test instanceof ScenarioDriven)
679690
) {
680691
$this->logJSErrors($test, $logEntries);
@@ -711,7 +722,8 @@ protected function formatLogEntries(array $logEntries): string
711722
protected function logJSErrors(ScenarioDriven $test, array $browserLogEntries): void
712723
{
713724
foreach ($browserLogEntries as $logEntry) {
714-
if (isset($logEntry['level'])
725+
if (
726+
isset($logEntry['level'])
715727
&& isset($logEntry['message'])
716728
&& $this->isJSError($logEntry['level'], $logEntry['message'])
717729
) {
@@ -1167,7 +1179,10 @@ public function click($link, $context = null): void
11671179
try {
11681180
$els = $this->match($page, $link);
11691181
} catch (MalformedLocatorException $exception) {
1170-
throw new ElementNotFound("name={$link}", "'{$link}' is invalid CSS and XPath selector and Link or Button");
1182+
throw new ElementNotFound(
1183+
"name={$link}",
1184+
"'{$link}' is invalid CSS and XPath selector and Link or Button"
1185+
);
11711186
}
11721187

11731188
$el = reset($els);
@@ -1237,7 +1252,6 @@ public function _findClickable(WebDriverSearchContext $page, $link): ?WebDriverE
12371252
".//input[./@type = 'submit' or ./@type = 'image' or ./@type = 'button'][./@name = {$locator} or ./@title = {$locator}]",
12381253
".//button[./@name = {$locator} or ./@title = {$locator}]"
12391254
);
1240-
12411255
$els = $page->findElements(WebDriverBy::xpath($xpath));
12421256
if (count($els) > 0) {
12431257
return reset($els);
@@ -1270,10 +1284,8 @@ protected function findFields($selector): array
12701284
$locator = static::xPathLiteral(trim((string) $selector));
12711285
// by text or label
12721286
$xpath = Locator::combine(
1273-
// @codingStandardsIgnoreStart
12741287
".//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')][(((./@name = {$locator}) or ./@id = //label[contains(normalize-space(string(.)), {$locator})]/@for) or ./@placeholder = {$locator})]",
12751288
".//label[contains(normalize-space(string(.)), {$locator})]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]"
1276-
// @codingStandardsIgnoreEnd
12771289
);
12781290
$fields = $this->getBaseElement()->findElements(WebDriverBy::xpath($xpath));
12791291
if (!empty($fields)) {
@@ -1321,7 +1333,10 @@ public function seeLink(string $text, string $url = null): void
13211333
$nodes = $this->filterNodesByHref($url, $nodes);
13221334
}
13231335

1324-
$this->assertNotEmpty($nodes, "No links containing text '{$text}' and URL '{$url}' were found in page {$currentUri}");
1336+
$this->assertNotEmpty(
1337+
$nodes,
1338+
"No links containing text '{$text}' and URL '{$url}' were found in page {$currentUri}"
1339+
);
13251340
}
13261341

13271342
public function dontSeeLink(string $text, string $url = ''): void
@@ -1332,7 +1347,10 @@ public function dontSeeLink(string $text, string $url = ''): void
13321347
$this->assertEmpty($nodes, "Link containing text '{$text}' was found in page {$currentUri}");
13331348
} else {
13341349
$nodes = $this->filterNodesByHref($url, $nodes);
1335-
$this->assertEmpty($nodes, "Link containing text '{$text}' and URL '{$url}' was found in page {$currentUri}");
1350+
$this->assertEmpty(
1351+
$nodes,
1352+
"Link containing text '{$text}' and URL '{$url}' was found in page {$currentUri}"
1353+
);
13361354
}
13371355
}
13381356

@@ -1529,15 +1547,15 @@ protected function proceedSeeInField(array $elements, $value): array
15291547

15301548
break;
15311549
case 'option':
1532-
// no break we need the trim text and the value also
15331550
if (!$el->isSelected()) {
15341551
break;
15351552
}
15361553

15371554
$currentValues[] = $el->getText();
1555+
// no break we need the trim text and the value also
15381556
case 'textarea':
1539-
// we include trimmed and real value of textarea for check
15401557
$currentValues[] = trim($el->getText());
1558+
// we include trimmed and real value of textarea for check
15411559
default:
15421560
$currentValues[] = $el->getAttribute('value'); // raw value
15431561
break;
@@ -1629,7 +1647,10 @@ public function selectOption($select, $option): void
16291647
return;
16301648
}
16311649

1632-
throw new ElementNotFound(json_encode($option, JSON_THROW_ON_ERROR), "Option inside {$select} matched by name or value");
1650+
throw new ElementNotFound(
1651+
json_encode($option, JSON_THROW_ON_ERROR),
1652+
"Option inside {$select} matched by name or value"
1653+
);
16331654
}
16341655

16351656
/**
@@ -1662,7 +1683,10 @@ public function _initializeSession(): void
16621683
$this->initialWindowSize();
16631684
} catch (WebDriverCurlException $exception) {
16641685
codecept_debug('Curl error: ' . $exception->getMessage());
1665-
throw new ConnectionException("Can't connect to WebDriver at {$this->wdHost}. Make sure that ChromeDriver, GeckoDriver or Selenium Server is running.");
1686+
throw new ConnectionException(
1687+
"Can't connect to WebDriver at {$this->wdHost}.'"
1688+
. ' Make sure that ChromeDriver, GeckoDriver or Selenium Server is running.'
1689+
);
16661690
}
16671691
}
16681692

@@ -1765,8 +1789,11 @@ public function unselectOption($select, $option): void
17651789
/**
17661790
* @param string|array|WebDriverBy|WebDriverElement $radioOrCheckbox
17671791
*/
1768-
protected function findCheckable(WebDriverSearchContext $context, $radioOrCheckbox, bool $byValue = false): ?WebDriverElement
1769-
{
1792+
protected function findCheckable(
1793+
WebDriverSearchContext $context,
1794+
$radioOrCheckbox,
1795+
bool $byValue = false
1796+
): ?WebDriverElement {
17701797
if ($radioOrCheckbox instanceof WebDriverElement) {
17711798
return $radioOrCheckbox;
17721799
}
@@ -1786,23 +1813,22 @@ protected function findCheckable(WebDriverSearchContext $context, $radioOrCheckb
17861813
$typeLiteral = static::xPathLiteral($contextType);
17871814
$inputLocatorFragment = "input[@type = {$typeLiteral}][@name = {$nameLiteral}]";
17881815
$xpath = Locator::combine(
1789-
// @codingStandardsIgnoreStart
17901816
"ancestor::form//{$inputLocatorFragment}[(@id = ancestor::form//label[contains(normalize-space(string(.)), {$locator})]/@for) or @placeholder = {$locator}]",
1791-
// @codingStandardsIgnoreEnd
17921817
"ancestor::form//label[contains(normalize-space(string(.)), {$locator})]//{$inputLocatorFragment}"
17931818
);
17941819
if ($byValue) {
17951820
$xpath = Locator::combine($xpath, "ancestor::form//{$inputLocatorFragment}[@value = {$locator}]");
17961821
}
17971822
} else {
17981823
$xpath = Locator::combine(
1799-
// @codingStandardsIgnoreStart
18001824
"//input[@type = 'checkbox' or @type = 'radio'][(@id = //label[contains(normalize-space(string(.)), {$locator})]/@for) or @placeholder = {$locator} or @name = {$locator}]",
1801-
// @codingStandardsIgnoreEnd
18021825
"//label[contains(normalize-space(string(.)), {$locator})]//input[@type = 'radio' or @type = 'checkbox']"
18031826
);
18041827
if ($byValue) {
1805-
$xpath = Locator::combine($xpath, sprintf("//input[@type = 'checkbox' or @type = 'radio'][@value = %s]", $locator));
1828+
$xpath = Locator::combine(
1829+
$xpath,
1830+
sprintf("//input[@type = 'checkbox' or @type = 'radio'][@value = %s]", $locator)
1831+
);
18061832
}
18071833
}
18081834

@@ -2446,7 +2472,9 @@ public function submitForm($selector, array $params, $button = null): void
24462472
$form = $this->matchFirstOrFail($this->getBaseElement(), $selector);
24472473

24482474
$fields = $form->findElements(
2449-
WebDriverBy::cssSelector('input:enabled[name],textarea:enabled[name],select:enabled[name],input[type=hidden][name]')
2475+
WebDriverBy::cssSelector(
2476+
'input:enabled[name],textarea:enabled[name],select:enabled[name],input[type=hidden][name]'
2477+
)
24502478
);
24512479
foreach ($fields as $field) {
24522480
$fieldName = $this->getSubmissionFormFieldName($field->getAttribute('name') ?? '');
@@ -3278,7 +3306,10 @@ public function appendField($field, string $value): void
32783306
return;
32793307
}
32803308

3281-
throw new ElementNotFound(json_encode($value, JSON_THROW_ON_ERROR), "Option inside {$field} matched by name or value");
3309+
throw new ElementNotFound(
3310+
json_encode($value, JSON_THROW_ON_ERROR),
3311+
"Option inside {$field} matched by name or value"
3312+
);
32823313
case "textarea":
32833314
$el->sendKeys($value);
32843315
return;
@@ -3491,7 +3522,7 @@ public function openNewTab(): void
34913522
public function seeNumberOfTabs($number): void
34923523
{
34933524
$this->assertSame(count($this->webDriver->getWindowHandles()), $number);
3494-
}
3525+
}
34953526

34963527
/**
34973528
* Closes current browser tab and switches to previous active tab.
@@ -3697,5 +3728,4 @@ private static function xPathLiteral($s): string
36973728

36983729
return sprintf('concat(%s)', implode(', ', $parts));
36993730
}
3700-
37013731
}

0 commit comments

Comments
 (0)