3 namespace Drupal\Tests\drupalmoduleupgrader\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 * A trait for tests that need a mock container; contains (deprecated) methods
9 * to mock basic translation and logging services as well.
11 trait ContainerMockTrait {
14 * @var \Symfony\Component\DependencyInjection\ContainerInterface
18 protected function mockContainer() {
19 if (empty($this->container)) {
20 // Using a ContainerBuilder lets us simply stick services into the
21 // container, which is a whole lot easier than mocking it!
22 $this->container = new ContainerBuilder();
26 protected function mockTranslator() {
27 $this->mockContainer();
29 // Mock the string_translation service; calling its translate()
30 // method will return the original, unprocessed string.
31 $translator = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface');
32 $translator->method('translate')->willReturnArgument(0);
33 $this->container->set('string_translation', $translator);
36 protected function mockLogger() {
37 $this->mockContainer();
39 // Mock the logger.factory service and a logger channel.
40 $factory = $this->getMock('\Drupal\Core\Logger\LoggerChannelFactoryInterface');
41 $channel = $this->getMock('\Drupal\Core\Logger\LoggerChannelInterface');
42 $factory->method('get')->willReturn($channel);
43 $this->container->set('logger.factory', $factory);