BrowserTestBaseTest.php

Same filename in this branch
  1. 9 core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php
  2. 9 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
Same filename in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php
  2. 8.9.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  3. 8.9.x core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php
  4. 10 core/tests/Drupal/KernelTests/Core/Test/BrowserTestBaseTest.php
  5. 10 core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  6. 10 core/tests/Drupal/Tests/Core/Test/BrowserTestBaseTest.php
  7. 11.x core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
  8. 11.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

namespace Drupal\Tests\Core\Test;

use Drupal\Tests\DrupalTestBrowser;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\BrowserTestBase;
use Behat\Mink\Driver\BrowserKitDriver;
use Behat\Mink\Session;

/**
 * @coversDefaultClass \Drupal\Tests\BrowserTestBase
 * @group Test
 */
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(BrowserTestBase::class)
            ->disableOriginalConstructor()
            ->onlyMethods([
            'getSession',
        ])
            ->getMockForAbstractClass();
        $btb->expects($this->any())
            ->method('getSession')
            ->willReturn($session);
        return $btb;
    }
    
    /**
     * @covers ::getHttpClient
     */
    public function testGetHttpClient() {
        // Our stand-in for the Guzzle client object.
        $expected = new \stdClass();
        $browserkit_client = $this->getMockBuilder(DrupalTestBrowser::class)
            ->onlyMethods([
            'getClient',
        ])
            ->getMockForAbstractClass();
        $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);
        $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() {
        // A driver type that isn't BrowserKitDriver. This should cause a
        // RuntimeException.
        $btb = $this->mockBrowserTestBaseWithDriver(new \stdClass());
        $ref_gethttpclient = new \ReflectionMethod($btb, 'getHttpClient');
        $ref_gethttpclient->setAccessible(TRUE);
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('The Mink client type stdClass does not support getHttpClient().');
        $ref_gethttpclient->invoke($btb);
    }
    
    /**
     * Tests 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()
            ->onlyMethods([
            $method,
        ])
            ->getMockForAbstractClass();
        $btb->expects($this->never())
            ->method($method);
        $ref_tearDown = new \ReflectionMethod($btb, 'tearDown');
        $ref_tearDown->setAccessible(TRUE);
        $ref_tearDown->invoke($btb);
    }

}

Classes

Title Deprecated Summary
BrowserTestBaseTest @coversDefaultClass \Drupal\Tests\BrowserTestBase @group Test

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