forked from phpbb-extensions/phpbb-ext-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpackager_test.php
More file actions
238 lines (211 loc) · 6.65 KB
/
packager_test.php
File metadata and controls
238 lines (211 loc) · 6.65 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?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 phpbb\skeleton\tests\helper;
use phpbb\skeleton\helper\packager;
use phpbb\di\service_collection;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\ContainerInterface;
class packager_test extends \phpbb_test_case
{
/** @var ContainerInterface|MockObject */
protected $container;
/** @var service_collection|MockObject */
protected $collection;
/** @var packager */
protected $packager;
/** @var string */
protected $root_path = '/tmp/phpbb/';
/**
* @return void
*/
public function setUpTheCollectionToReturnAFakeSkeletonClass(): void
{
// Set up the collection to return a fake skeleton class
$skeleton = $this->getMockBuilder(\phpbb\skeleton\skeleton::class)
->disableOriginalConstructor()
->getMock();
$skeleton->method('get_name')->willReturn('phplistener');
$skeleton->method('get_default')->willReturn(false);
$skeleton->method('get_dependencies')->willReturn([]);
$skeleton->method('get_files')->willReturn(['test.php']);
$skeleton->method('get_group')->willReturn('BACK_END');
$this->collection->method('getIterator')->willReturn(new \ArrayIterator([$skeleton]));
}
protected function setUp(): void
{
$this->container = $this->createMock(ContainerInterface::class);
$this->collection = $this->createMock(service_collection::class);
$this->packager = new packager($this->container, $this->collection, $this->root_path);
}
public function test_get_composer_dialog_values_returns_expected_structure()
{
$result = $this->packager->get_composer_dialog_values();
$this->assertArrayHasKey('author', $result);
$this->assertArrayHasKey('extension', $result);
$this->assertArrayHasKey('requirements', $result);
}
public function test_get_component_dialog_values_calls_collection()
{
$this->setUpTheCollectionToReturnAFakeSkeletonClass();
$result = $this->packager->get_component_dialog_values();
$this->assertArrayHasKey('phplistener', $result);
$this->assertSame([
'default' => false,
'dependencies' => [],
'files' => ['test.php'],
'group' => 'BACK_END'
], $result['phplistener']);
}
public function test_create_extension_runs_without_exception()
{
// Create a partial mock for packager, stubbing get_template_engine
$packager = $this->getMockBuilder(packager::class)
->setConstructorArgs([$this->container, $this->collection, $this->root_path])
->setMethods(['get_template_engine'])
->getMock();
// Mock the template engine
$templateMock = $this->createMock(\phpbb\template\twig\twig::class);
$templateMock->method('set_custom_style')->willReturnSelf();
$templateMock->method('assign_vars')->willReturnSelf();
$templateMock->method('set_filenames')->willReturnSelf();
$templateMock->method('assign_display')->willReturn('dummy');
$packager->method('get_template_engine')->willReturn($templateMock);
$this->setUpTheCollectionToReturnAFakeSkeletonClass();
// You can mock dependencies further as needed
$data = [
'extension' => [
'vendor_name' => 'testvendor',
'extension_name' => 'testext',
'extension_display_name' => 'Test Ext',
'extension_version' => '1.0.0',
],
'requirements' => [
'phpbb_version_min' => '3.2.0',
],
'components' => [],
'authors' => [],
];
// No assertions, just ensure no exceptions
$packager->create_extension($data);
$this->assertTrue(true);
}
public function test_create_zip_creates_zip_file()
{
$data = [
'extension' => [
'vendor_name' => 'testvendor',
'extension_name' => 'testext',
'extension_version' => '1.0.0',
],
];
// You might want to mock filesystem interactions or use vfsStream
$zipPath = $this->packager->create_zip($data);
$this->assertIsString($zipPath);
// Clean up or further assertions here
}
/**
* @dataProvider provide_language_version_data
*/
public function test_get_language_version_data_returns_expected($phpbb_version, $expected)
{
$data = [
'requirements' => [
'phpbb_version_min' => $phpbb_version
]
];
$result = $this->invokeMethod($this->packager, 'get_language_version_data', [$data]);
$this->assertSame($expected['class'], $result['class']);
$this->assertSame($expected['object'], $result['object']);
$this->assertSame($expected['function'], $result['function']);
$this->assertSame($expected['indent']['class'], $result['indent']['class']);
$this->assertSame($expected['indent']['object'], $result['indent']['object']);
}
public function provide_language_version_data()
{
return [
'3.1 version' => [
'3.1.0',
[
'class' => '\phpbb\user',
'object' => 'user',
'function' => 'add_lang_ext',
'indent' => [
'class' => "\t\t\t",
'object' => "\t"
]
]
],
'3.2 version' => [
'3.2.0',
[
'class' => '\phpbb\language\language',
'object' => 'language',
'function' => 'add_lang',
'indent' => [
'class' => '',
'object' => ''
]
]
],
'null version' => [
null,
[
'class' => '\phpbb\language\language',
'object' => 'language',
'function' => 'add_lang',
'indent' => [
'class' => '',
'object' => ''
]
]
]
];
}
public function test_get_template_engine_returns_twig_instance()
{
// Mock all required dependencies for the container
$filesystem = $this->createMock(\phpbb\filesystem\filesystem::class);
$path_helper = $this->createMock(\phpbb\path_helper::class);
$ext_manager = $this->createMock(\phpbb\extension\manager::class);
$user = $this->createMock(\phpbb\user::class);
$this->container->expects($this->exactly(4))
->method('get')
->withConsecutive(
['path_helper'],
['filesystem'],
['ext.manager'],
['user']
)
->willReturnOnConsecutiveCalls(
$path_helper,
$filesystem,
$ext_manager,
$user
);
$this->container->expects($this->once())
->method('getParameter')
->with('core.cache_dir')
->willReturn(false);
// Call the protected method using your invokeMethod helper
$twig = $this->invokeMethod($this->packager, 'get_template_engine');
$this->assertInstanceOf(\phpbb\template\twig\twig::class, $twig);
}
// Helper for protected/private method invocation
protected function invokeMethod($object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}