aboutsummaryrefslogtreecommitdiffstats
path: root/tests/di/create_container_test.php
blob: 1fd2cbd7eee4a0c292cfc47cbdf3b4ea8795125b (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
<?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
{
	require_once dirname(__FILE__) . '/fixtures/ext/vendor/enabled_4/di/extension.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->phpbb_root_path . 'fixtures/', 'php');
			$this->builder->with_config($this->config_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);
			$this->assertFalse($container->hasParameter('container_exception'));

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

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

			// Checks inject_config
			$this->assertTrue($container->hasParameter('core.table_prefix'));

			// Checks use_extensions
			$this->assertTrue($container->hasParameter('enabled'));
			$this->assertTrue($container->hasParameter('enabled_2'));
			$this->assertTrue($container->hasParameter('enabled_3'));
			$this->assertTrue($container->hasParameter('enabled_4'));
			$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->assertTrue($container->isFrozen());
		}

		public function test_without_cache()
		{
			$this->builder->without_cache();
			$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_without_extensions()
		{
			$this->builder->without_extensions();
			$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_without_compiled_container()
		{
			$this->builder->without_compiled_container();
			$container = $this->builder->get_container();
			$this->assertInstanceOf('Symfony\Component\DependencyInjection\ContainerBuilder', $container);

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

		public function test_with_config_path()
		{
			$this->builder->with_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_with_custom_parameters()
		{
			$this->builder->with_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\extension
{
	class manager_mock extends \phpbb\extension\manager
	{
		public function __construct()
		{
		}

		public function all_enabled($phpbb_relative = true)
		{
			return array(
				'vendor/enabled' => dirname(__FILE__) . '/fixtures/ext/vendor/enabled/',
				'vendor/enabled-2' => dirname(__FILE__) . '/fixtures/ext/vendor/enabled-2/',
				'vendor/enabled-3' => dirname(__FILE__) . '/fixtures/ext/vendor/enabled-3/',
				'vendor/enabled_4' => dirname(__FILE__) . '/fixtures/ext/vendor/enabled_4/',
			);
		}
	}
}