Skip to content

Commit f62470d

Browse files
authored
Increase PHPStan level to 9 (#58)
1 parent 0d75b54 commit f62470d

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

phpstan.dist.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ includes:
33
- vendor/phpstan/phpstan-phpunit/rules.neon
44

55
parameters:
6-
level: 8
6+
level: 9
77
paths:
88
- src
99
- tests

src/WebdriverClassicDriver.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public function getWindowNames(): array
280280

281281
public function getWindowName(): string
282282
{
283-
$name = (string)$this->evaluateScript('window.name');
283+
$name = $this->getAsString($this->evaluateScript('window.name'), 'Window name');
284284

285285
if ($name === '') {
286286
$name = self::W3C_WINDOW_HANDLE_PREFIX . $this->getWebDriver()->getWindowHandle();
@@ -317,22 +317,22 @@ public function getText(
317317
return trim(str_replace(
318318
["\r\n", "\r", "\n", "\xc2\xa0"],
319319
' ',
320-
$this->getElementDomProperty($this->findElement($xpath), 'innerText')
320+
$this->getAsString($this->getElementDomProperty($this->findElement($xpath), 'innerText'), 'The element\'s innerText')
321321
));
322322
}
323323

324324
public function getHtml(
325325
#[Language('XPath')]
326326
string $xpath
327327
): string {
328-
return $this->getElementDomProperty($this->findElement($xpath), 'innerHTML');
328+
return $this->getAsString($this->getElementDomProperty($this->findElement($xpath), 'innerHTML'), 'The element\'s innerHTML');
329329
}
330330

331331
public function getOuterHtml(
332332
#[Language('XPath')]
333333
string $xpath
334334
): string {
335-
return $this->getElementDomProperty($this->findElement($xpath), 'outerHTML');
335+
return $this->getAsString($this->getElementDomProperty($this->findElement($xpath), 'outerHTML'), 'The element\'s outerHTML');
336336
}
337337

338338
public function getAttribute(
@@ -344,7 +344,8 @@ public function getAttribute(
344344
// so we cannot use webdriver api for this. See also: https://w3c.github.io/webdriver/#dfn-get-element-attribute
345345
$escapedName = $this->jsonEncode($name, 'get attribute', 'attribute name');
346346
$script = "return arguments[0].getAttribute($escapedName)";
347-
return $this->executeJsOnXpath($xpath, $script);
347+
$result = $this->executeJsOnXpath($xpath, $script);
348+
return $result === null ? null : $this->getAsString($result, "The element's $name attribute");
348349
}
349350

350351
/**
@@ -412,7 +413,7 @@ public function setValue(
412413
if (is_array($value)) {
413414
$this->deselectAllOptions($element);
414415
foreach ($value as $option) {
415-
$this->selectOptionOnElement($element, $option, true);
416+
$this->selectOptionOnElement($element, $this->getAsString($option, 'Option value'), true);
416417
}
417418
return;
418419
}
@@ -941,12 +942,13 @@ private function charToSynOptions($char, ?string $modifier = null): string
941942
* Executes JS on a given element - pass in a js script string and argument[0] will
942943
* be replaced with a reference to the result of the $xpath query
943944
*
944-
* @param string $xpath the xpath to search with
945-
* @param string $script the script to execute
945+
* Example:
946+
* ```
947+
* $this->executeJsOnXpath($xpath, 'return argument[0].childNodes.length');
948+
* ```
946949
*
947950
* @return mixed
948951
* @throws DriverException
949-
* @example $this->executeJsOnXpath($xpath, 'return argument[0].childNodes.length');
950952
*/
951953
private function executeJsOnXpath(
952954
#[Language('XPath')]
@@ -960,11 +962,13 @@ private function executeJsOnXpath(
960962
/**
961963
* Executes JS on a given element - pass in a js script string and argument[0] will contain a reference to the element
962964
*
963-
* @param RemoteWebElement $element the webdriver element
964-
* @param string $script the script to execute
965+
* Example:
966+
* ```
967+
* $this->executeJsOnElement($element, 'return argument[0].childNodes.length');
968+
* ```
969+
*
965970
* @return mixed
966971
* @throws DriverException
967-
* @example $this->executeJsOnXpath($xpath, 'return argument[0].childNodes.length');
968972
*/
969973
private function executeJsOnElement(
970974
RemoteWebElement $element,
@@ -1257,5 +1261,21 @@ private function getElementDomProperty(RemoteWebElement $element, string $proper
12571261
}
12581262
}
12591263

1264+
/**
1265+
* @param mixed $value
1266+
* @throws DriverException
1267+
*/
1268+
private function getAsString($value, string $name): string
1269+
{
1270+
if (!is_scalar($value)) {
1271+
$actualType = gettype($value);
1272+
throw new DriverException(
1273+
"$name should be a string or at least a scalar value, but received `$actualType` instead"
1274+
);
1275+
}
1276+
1277+
return (string)$value;
1278+
}
1279+
12601280
// </editor-fold>
12611281
}

tests/Custom/WebDriverTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mink\WebdriverClassicDriver\Tests\Custom;
44

55
use Behat\Mink\Exception\DriverException;
6+
use Facebook\WebDriver\Remote\RemoteWebElement;
67
use Mink\WebdriverClassicDriver\Tests\WebDriverMockingTrait;
78
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;
89

@@ -69,4 +70,28 @@ public function testClassicDriverCanProvideBrowserName(): void
6970
$this->driver->getBrowserName()
7071
);
7172
}
73+
74+
public function testThatDriverCatchesUnexpectedAttributeValueType(): void
75+
{
76+
$mockWebDriver = $this->createMockWebDriver();
77+
$mockElement = $this->createMock(RemoteWebElement::class);
78+
$mockWebDriver
79+
->expects($this->once())
80+
->method('findElement')
81+
->willReturn($mockElement);
82+
$mockWebDriver
83+
->expects($this->once())
84+
->method('executeScript')
85+
->with('return arguments[0].getAttribute("some-attribute")', [$mockElement])
86+
->willReturn(['invalid attribute value']);
87+
88+
$driver = new WebdriverClassicDriver('fake browser', [], 'example.com', fn() => $mockWebDriver);
89+
90+
$driver->start();
91+
92+
$this->expectException(DriverException::class);
93+
$this->expectExceptionMessage('The element\'s some-attribute attribute should be a string or at least a scalar value, but received `array` instead');
94+
95+
$driver->getAttribute('//fake', 'some-attribute');
96+
}
7297
}

0 commit comments

Comments
 (0)