Skip to content

Commit

Permalink
Merge pull request #68 from stof/extra_tests
Browse files Browse the repository at this point in the history
Added extra unit tests in the driver
  • Loading branch information
stof committed May 2, 2014
2 parents 1ac49bc + 02eec04 commit 31c4e38
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 4 deletions.
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
*/
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;
}
}

0 comments on commit 31c4e38

Please sign in to comment.