ContainerMockTrait.php in Drupal 7 to 8/9 Module Upgrader 8
File
tests/src/Unit/ContainerMockTrait.php
View source
<?php
namespace Drupal\Tests\drupalmoduleupgrader\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
trait ContainerMockTrait {
protected $container;
protected function mockContainer() {
if (empty($this->container)) {
$this->container = new ContainerBuilder();
}
}
protected function mockTranslator() {
$this
->mockContainer();
$translator = $this
->createMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$translator
->method('translate')
->willReturnArgument(0);
$this->container
->set('string_translation', $translator);
}
protected function mockLogger() {
$this
->mockContainer();
$factory = $this
->createMock('\\Drupal\\Core\\Logger\\LoggerChannelFactoryInterface');
$channel = $this
->createMock('\\Drupal\\Core\\Logger\\LoggerChannelInterface');
$factory
->method('get')
->willReturn($channel);
$this->container
->set('logger.factory', $factory);
}
}
Traits
Name |
Description |
ContainerMockTrait |
A trait for tests that need a mock container; contains (deprecated) methods
to mock basic translation and logging services as well. |