Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / tests / src / Unit / ContainerMockTrait.php
1 <?php
2
3 namespace Drupal\Tests\drupalmoduleupgrader\Unit;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6
7 /**
8  * A trait for tests that need a mock container; contains (deprecated) methods
9  * to mock basic translation and logging services as well.
10  */
11 trait ContainerMockTrait {
12
13   /**
14    * @var \Symfony\Component\DependencyInjection\ContainerInterface
15    */
16   protected $container;
17
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();
23     }
24   }
25
26   protected function mockTranslator() {
27     $this->mockContainer();
28
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);
34   }
35
36   protected function mockLogger() {
37     $this->mockContainer();
38
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);
44   }
45
46 }