Version 1
[yaffs-website] / vendor / behat / mink-browserkit-driver / tests / Custom / ErrorHandlingTest.php
1 <?php
2
3 namespace Behat\Mink\Tests\Driver\Custom;
4
5 use Behat\Mink\Driver\BrowserKitDriver;
6 use Symfony\Component\BrowserKit\Client;
7 use Symfony\Component\BrowserKit\Response;
8
9 class ErrorHandlingTest 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 submit or reset 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 }