aboutsummaryrefslogtreecommitdiffstats
path: root/tests/di/create_container_test.php
blob: 4e477519998ebb95656f525ba800cfbe586f37e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace
{
	define('PHPBB_ENVIRONMENT', 'production');

	require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';

	class phpbb_di_container_test extends \phpbb_test_case
	{
		protected $config_php;

		/**
		* @var \phpbb\di\container_builder
		*/
		protected $builder;
		protected $phpbb_root_path;
		protected $filename;

		public function setUp()
		{
			$this->phpbb_root_path = dirname(__FILE__) . '/';
			$this->config_php = new \phpbb\config_php_file($this->phpbb_root_path . 'fixtures/', 'php');
			$this->builder = new phpbb_mock_phpbb_di_container_builder($this->config_php, $this->phpbb_root_path . 'fixtures/', 'php');

			$this->filename = $this->phpbb_root_path . '../tmp/container.php';
			if (is_file($this->filename))
			{
				unlink($this->filename);
			}

			parent::setUp();
		}

		public function test_default_container()
		{
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			// Checks the core services
			$this->assertTrue($container->hasParameter('core'));

			// Checks compile_container
			$this->assertTrue($container->isFrozen());

			// Checks inject_config
			$this->assertTrue($container->hasParameter('dbal.dbhost'));

			// Checks use_extensions
			$this->assertTrue($container->hasParameter('enabled'));
			$this->assertTrue($container->hasParameter('enabled_2'));
			$this->assertTrue($container->hasParameter('enabled_3'));
			$this->assertFalse($container->hasParameter('disabled'));
			$this->assertFalse($container->hasParameter('available'));

			// Checks set_custom_parameters
			$this->assertTrue($container->hasParameter('core.root_path'));

			// Checks dump_container
			$this->assertTrue(is_file($this->filename));

			// Checks the construction of a dumped container
			$container = $this->builder->get_container();
			$this->assertInstanceOf('phpbb_cache_container', $container);
			$this->assertFalse($container->isFrozen());
			$container->getParameterBag(); // needed, otherwise the container is not marked as frozen
			$this->assertTrue($container->isFrozen());
		}

		public function test_dump_container()
		{
			$this->builder->set_dump_container(false);
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			// Checks dump_container
			$this->assertFalse(is_file($this->filename));

			// Checks the construction of a dumped container
			$container = $this->builder->get_container();
			$this->assertNotInstanceOf('phpbb_cache_container', $container);
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);
			$this->assertTrue($container->isFrozen());
		}

		public function test_use_extensions()
		{
			$this->builder->set_use_extensions(false);
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			// Checks the core services
			$this->assertTrue($container->hasParameter('core'));

			// Checks use_extensions
			$this->assertFalse($container->hasParameter('enabled'));
			$this->assertFalse($container->hasParameter('disabled'));
			$this->assertFalse($container->hasParameter('available'));
		}

		public function test_compile_container()
		{
			$this->builder->set_compile_container(false);
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			// Checks compile_container
			$this->assertFalse($container->isFrozen());
		}

		public function test_inject_config()
		{
			$this->builder->set_inject_config(false);
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			// Checks inject_config
			$this->assertFalse($container->hasParameter('dbal.dbhost'));
		}

		public function test_set_config_path()
		{
			$this->builder->set_config_path($this->phpbb_root_path . 'fixtures/other_config/');
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			$this->assertTrue($container->hasParameter('other_config'));
			$this->assertFalse($container->hasParameter('core'));
		}

		public function test_set_custom_parameters()
		{
			$this->builder->set_custom_parameters(array('my_parameter' => true));
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

			$this->assertTrue($container->hasParameter('my_parameter'));
		}
	}
}

namespace phpbb\db\driver
{
	class container_mock extends \phpbb\db\driver\driver
	{
		public function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
		{
		}

		public function sql_query($query = '', $cache_ttl = 0)
		{
		}

		public function sql_fetchrow($query_id = false)
		{
		}

		public function sql_freeresult($query_id = false)
		{
		}

		function sql_server_info($raw = false, $use_cache = true)
		{
		}

		function sql_affectedrows()
		{
		}

		function sql_rowseek($rownum, &$query_id)
		{
		}

		function sql_nextid()
		{
		}

		function sql_escape($msg)
		{
		}

		function sql_like_expression($expression)
		{
		}

		function sql_not_like_expression($expression)
		{
		}

		function sql_fetchrowset($query_id = false)
		{
			return array(
				array('ext_name' => 'vendor/enabled'),
				array('ext_name' => 'vendor/enabled-2'),
				array('ext_name' => 'vendor/enabled-3'),
			);
		}
	}
}