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