ErrorHandlingTest.php 4.3 KB

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