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

Added extra unit tests in the driver #68

Merged
merged 1 commit into from
May 2, 2014
Merged
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
9 changes: 6 additions & 3 deletions src/Behat/Mink/Driver/BrowserKitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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;
Expand Down Expand Up @@ -489,8 +490,9 @@ public function click($xpath)
} elseif ($this->canResetForm($crawlerNode)) {
$this->resetForm($crawlerNode);
} else {
$message = 'BrowserKit driver supports clicking on links and buttons only. But "%s" provided';
throw new DriverException(sprintf($message, $tagName));
$message = sprintf('%%s supports clicking on links and buttons only. But "%s" provided', $tagName);

throw new UnsupportedDriverActionException($message, $this);
}
}

Expand Down Expand Up @@ -690,7 +692,7 @@ private function getFormNode(\DOMElement $element)
$formId = $element->getAttribute('form');
$formNode = $element->ownerDocument->getElementById($formId);

if (null === $formNode) {
if (null === $formNode || 'form' !== $formNode->nodeName) {
throw new DriverException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
}

Expand Down Expand Up @@ -728,6 +730,7 @@ private function getFieldPosition(\DOMElement $fieldNode)
// more than one element contains this name !
// so we need to find the position of $fieldNode
foreach ($elements as $key => $element) {
/** @var \DOMElement $element */
if ($element->getNodePath() === $fieldNode->getNodePath()) {
return $key;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Behat/Mink/Driver/BrowserKitDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\HttpKernel\Client;

/**
* @group browserkitdriver
* @group functional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw you did the opposite change for the MinkGoutteDriver. Technically all drivers tests are functional ones, but all Mink tests are unit tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. I did exactly the same change: https://github.com/Behat/MinkGoutteDriver/blob/master/tests/Behat/Mink/Driver/GoutteDriverTest.php#L8

Having a different group per driver is a left-over from Mink 1.3 when the drivers were in the main repo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

*/
class BrowserKitDriverTest extends GeneralDriverTest
{
Expand Down
181 changes: 181 additions & 0 deletions tests/Behat/Mink/Driver/ExtraDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace Tests\Behat\Mink\Driver;

use Behat\Mink\Driver\BrowserKitDriver;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Response;

class ExtraDriverTest extends \PHPUnit_Framework_TestCase
{
/**
* @var TestClient
*/
private $client;

protected function setUp()
{
$this->client = new TestClient();
}

public function testGetClient()
{
$this->assertSame($this->client, $this->getDriver()->getClient());
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Unable to access the response before visiting a page
*/
public function testGetResponseHeaderWithoutVisit()
{
$this->getDriver()->getResponseHeaders();
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Unable to access the response content before visiting a page
*/
public function testFindWithoutVisit()
{
$this->getDriver()->find('//html');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Unable to access the request before visiting a page
*/
public function testGetCurrentUrlWithoutVisit()
{
$this->getDriver()->getCurrentUrl();
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage The selected node has an invalid form attribute (foo)
*/
public function testNotMatchingHtml5FormId()
{
$html = <<<'HTML'
<html>
<body>
<form id="test">
<input name="test" value="foo" form="foo">
<input type="submit">
</form>
</body>
</html>
HTML;

$this->client->setNextResponse(new Response($html));

$driver = $this->getDriver();
$driver->visit('/index.php');
$driver->setValue('//input[./@name="test"]', 'bar');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage The selected node has an invalid form attribute (foo)
*/
public function testInvalidHtml5FormId()
{
$html = <<<'HTML'
<html>
<body>
<form id="test">
<input name="test" value="foo" form="foo">
<input type="submit">
</form>
<div id="foo"></div>
</body>
</html>
HTML;

$this->client->setNextResponse(new Response($html));

$driver = $this->getDriver();
$driver->visit('/index.php');
$driver->setValue('//input[./@name="test"]', 'bar');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage The selected node does not have a form ancestor.
*/
public function testManipulateInputWithoutForm()
{
$html = <<<'HTML'
<html>
<body>
<form id="test">
<input type="submit">
</form>
<div id="foo">
<input name="test" value="foo">
</div>
</body>
</html>
HTML;

$this->client->setNextResponse(new Response($html));

$driver = $this->getDriver();
$driver->visit('/index.php');
$driver->setValue('//input[./@name="test"]', 'bar');
}

/**
* @expectedException \Behat\Mink\Exception\DriverException
* @expectedExceptionMessage Behat\Mink\Driver\BrowserKitDriver supports clicking on links and buttons only. But "div" provided
*/
public function testClickOnUnsupportedElement()
{
$html = <<<'HTML'
<html>
<body>
<div></div>
</body>
</html>
HTML;

$this->client->setNextResponse(new Response($html));

$driver = $this->getDriver();
$driver->visit('/index.php');
$driver->click('//div');
}

private function getDriver()
{
return new BrowserKitDriver($this->client);
}
}

class TestClient extends Client
{
protected $nextResponse = null;
protected $nextScript = null;

public function setNextResponse(Response $response)
{
$this->nextResponse = $response;
}

public function setNextScript($script)
{
$this->nextScript = $script;
}

protected function doRequest($request)
{
if (null === $this->nextResponse) {
return new Response();
}

$response = $this->nextResponse;
$this->nextResponse = null;

return $response;
}
}