Skip to content

Commit

Permalink
feat: adds supports for 7.4 and compat with psr/log 1
Browse files Browse the repository at this point in the history
  • Loading branch information
luislard committed Nov 28, 2023
1 parent 6f2c987 commit 163498e
Show file tree
Hide file tree
Showing 6 changed files with 655 additions and 22 deletions.
18 changes: 3 additions & 15 deletions .github/workflows/php-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,26 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: [ '8.0', '8.1', '8.2' ]
php-versions: [ '7.4', '8.0', '8.1', '8.2' ]
dependency-versions: ['highest', 'lowest']

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use coverage?
if: ${{ matrix.php-versions == '7.4' }}
run: echo "USE_COVERAGE=yes" >> $GITHUB_ENV

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: zend.assertions=1, error_reporting=E_ALL, display_errors=On
coverage: ${{ ((env.USE_COVERAGE == 'yes') && 'xdebug') || 'none' }}
coverage: ${{ env.USE_COVERAGE == 'yes' && 'xdebug' || 'none' }}

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency-versions }}

- name: Run unit tests
run: composer tests:unit:${{ ((env.USE_COVERAGE == 'yes') && 'codecov') || 'no-cov' }}
run: composer tests:unit:no-cov

- name: Update coverage
if: ${{ env.USE_COVERAGE == 'yes' }}
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
flags: unittests
verbose: true

9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
}
],
"require": {
"php": ">=8.0 < 8.3",
"psr/log": "^2.0||^3.0",
"php": "^7.4||>=8.0 < 8.3",
"psr/log": ">1.0.1 <2.0||^2.0||^3.0",
"wecodemore/wordpress-early-hook": "^1.1.0",
"monolog/monolog": "^2.3.5"
"monolog/monolog": ">=2.0.1 <3"
},
"require-dev": {
"brain/monkey": "^2.6.1",
Expand All @@ -52,8 +52,7 @@
"symfony/process": "^v4.4.44",
"globalis/wp-cli-bin": "^2.7.1",
"vimeo/psalm": "^5.15",
"phpunit/phpunit": "^9.6",
"fig/log-test": "^1.1"
"phpunit/phpunit": "^9.6"
},
"provide": {
"psr/log-implementation": "1.0.0"
Expand Down
139 changes: 139 additions & 0 deletions tests/src/TestLogger/AbstractLoggerV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

declare(strict_types=1);

# -*- coding: utf-8 -*-
/**
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author AuthorName <hello@inpsyde.com>
* @license GPLv2+
* @link https://www.inpsyde.com
*/

namespace Inpsyde\Wonolog\Tests\TestLogger;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

abstract class AbstractLoggerV1 implements LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function emergency($message, array $context = array())

Check failure on line 33 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays

Check failure on line 33 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function alert($message, array $context = array())

Check failure on line 49 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays

Check failure on line 49 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays
{
$this->log(LogLevel::ALERT, $message, $context);
}

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function critical($message, array $context = array())

Check failure on line 64 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays

Check failure on line 64 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays
{
$this->log(LogLevel::CRITICAL, $message, $context);
}

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function error($message, array $context = array())

Check failure on line 78 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays

Check failure on line 78 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays
{
$this->log(LogLevel::ERROR, $message, $context);
}

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function warning($message, array $context = array())

Check failure on line 94 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays

Check failure on line 94 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays
{
$this->log(LogLevel::WARNING, $message, $context);
}

/**
* Normal but significant events.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function notice($message, array $context = array())

Check failure on line 107 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays

Check failure on line 107 in tests/src/TestLogger/AbstractLoggerV1.php

View workflow job for this annotation

GitHub Actions / coding-standards-analysis-php / coding-standards-php

Short array syntax must be used to define arrays
{
$this->log(LogLevel::NOTICE, $message, $context);
}

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function info($message, array $context = array())
{
$this->log(LogLevel::INFO, $message, $context);
}

/**
* Detailed debug information.
*
* @param string $message
* @param mixed[] $context
*
* @return void
*/
public function debug($message, array $context = array())
{
$this->log(LogLevel::DEBUG, $message, $context);
}
}
161 changes: 161 additions & 0 deletions tests/src/TestLogger/TestLoggerV1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);

# -*- coding: utf-8 -*-
/**
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author AuthorName <hello@inpsyde.com>
* @license GPLv2+
* @link https://www.inpsyde.com
*/

namespace Inpsyde\Wonolog\Tests\TestLogger;

/**
* Used for testing purposes.
*
* It records all records and gives you access to them for verification.
*
* @method bool hasEmergency($record)
* @method bool hasAlert($record)
* @method bool hasCritical($record)
* @method bool hasError($record)
* @method bool hasWarning($record)
* @method bool hasNotice($record)
* @method bool hasInfo($record)
* @method bool hasDebug($record)
*
* @method bool hasEmergencyRecords()
* @method bool hasAlertRecords()
* @method bool hasCriticalRecords()
* @method bool hasErrorRecords()
* @method bool hasWarningRecords()
* @method bool hasNoticeRecords()
* @method bool hasInfoRecords()
* @method bool hasDebugRecords()
*
* @method bool hasEmergencyThatContains($message)
* @method bool hasAlertThatContains($message)
* @method bool hasCriticalThatContains($message)
* @method bool hasErrorThatContains($message)
* @method bool hasWarningThatContains($message)
* @method bool hasNoticeThatContains($message)
* @method bool hasInfoThatContains($message)
* @method bool hasDebugThatContains($message)
*
* @method bool hasEmergencyThatMatches($message)
* @method bool hasAlertThatMatches($message)
* @method bool hasCriticalThatMatches($message)
* @method bool hasErrorThatMatches($message)
* @method bool hasWarningThatMatches($message)
* @method bool hasNoticeThatMatches($message)
* @method bool hasInfoThatMatches($message)
* @method bool hasDebugThatMatches($message)
*
* @method bool hasEmergencyThatPasses($message)
* @method bool hasAlertThatPasses($message)
* @method bool hasCriticalThatPasses($message)
* @method bool hasErrorThatPasses($message)
* @method bool hasWarningThatPasses($message)
* @method bool hasNoticeThatPasses($message)
* @method bool hasInfoThatPasses($message)
* @method bool hasDebugThatPasses($message)
*/
class TestLoggerV1 extends AbstractLoggerV1
{
/**
* @var array
*/
public $records = [];

public $recordsByLevel = [];

/**
* @inheritdoc
*/
public function log($level, $message, array $context = [])
{
$record = [
'level' => $level,
'message' => $message,
'context' => $context,
];

$this->recordsByLevel[$record['level']][] = $record;
$this->records[] = $record;
}

public function hasRecords($level)
{
return isset($this->recordsByLevel[$level]);
}

public function hasRecord($record, $level)
{
if (is_string($record)) {
$record = ['message' => $record];
}
return $this->hasRecordThatPasses(function ($rec) use ($record) {
if ($rec['message'] !== $record['message']) {
return false;
}
if (isset($record['context']) && $rec['context'] !== $record['context']) {
return false;
}
return true;
}, $level);
}

public function hasRecordThatContains($message, $level)
{
return $this->hasRecordThatPasses(function ($rec) use ($message) {
return strpos($rec['message'], $message) !== false;
}, $level);
}

public function hasRecordThatMatches($regex, $level)
{
return $this->hasRecordThatPasses(function ($rec) use ($regex) {
return preg_match($regex, $rec['message']) > 0;
}, $level);
}

public function hasRecordThatPasses(callable $predicate, $level)
{
if (!isset($this->recordsByLevel[$level])) {
return false;
}
foreach ($this->recordsByLevel[$level] as $i => $rec) {
if (call_user_func($predicate, $rec, $i)) {
return true;
}
}
return false;
}

public function __call($method, $args)
{
if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
$genericMethod = $matches[1] . ('Records' !== $matches[3] ? 'Record' : '') . $matches[3];
$level = strtolower($matches[2]);
if (method_exists($this, $genericMethod)) {
$args[] = $level;
return call_user_func_array([$this, $genericMethod], $args);
}
}
throw new \BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
}

public function reset()
{
$this->records = [];
$this->recordsByLevel = [];
}

}
Loading

0 comments on commit 163498e

Please sign in to comment.