Skip to content

Add more tests #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ext extends \phpbb\extension\base
const DEFAULT_PHPBB_MIN = '3.3.0';
const DEFAULT_PHPBB_MAX = '4.0.0@dev';

const REQUIRE_PHPBB_MIN = '3.2.3';
const REQUIRE_PHPBB_MAX = '4.0.0-dev';
const REQUIRE_PHP = 50600;

/**
* @var array An array of installation error messages
*/
Expand All @@ -40,31 +44,32 @@ public function is_enableable()
}

/**
* Check phpBB 3.2.3 minimum requirement.
* Check phpBB requirements.
*
* @param string $phpBB_version
* @return void
*/
protected function phpbb_requirement()
protected function phpbb_requirement($phpBB_version = PHPBB_VERSION)
{
if (phpbb_version_compare(PHPBB_VERSION, '3.2.3', '<'))
if (phpbb_version_compare($phpBB_version, self::REQUIRE_PHPBB_MIN, '<'))
{
$this->errors[] = 'PHPBB_VERSION_MIN_ERROR';
}

else if (phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='))
else if (phpbb_version_compare($phpBB_version, self::REQUIRE_PHPBB_MAX, '>='))
{
$this->errors[] = 'PHPBB_VERSION_MAX_ERROR';
}
}

/**
* Check PHP 5.6.0 minimum requirement.
* Check PHP minimum requirement.
*
* @param int $php_version
* @return void
*/
protected function php_requirement()
protected function php_requirement($php_version = PHP_VERSION_ID)
{
if (PHP_VERSION_ID < 50600)
if ($php_version < self::REQUIRE_PHP)
{
$this->errors[] = 'PHP_VERSION_ERROR';
}
Expand All @@ -73,11 +78,12 @@ protected function php_requirement()
/**
* Check PHP ZipArchive binary requirement.
*
* @param string $zip_class
* @return void
*/
protected function ziparchive_exists()
protected function ziparchive_exists($zip_class = 'ZipArchive')
{
if (!class_exists('ZipArchive'))
if (!class_exists($zip_class, false))
{
$this->errors[] = 'NO_ZIPARCHIVE_ERROR';
}
Expand Down
5 changes: 3 additions & 2 deletions skeleton/notification/type/sample.php.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set is_phpbb_pre_32 = skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.2", "<") %}
<?php
/**
*
Expand Down Expand Up @@ -157,7 +158,7 @@ class sample extends \phpbb\notification\type\base
*
* @param array $data The type specific data
* @param array $pre_create_data Data from pre_create_insert_array()
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.2", "<") %}{# for phpBB 3.1.x only #}
{% if is_phpbb_pre_32 %}{# for phpBB 3.1.x only #}
*
* @return array Array of data ready to be inserted into the database
{% endif %}{# for phpBB >= 3.2.x #}
Expand All @@ -166,7 +167,7 @@ class sample extends \phpbb\notification\type\base
{
$this->set_data('{{ EXTENSION.extension_name|lower }}_sample_name', $data['{{ EXTENSION.extension_name|lower }}_sample_name']);

{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.2", "<") %}{# for phpBB 3.1.x only #}
{% if is_phpbb_pre_32 %}{# for phpBB 3.1.x only #}
return parent::create_insert_array($data, $pre_create_data);
{% else %}{# for phpBB >= 3.2.x #}
parent::create_insert_array($data, $pre_create_data);
Expand Down
7 changes: 4 additions & 3 deletions skeleton/phpunit.xml.dist.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set is_phpbb_pre_33 = skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %}
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="true"
Expand All @@ -8,7 +9,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %}
{% if is_phpbb_pre_33 %}
syntaxCheck="false"
{% endif %}
verbose="true"
Expand All @@ -20,12 +21,12 @@
<exclude>./tests/functional</exclude>
</testsuite>
<testsuite name="Extension Functional Tests">
<directory suffix="_test.php"{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %} phpVersion="5.3.19" phpVersionOperator=">="{% endif %}>./tests/functional/</directory>
<directory suffix="_test.php"{% if is_phpbb_pre_33 %} phpVersion="5.3.19" phpVersionOperator=">="{% endif %}>./tests/functional/</directory>
</testsuite>
</testsuites>

<filter>
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %}
{% if is_phpbb_pre_33 %}
<blacklist>
<directory>./tests/</directory>
</blacklist>
Expand Down
164 changes: 164 additions & 0 deletions tests/ext_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?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;

use phpbb\db\migrator;
use phpbb\finder;
use phpbb\language\language;
use phpbb\skeleton\ext;
use Symfony\Component\DependencyInjection\ContainerInterface;

class ext_test extends \phpbb_test_case
{
protected $ext;

protected function setUp(): void
{
$this->ext = new ext(
$this->createMock(ContainerInterface::class),
$this->createMock(finder::class),
$this->createMock(migrator::class),
'phpbb/skeleton',
''
);
}

public function test_ext_is_instance_of_base()
{
$this->assertInstanceOf(ext::class, $this->ext);
}

public function test_is_enableable_true()
{
$ext = $this->getMockBuilder(ext::class)
->disableOriginalConstructor()
->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement'])
->getMock();

$ext->method('ziparchive_exists')->willReturn(null);
$ext->method('phpbb_requirement')->willReturn(null);
$ext->method('php_requirement')->willReturn(null);

$this->setExtErrors($ext, []);
$this->assertTrue($ext->is_enableable());
}

public function test_is_enableable_with_errors()
{
$ext = $this->getMockBuilder(ext::class)
->disableOriginalConstructor()
->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement', 'enable_failed'])
->getMock();

$ext->method('ziparchive_exists')->willReturnCallback(function () use ($ext) {
$this->appendExtError($ext, 'NO_ZIPARCHIVE_ERROR');
});
$ext->method('phpbb_requirement')->willReturn(null);
$ext->method('php_requirement')->willReturn(null);
$ext->method('enable_failed')->willReturn(['NO_ZIPARCHIVE_ERROR']);

$this->setExtErrors($ext, ['NO_ZIPARCHIVE_ERROR']);
$this->assertEquals(['NO_ZIPARCHIVE_ERROR'], $ext->is_enableable());
}

public function test_enable_failed_returns_expected()
{
$ext = $this->getMockBuilder(ext::class)
->disableOriginalConstructor()
->getMock();

$this->setExtErrors($ext, ['SOME_ERROR']);

$languageMock = $this->createMock(language::class);
$languageMock->method('add_lang')->willReturn(null);
$languageMock->method('lang')->willReturnCallback(function ($msg) {
return "LANG: $msg";
});

$containerMock = $this->createMock(ContainerInterface::class);
$containerMock->method('get')->with('language')->willReturn($languageMock);

$this->setProperty($ext, 'container', $containerMock);

$method = (new \ReflectionClass($ext))->getMethod('enable_failed');
$method->setAccessible(true);

$this->assertEquals(['LANG: SOME_ERROR'], $method->invoke($ext));
}

public function test_phpbb_requirement_min_error()
{
$this->setExtErrors($this->ext, []);
$this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['3.2.2']);
$this->assertContains('PHPBB_VERSION_MIN_ERROR', $this->getExtErrors($this->ext));
}

public function test_phpbb_requirement_max_error()
{
$this->setExtErrors($this->ext, []);
$this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['4.0.0-dev']);
$this->assertContains('PHPBB_VERSION_MAX_ERROR', $this->getExtErrors($this->ext));
}

public function test_php_requirement_error()
{
$this->setExtErrors($this->ext, []);
$this->invokeProtectedMethod($this->ext, 'php_requirement', [50500]);
$this->assertContains('PHP_VERSION_ERROR', $this->getExtErrors($this->ext));
}

public function test_ziparchive_exists_error()
{
$this->setExtErrors($this->ext, []);
$this->invokeProtectedMethod($this->ext, 'ziparchive_exists', ['NotZipArchive']);
$this->assertContains('NO_ZIPARCHIVE_ERROR', $this->getExtErrors($this->ext));
}

// --- Helpers ---

protected function invokeProtectedMethod($object, string $methodName, array $args = [])
{
$method = (new \ReflectionClass($object))->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $args);
}

protected function getExtErrors($ext): array
{
$prop = (new \ReflectionClass($ext))->getProperty('errors');
$prop->setAccessible(true);
return $prop->getValue($ext);
}

protected function setExtErrors($ext, array $errors): void
{
$prop = (new \ReflectionClass($ext))->getProperty('errors');
$prop->setAccessible(true);
$prop->setValue($ext, $errors);
}

protected function appendExtError($ext, string $error): void
{
$errors = $this->getExtErrors($ext);
$errors[] = $error;
$this->setExtErrors($ext, $errors);
}

protected function setProperty($object, string $property, $value): void
{
$prop = (new \ReflectionClass($object))->getProperty($property);
$prop->setAccessible(true);
$prop->setValue($object, $value);
}
}
Loading