d3c324f9e8041d9335798b3a9f8b72828edfbf4d
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / tests / src / Unit / TestBase.php
1 <?php
2
3 namespace Drupal\Tests\drupalmoduleupgrader\Unit;
4
5 use Drupal\drupalmoduleupgrader\Target;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Base class for all DMU tests, providing a useful environment:
10  *
11  * - A module called foo, mocked in memory using vfsStream. The actual
12  *   module files are empty and should be filled in by subclasses.
13  * - A TargetInterface instance for the foo module.
14  * - A Drupal database connection to an empty in-memory SQLite database.
15  * - A container with mocked string_translation and logger.factory services.
16  */
17 abstract class TestBase extends UnitTestCase {
18
19   use ContainerMockTrait;
20   use SQLiteDatabaseTrait;
21   use ModuleMockerTrait;
22
23   /**
24    * The parsed annotations for the test class and method being executed.
25    *
26    * @var array
27    */
28   protected $info;
29
30   /**
31    * @var \org\bovigo\vfs\vfsStreamDirectory
32    */
33   protected $dir;
34
35   /**
36    * @var \Drupal\drupalmoduleupgrader\TargetInterface
37    */
38   protected $target;
39
40   /**
41    * Mocks an entire module, called foo, in a virtual file system.
42    */
43   public function setUp() {
44     $this->info = $this->getAnnotations();
45
46     $this->dir = $this->mockModule('foo');
47
48     $this->mockContainer();
49     $this->mockTranslator();
50     $this->mockLogger();
51     $this->initDB();
52
53     // At the time of this writing, Target will pull the indexer manager out
54     // of the container right away, so let's mock it.
55     $indexers = $this->getMock('\Drupal\Component\Plugin\PluginManagerInterface');
56     $this->container->set('plugin.manager.drupalmoduleupgrader.indexer', $indexers);
57
58     $this->target = new Target($this->dir->url(), $this->container);
59   }
60
61   /**
62    * Instantiates the plugin class covered by this test (as indicated by the
63    * @covers annotation). The plugin instance is given a randomly generated
64    * ID and description. Dependencies will be pulled from $this->container,
65    * so this should only be called once the mock container is ready.
66    *
67    * @param array $configuration
68    *  Additional configuration to pass to the instance.
69    * @param array $plugin_definition
70    *  Additional definition info to pass to the instance.
71    *
72    * @return object
73    *  A plugin instance.
74    */
75   protected function getPlugin(array $configuration = [], $plugin_definition = []) {
76     $plugin_definition['description'] = $this->getRandomGenerator()->sentences(4);
77
78     $class = $this->info['class']['covers'][0];
79     return $class::create($this->container, $configuration, $this->randomMachineName(), $plugin_definition);
80   }
81
82 }