a77ad091644775080e91c7e2e70afd40c74af4b0
[yaffs-website] / web / core / tests / Drupal / Tests / Core / DependencyInjection / Compiler / BackendCompilerPassTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest.
6  */
7
8 namespace Drupal\Tests\Core\DependencyInjection\Compiler;
9
10 use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
11 use Drupal\Tests\UnitTestCase;
12 use Symfony\Component\DependencyInjection\Alias;
13 use Symfony\Component\DependencyInjection\ContainerBuilder;
14 use Symfony\Component\DependencyInjection\Definition;
15
16 /**
17  * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
18  * @group DependencyInjection
19  */
20 class BackendCompilerPassTest extends UnitTestCase {
21
22   /**
23    * The tested backend compiler pass.
24    *
25    * @var \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
26    */
27   protected $backendPass;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     $this->backendPass = new BackendCompilerPass();
34   }
35
36   /**
37    * Tests the process method.
38    *
39    * @param string $expected_class
40    *   The expected used class.
41    * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
42    *   The container.
43    *
44    * @dataProvider providerTestProcess
45    *
46    * @covers ::process
47    */
48   public function testProcess($expected_class, ContainerBuilder $container) {
49     $this->backendPass->process($container);
50
51     $this->assertEquals($expected_class, get_class($container->get('service')));
52   }
53
54   /**
55    * Provides test data for testProcess().
56    *
57    * @return array
58    */
59   public function providerTestProcess() {
60     $data = [];
61     // Add a container with no set default_backend.
62     $prefix = __NAMESPACE__ . '\\ServiceClass';
63     $service = (new Definition($prefix . 'Default'))->addTag('backend_overridable');
64     $container = $this->getMysqlContainer($service);
65
66     $data[] = [$prefix . 'Default', $container];
67
68     // Set the default_backend so the mysql service should be used.
69     $container = $this->getMysqlContainer($service);
70     $container->setParameter('default_backend', 'mysql');
71     $data[] = [$prefix . 'Mysql', $container];
72
73     // Configure a manual alias for the service, so ensure that it is not
74     // overridden by the default backend.
75     $container = $this->getMysqlContainer($service);
76     $container->setParameter('default_backend', 'mysql');
77     $container->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
78     $container->setAlias('service', new Alias('mariadb.service'));
79     $data[] = [$prefix . 'MariaDb', $container];
80
81     // Check the database driver is the default.
82     $container = $this->getSqliteContainer($service);
83     $data[] = [$prefix . 'Sqlite', $container];
84
85     // Test the opt out.
86     $container = $this->getSqliteContainer($service);
87     $container->setParameter('default_backend', '');
88     $data[] = [$prefix . 'Default', $container];
89
90     return $data;
91   }
92
93   /**
94    * Creates a container with a sqlite database service in it.
95    *
96    * This is necessary because the container clone does not clone the parameter
97    * bag so the setParameter() call effects the parent container as well.
98    *
99    * @param $service
100    * @return \Symfony\Component\DependencyInjection\ContainerBuilder
101    */
102   protected function getSqliteContainer($service) {
103     $container = new ContainerBuilder();
104     $container->setDefinition('service', $service);
105     $container->setDefinition('sqlite.service', new Definition(__NAMESPACE__ . '\\ServiceClassSqlite'));
106     $mock = $this->getMockBuilder('Drupal\Core\Database\Driver\sqlite\Connection')->setMethods(NULL)->disableOriginalConstructor()->getMock();
107     $container->set('database', $mock);
108     return $container;
109   }
110
111   /**
112    * Creates a container with a mysql database service definition in it.
113    *
114    * This is necessary because the container clone does not clone the parameter
115    * bag so the setParameter() call effects the parent container as well.
116    *
117    * @param $service
118    * @return \Symfony\Component\DependencyInjection\ContainerBuilder
119    */
120   protected function getMysqlContainer($service) {
121     $container = new ContainerBuilder();
122     $container->setDefinition('service', $service);
123     $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
124     return $container;
125   }
126
127 }
128
129 class ServiceClassDefault {
130 }
131
132 class ServiceClassMysql extends ServiceClassDefault {
133 }
134
135 class ServiceClassMariaDb extends ServiceClassMysql {
136 }
137
138 class ServiceClassSqlite extends ServiceClassDefault {
139 }