Skip to content

Commit

Permalink
Add compatibility with PHP 8.1 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
gusiushub authored Nov 23, 2023
1 parent 55d732a commit 44c724d
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- 7.2
- 7.3
- 7.4
- 8.1

steps:
- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
],
"require": {
"yiisoft/yii2": "~2.0.0",
"psr/log": "~1.0.2|~1.1.0"
"psr/log": "~1.0.2|~1.1.0|~3.0.0"
},
"require-dev": {
"phpunit/phpunit": "~4.4"
"phpunit/phpunit": "~4.4|~10.4.2"
},
"config": {
"process-timeout": 1800,
Expand Down
17 changes: 6 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
21 changes: 21 additions & 0 deletions tests/NewPsrArrayLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace samdark\log\tests;

use Psr\Log\AbstractLogger;

class NewPsrArrayLogger extends AbstractLogger
{
public array $logs = [];

/**
* @inheritdoc
*/
public function log($level, string|\Stringable $message, array $context = []): void
{
$this->logs[] = [
'level' => $level,
'message' => $message,
'context' => $context,
];
}
}
20 changes: 20 additions & 0 deletions tests/PsrArrayLoggerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace samdark\log\tests;

use Psr\Log\AbstractLogger;

class PsrArrayLoggerFactory
{
/**
* @return AbstractLogger
*/
public static function getInstance()
{
if (version_compare(PHP_VERSION, '8.0', '<')) {
return new PsrArrayLogger();
} else {
return new NewPsrArrayLogger();
}
}
}

41 changes: 30 additions & 11 deletions tests/PsrTargetLevelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

use Psr\Log\LogLevel;
use samdark\log\PsrTarget;
use yii\base\InvalidConfigException;
use yii\log\Dispatcher;
use yii\log\Logger;
use PHPUnit\Framework\TestCase;

class PsrTargetLevelsTest extends \PHPUnit_Framework_TestCase
class PsrTargetLevelsTest extends TestCase
{
public function testYiiLevelsDataProvider()
/**
* @doesNotPerformAssertions
*/
public static function testYiiLevelsDataProvider()
{
$context = [
'category' => 'application',
Expand Down Expand Up @@ -73,7 +76,7 @@ public function testYiiLevelsDataProvider()
*/
public function testYiiLevelsFilter($message, $level, $expected)
{
$psrLogger = new PsrArrayLogger();
$psrLogger = PsrArrayLoggerFactory::getInstance();
$logger = new Logger();
new Dispatcher([
'logger' => $logger,
Expand Down Expand Up @@ -101,7 +104,10 @@ public function testYiiLevelsFilter($message, $level, $expected)
$this->assertEquals($expected, $logEntry);
}

public function testPsrLevelsDataProvider()
/**
* @doesNotPerformAssertions
*/
public static function testPsrLevelsDataProvider()
{
$context = [
'category' => 'application',
Expand Down Expand Up @@ -182,7 +188,7 @@ public function testPsrLevelsDataProvider()
*/
public function testPsrLevelsFilter($message, $level, $expected)
{
$psrLogger = new PsrArrayLogger();
$psrLogger = PsrArrayLoggerFactory::getInstance();
$logger = new Logger();
new Dispatcher([
'logger' => $logger,
Expand Down Expand Up @@ -210,7 +216,10 @@ public function testPsrLevelsFilter($message, $level, $expected)
$this->assertEquals($expected, $logEntry);
}

public function testMixedLevelsDataProvider()
/**
* @doesNotPerformAssertions
*/
public static function testMixedLevelsDataProvider()
{
$context = [
'category' => 'application',
Expand Down Expand Up @@ -258,7 +267,7 @@ public function testMixedLevelsDataProvider()
*/
public function testMixedLevelsFilter($message, $level, $expected)
{
$psrLogger = new PsrArrayLogger();
$psrLogger = PsrArrayLoggerFactory::getInstance();
$logger = new Logger();
new Dispatcher([
'logger' => $logger,
Expand Down Expand Up @@ -287,19 +296,29 @@ public function testMixedLevelsFilter($message, $level, $expected)

public function testIncorrectLevelsTypeFilter()
{
$this->setExpectedException('yii\base\InvalidConfigException');
if (method_exists(TestCase::class, 'setExpectedException')) {
$this->setExpectedException('yii\base\InvalidConfigException');
} else {
$this->expectException('yii\base\InvalidConfigException');
}

new PsrTarget(['levels' => 'string']);
}

public function testIncorrectLevelsFilter()
{
$this->setExpectedException('yii\base\InvalidConfigException');
if (method_exists(TestCase::class, 'setExpectedException')) {
$this->setExpectedException('yii\base\InvalidConfigException');
} else {
$this->expectException('yii\base\InvalidConfigException');
}

new PsrTarget(['levels' => ['not existing level']]);
}

public function testContextMessageNotFiltered()
{
$psrLogger = new PsrArrayLogger();
$psrLogger = PsrArrayLoggerFactory::getInstance();
$logger = new Logger();
new Dispatcher([
'logger' => $logger,
Expand Down
10 changes: 7 additions & 3 deletions tests/PsrTargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
use Psr\Log\LogLevel;
use yii\log\Dispatcher;
use yii\log\Logger;
use PHPUnit\Framework\TestCase;

class PsrTargetTest extends \PHPUnit_Framework_TestCase
class PsrTargetTest extends TestCase
{
public function testLogDataProvider()
/**
* @doesNotPerformAssertions
*/
public static function testLogDataProvider()
{
$context = [
'category' => 'application',
Expand Down Expand Up @@ -103,7 +107,7 @@ public function testLogDataProvider()
*/
public function testLog($message, $level, $expected)
{
$psrLogger = new PsrArrayLogger();
$psrLogger = PsrArrayLoggerFactory::getInstance();

$logger = new Logger();
new Dispatcher([
Expand Down

0 comments on commit 44c724d

Please sign in to comment.