Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Updated the driver for the Mink 2.0 API #44

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

"require": {
"php": ">=5.3.1",
"behat/mink": "~1.6@dev",
"symfony/browser-kit": "~2.0",
"symfony/dom-crawler": "~2.0"
"behat/mink": "dev-2-architecture-changes",
"symfony/browser-kit": "~2.3",
"symfony/dom-crawler": "~2.3"
},

"require-dev": {
Expand Down
149 changes: 3 additions & 146 deletions src/Behat/Mink/Driver/BrowserKitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@

namespace Behat\Mink\Driver;

use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\DriverException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Session;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\Request;
use Symfony\Component\BrowserKit\Response;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
Expand All @@ -25,8 +22,6 @@
use Symfony\Component\DomCrawler\Field\InputFormField;
use Symfony\Component\DomCrawler\Field\TextareaFormField;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use Symfony\Component\HttpKernel\Client as HttpKernelClient;

/**
Expand All @@ -36,7 +31,6 @@
*/
class BrowserKitDriver extends CoreDriver
{
private $session;
private $client;

/**
Expand All @@ -45,8 +39,6 @@ class BrowserKitDriver extends CoreDriver
private $forms = array();
private $serverParameters = array();
private $started = false;
private $removeScriptFromUrl = false;
private $removeHostFromUrl = false;

/**
* Initializes BrowserKit driver.
Expand Down Expand Up @@ -74,46 +66,6 @@ public function getClient()
return $this->client;
}

/**
* {@inheritdoc}
*/
public function setSession(Session $session)
{
$this->session = $session;
}

/**
* Tells driver to remove hostname from URL.
*
* @param Boolean $remove
*
* @deprecated Deprecated as of 1.2, to be removed in 2.0. Pass the base url in the constructor instead.
*/
public function setRemoveHostFromUrl($remove = true)
{
trigger_error(
'setRemoveHostFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.',
E_USER_DEPRECATED
);
$this->removeHostFromUrl = (bool) $remove;
}

/**
* Tells driver to remove script name from URL.
*
* @param Boolean $remove
*
* @deprecated Deprecated as of 1.2, to be removed in 2.0. Pass the base url in the constructor instead.
*/
public function setRemoveScriptFromUrl($remove = true)
{
trigger_error(
'setRemoveScriptFromUrl() is deprecated as of 1.2 and will be removed in 2.0. Pass the base url in the constructor instead.',
E_USER_DEPRECATED
);
$this->removeScriptFromUrl = (bool) $remove;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -155,7 +107,7 @@ public function reset()
*/
public function visit($url)
{
$this->client->request('GET', $this->prepareUrl($url), array(), array(), $this->serverParameters);
$this->client->request('GET', $url, array(), array(), $this->serverParameters);
$this->forms = array();
}

Expand All @@ -164,19 +116,7 @@ public function visit($url)
*/
public function getCurrentUrl()
{
if (method_exists($this->client, 'getInternalRequest')) {
$request = $this->client->getInternalRequest();
} else {
// BC layer for BrowserKit 2.2.x and older
$request = $this->client->getRequest();

if (null !== $request && !$request instanceof Request && !$request instanceof HttpFoundationRequest) {
throw new DriverException(sprintf(
'The BrowserKit client returned an unsupported request implementation: %s. Please upgrade your BrowserKit package to 2.3 or newer.',
get_class($request)
));
}
}
$request = $this->client->getInternalRequest();

if ($request === null) {
throw new DriverException('Unable to access the request before visiting a page');
Expand Down Expand Up @@ -351,7 +291,7 @@ public function find($xpath)

$elements = array();
foreach ($nodes as $i => $node) {
$elements[] = new NodeElement(sprintf('(%s)[%d]', $xpath, $i + 1), $this->session);
$elements[] = sprintf('(%s)[%d]', $xpath, $i + 1);
}

return $elements;
Expand Down Expand Up @@ -564,16 +504,6 @@ public function submitForm($xpath)
*/
protected function getResponse()
{
if (!method_exists($this->client, 'getInternalResponse')) {
$implementationResponse = $this->client->getResponse();

if (null === $implementationResponse) {
throw new DriverException('Unable to access the response before visiting a page');
}

return $this->convertImplementationResponse($implementationResponse);
}

$response = $this->client->getInternalResponse();

if (null === $response) {
Expand All @@ -583,79 +513,6 @@ protected function getResponse()
return $response;
}

/**
* Gets the BrowserKit Response for legacy BrowserKit versions.
*
* Before 2.3.0, there was no Client::getInternalResponse method, and the
* return value of Client::getResponse can be anything when the implementation
* uses Client::filterResponse because of a bad choice done in BrowserKit and
* kept for BC reasons (the Client::getInternalResponse method has been added
* to solve it).
*
* This implementation supports client which don't rely Client::filterResponse
* and clients which use an HttpFoundation Response (like the HttpKernel client).
*
* @param object $response the response specific to the BrowserKit implementation
*
* @return Response
*
* @throws DriverException If the response cannot be converted to a BrowserKit response
*/
private function convertImplementationResponse($response)
{
if ($response instanceof Response) {
return $response;
}

// due to a bug, the HttpKernel client implementation returns the HttpFoundation response
// The conversion logic is copied from Symfony\Component\HttpKernel\Client::filterResponse
if ($response instanceof HttpFoundationResponse) {
$headers = $response->headers->all();
if ($response->headers->getCookies()) {
$cookies = array();
foreach ($response->headers->getCookies() as $cookie) {
$cookies[] = new Cookie(
$cookie->getName(),
$cookie->getValue(),
$cookie->getExpiresTime(),
$cookie->getPath(),
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttpOnly()
);
}
$headers['Set-Cookie'] = $cookies;
}

// this is needed to support StreamedResponse
ob_start();
$response->sendContent();
$content = ob_get_clean();

return new Response($content, $response->getStatusCode(), $headers);
}

throw new DriverException(sprintf(
'The BrowserKit client returned an unsupported response implementation: %s. Please upgrade your BrowserKit package to 2.3 or newer.',
get_class($response)
));
}

/**
* Prepares URL for visiting.
* Removes "*.php/" from urls and then passes it to BrowserKitDriver::visit().
*
* @param string $url
*
* @return string
*/
protected function prepareUrl($url)
{
$replacement = ($this->removeHostFromUrl ? '' : '$1') . ($this->removeScriptFromUrl ? '' : '$2');

return preg_replace('#(https?\://[^/]+)(/[^/\.]+\.php)?#', $replacement, $url);
}

/**
* Returns form field from XPath query.
*
Expand Down