Skip to content

Commit

Permalink
Support psr/log:^2.0,^3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Oct 13, 2021
1 parent 7ec2e2a commit 8431379
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -66,7 +66,7 @@
"bamarni/composer-bin-plugin": "^1.4.1",
"php-http/client-integration-tests": "^3.0",
"phpunit/phpunit": "^8.5.5 || ^9.3.5",
"psr/log": "^1.1"
"psr/log": "^1.1 || ^2.0 || ^3.0"
},
"suggest": {
"ext-curl": "Required for CURL handler support",
Expand Down
1 change: 0 additions & 1 deletion tests/MiddlewareTest.php
Expand Up @@ -19,7 +19,6 @@
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\Test\TestLogger;

class MiddlewareTest extends TestCase
{
Expand Down
95 changes: 95 additions & 0 deletions tests/TestLogger.php
@@ -0,0 +1,95 @@
<?php

namespace GuzzleHttp\Tests;

use Psr\Log\AbstractLogger;

/**
* Used for testing purposes.
*
* It records all records and gives you access to them for verification.
*/
class TestLogger extends AbstractLogger
{
public $records = [];
public $recordsByLevel = [];

public function log($level, $message, array $context = []): void
{
$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(static 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(static function ($rec) use ($message) {
return strpos($rec['message'], $message) !== false;
}, $level);
}

public function hasRecordThatMatches($regex, $level)
{
return $this->hasRecordThatPasses(static 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 ' . static::class . '::' . $method . '()');
}

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

0 comments on commit 8431379

Please sign in to comment.