Added the support of Goutte 2.0

This commit is contained in:
Christophe Coevoet
2014-05-15 21:56:36 +02:00
parent 2f1c8f8b16
commit 652f0e22a5

View File

@@ -48,14 +48,10 @@ class GoutteFactory implements DriverFactory
->arrayNode('guzzle_parameters') ->arrayNode('guzzle_parameters')
->useAttributeAsKey('key') ->useAttributeAsKey('key')
->prototype('variable')->end() ->prototype('variable')->end()
->validate() ->info(
->always() "For Goutte 1.x, these are the second argument of the Guzzle3 client constructor.\n".
->then(function ($v) { 'For Goutte 2.x, these are the elements passed in the "defaults" key of the Guzzle4 config.'
$v['redirect.disable'] = true; )
return $v;
})
->end()
->end() ->end()
->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( $clientDefinition = new Definition('Behat\Mink\Driver\Goutte\Client', array(
$config['server_parameters'], $config['server_parameters'],
)); ));
$clientDefinition->addMethodCall('setClient', array( $clientDefinition->addMethodCall('setClient', array($guzzleClient));
new Definition('Guzzle\Http\Client', array(
null,
$config['guzzle_parameters'],
)),
));
return new Definition('Behat\Mink\Driver\GoutteDriver', array( return new Definition('Behat\Mink\Driver\GoutteDriver', array(
$clientDefinition, $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;
}
} }