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