Skip to content

Commit

Permalink
Fix the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed Jan 15, 2020
1 parent 88bfed6 commit 8a7611e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
/phpunit.xml
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},

"require-dev": {
"mink/driver-testsuite": "dev-master",
"friends-of-behat/mink-driver-testsuite": "dev-master",
"symfony/http-kernel": "^4.4|^5.0"
},

Expand Down
20 changes: 12 additions & 8 deletions src/BrowserKitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Behat\Mink\Exception\DriverException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\Exception\BadMethodCallException;
use Symfony\Component\BrowserKit\Response;
Expand All @@ -23,7 +23,7 @@
use Symfony\Component\DomCrawler\Field\InputFormField;
use Symfony\Component\DomCrawler\Field\TextareaFormField;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\HttpKernel\Client as HttpKernelClient;
use Symfony\Component\HttpKernel\HttpKernelBrowser;

/**
* Symfony2 BrowserKit driver.
Expand All @@ -46,23 +46,23 @@ class BrowserKitDriver extends CoreDriver
/**
* Initializes BrowserKit driver.
*
* @param Client $client BrowserKit client instance
* @param AbstractBrowser $client BrowserKit client instance
* @param string|null $baseUrl Base URL for HttpKernel clients
*/
public function __construct(Client $client, $baseUrl = null)
public function __construct(AbstractBrowser $client, $baseUrl = null)
{
$this->client = $client;
$this->client->followRedirects(true);

if ($baseUrl !== null && $client instanceof HttpKernelClient) {
if ($baseUrl !== null && $client instanceof HttpKernelBrowser) {
$client->setServerParameter('SCRIPT_FILENAME', parse_url($baseUrl, PHP_URL_PATH));
}
}

/**
* Returns BrowserKit HTTP client instance.
*
* @return Client
* @return AbstractBrowser
*/
public function getClient()
{
Expand Down Expand Up @@ -874,9 +874,13 @@ private function getFilteredCrawler($xpath)
*/
private function getCrawler()
{
$crawler = $this->client->getCrawler();
try {
$crawler = $this->client->getCrawler();

if (null === $crawler) {
if (null === $crawler) {
throw new DriverException('Unable to access the response content before visiting a page');
}
} catch (BadMethodCallException $exception) {
throw new DriverException('Unable to access the response content before visiting a page');
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Custom/BaseUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Behat\Mink\Session;
use Behat\Mink\Tests\Driver\Util\FixturesKernel;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Client;
use Symfony\Component\HttpKernel\HttpKernelBrowser;

/**
* @group functional
Expand All @@ -15,7 +15,7 @@ class BaseUrlTest extends TestCase
{
public function testBaseUrl()
{
$client = new Client(new FixturesKernel());
$client = new HttpKernelBrowser(new FixturesKernel());
$driver = new BrowserKitDriver($client, 'http://localhost/foo/');
$session = new Session($driver);

Expand Down
47 changes: 22 additions & 25 deletions tests/Custom/ErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Behat\Mink\Tests\Driver\Custom;

use Behat\Mink\Driver\BrowserKitDriver;
use Behat\Mink\Exception\DriverException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\BrowserKit\Client;
Expand All @@ -26,50 +27,49 @@ public function testGetClient()
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Unable to access the response before visiting a page
*
* Looks like we have to mark these tests as "legacy", otherwise we get deprecation errors.
* Although the deprecations are handled, there's no way to avoid the deprecation message here.
* @group legacy
*/
public function testGetResponseHeaderWithoutVisit()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('Unable to access the response before visiting a page');

$this->getDriver()->getResponseHeaders();
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Unable to access the response content before visiting a page
*
* Looks like we have to mark these tests as "legacy", otherwise we get deprecation errors.
* Although the deprecations are handled, there's no way to avoid the deprecation message here.
* @group legacy
*/
public function testFindWithoutVisit()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('Unable to access the response content before visiting a page');

$this->getDriver()->find('//html');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Unable to access the request before visiting a page
*
* Looks like we have to mark these tests as "legacy", otherwise we get deprecation errors.
* Although the deprecations are handled, there's no way to avoid the deprecation message here.
* @group legacy
*/
public function testGetCurrentUrlWithoutVisit()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('Unable to access the request before visiting a page');

$this->getDriver()->getCurrentUrl();
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage The selected node has an invalid form attribute (foo)
*/
public function testNotMatchingHtml5FormId()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('The selected node has an invalid form attribute (foo)');

$html = <<<'HTML'
<html>
<body>
Expand All @@ -88,12 +88,11 @@ public function testNotMatchingHtml5FormId()
$driver->setValue('//input[./@name="test"]', 'bar');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage The selected node has an invalid form attribute (foo)
*/
public function testInvalidHtml5FormId()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('The selected node has an invalid form attribute (foo)');

$html = <<<'HTML'
<html>
<body>
Expand All @@ -113,12 +112,11 @@ public function testInvalidHtml5FormId()
$driver->setValue('//input[./@name="test"]', 'bar');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage The selected node does not have a form ancestor.
*/
public function testManipulateInputWithoutForm()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('The selected node does not have a form ancestor.');

$html = <<<'HTML'
<html>
<body>
Expand All @@ -139,12 +137,11 @@ public function testManipulateInputWithoutForm()
$driver->setValue('//input[./@name="test"]', 'bar');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Behat\Mink\Driver\BrowserKitDriver supports clicking on links and submit or reset buttons only. But "div" provided
*/
public function testClickOnUnsupportedElement()
{
$this->expectException(DriverException::class);
$this->expectExceptionMessage('Behat\Mink\Driver\BrowserKitDriver supports clicking on links and submit or reset buttons only. But "div" provided');

$html = <<<'HTML'
<html>
<body>
Expand Down

0 comments on commit 8a7611e

Please sign in to comment.