Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
Upgrade to BrowserKitDriver v2 and Goutte 4
Browse files Browse the repository at this point in the history
Goutte 4 is a shortcut for using the Symfony components and does not
need custom logic anymore.
  • Loading branch information
stof committed Dec 17, 2021
1 parent 220b696 commit 2605e73
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 99 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require "vendor/autoload.php";
use Behat\Mink\Mink,
Behat\Mink\Session,
Behat\Mink\Driver\GoutteDriver,
Behat\Mink\Driver\Goutte\Client as GoutteClient;
Goutte\Client as GoutteClient;

$mink = new Mink(array(
'goutte' => new Session(new GoutteDriver(new GoutteClient())),
Expand All @@ -39,8 +39,8 @@ Add a file composer.json with content:
``` json
{
"require": {
"behat/mink": "~1.5",
"behat/mink-goutte-driver": "~1.0"
"behat/mink": "^1.9",
"behat/mink-goutte-driver": "^2.0"
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

"require": {
"php": ">=7.2",
"behat/mink-browserkit-driver": "~1.2@dev",
"fabpot/goutte": "~1.0.4|~2.0|~3.1"
"behat/mink-browserkit-driver": "^2.0@dev",
"fabpot/goutte": "^4.0"
},

"require-dev": {
Expand Down
23 changes: 0 additions & 23 deletions src/Goutte/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,10 @@
namespace Behat\Mink\Driver\Goutte;

use Goutte\Client as BaseClient;
use Symfony\Component\BrowserKit\Response;

/**
* Client overrides to support Mink functionality.
*/
class Client extends BaseClient
{
/**
* Reads response meta tags to guess content-type charset.
*
* @param Response $response
*
* @return Response
*/
protected function filterResponse($response)
{
$contentType = $response->getHeader('Content-Type');

if (!$contentType || false === strpos($contentType, 'charset=')) {
if (preg_match('/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9]+)/i', $response->getContent(), $matches)) {
$headers = $response->getHeaders();
$headers['Content-Type'] = $contentType.';charset='.$matches[1];

$response = new Response($response->getContent(), $response->getStatus(), $headers);
}
}

return parent::filterResponse($response);
}
}
53 changes: 1 addition & 52 deletions src/GoutteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace Behat\Mink\Driver;

use Behat\Mink\Driver\Goutte\Client as ExtendedClient;
use Goutte\Client;

/**
Expand All @@ -27,56 +26,6 @@ class GoutteDriver extends BrowserKitDriver
*/
public function __construct(Client $client = null)
{
parent::__construct($client ?: new ExtendedClient());
}

/**
* {@inheritdoc}
*/
public function setBasicAuth($user, $password)
{
if (false === $user) {
$this->getClient()->resetAuth();

return;
}

$this->getClient()->setAuth($user, $password);
}

/**
* Gets the Goutte client.
*
* The method is overwritten only to provide the appropriate return type hint.
*
* @return Client
*/
public function getClient()
{
return parent::getClient();
}

/**
* {@inheritdoc}
*/
public function reset()
{
parent::reset();
$this->getClient()->resetAuth();
}

/**
* {@inheritdoc}
*/
protected function prepareUrl($url)
{
$parts = parse_url($url);
if (isset($parts['user']) || isset($parts['pass'])) {
$this->setBasicAuth(
isset($parts['user']) ? $parts['user'] : '',
isset($parts['pass']) ? $parts['pass'] : ''
);
}
return $url;
parent::__construct($client ?? new Client());
}
}
3 changes: 2 additions & 1 deletion tests/Custom/InstantiationTest.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\GoutteDriver;
use Goutte\Client;
use PHPUnit\Framework\TestCase;

class InstantiationTest extends TestCase
Expand All @@ -23,6 +24,6 @@ public function testInstantiateWithoutClient()
{
$driver = new GoutteDriver();

$this->assertInstanceOf('Behat\Mink\Driver\Goutte\Client', $driver->getClient());
$this->assertInstanceOf(Client::class, $driver->getClient());
}
}
18 changes: 0 additions & 18 deletions tests/GoutteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,4 @@ protected function supportsJs()
{
return false;
}

public function skipMessage($testCase, $test)
{
if (
'Behat\Mink\Tests\Driver\Form\Html5Test' === $testCase
&& in_array($test, array(
'testHtml5FormAction',
'testHtml5FormMethod',
))
&& !method_exists('Symfony\Component\DomCrawler\Tests\FormTest', 'testGetMethodWithOverride')
// Symfony 4.4 removed tests from dist archives, making the previous detection return a false-positive
&& !method_exists('Symfony\Component\DomCrawler\Form', 'getName')
) {
return 'Mink Goutte doesn\'t support HTML5 form attributes before Symfony 3.3';
}

return parent::skipMessage($testCase, $test);
}
}

0 comments on commit 2605e73

Please sign in to comment.