3 namespace Drupal\Tests\Core\Test;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Tests\BrowserTestBase;
7 use Behat\Mink\Driver\GoutteDriver;
8 use Behat\Mink\Session;
12 * @coversDefaultClass \Drupal\Tests\BrowserTestBase
15 class BrowserTestBaseTest extends UnitTestCase {
17 protected function mockBrowserTestBaseWithDriver($driver) {
18 $session = $this->getMockBuilder(Session::class)
19 ->disableOriginalConstructor()
20 ->setMethods(['getDriver'])
22 $session->expects($this->once())
24 ->willReturn($driver);
26 $btb = $this->getMockBuilder(BrowserTestBase::class)
27 ->disableOriginalConstructor()
28 ->setMethods(['getSession'])
29 ->getMockForAbstractClass();
30 $btb->expects($this->once())
31 ->method('getSession')
32 ->willReturn($session);
38 * @covers ::getHttpClient
40 public function testGetHttpClient() {
41 // Our stand-in for the Guzzle client object.
42 $expected = new \stdClass();
44 $browserkit_client = $this->getMockBuilder(Client::class)
45 ->setMethods(['getClient'])
46 ->getMockForAbstractClass();
47 $browserkit_client->expects($this->once())
49 ->willReturn($expected);
51 // Because the driver is a GoutteDriver, we'll get back a client.
52 $driver = $this->getMockBuilder(GoutteDriver::class)
53 ->setMethods(['getClient'])
55 $driver->expects($this->once())
57 ->willReturn($browserkit_client);
59 $btb = $this->mockBrowserTestBaseWithDriver($driver);
61 $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
62 $ref_gethttpclient->setAccessible(TRUE);
64 $this->assertSame(get_class($expected), get_class($ref_gethttpclient->invoke($btb)));
68 * @covers ::getHttpClient
70 public function testGetHttpClientException() {
71 // A driver type that isn't GoutteDriver. This should cause a
73 $btb = $this->mockBrowserTestBaseWithDriver(new \stdClass());
75 $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
76 $ref_gethttpclient->setAccessible(TRUE);
78 $this->setExpectedException(\RuntimeException::class, 'The Mink client type stdClass does not support getHttpClient().');
79 $ref_gethttpclient->invoke($btb);