Skip to content

Commit 31c4e38

Browse files
committed
Merge pull request #68 from stof/extra_tests
Added extra unit tests in the driver
2 parents 1ac49bc + 02eec04 commit 31c4e38

3 files changed

Lines changed: 188 additions & 4 deletions

File tree

src/Behat/Mink/Driver/BrowserKitDriver.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Behat\Mink\Element\NodeElement;
1414
use Behat\Mink\Exception\DriverException;
15+
use Behat\Mink\Exception\UnsupportedDriverActionException;
1516
use Behat\Mink\Session;
1617
use Symfony\Component\BrowserKit\Client;
1718
use Symfony\Component\BrowserKit\Cookie;
@@ -489,8 +490,9 @@ public function click($xpath)
489490
} elseif ($this->canResetForm($crawlerNode)) {
490491
$this->resetForm($crawlerNode);
491492
} else {
492-
$message = 'BrowserKit driver supports clicking on links and buttons only. But "%s" provided';
493-
throw new DriverException(sprintf($message, $tagName));
493+
$message = sprintf('%%s supports clicking on links and buttons only. But "%s" provided', $tagName);
494+
495+
throw new UnsupportedDriverActionException($message, $this);
494496
}
495497
}
496498

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

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

@@ -728,6 +730,7 @@ private function getFieldPosition(\DOMElement $fieldNode)
728730
// more than one element contains this name !
729731
// so we need to find the position of $fieldNode
730732
foreach ($elements as $key => $element) {
733+
/** @var \DOMElement $element */
731734
if ($element->getNodePath() === $fieldNode->getNodePath()) {
732735
return $key;
733736
}

tests/Behat/Mink/Driver/BrowserKitDriverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Symfony\Component\HttpKernel\Client;
88

99
/**
10-
* @group browserkitdriver
10+
* @group functional
1111
*/
1212
class BrowserKitDriverTest extends GeneralDriverTest
1313
{
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
3+
namespace Tests\Behat\Mink\Driver;
4+
5+
use Behat\Mink\Driver\BrowserKitDriver;
6+
use Symfony\Component\BrowserKit\Client;
7+
use Symfony\Component\BrowserKit\Response;
8+
9+
class ExtraDriverTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var TestClient
13+
*/
14+
private $client;
15+
16+
protected function setUp()
17+
{
18+
$this->client = new TestClient();
19+
}
20+
21+
public function testGetClient()
22+
{
23+
$this->assertSame($this->client, $this->getDriver()->getClient());
24+
}
25+
26+
/**
27+
* @expectedException \Behat\Mink\Exception\DriverException
28+
* @expectedExceptionMessage Unable to access the response before visiting a page
29+
*/
30+
public function testGetResponseHeaderWithoutVisit()
31+
{
32+
$this->getDriver()->getResponseHeaders();
33+
}
34+
35+
/**
36+
* @expectedException \Behat\Mink\Exception\DriverException
37+
* @expectedExceptionMessage Unable to access the response content before visiting a page
38+
*/
39+
public function testFindWithoutVisit()
40+
{
41+
$this->getDriver()->find('//html');
42+
}
43+
44+
/**
45+
* @expectedException \Behat\Mink\Exception\DriverException
46+
* @expectedExceptionMessage Unable to access the request before visiting a page
47+
*/
48+
public function testGetCurrentUrlWithoutVisit()
49+
{
50+
$this->getDriver()->getCurrentUrl();
51+
}
52+
53+
/**
54+
* @expectedException \Behat\Mink\Exception\DriverException
55+
* @expectedExceptionMessage The selected node has an invalid form attribute (foo)
56+
*/
57+
public function testNotMatchingHtml5FormId()
58+
{
59+
$html = <<<'HTML'
60+
<html>
61+
<body>
62+
<form id="test">
63+
<input name="test" value="foo" form="foo">
64+
<input type="submit">
65+
</form>
66+
</body>
67+
</html>
68+
HTML;
69+
70+
$this->client->setNextResponse(new Response($html));
71+
72+
$driver = $this->getDriver();
73+
$driver->visit('/index.php');
74+
$driver->setValue('//input[./@name="test"]', 'bar');
75+
}
76+
77+
/**
78+
* @expectedException \Behat\Mink\Exception\DriverException
79+
* @expectedExceptionMessage The selected node has an invalid form attribute (foo)
80+
*/
81+
public function testInvalidHtml5FormId()
82+
{
83+
$html = <<<'HTML'
84+
<html>
85+
<body>
86+
<form id="test">
87+
<input name="test" value="foo" form="foo">
88+
<input type="submit">
89+
</form>
90+
<div id="foo"></div>
91+
</body>
92+
</html>
93+
HTML;
94+
95+
$this->client->setNextResponse(new Response($html));
96+
97+
$driver = $this->getDriver();
98+
$driver->visit('/index.php');
99+
$driver->setValue('//input[./@name="test"]', 'bar');
100+
}
101+
102+
/**
103+
* @expectedException \Behat\Mink\Exception\DriverException
104+
* @expectedExceptionMessage The selected node does not have a form ancestor.
105+
*/
106+
public function testManipulateInputWithoutForm()
107+
{
108+
$html = <<<'HTML'
109+
<html>
110+
<body>
111+
<form id="test">
112+
<input type="submit">
113+
</form>
114+
<div id="foo">
115+
<input name="test" value="foo">
116+
</div>
117+
</body>
118+
</html>
119+
HTML;
120+
121+
$this->client->setNextResponse(new Response($html));
122+
123+
$driver = $this->getDriver();
124+
$driver->visit('/index.php');
125+
$driver->setValue('//input[./@name="test"]', 'bar');
126+
}
127+
128+
/**
129+
* @expectedException \Behat\Mink\Exception\DriverException
130+
* @expectedExceptionMessage Behat\Mink\Driver\BrowserKitDriver supports clicking on links and buttons only. But "div" provided
131+
*/
132+
public function testClickOnUnsupportedElement()
133+
{
134+
$html = <<<'HTML'
135+
<html>
136+
<body>
137+
<div></div>
138+
</body>
139+
</html>
140+
HTML;
141+
142+
$this->client->setNextResponse(new Response($html));
143+
144+
$driver = $this->getDriver();
145+
$driver->visit('/index.php');
146+
$driver->click('//div');
147+
}
148+
149+
private function getDriver()
150+
{
151+
return new BrowserKitDriver($this->client);
152+
}
153+
}
154+
155+
class TestClient extends Client
156+
{
157+
protected $nextResponse = null;
158+
protected $nextScript = null;
159+
160+
public function setNextResponse(Response $response)
161+
{
162+
$this->nextResponse = $response;
163+
}
164+
165+
public function setNextScript($script)
166+
{
167+
$this->nextScript = $script;
168+
}
169+
170+
protected function doRequest($request)
171+
{
172+
if (null === $this->nextResponse) {
173+
return new Response();
174+
}
175+
176+
$response = $this->nextResponse;
177+
$this->nextResponse = null;
178+
179+
return $response;
180+
}
181+
}

0 commit comments

Comments
 (0)