From 652f0e22a58102a6362c4c945128c2a3d48df7d0 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Thu, 15 May 2014 21:56:36 +0200 Subject: [PATCH] Added the support of Goutte 2.0 --- .../ServiceContainer/Driver/GoutteFactory.php | 54 ++++++++++++++----- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/Behat/MinkExtension/ServiceContainer/Driver/GoutteFactory.php b/src/Behat/MinkExtension/ServiceContainer/Driver/GoutteFactory.php index e29b484..de774a6 100644 --- a/src/Behat/MinkExtension/ServiceContainer/Driver/GoutteFactory.php +++ b/src/Behat/MinkExtension/ServiceContainer/Driver/GoutteFactory.php @@ -48,14 +48,10 @@ class GoutteFactory implements DriverFactory ->arrayNode('guzzle_parameters') ->useAttributeAsKey('key') ->prototype('variable')->end() - ->validate() - ->always() - ->then(function ($v) { - $v['redirect.disable'] = true; - - return $v; - }) - ->end() + ->info( + "For Goutte 1.x, these are the second argument of the Guzzle3 client constructor.\n". + 'For Goutte 2.x, these are the elements passed in the "defaults" key of the Guzzle4 config.' + ) ->end() ->end() ; @@ -72,18 +68,48 @@ class GoutteFactory implements DriverFactory ); } + if ($this->isGoutte1()) { + $guzzleClient = $this->buildGuzzle3Client($config['guzzle_parameters']); + } else { + $guzzleClient = $this->buildGuzzle4Client($config['guzzle_parameters']); + } + $clientDefinition = new Definition('Behat\Mink\Driver\Goutte\Client', array( $config['server_parameters'], )); - $clientDefinition->addMethodCall('setClient', array( - new Definition('Guzzle\Http\Client', array( - null, - $config['guzzle_parameters'], - )), - )); + $clientDefinition->addMethodCall('setClient', array($guzzleClient)); return new Definition('Behat\Mink\Driver\GoutteDriver', array( $clientDefinition, )); } + + private function buildGuzzle4Client(array $parameters) + { + // Force the parameters set by default in Goutte to reproduce its behavior + $parameters['allow_redirects'] = false; + $parameters['cookies'] = true; + + return new Definition('GuzzleHttp\Client', array(array('defaults' => $parameters))); + + } + + private function buildGuzzle3Client(array $parameters) + { + // Force the parameters set by default in Goutte to reproduce its behavior + $parameters['redirect.disable'] = true; + + return new Definition('Guzzle\Http\Client', array(null, $parameters)); + } + + private function isGoutte1() + { + $refl = new \ReflectionParameter(array('Goutte\Client', 'setClient'), 0); + + if ($refl->getClass() && 'Guzzle\Http\ClientInterface' === $refl->getClass()->getName()) { + return true; + } + + return false; + } }