Skip to content

Commit 58c6f86

Browse files
authored
Merge pull request #211 from andershagbard/psr-log-update
Create mock for psr/log to allow updating package
2 parents ab5b2a7 + 98d36b3 commit 58c6f86

File tree

6 files changed

+163
-18
lines changed

6 files changed

+163
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
## Unreleased
99

10+
- [#211](https://github.com/Shopify/shopify-api-php/pull/211) Change requirement of `psr/log` from `^1.1` to `^1.1 || ^2.0 || ^3.0`
1011
- [#210](https://github.com/Shopify/shopify-api-php/pull/210) Add `ext-json` as a dependency in `composer.json`
1112
- [#212](https://github.com/Shopify/shopify-api-php/pull/212) Allow a scheme in the `Context::$HOST_NAME` URL to enable support for HTTP apps
1213

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"php": "^7.4 || ^8.0 || ^8.1",
3535
"ext-json": "*",
3636
"ramsey/uuid": "^4.1",
37-
"psr/log": "^1.1",
37+
"psr/log": "^1.1 || ^2.0 || ^3.0",
3838
"firebase/php-jwt": "^5.2",
3939
"psr/http-client": "^1.0",
4040
"guzzlehttp/guzzle": "^7.0",

composer.lock

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Clients/HttpTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use org\bovigo\vfs\vfsStream;
88
use PHPUnit\Framework\MockObject\MockObject;
9-
use Psr\Log\Test\TestLogger;
109
use Shopify\Clients\Http;
1110
use Shopify\Context;
1211
use ShopifyTest\BaseTestCase;
1312
use ShopifyTest\HttpResponseMatcher;
13+
use ShopifyTest\LogMock;
1414

1515
final class HttpTest extends BaseTestCase
1616
{
@@ -442,7 +442,7 @@ public function testDeprecatedRequestsAreLogged()
442442
->method('getApiDeprecationTimestampFilePath')
443443
->willReturn(vfsStream::url('test/timestamp_file'));
444444

445-
$testLogger = new TestLogger();
445+
$testLogger = new LogMock();
446446
Context::$LOGGER = $testLogger;
447447

448448
$this->mockTransportRequests([
@@ -480,7 +480,7 @@ public function testDeprecationLogBackoffPeriod()
480480
->method('getApiDeprecationTimestampFilePath')
481481
->willReturn(vfsStream::url('test/timestamp_file'));
482482

483-
$testLogger = new TestLogger();
483+
$testLogger = new LogMock();
484484
Context::$LOGGER = $testLogger;
485485

486486
$this->mockTransportRequests([

tests/ContextTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace ShopifyTest;
66

77
use Psr\Log\LogLevel;
8-
use Psr\Log\Test\TestLogger;
98
use ReflectionClass;
109
use Shopify\Auth\Scopes;
1110
use Shopify\Context;
@@ -82,7 +81,7 @@ public function testThrowsIfPrivateApp()
8281

8382
public function testCanAddOverrideLogger()
8483
{
85-
$testLogger = new TestLogger();
84+
$testLogger = new LogMock();
8685

8786
Context::log('Logging something!', LogLevel::DEBUG);
8887
$this->assertEmpty($testLogger->records);

tests/LogMock.php

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

0 commit comments

Comments
 (0)