BrowserTestBaseTest.php 3.71 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Tests\Core\Test;

5 6
use Behat\Mink\Driver\GoutteDriver;
use Drupal\Tests\DrupalTestBrowser;
7 8
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\BrowserTestBase;
9
use Behat\Mink\Driver\BrowserKitDriver;
10 11 12 13 14 15 16 17 18 19 20 21 22 23
use Behat\Mink\Session;
use Goutte\Client;

/**
 * @coversDefaultClass \Drupal\Tests\BrowserTestBase
 * @group Test
 */
class BrowserTestBaseTest extends UnitTestCase {

  protected function mockBrowserTestBaseWithDriver($driver) {
    $session = $this->getMockBuilder(Session::class)
      ->disableOriginalConstructor()
      ->setMethods(['getDriver'])
      ->getMock();
24
    $session->expects($this->any())
25 26 27 28 29 30 31
      ->method('getDriver')
      ->willReturn($driver);

    $btb = $this->getMockBuilder(BrowserTestBase::class)
      ->disableOriginalConstructor()
      ->setMethods(['getSession'])
      ->getMockForAbstractClass();
32
    $btb->expects($this->any())
33 34 35 36 37 38 39 40 41 42 43 44 45
      ->method('getSession')
      ->willReturn($session);

    return $btb;
  }

  /**
   * @covers ::getHttpClient
   */
  public function testGetHttpClient() {
    // Our stand-in for the Guzzle client object.
    $expected = new \stdClass();

46
    $browserkit_client = $this->getMockBuilder(DrupalTestBrowser::class)
47 48 49 50 51 52
      ->setMethods(['getClient'])
      ->getMockForAbstractClass();
    $browserkit_client->expects($this->once())
      ->method('getClient')
      ->willReturn($expected);

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    // Because the driver is a BrowserKitDriver, we'll get back a client.
    $driver = new BrowserKitDriver($browserkit_client);
    $btb = $this->mockBrowserTestBaseWithDriver($driver);

    $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
    $ref_gethttpclient->setAccessible(TRUE);

    $this->assertSame(get_class($expected), get_class($ref_gethttpclient->invoke($btb)));
  }

  /**
   * @covers ::getHttpClient
   *
   * @group legacy
   */
  public function testGetHttpClientGoutte() {
    // Our stand-in for the Guzzle client object.
    $expected = new \stdClass();

    $browserkit_client = $this->getMockBuilder(Client::class)
73
      ->setMethods(['getClient'])
74 75
      ->getMockForAbstractClass();
    $browserkit_client->expects($this->once())
76
      ->method('getClient')
77
      ->willReturn($expected);
78

79 80
    // Because the driver is a GoutteDriver, we'll get back a client.
    $driver = new GoutteDriver($browserkit_client);
81 82 83 84 85 86 87 88 89 90 91 92
    $btb = $this->mockBrowserTestBaseWithDriver($driver);

    $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
    $ref_gethttpclient->setAccessible(TRUE);

    $this->assertSame(get_class($expected), get_class($ref_gethttpclient->invoke($btb)));
  }

  /**
   * @covers ::getHttpClient
   */
  public function testGetHttpClientException() {
93
    // A driver type that isn't BrowserKitDriver. This should cause a
94 95 96 97 98 99
    // RuntimeException.
    $btb = $this->mockBrowserTestBaseWithDriver(new \stdClass());

    $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
    $ref_gethttpclient->setAccessible(TRUE);

100 101
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('The Mink client type stdClass does not support getHttpClient().');
102 103 104
    $ref_gethttpclient->invoke($btb);
  }

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  /**
   * Test that tearDown doesn't call cleanupEnvironment if setUp is not called.
   *
   * @covers ::tearDown
   */
  public function testTearDownWithoutSetUp() {
    $method = 'cleanupEnvironment';
    $this->assertTrue(method_exists(BrowserTestBase::class, $method));
    $btb = $this->getMockBuilder(BrowserTestBase::class)
      ->disableOriginalConstructor()
      ->setMethods([$method])
      ->getMockForAbstractClass();
    $btb->expects($this->never())->method($method);
    $ref_tearDown = new \ReflectionMethod($btb, 'tearDown');
    $ref_tearDown->setAccessible(TRUE);
    $ref_tearDown->invoke($btb);
  }

123
}