BrowserTestBaseTest.php

Same filename in this branch
  1. main core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
Same filename and directory in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  2. 11.x core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php
  3. 10 core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php
  4. 10 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  5. 10 core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php
  6. 9 core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php
  7. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  8. 9 core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php
  9. 8.9.x core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php
  10. 8.9.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  11. 8.9.x core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php

Namespace

Drupal\Tests\Core\Test

File

core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Test;

use Behat\Mink\Driver\BrowserKitDriver;
use Behat\Mink\Session;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\DrupalTestBrowser;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests Drupal\Tests\BrowserTestBase.
 */
class BrowserTestBaseTest extends UnitTestCase {
  protected function mockBrowserTestBaseWithDriver($driver) {
    $session = $this->getMockBuilder(Session::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getDriver',
    ])
      ->getMock();
    $session->expects($this->any())
      ->method('getDriver')
      ->willReturn($driver);
    $btb = $this->getMockBuilder(BrowserTestBaseMockableClassTest::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getSession',
    ])
      ->getMock();
    $btb->expects($this->any())
      ->method('getSession')
      ->willReturn($session);
    return $btb;
  }
  
  /**
   * Tests get http client.
   */
  public function testGetHttpClient() : void {
    // Our stand-in for the Guzzle client object.
    $expected = new \stdClass();
    $browserkit_client = $this->getMockBuilder(DrupalTestBrowser::class)
      ->onlyMethods([
      'getClient',
    ])
      ->getMock();
    $browserkit_client->expects($this->once())
      ->method('getClient')
      ->willReturn($expected);
    // Because the driver is a BrowserKitDriver, we'll get back a client.
    $driver = new BrowserKitDriver($browserkit_client);
    $btb = $this->mockBrowserTestBaseWithDriver($driver);
    $reflected_get_http_client = new \ReflectionMethod($btb, 'getHttpClient');
    $this->assertSame(get_class($expected), get_class($reflected_get_http_client->invoke($btb)));
  }
  
  /**
   * Tests get http client exception.
   */
  public function testGetHttpClientException() : void {
    // A driver type that isn't BrowserKitDriver. This should cause a
    // RuntimeException.
    $btb = $this->mockBrowserTestBaseWithDriver(new \stdClass());
    $reflected_get_http_client = new \ReflectionMethod($btb, 'getHttpClient');
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('The Mink client type stdClass does not support getHttpClient().');
    $reflected_get_http_client->invoke($btb);
  }
  
  /**
   * Tests that tearDown doesn't call cleanupEnvironment if setUp is not called.
   */
  public function testTearDownWithoutSetUp() : void {
    $method = 'cleanupEnvironment';
    $this->assertTrue(method_exists(BrowserTestBase::class, $method));
    $btb = $this->getMockBuilder(BrowserTestBaseMockableClassTest::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      $method,
    ])
      ->getMock();
    $btb->expects($this->never())
      ->method($method);
    $ref_tearDown = new \ReflectionMethod($btb, 'tearDown');
    $ref_tearDown->invoke($btb);
  }

}

/**
 * A class extending BrowserTestBase for testing purposes.
 *
 * @phpstan-ignore testClass.missingAttribute.Group, testClass.missingAttribute.RunInSeparateProcesses
 */
class BrowserTestBaseMockableClassTest extends BrowserTestBase {

}

Classes

Title Deprecated Summary
BrowserTestBaseMockableClassTest A class extending BrowserTestBase for testing purposes.
BrowserTestBaseTest Tests Drupal\Tests\BrowserTestBase.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.