BrowserTestBaseTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Drupal\Tests\Core\Test;
  3. use Drupal\Tests\UnitTestCase;
  4. use Drupal\Tests\BrowserTestBase;
  5. use Behat\Mink\Driver\GoutteDriver;
  6. use Behat\Mink\Session;
  7. use Goutte\Client;
  8. /**
  9. * @coversDefaultClass \Drupal\Tests\BrowserTestBase
  10. * @group Test
  11. */
  12. class BrowserTestBaseTest extends UnitTestCase {
  13. protected function mockBrowserTestBaseWithDriver($driver) {
  14. $session = $this->getMockBuilder(Session::class)
  15. ->disableOriginalConstructor()
  16. ->setMethods(['getDriver'])
  17. ->getMock();
  18. $session->expects($this->once())
  19. ->method('getDriver')
  20. ->willReturn($driver);
  21. $btb = $this->getMockBuilder(BrowserTestBase::class)
  22. ->disableOriginalConstructor()
  23. ->setMethods(['getSession'])
  24. ->getMockForAbstractClass();
  25. $btb->expects($this->once())
  26. ->method('getSession')
  27. ->willReturn($session);
  28. return $btb;
  29. }
  30. /**
  31. * @covers ::getHttpClient
  32. */
  33. public function testGetHttpClient() {
  34. // Our stand-in for the Guzzle client object.
  35. $expected = new \stdClass();
  36. $browserkit_client = $this->getMockBuilder(Client::class)
  37. ->setMethods(['getClient'])
  38. ->getMockForAbstractClass();
  39. $browserkit_client->expects($this->once())
  40. ->method('getClient')
  41. ->willReturn($expected);
  42. // Because the driver is a GoutteDriver, we'll get back a client.
  43. $driver = $this->getMockBuilder(GoutteDriver::class)
  44. ->setMethods(['getClient'])
  45. ->getMock();
  46. $driver->expects($this->once())
  47. ->method('getClient')
  48. ->willReturn($browserkit_client);
  49. $btb = $this->mockBrowserTestBaseWithDriver($driver);
  50. $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
  51. $ref_gethttpclient->setAccessible(TRUE);
  52. $this->assertSame(get_class($expected), get_class($ref_gethttpclient->invoke($btb)));
  53. }
  54. /**
  55. * @covers ::getHttpClient
  56. */
  57. public function testGetHttpClientException() {
  58. // A driver type that isn't GoutteDriver. This should cause a
  59. // RuntimeException.
  60. $btb = $this->mockBrowserTestBaseWithDriver(new \stdClass());
  61. $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
  62. $ref_gethttpclient->setAccessible(TRUE);
  63. $this->expectException(\RuntimeException::class);
  64. $this->expectExceptionMessage('The Mink client type stdClass does not support getHttpClient().');
  65. $ref_gethttpclient->invoke($btb);
  66. }
  67. /**
  68. * Test that tearDown doesn't call cleanupEnvironment if setUp is not called.
  69. *
  70. * @covers ::tearDown
  71. */
  72. public function testTearDownWithoutSetUp() {
  73. $method = 'cleanupEnvironment';
  74. $this->assertTrue(method_exists(BrowserTestBase::class, $method));
  75. $btb = $this->getMockBuilder(BrowserTestBase::class)
  76. ->disableOriginalConstructor()
  77. ->setMethods([$method])
  78. ->getMockForAbstractClass();
  79. $btb->expects($this->never())->method($method);
  80. $ref_tearDown = new \ReflectionMethod($btb, 'tearDown');
  81. $ref_tearDown->setAccessible(TRUE);
  82. $ref_tearDown->invoke($btb);
  83. }
  84. }