Skip to content

Commit 772b8c6

Browse files
committed
Add tests to the ext class
1 parent 6bcf804 commit 772b8c6

File tree

2 files changed

+180
-10
lines changed

2 files changed

+180
-10
lines changed

ext.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class ext extends \phpbb\extension\base
1919
const DEFAULT_PHPBB_MIN = '3.3.0';
2020
const DEFAULT_PHPBB_MAX = '4.0.0@dev';
2121

22+
const REQUIRE_PHPBB_MIN = '3.2.3';
23+
const REQUIRE_PHPBB_MAX = '4.0.0-dev';
24+
const REQUIRE_PHP = 50600;
25+
2226
/**
2327
* @var array An array of installation error messages
2428
*/
@@ -40,31 +44,32 @@ public function is_enableable()
4044
}
4145

4246
/**
43-
* Check phpBB 3.2.3 minimum requirement.
47+
* Check phpBB requirements.
4448
*
49+
* @param string $phpBB_version
4550
* @return void
4651
*/
47-
protected function phpbb_requirement()
52+
protected function phpbb_requirement($phpBB_version = PHPBB_VERSION)
4853
{
49-
if (phpbb_version_compare(PHPBB_VERSION, '3.2.3', '<'))
54+
if (phpbb_version_compare($phpBB_version, self::REQUIRE_PHPBB_MIN, '<'))
5055
{
5156
$this->errors[] = 'PHPBB_VERSION_MIN_ERROR';
5257
}
53-
54-
else if (phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='))
58+
else if (phpbb_version_compare($phpBB_version, self::REQUIRE_PHPBB_MAX, '>='))
5559
{
5660
$this->errors[] = 'PHPBB_VERSION_MAX_ERROR';
5761
}
5862
}
5963

6064
/**
61-
* Check PHP 5.6.0 minimum requirement.
65+
* Check PHP minimum requirement.
6266
*
67+
* @param int $php_version
6368
* @return void
6469
*/
65-
protected function php_requirement()
70+
protected function php_requirement($php_version = PHP_VERSION_ID)
6671
{
67-
if (PHP_VERSION_ID < 50600)
72+
if ($php_version < self::REQUIRE_PHP)
6873
{
6974
$this->errors[] = 'PHP_VERSION_ERROR';
7075
}
@@ -73,11 +78,12 @@ protected function php_requirement()
7378
/**
7479
* Check PHP ZipArchive binary requirement.
7580
*
81+
* @param string $zip_class
7682
* @return void
7783
*/
78-
protected function ziparchive_exists()
84+
protected function ziparchive_exists($zip_class = 'ZipArchive')
7985
{
80-
if (!class_exists('ZipArchive'))
86+
if (!class_exists($zip_class, false))
8187
{
8288
$this->errors[] = 'NO_ZIPARCHIVE_ERROR';
8389
}

tests/ext_test.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
* For full copyright and license information, please see
10+
* the docs/CREDITS.txt file.
11+
*
12+
*/
13+
14+
namespace phpbb\skeleton\tests;
15+
16+
use phpbb\db\migrator;
17+
use phpbb\finder;
18+
use phpbb\language\language;
19+
use phpbb\skeleton\ext;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
22+
class ext_test extends \phpbb_test_case
23+
{
24+
protected $ext;
25+
26+
protected function setUp(): void
27+
{
28+
$this->ext = new ext(
29+
$this->createMock(ContainerInterface::class),
30+
$this->createMock(finder::class),
31+
$this->createMock(migrator::class),
32+
'phpbb/skeleton',
33+
''
34+
);
35+
}
36+
37+
public function test_ext_is_instance_of_base()
38+
{
39+
$this->assertInstanceOf(ext::class, $this->ext);
40+
}
41+
42+
public function test_is_enableable_true()
43+
{
44+
$ext = $this->getMockBuilder(ext::class)
45+
->disableOriginalConstructor()
46+
->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement'])
47+
->getMock();
48+
49+
$ext->method('ziparchive_exists')->willReturn(null);
50+
$ext->method('phpbb_requirement')->willReturn(null);
51+
$ext->method('php_requirement')->willReturn(null);
52+
53+
$this->setExtErrors($ext, []);
54+
$this->assertTrue($ext->is_enableable());
55+
}
56+
57+
public function test_is_enableable_with_errors()
58+
{
59+
$ext = $this->getMockBuilder(ext::class)
60+
->disableOriginalConstructor()
61+
->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement', 'enable_failed'])
62+
->getMock();
63+
64+
$ext->method('ziparchive_exists')->willReturnCallback(function () use ($ext) {
65+
$this->appendExtError($ext, 'NO_ZIPARCHIVE_ERROR');
66+
});
67+
$ext->method('phpbb_requirement')->willReturn(null);
68+
$ext->method('php_requirement')->willReturn(null);
69+
$ext->method('enable_failed')->willReturn(['NO_ZIPARCHIVE_ERROR']);
70+
71+
$this->setExtErrors($ext, ['NO_ZIPARCHIVE_ERROR']);
72+
$this->assertEquals(['NO_ZIPARCHIVE_ERROR'], $ext->is_enableable());
73+
}
74+
75+
public function test_enable_failed_returns_expected()
76+
{
77+
$ext = $this->getMockBuilder(ext::class)
78+
->disableOriginalConstructor()
79+
->getMock();
80+
81+
$this->setExtErrors($ext, ['SOME_ERROR']);
82+
83+
$languageMock = $this->createMock(language::class);
84+
$languageMock->method('add_lang')->willReturn(null);
85+
$languageMock->method('lang')->willReturnCallback(function ($msg) {
86+
return "LANG: $msg";
87+
});
88+
89+
$containerMock = $this->createMock(ContainerInterface::class);
90+
$containerMock->method('get')->with('language')->willReturn($languageMock);
91+
92+
$this->setProperty($ext, 'container', $containerMock);
93+
94+
$method = (new \ReflectionClass($ext))->getMethod('enable_failed');
95+
$method->setAccessible(true);
96+
97+
$this->assertEquals(['LANG: SOME_ERROR'], $method->invoke($ext));
98+
}
99+
100+
public function test_phpbb_requirement_min_error()
101+
{
102+
$this->setExtErrors($this->ext, []);
103+
$this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['3.2.2']);
104+
$this->assertContains('PHPBB_VERSION_MIN_ERROR', $this->getExtErrors($this->ext));
105+
}
106+
107+
public function test_phpbb_requirement_max_error()
108+
{
109+
$this->setExtErrors($this->ext, []);
110+
$this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['4.0.0-dev']);
111+
$this->assertContains('PHPBB_VERSION_MAX_ERROR', $this->getExtErrors($this->ext));
112+
}
113+
114+
public function test_php_requirement_error()
115+
{
116+
$this->setExtErrors($this->ext, []);
117+
$this->invokeProtectedMethod($this->ext, 'php_requirement', [50500]);
118+
$this->assertContains('PHP_VERSION_ERROR', $this->getExtErrors($this->ext));
119+
}
120+
121+
public function test_ziparchive_exists_error()
122+
{
123+
$this->setExtErrors($this->ext, []);
124+
$this->invokeProtectedMethod($this->ext, 'ziparchive_exists', ['NotZipArchive']);
125+
$this->assertContains('NO_ZIPARCHIVE_ERROR', $this->getExtErrors($this->ext));
126+
}
127+
128+
// --- Helpers ---
129+
130+
protected function invokeProtectedMethod($object, string $methodName, array $args = [])
131+
{
132+
$method = (new \ReflectionClass($object))->getMethod($methodName);
133+
$method->setAccessible(true);
134+
return $method->invokeArgs($object, $args);
135+
}
136+
137+
protected function getExtErrors($ext): array
138+
{
139+
$prop = (new \ReflectionClass($ext))->getProperty('errors');
140+
$prop->setAccessible(true);
141+
return $prop->getValue($ext);
142+
}
143+
144+
protected function setExtErrors($ext, array $errors): void
145+
{
146+
$prop = (new \ReflectionClass($ext))->getProperty('errors');
147+
$prop->setAccessible(true);
148+
$prop->setValue($ext, $errors);
149+
}
150+
151+
protected function appendExtError($ext, string $error): void
152+
{
153+
$errors = $this->getExtErrors($ext);
154+
$errors[] = $error;
155+
$this->setExtErrors($ext, $errors);
156+
}
157+
158+
protected function setProperty($object, string $property, $value): void
159+
{
160+
$prop = (new \ReflectionClass($object))->getProperty($property);
161+
$prop->setAccessible(true);
162+
$prop->setValue($object, $value);
163+
}
164+
}

0 commit comments

Comments
 (0)