Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan improvements #100

Merged
merged 11 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion http-kernel-fixtures/cookie_page2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<body>
Previous cookie: <?php
/** @var \Symfony\Component\HttpFoundation\Request $request */
echo $request->cookies->has('srvr_cookie') ? html_escape_value($request->cookies->get('srvr_cookie')) : 'NO';
echo $request->cookies->has('srvr_cookie') ? html_escape_value($request->cookies->get('srvr_cookie') ?? '') : 'NO';
?>
</body>
</html>
1 change: 1 addition & 0 deletions http-kernel-fixtures/sub-folder/cookie_page1.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/** @var \Symfony\Component\HttpFoundation\Request $request */
$requestUri = $request->server->get('REQUEST_URI');
assert(is_string($requestUri));
$resp = new Symfony\Component\HttpFoundation\Response();
$cook = Symfony\Component\HttpFoundation\Cookie::create('srvr_cookie', 'srv_var_is_set_sub_folder', 0, dirname($requestUri));
$resp->headers->setCookie($cook);
Expand Down
4 changes: 3 additions & 1 deletion http-kernel-fixtures/sub-folder/cookie_page4.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
/** @var \Symfony\Component\HttpFoundation\Request $request */
$resp = new Symfony\Component\HttpFoundation\Response();
$cookiePath = dirname($request->server->get('REQUEST_URI')) . '/';
$requestUri = $request->server->get('REQUEST_URI');
assert(is_string($requestUri));
$cookiePath = dirname($requestUri) . '/';
$cookie = Symfony\Component\HttpFoundation\Cookie::create('srvr_cookie', 'srv_var_is_set', 0, $cookiePath);
$resp->headers->setCookie($cookie);
?>
Expand Down
3 changes: 1 addition & 2 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
parameters:
level: 8
level: 9
paths:
- src
- tests
- http-kernel-fixtures
- web-fixtures
checkMissingIterableValueType: false

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
Expand Down
7 changes: 4 additions & 3 deletions src/FixturesKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Behat\Mink\Tests\Driver\Util;

use Behat\Mink\Tests\Driver\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -25,8 +26,8 @@ public function handle(Request $request, $type = 1 /* self::MAIN_REQUEST */ , $c

private function handleFixtureRequest(Request $request): Response
{
$fixturesDir = realpath(__DIR__ . '/../web-fixtures');
$overwriteDir = realpath(__DIR__ . '/../http-kernel-fixtures');
$fixturesDir = realpath(TestCase::WEB_FIXTURES_DIR);
$overwriteDir = realpath(TestCase::KERNEL_FIXTURES_DIR);

require_once $fixturesDir . '/utils.php';

Expand Down Expand Up @@ -61,7 +62,7 @@ private function prepareSession(Request $request): void
$cookies = $request->cookies;

if ($cookies->has($session->getName())) {
$session->setId($cookies->get($session->getName()));
$session->setId((string) $cookies->getString($session->getName()));
uuf6429 marked this conversation as resolved.
Show resolved Hide resolved
} else {
$session->migrate(false);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract public function createDriver();
*/
public function mapRemoteFilePath($file)
{
if (!isset($_SERVER['TEST_MACHINE_BASE_PATH']) || !isset($_SERVER['DRIVER_MACHINE_BASE_PATH'])) {
if (!isset($_SERVER['TEST_MACHINE_BASE_PATH'], $_SERVER['DRIVER_MACHINE_BASE_PATH'])) {
return $file;
}

Expand Down
9 changes: 5 additions & 4 deletions tests/Basic/BasicAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public function testSetBasicAuth(string $user, string $pass, string $pageText):
$this->assertStringContainsString($pageText, $session->getPage()->getContent());
}

/**
* @return iterable<string, array{string, string, string}>
aik099 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function setBasicAuthDataProvider(): iterable
{
return [
['mink-user', 'mink-password', 'is authenticated'],
['', '', 'is not authenticated'],
];
yield 'valid credentials' => ['mink-user', 'mink-password', 'is authenticated'];
yield 'no credentials' => ['', '', 'is not authenticated'];
}

public function testBasicAuthInUrl(): void
Expand Down
15 changes: 9 additions & 6 deletions tests/Basic/BestPracticesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function testImplementFindXpath(): void
{
$driver = $this->createDriver();

$this->assertNotImplementMethod('find', $driver, 'The driver should overwrite `findElementXpaths` rather than `find` for forward compatibility with Mink 2.');
$this->assertImplementMethod('findElementXpaths', $driver, 'The driver must be able to find elements.');
$this->assertNotImplementMethod('setSession', $driver, 'The driver should not deal with the Session directly for forward compatibility with Mink 2.');
$this->assertMethodIsNotImplemented('find', $driver, 'The driver should overwrite `findElementXpaths` rather than `find` for forward compatibility with Mink 2.');
$this->assertMethodIsImplemented('findElementXpaths', $driver, 'The driver must be able to find elements.');
$this->assertMethodIsNotImplemented('setSession', $driver, 'The driver should not deal with the Session directly for forward compatibility with Mink 2.');
}

/**
Expand All @@ -36,9 +36,12 @@ public function testImplementBasicApi(string $method): void
{
$driver = $this->createDriver();

$this->assertImplementMethod($method, $driver, 'The driver is unusable when this method is not implemented.');
$this->assertMethodIsImplemented($method, $driver, 'The driver is unusable when this method is not implemented.');
}

/**
* @return iterable<array{string}>
*/
public static function provideRequiredMethods(): iterable
{
return [
Expand All @@ -53,7 +56,7 @@ public static function provideRequiredMethods(): iterable
];
}

private function assertImplementMethod(string $method, object $object, string $reason = ''): void
private function assertMethodIsImplemented(string $method, object $object, string $reason = ''): void
aik099 marked this conversation as resolved.
Show resolved Hide resolved
{
$ref = new \ReflectionClass(get_class($object));
$refMethod = $ref->getMethod($method);
Expand All @@ -67,7 +70,7 @@ private function assertImplementMethod(string $method, object $object, string $r
$this->assertNotSame(CoreDriver::class, $refMethod->getDeclaringClass()->name, $message);
}

private function assertNotImplementMethod(string $method, object $object, string $reason = ''): void
private function assertMethodIsNotImplemented(string $method, object $object, string $reason = ''): void
{
$ref = new \ReflectionClass(get_class($object));
$refMethod = $ref->getMethod($method);
Expand Down
3 changes: 3 additions & 0 deletions tests/Basic/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function testGetAttribute(string $attributeName, ?string $attributeValue)
$this->assertSame($attributeValue, $element->getAttribute($attributeName));
}

/**
* @return iterable<array{string, mixed}>
*/
public static function getAttributeDataProvider(): iterable
{
return [
Expand Down
7 changes: 7 additions & 0 deletions tests/Basic/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Behat\Mink\Tests\Driver\TestCase;

/**
* @phpstan-type TCookieRemovalMode 'session_reset'|'cookie_delete'
*/
final class CookieTest extends TestCase
{
/**
Expand Down Expand Up @@ -87,6 +90,9 @@ public function testCookieWithPaths(string $cookieRemovalMode): void
$this->assertStringContainsString('Previous cookie: NO', $session->getPage()->getText());
}

/**
* @phpstan-return iterable<array{TCookieRemovalMode}>
*/
public static function cookieWithPathsDataProvider(): iterable
{
return [
Expand All @@ -96,6 +102,7 @@ public static function cookieWithPathsDataProvider(): iterable
}

/**
* @phpstan-param TCookieRemovalMode $cookieRemovalMode
* @dataProvider cookieWithPathsDataProvider
*/
public function testCookieInSubPath(string $cookieRemovalMode): void
Expand Down
10 changes: 4 additions & 6 deletions tests/Basic/IFrameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ public function testIFrame(string $iframeIdentifier, string $elementSelector, st
}

/**
* @return array
* @return iterable<string, array{string, string, string}>
*/
public static function iFrameDataProvider()
public static function iFrameDataProvider(): iterable
{
return array(
'by name' => array('subframe_by_name', '#text', 'iFrame div text'),
'by id' => array('subframe_by_id', '#foobar', 'Some accentués characters'),
);
yield 'by name' => ['subframe_by_name', '#text', 'iFrame div text'];
yield 'by id' => ['subframe_by_id', '#foobar', 'Some accentués characters'];
}
}
20 changes: 12 additions & 8 deletions tests/Form/GeneralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ public function testFormSubmitWays(string $submitVia): void
}
}

/**
* @return iterable<array{string}>
*/
public static function formSubmitWaysDataProvider(): iterable
{
return [
['Save'],
['input-type-image'],
['button-without-type'],
['button-type-submit'],
];
yield ['Save'];
yield ['input-type-image'];
yield ['button-without-type'];
yield ['button-type-submit'];
}

public function testFormSubmit(): void
Expand Down Expand Up @@ -189,7 +190,7 @@ public function testAdvancedForm(): void
$notes->setValue('new notes');
$this->assertEquals('new notes', $notes->getValue());

$about->attachFile($this->mapRemoteFilePath(__DIR__ . '/../../web-fixtures/some_file.txt'));
$about->attachFile($this->mapRemoteFilePath(self::WEB_FIXTURES_DIR . '/some_file.txt'));

$button = $page->findButton('Register');
$this->assertNotNull($button);
Expand Down Expand Up @@ -352,7 +353,7 @@ public function testSubmitEmptyTextarea(): void
/**
* @dataProvider provideInvalidValues
*
* @param mixed $value
* @param array<array-key, mixed>|bool|string $value
*/
public function testSetInvalidValueInField(string $field, $value): void
{
Expand All @@ -366,6 +367,9 @@ public function testSetInvalidValueInField(string $field, $value): void
$color->setValue($value);
}

/**
* @return iterable<string, array{string, mixed}>
*/
public static function provideInvalidValues(): iterable
{
$trueValue = ['true', true];
Expand Down
5 changes: 4 additions & 1 deletion tests/Form/Html5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function testHtml5FormMethod(): void
/**
* @dataProvider provideInvalidValues
*
* @param mixed $value
* @param array<array-key, mixed>|bool|string $value
*/
public function testSetInvalidValueInField(string $field, $value): void
{
Expand All @@ -177,6 +177,9 @@ public function testSetInvalidValueInField(string $field, $value): void
$color->setValue($value);
}

/**
* @return iterable<string, array{string, mixed}>
*/
public static function provideInvalidValues(): iterable
{
$trueValue = ['true', true];
Expand Down
3 changes: 3 additions & 0 deletions tests/Form/RadioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public function testSetBooleanValue(bool $value): void
$option->setValue($value);
}

/**
* @return iterable<array{bool}>
*/
public static function provideBooleanValues(): iterable
{
yield [true];
Expand Down
6 changes: 6 additions & 0 deletions tests/Form/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public function testElementSelectedStateCheck(string $selectName, string $option
$this->assertTrue($option->isSelected());
}

/**
* @return iterable<array{string, string, string}>
*/
public static function elementSelectedStateCheckDataProvider(): iterable
{
return [
Expand Down Expand Up @@ -119,6 +122,9 @@ public function testSetBooleanValue(bool $value): void
$select->setValue($value);
}

/**
* @return iterable<array{bool}>
*/
public static function provideBooleanValues(): iterable
{
yield [true];
Expand Down
40 changes: 20 additions & 20 deletions tests/Js/ChangeEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,18 @@ public function testSetValueChangeEvent(string $elementId, string $valueForEmpty
}
}

/**
* @return iterable<string, array{string, string, string}>
*/
public static function setValueChangeEventDataProvider(): iterable
{
$file1 = __DIR__ . '/../../web-fixtures/file1.txt';
$file2 = __DIR__ . '/../../web-fixtures/file2.txt';

return [
'input default' => ['the-input-default', 'from empty', 'from existing'],
'input text' => ['the-input-text', 'from empty', 'from existing'],
'input email' => ['the-email', 'from empty', 'from existing'],
'textarea' => ['the-textarea', 'from empty', 'from existing'],
'file' => ['the-file', $file1, $file2],
'select' => ['the-select', '30'],
'radio' => ['the-radio-m', 'm'],
];
yield 'input default' => ['the-input-default', 'from empty', 'from existing'];
yield 'input text' => ['the-input-text', 'from empty', 'from existing'];
yield 'input email' => ['the-email', 'from empty', 'from existing'];
yield 'textarea' => ['the-textarea', 'from empty', 'from existing'];
yield 'file' => ['the-file', self::WEB_FIXTURES_DIR . '/file1.txt', self::WEB_FIXTURES_DIR . '/file2.txt'];
yield 'select' => ['the-select', '30', ''];
yield 'radio' => ['the-radio-m', 'm', ''];
}

/**
Expand All @@ -95,12 +93,13 @@ public function testSelectOptionChangeEvent(string $elementId, string $elementVa
$this->assertElementChangeCount($elementId);
}

/**
* @return iterable<string, array{string, string}>
*/
public static function selectOptionChangeEventDataProvider(): iterable
{
return [
'select' => ['the-select', '30'],
'radio' => ['the-radio-m', 'm'],
];
yield 'select' => ['the-select', '30'];
yield 'radio' => ['the-radio-m', 'm'];
}

/**
Expand Down Expand Up @@ -145,12 +144,13 @@ public function testUncheckChangeEvent(bool $useSetValue): void
$this->assertElementChangeCount('the-checked-checkbox');
}

/**
* @return iterable<array{mixed}>
*/
public static function checkboxTestWayDataProvider(): iterable
{
return [
[true],
[false],
];
yield [true];
yield [false];
}

private function assertElementChangeCount(string $elementId, string $message = ''): void
Expand Down
17 changes: 9 additions & 8 deletions tests/Js/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ public function testKeyboardEvents(?string $modifier, string $eventProperties):
$this->assertEquals('key upped:78 / ' . $eventProperties, $event->getText());
}

/**
* @return iterable<string, array{KeyModifier::*|null, string}>
*/
public static function provideKeyboardEventsModifiers(): iterable
{
return [
'none' => [null, '0 / 0 / 0 / 0'],
'alt' => [KeyModifier::ALT, '1 / 0 / 0 / 0'],
// jQuery considers ctrl as being a metaKey in the normalized event
'ctrl' => [KeyModifier::CTRL, '0 / 1 / 0 / 1'],
'shift' => [KeyModifier::SHIFT, '0 / 0 / 1 / 0'],
'meta' => [KeyModifier::META, '0 / 0 / 0 / 1'],
];
yield 'none' => [null, '0 / 0 / 0 / 0'];
yield 'alt' => [KeyModifier::ALT, '1 / 0 / 0 / 0'];
// jQuery considers ctrl as being a metaKey in the normalized event
yield 'ctrl' => [KeyModifier::CTRL, '0 / 1 / 0 / 1'];
yield 'shift' => [KeyModifier::SHIFT, '0 / 0 / 1 / 0'];
yield 'meta' => [KeyModifier::META, '0 / 0 / 0 / 1'];
}
}
Loading
Loading