3 namespace Drupal\Tests\drupalmoduleupgrader\Unit;
5 use Drupal\drupalmoduleupgrader\Target;
6 use Drupal\Tests\UnitTestCase;
9 * Base class for all DMU tests, providing a useful environment:
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.
17 abstract class TestBase extends UnitTestCase {
19 use ContainerMockTrait;
20 use SQLiteDatabaseTrait;
21 use ModuleMockerTrait;
24 * The parsed annotations for the test class and method being executed.
31 * @var \org\bovigo\vfs\vfsStreamDirectory
36 * @var \Drupal\drupalmoduleupgrader\TargetInterface
41 * Mocks an entire module, called foo, in a virtual file system.
43 public function setUp() {
44 $this->info = $this->getAnnotations();
46 $this->dir = $this->mockModule('foo');
48 $this->mockContainer();
49 $this->mockTranslator();
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);
58 $this->target = new Target($this->dir->url(), $this->container);
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.
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.
75 protected function getPlugin(array $configuration = [], $plugin_definition = []) {
76 $plugin_definition['description'] = $this->getRandomGenerator()->sentences(4);
78 $class = $this->info['class']['covers'][0];
79 return $class::create($this->container, $configuration, $this->randomMachineName(), $plugin_definition);