Skip to content

Commit a626308

Browse files
authored
Merge pull request #16 from recca0120/upgrade-psr-log
Support psr/log 2, 3
2 parents b03bbd4 + 8aaa3c7 commit a626308

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
php: ['7.2', '7.3', '7.4', '8.0', '8.1']
13+
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
1414

1515
steps:
1616
- name: Checkout code

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php": "^7.2 || ^8.0",
1515
"guzzlehttp/psr7": "^1.7 || ^2.0",
1616
"php-http/client-common": "^2.0",
17-
"psr/log": "^1.1",
17+
"psr/log": "^1.1 || ^2.0 || ^3.0",
1818
"symfony/filesystem": "^4.0|^5.0|^6.0",
1919
"symfony/options-resolver": "^4.0|^5.0|^6.0"
2020
},

tests/Recorder/FilesystemRecorderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use GuzzleHttp\Psr7\Response;
88
use Http\Client\Plugin\Vcr\Recorder\FilesystemRecorder;
9+
use Http\Client\Plugin\Vcr\Tests\Stub\TestLogger;
910
use PHPUnit\Framework\TestCase;
10-
use Psr\Log\Test\TestLogger;
1111
use Symfony\Component\Filesystem\Filesystem;
1212

1313
/**

tests/Stub/TestLogger.php

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin\Vcr\Tests\Stub;
4+
5+
use Psr\Log\AbstractLogger;
6+
7+
/**
8+
* Used for testing purposes.
9+
*
10+
* It records all records and gives you access to them for verification.
11+
*
12+
* @method bool hasEmergency($record)
13+
* @method bool hasAlert($record)
14+
* @method bool hasCritical($record)
15+
* @method bool hasError($record)
16+
* @method bool hasWarning($record)
17+
* @method bool hasNotice($record)
18+
* @method bool hasInfo($record)
19+
* @method bool hasDebug($record)
20+
* @method bool hasEmergencyRecords()
21+
* @method bool hasAlertRecords()
22+
* @method bool hasCriticalRecords()
23+
* @method bool hasErrorRecords()
24+
* @method bool hasWarningRecords()
25+
* @method bool hasNoticeRecords()
26+
* @method bool hasInfoRecords()
27+
* @method bool hasDebugRecords()
28+
* @method bool hasEmergencyThatContains($message)
29+
* @method bool hasAlertThatContains($message)
30+
* @method bool hasCriticalThatContains($message)
31+
* @method bool hasErrorThatContains($message)
32+
* @method bool hasWarningThatContains($message)
33+
* @method bool hasNoticeThatContains($message)
34+
* @method bool hasInfoThatContains($message)
35+
* @method bool hasDebugThatContains($message)
36+
* @method bool hasEmergencyThatMatches($message)
37+
* @method bool hasAlertThatMatches($message)
38+
* @method bool hasCriticalThatMatches($message)
39+
* @method bool hasErrorThatMatches($message)
40+
* @method bool hasWarningThatMatches($message)
41+
* @method bool hasNoticeThatMatches($message)
42+
* @method bool hasInfoThatMatches($message)
43+
* @method bool hasDebugThatMatches($message)
44+
* @method bool hasEmergencyThatPasses($message)
45+
* @method bool hasAlertThatPasses($message)
46+
* @method bool hasCriticalThatPasses($message)
47+
* @method bool hasErrorThatPasses($message)
48+
* @method bool hasWarningThatPasses($message)
49+
* @method bool hasNoticeThatPasses($message)
50+
* @method bool hasInfoThatPasses($message)
51+
* @method bool hasDebugThatPasses($message)
52+
*/
53+
class TestLogger extends AbstractLogger
54+
{
55+
/**
56+
* @var array
57+
*/
58+
public $records = [];
59+
60+
public $recordsByLevel = [];
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function log($level, $message, array $context = []): void
66+
{
67+
$record = [
68+
'level' => $level,
69+
'message' => $message,
70+
'context' => $context,
71+
];
72+
73+
$this->recordsByLevel[$record['level']][] = $record;
74+
$this->records[] = $record;
75+
}
76+
77+
public function hasRecords($level)
78+
{
79+
return isset($this->recordsByLevel[$level]);
80+
}
81+
82+
public function hasRecord($record, $level)
83+
{
84+
if (is_string($record)) {
85+
$record = ['message' => $record];
86+
}
87+
88+
return $this->hasRecordThatPasses(function ($rec) use ($record) {
89+
if ($rec['message'] !== $record['message']) {
90+
return false;
91+
}
92+
if (isset($record['context']) && $rec['context'] !== $record['context']) {
93+
return false;
94+
}
95+
96+
return true;
97+
}, $level);
98+
}
99+
100+
public function hasRecordThatContains($message, $level)
101+
{
102+
return $this->hasRecordThatPasses(function ($rec) use ($message) {
103+
return false !== strpos($rec['message'], $message);
104+
}, $level);
105+
}
106+
107+
public function hasRecordThatMatches($regex, $level)
108+
{
109+
return $this->hasRecordThatPasses(function ($rec) use ($regex) {
110+
return preg_match($regex, $rec['message']) > 0;
111+
}, $level);
112+
}
113+
114+
public function hasRecordThatPasses(callable $predicate, $level)
115+
{
116+
if (!isset($this->recordsByLevel[$level])) {
117+
return false;
118+
}
119+
foreach ($this->recordsByLevel[$level] as $i => $rec) {
120+
if (call_user_func($predicate, $rec, $i)) {
121+
return true;
122+
}
123+
}
124+
125+
return false;
126+
}
127+
128+
public function __call($method, $args)
129+
{
130+
if (preg_match('/(.*)(Debug|Info|Notice|Warning|Error|Critical|Alert|Emergency)(.*)/', $method, $matches) > 0) {
131+
$genericMethod = $matches[1].('Records' !== $matches[3] ? 'Record' : '').$matches[3];
132+
$level = strtolower($matches[2]);
133+
if (method_exists($this, $genericMethod)) {
134+
$args[] = $level;
135+
136+
return call_user_func_array([$this, $genericMethod], $args);
137+
}
138+
}
139+
throw new \BadMethodCallException('Call to undefined method '.get_class($this).'::'.$method.'()');
140+
}
141+
142+
public function reset()
143+
{
144+
$this->records = [];
145+
$this->recordsByLevel = [];
146+
}
147+
}

0 commit comments

Comments
 (0)