BrowserTestBaseTest.php in Drupal 9
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;
class BrowserTestBaseTest extends UnitTestCase {
protected function mockBrowserTestBaseWithDriver($driver) {
$session = $this
->getMockBuilder(Session::class)
->disableOriginalConstructor()
->setMethods([
'getDriver',
])
->getMock();
$session
->expects($this
->any())
->method('getDriver')
->willReturn($driver);
$btb = $this
->getMockBuilder(BrowserTestBase::class)
->disableOriginalConstructor()
->setMethods([
'getSession',
])
->getMockForAbstractClass();
$btb
->expects($this
->any())
->method('getSession')
->willReturn($session);
return $btb;
}
public function testGetHttpClient() {
$expected = new \stdClass();
$browserkit_client = $this
->getMockBuilder(DrupalTestBrowser::class)
->setMethods([
'getClient',
])
->getMockForAbstractClass();
$browserkit_client
->expects($this
->once())
->method('getClient')
->willReturn($expected);
$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)));
}
public function testGetHttpClientException() {
$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);
}
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);
}
}