Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

- [#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`
- [#210](https://github.com/Shopify/shopify-api-php/pull/210) Add `ext-json` as a dependency in `composer.json`
- [#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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"php": "^7.4 || ^8.0 || ^8.1",
"ext-json": "*",
"ramsey/uuid": "^4.1",
"psr/log": "^1.1",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"firebase/php-jwt": "^5.2",
"psr/http-client": "^1.0",
"guzzlehttp/guzzle": "^7.0",
Expand Down
25 changes: 13 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/Clients/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\Test\TestLogger;
use Shopify\Clients\Http;
use Shopify\Context;
use ShopifyTest\BaseTestCase;
use ShopifyTest\HttpResponseMatcher;
use ShopifyTest\LogMock;

final class HttpTest extends BaseTestCase
{
Expand Down Expand Up @@ -442,7 +442,7 @@ public function testDeprecatedRequestsAreLogged()
->method('getApiDeprecationTimestampFilePath')
->willReturn(vfsStream::url('test/timestamp_file'));

$testLogger = new TestLogger();
$testLogger = new LogMock();
Context::$LOGGER = $testLogger;

$this->mockTransportRequests([
Expand Down Expand Up @@ -480,7 +480,7 @@ public function testDeprecationLogBackoffPeriod()
->method('getApiDeprecationTimestampFilePath')
->willReturn(vfsStream::url('test/timestamp_file'));

$testLogger = new TestLogger();
$testLogger = new LogMock();
Context::$LOGGER = $testLogger;

$this->mockTransportRequests([
Expand Down
3 changes: 1 addition & 2 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace ShopifyTest;

use Psr\Log\LogLevel;
use Psr\Log\Test\TestLogger;
use ReflectionClass;
use Shopify\Auth\Scopes;
use Shopify\Context;
Expand Down Expand Up @@ -82,7 +81,7 @@ public function testThrowsIfPrivateApp()

public function testCanAddOverrideLogger()
{
$testLogger = new TestLogger();
$testLogger = new LogMock();

Context::log('Logging something!', LogLevel::DEBUG);
$this->assertEmpty($testLogger->records);
Expand Down
144 changes: 144 additions & 0 deletions tests/LogMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

namespace ShopifyTest;

/**
* 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 LogMock
{
/** @var array */
public $records = [];

/** @var array */
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 = [];
}
}