Skip to content

Commit e72408f

Browse files
authored
Merge pull request #225 from dunglas/fix-deprecations
Fix remaining deprecations
2 parents b2d4545 + 1c75dde commit e72408f

File tree

8 files changed

+95
-12
lines changed

8 files changed

+95
-12
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ install:
3030
fi
3131

3232
script:
33-
- vendor/bin/phpunit
33+
- vendor/bin/simple-phpunit
3434
- if [[ $lint = '1' ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
3535
- if [[ $lint = '1' ]]; then php phpstan.phar analyse src tests --level=6; fi

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Allow to override the `APP_ENV` environment variable passed to the web server by setting `PANTHER_APP_ENV`
88
* Fix using assertions with a client created through `PantherTestCase::createClient()`
99
* Don't call `PantherTestCase::getClient()` if this method isn't `static`
10+
* Fix remaining deprecations
1011

1112
0.5.0
1213
-----

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"php": ">=7.1",
2121
"facebook/webdriver": "^1.7.1",
2222
"symfony/browser-kit": "^4.3",
23+
"symfony/dom-crawler": "^4.3",
2324
"symfony/http-client": "^4.3",
2425
"symfony/polyfill-php72": "^1.9",
2526
"symfony/process": "^3.4 || ^4.0"
@@ -39,10 +40,10 @@
3940
"sort-packages": true
4041
},
4142
"require-dev": {
42-
"guzzlehttp/guzzle": "^6.3",
4343
"fabpot/goutte": "^3.2.3",
44-
"phpunit/phpunit": "^7.3",
44+
"guzzlehttp/guzzle": "^6.3",
4545
"symfony/css-selector": "^3.4 || ^4.0",
46-
"symfony/framework-bundle": "^3.4 || ^4.0"
46+
"symfony/framework-bundle": "^3.4 || ^4.0",
47+
"symfony/phpunit-bridge": "^4.3.3"
4748
}
4849
}

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
parameters:
2+
autoload_files:
3+
- vendor/bin/.phpunit/phpunit-7.4/vendor/autoload.php
24
inferPrivatePropertyTypeFromConstructor: true
35
ignoreErrors:
46
# https://github.com/symfony/symfony/pull/33289
@@ -11,6 +13,8 @@ parameters:
1113
- '#Call to private static method getClient\(\) of class Symfony\\Bundle\\FrameworkBundle\\Test\\WebTestCase\.#'
1214
- '#Call to an undefined static method Symfony\\Component\\Panther\\PantherTestCase::baseAssertPageTitleContains\(\)\.#'
1315
- '#Call to an undefined static method Symfony\\Component\\Panther\\PantherTestCase::baseAssertPageTitleSame\(\)\.#'
16+
- '#Call to function method_exists\(\) with #'
17+
- '#Call to an undefined static method Symfony\\Component\\Panther\\PantherTestCase::assertStringContainsString\(\)\.#'
1418
# To fix in PHP WebDriver
1519
- '#Parameter \#2 \$desired_capabilities of static method Facebook\\WebDriver\\Remote\\RemoteWebDriver::create\(\) expects array\|Facebook\\WebDriver\\Remote\\DesiredCapabilities\|null, Facebook\\WebDriver\\WebDriverCapabilities given\.#'
1620
# Require a redesign of the underlying Symfony components

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<php>
1212
<env name="KERNEL_CLASS" value="Symfony\Component\Panther\Tests\DummyKernel" />
13+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[total]=1" />
1314
</php>
1415

1516
<testsuites>

src/DomCrawler/Crawler.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Facebook\WebDriver\WebDriver;
1818
use Facebook\WebDriver\WebDriverBy;
1919
use Facebook\WebDriver\WebDriverElement;
20+
use Symfony\Component\CssSelector\CssSelectorConverter;
2021
use Symfony\Component\DomCrawler\Crawler as BaseCrawler;
2122
use Symfony\Component\Panther\ExceptionThrower;
2223

@@ -154,9 +155,15 @@ public function parents()
154155
/**
155156
* @see https://github.com/symfony/symfony/issues/26432
156157
*/
157-
public function children()
158+
public function children(string $selector = null)
158159
{
159-
return $this->createSubCrawlerFromXpath('child::*');
160+
$xpath = 'child::*';
161+
if (null !== $selector) {
162+
$converter = $this->createCssSelectorConverter();
163+
$xpath = $converter->toXPath($selector, 'child::');
164+
}
165+
166+
return $this->createSubCrawlerFromXpath($xpath);
160167
}
161168

162169
public function attr($attribute): string
@@ -174,16 +181,32 @@ public function nodeName(): string
174181
return $this->getElementOrThrow()->getTagName();
175182
}
176183

177-
public function text(): string
184+
public function text($default = null): string
178185
{
179-
return $this->getElementOrThrow()->getText();
186+
try {
187+
return $this->getElementOrThrow()->getText();
188+
} catch (\InvalidArgumentException $e) {
189+
if (null === $default) {
190+
throw $e;
191+
}
192+
193+
return (string) $default;
194+
}
180195
}
181196

182-
public function html(): string
197+
public function html($default = null): string
183198
{
184-
$this->getElementOrThrow();
199+
try {
200+
$this->getElementOrThrow();
201+
202+
return $this->attr('outerHTML');
203+
} catch (\InvalidArgumentException $e) {
204+
if (null === $default) {
205+
throw $e;
206+
}
185207

186-
return $this->attr('outerHTML');
208+
return (string) $default;
209+
}
187210
}
188211

189212
public function evaluate($xpath): self
@@ -453,4 +476,16 @@ public function findElements(WebDriverBy $locator)
453476
{
454477
return $this->getElementOrThrow()->findElements($locator);
455478
}
479+
480+
/**
481+
* @throws \LogicException If the CssSelector Component is not available
482+
*/
483+
private function createCssSelectorConverter(): CssSelectorConverter
484+
{
485+
if (!class_exists(CssSelectorConverter::class)) {
486+
throw new \LogicException('To filter with a CSS selector, install the CssSelector component ("composer require symfony/css-selector"). Or use filterXpath instead.');
487+
}
488+
489+
return new CssSelectorConverter();
490+
}
456491
}

src/PantherTestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public static function assertPageTitleContains(string $expectedTitle, string $me
5757
{
5858
$client = self::getClient();
5959
if ($client instanceof PantherClient) {
60-
self::assertStringContainsString($expectedTitle, $client->getTitle());
60+
if (method_exists(self::class, 'assertStringContainsString')) {
61+
self::assertStringContainsString($expectedTitle, $client->getTitle());
62+
63+
return;
64+
}
65+
66+
self::assertContains($expectedTitle, $client->getTitle());
6167

6268
return;
6369
}

tests/DomCrawler/CrawlerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ public function testChildren(callable $clientFactory): void
197197
$this->assertSame(['h1', 'main', 'p', 'p', 'input'], $names);
198198
}
199199

200+
/**
201+
* @dataProvider clientFactoryProvider
202+
*
203+
* @param mixed $clientFactory
204+
*/
205+
public function testChildrenFilter($clientFactory): void
206+
{
207+
$crawler = $this->request($clientFactory, '/basic.html');
208+
209+
$names = [];
210+
$crawler->filter('body')->children('p')->each(function (Crawler $c, int $i) use (&$names) {
211+
$names[$i] = $c->nodeName();
212+
});
213+
214+
$this->assertSame(['p', 'p'], $names);
215+
}
216+
200217
/**
201218
* @dataProvider clientFactoryProvider
202219
*/
@@ -279,4 +296,22 @@ public function testImage(callable $clientFactory, string $type): void
279296
$this->assertSame('GET', $image->getMethod());
280297
$this->assertSame('https://api-platform.com/logo-250x250.png', $image->getUri());
281298
}
299+
300+
/**
301+
* @dataProvider clientFactoryProvider
302+
*/
303+
public function testTextDefault(callable $clientFactory): void
304+
{
305+
$crawler = $this->request($clientFactory, '/basic.html');
306+
$this->assertSame('default', $crawler->filter('header')->text('default'));
307+
}
308+
309+
/**
310+
* @dataProvider clientFactoryProvider
311+
*/
312+
public function testHtmlDefault(callable $clientFactory): void
313+
{
314+
$crawler = $this->request($clientFactory, '/basic.html');
315+
$this->assertSame('default', $crawler->filter('header')->html('default'));
316+
}
282317
}

0 commit comments

Comments
 (0)