Skip to content

Commit

Permalink
Merge pull request #4289 from Icinga/bugfix/php8-4287
Browse files Browse the repository at this point in the history
Support PHP 8
  • Loading branch information
nilmerg committed Apr 9, 2021
2 parents f782c08 + b3eeb5a commit 4bc5350
Show file tree
Hide file tree
Showing 36 changed files with 138 additions and 213 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/php.yml
Expand Up @@ -17,11 +17,12 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0']
os: ['ubuntu-latest']
include:
- php: '5.6'
allow_failure: true
phplint_options: "--exclude='{(?>.*/)?test/php/.*}' --exclude=library/Icinga/Test/BaseTestCase.php"
- php: '7.0'
allow_failure: true

Expand All @@ -40,7 +41,7 @@ jobs:

- name: PHP Lint
if: success() || matrix.allow_failure
run: ./vendor/bin/phplint -n --exclude={^vendor/.*} --exclude=library/Icinga/Util/String.php -- .
run: ./vendor/bin/phplint -n --exclude={^vendor/.*} --exclude=library/Icinga/Util/String.php ${{ matrix.phplint_options }} -- .

- name: PHP CodeSniffer
if: success() || matrix.allow_failure
Expand All @@ -51,14 +52,16 @@ jobs:
runs-on: ${{ matrix.os }}

env:
phpunit-version: 5.7.27
mockery-version: 0.9.9 # *Any* other version is not compatible with PHP 5.6 and anything above phpunit 6
phpunit-version: 9.5.4

strategy:
fail-fast: false
matrix:
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
php: ['7.2', '7.3', '7.4', '8.0']
os: ['ubuntu-latest']
include:
- php: '7.2'
phpunit-version: 8.5.15

services:
mysql:
Expand Down Expand Up @@ -98,13 +101,13 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: phpunit:${{ env.phpunit-version }}
tools: phpunit:${{ matrix.phpunit-version || env.phpunit-version }}
extensions: mysql, pgsql, ldap

- name: Setup dependencies
run: |
sudo locale-gen en_US.UTF-8 de_DE.UTF-8 fr_FR.UTF-8
composer require -n --no-progress mockery/mockery:${{ env.mockery-version }}
composer require -n --no-progress mockery/mockery
- name: PHPUnit
env:
Expand Down
2 changes: 1 addition & 1 deletion library/Icinga/Application/ApplicationBootstrap.php
Expand Up @@ -605,7 +605,7 @@ protected function setupErrorHandling()
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
if (error_reporting() === 0) {
if (! (error_reporting() & $errno)) {
// Error was suppressed with the @-operator
return false; // Continue with the normal error handler
}
Expand Down
22 changes: 19 additions & 3 deletions library/Icinga/Test/BaseTestCase.php
Expand Up @@ -23,7 +23,6 @@ function mt()
use Exception;
use RuntimeException;
use Mockery;
use PHPUnit_Framework_TestCase;
use Icinga\Application\Icinga;
use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory;
Expand All @@ -32,7 +31,7 @@ function mt()
/**
* Class BaseTestCase
*/
abstract class BaseTestCase extends PHPUnit_Framework_TestCase implements DbTest
abstract class BaseTestCase extends Mockery\Adapter\Phpunit\MockeryTestCase implements DbTest
{
/**
* Path to application/
Expand Down Expand Up @@ -138,7 +137,7 @@ public static function setupDirectories()
/**
* Setup MVC bootstrapping and ensure that the Icinga-Mock gets reinitialized
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
$this->setupIcingaMock();
Expand Down Expand Up @@ -334,6 +333,23 @@ public function setupDbProvider($resource)
$adapter->exec('DROP TABLE ' . $table . ';');
}
}

/**
* Add assertMatchesRegularExpression() method for phpunit >= 8.0 < 9.0 for compatibility with PHP 7.2.
*
* @TODO Remove once PHP 7.2 support is not needed for testing anymore.
*/
public static function assertMatchesRegularExpression(
string $pattern,
string $string,
string $message = ''
): void {
if (method_exists(parent::class, 'assertMatchesRegularExpression')) {
parent::assertMatchesRegularExpression($pattern, $string, $message);
} else {
static::assertRegExp($pattern, $string, $message);
}
}
}

BaseTestCase::setupTimezone();
Expand Down
Expand Up @@ -96,7 +96,7 @@ protected function joinBaseTables()
array()
);

if (@func_get_arg(0) === false) {
if (func_num_args() === 0 || func_get_arg(0) === false) {
$this->select->where(
"hdh.actual_start_time > '1970-01-02 00:00:00'"
);
Expand Down
Expand Up @@ -97,7 +97,7 @@ protected function joinBaseTables()
array()
);

if (@func_get_arg(0) === false) {
if (func_num_args() === 0 || func_get_arg(0) === false) {
$this->select->where(
"sdh.actual_start_time > '1970-01-02 00:00:00'"
);
Expand Down
Expand Up @@ -20,7 +20,7 @@ class PluginOutputTest extends BaseTestCase

protected static $statusTags = array('OK', 'WARNING', 'CRITICAL', 'UNKNOWN', 'UP', 'DOWN');

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand All @@ -45,7 +45,7 @@ protected function checkOutput($output, $html, $regexp = false, $isHtml = false)
$html,
preg_quote(self::SUFFIX, '~')
);
$this->assertRegExp($expect, $actual, 'Output must match example regexp');
$this->assertMatchesRegularExpression($expect, $actual, 'Output must match example regexp');
} else {
$expect = $prefix . $html . self::SUFFIX;
$this->assertEquals($expect, $actual, 'Output must match example');
Expand Down
Expand Up @@ -8,19 +8,17 @@

class PerfdataTest extends BaseTestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testWhetherFromStringThrowsExceptionWhenGivenAnEmptyString()
{
$this->expectException(\InvalidArgumentException::class);

Perfdata::fromString('');
}

/**
* @expectedException \InvalidArgumentException
*/
public function testWhetherFromStringThrowsExceptionWhenGivenAnInvalidString()
{
$this->expectException(\InvalidArgumentException::class);

Perfdata::fromString('test');
}

Expand Down
Expand Up @@ -16,11 +16,10 @@ protected function curlExec(array $options)

class RestRequestTest extends BaseTestCase
{
/**
* @expectedException \Icinga\Exception\Json\JsonDecodeException
*/
public function testInvalidServerResponseHandling()
{
$this->expectException(\Icinga\Exception\Json\JsonDecodeException::class);

MockedRestRequest::get('http://localhost')->send();
}
}
2 changes: 1 addition & 1 deletion modules/monitoring/test/php/regression/Bug7043Test.php
Expand Up @@ -24,7 +24,7 @@ public static function setModuleConfig($moduleName, $configName, $config)

class Bug7043Test extends BaseTestCase
{
public function tearDown()
public function tearDown(): void
{
parent::tearDown();
Mockery::close(); // Necessary because some tests run in a separate process
Expand Down
4 changes: 2 additions & 2 deletions modules/setup/application/views/scripts/index/index.phtml
Expand Up @@ -9,10 +9,10 @@ $currentPos = array_search($wizard->getCurrentPage(), $pages, true);
list($configPagesLeft, $configPagesRight) = array_chunk($configPages, count($configPages) / 2, true);

$visitedPages = array_keys($wizard->getPageData());
$maxProgress = @max(array_keys(array_filter(
$maxProgress = max(array_merge([0], array_keys(array_filter(
$pages,
function ($page) use ($visitedPages) { return in_array($page->getName(), $visitedPages); }
)));
))));

?>
<div id="setup-content-wrapper" data-base-target="layout">
Expand Down
Expand Up @@ -12,7 +12,7 @@

class DateFormatTest extends BaseTestCase
{
public function tearDown()
public function tearDown(): void
{
DateTimeFactory::setConfig(array('timezone' => date_default_timezone_get()));
}
Expand Down
4 changes: 0 additions & 4 deletions test/php/bootstrap.php
Expand Up @@ -36,10 +36,6 @@

require_once($icingaLibPath . '/Test/ClassLoader.php');

if (! class_exists('PHPUnit_Framework_TestCase')) {
require_once __DIR__ . '/phpunit-compat.php';
}

$loader = new Icinga\Test\ClassLoader();
$loader->registerNamespace('Tests', $testLibraryPath);
$loader->registerNamespace('Icinga', $icingaLibPath);
Expand Down
4 changes: 2 additions & 2 deletions test/php/library/Icinga/Application/ClassLoaderTest.php
Expand Up @@ -26,7 +26,7 @@ public function testFlag()
EOD;

public function setUp()
public function setUp(): void
{
parent::setUp();
$tempDir = sys_get_temp_dir();
Expand All @@ -35,7 +35,7 @@ public function setUp()
file_put_contents($this->baseDir. self::$classFile, self::$classContent);
}

public function tearDown()
public function tearDown(): void
{
parent::tearDown();
system('rm -rf '. $this->baseDir);
Expand Down
14 changes: 6 additions & 8 deletions test/php/library/Icinga/Application/ConfigTest.php
Expand Up @@ -11,7 +11,7 @@ class ConfigTest extends BaseTestCase
/**
* Set up config dir
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
$this->oldConfigDir = Config::$configDir;
Expand All @@ -21,7 +21,7 @@ public function setUp()
/**
* Reset config dir
*/
public function tearDown()
public function tearDown(): void
{
parent::tearDown();
Config::$configDir = $this->oldConfigDir;
Expand Down Expand Up @@ -185,11 +185,10 @@ public function testWhetherConfigKnowsWhichSectionsItHas()
);
}

/**
* @expectedException UnexpectedValueException
*/
public function testWhetherAnExceptionIsThrownWhenTryingToAccessASectionPropertyOnANonSection()
{
$this->expectException(\UnexpectedValueException::class);

$config = Config::fromArray(array('a' => 'b'));
$config->get('a', 'b');
}
Expand Down Expand Up @@ -234,11 +233,10 @@ public function testWhetherItIsPossibleToInitializeAConfigFromAIniFile()
);
}

/**
* @expectedException Icinga\Exception\NotReadableError
*/
public function testWhetherFromIniThrowsAnExceptionOnInsufficientPermission()
{
$this->expectException(\Icinga\Exception\NotReadableError::class);

Config::fromIni('/etc/shadow');
}

Expand Down
15 changes: 6 additions & 9 deletions test/php/library/Icinga/Application/Hook/AuditHookTest.php
Expand Up @@ -32,27 +32,24 @@ public function testFormatMessageResolvesParametersWithSingleBraces()
$this->assertEquals('foo', (new TestAuditHook())->formatMessage('{{te{.}st}}', ['te{' => ['}st' => 'foo']]));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testFormatMessageComplainsAboutUnresolvedParameters()
{
$this->expectException(\InvalidArgumentException::class);

(new TestAuditHook())->formatMessage('{{missing}}', []);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testFormatMessageComplainsAboutNonScalarParameters()
{
$this->expectException(\InvalidArgumentException::class);

(new TestAuditHook())->formatMessage('{{test}}', ['test' => ['foo' => 'bar']]);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testFormatMessageComplainsAboutNonArrayParameters()
{
$this->expectException(\InvalidArgumentException::class);

(new TestAuditHook())->formatMessage('{{test.foo}}', ['test' => 'foo']);
}
}
5 changes: 2 additions & 3 deletions test/php/library/Icinga/Data/ConfigObjectTest.php
Expand Up @@ -115,11 +115,10 @@ public function testWhetherItIsPossibleToSetPropertiesAndSections()
);
}

/**
* @expectedException \Icinga\Exception\ProgrammingError
*/
public function testWhetherItIsNotPossibleToAppendProperties()
{
$this->expectException(\Icinga\Exception\ProgrammingError::class);

$config = new ConfigObject();
$config[] = 'test';
}
Expand Down
Expand Up @@ -10,7 +10,7 @@ class ArrayDatasourceTest extends BaseTestCase
{
private $sampleData;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->sampleData = array(
Expand Down
2 changes: 1 addition & 1 deletion test/php/library/Icinga/Data/Filter/FilterTest.php
Expand Up @@ -61,7 +61,7 @@ class FilterTest extends BaseTestCase

private $sampleData;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->sampleData = array(
Expand Down
4 changes: 2 additions & 2 deletions test/php/library/Icinga/File/Ini/IniParserTest.php
Expand Up @@ -12,13 +12,13 @@ class IniParserTest extends BaseTestCase
{
protected $tempFile;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->tempFile = tempnam(sys_get_temp_dir(), 'icinga-ini-parser-test');
}

public function tearDown()
public function tearDown(): void
{
parent::tearDown();
unlink($this->tempFile);
Expand Down

0 comments on commit 4bc5350

Please sign in to comment.