move logger handling to separate class
Dieser Commit ist enthalten in:
Ursprung
621ea0ebe9
Commit
5cbbc8295f
@ -18,6 +18,7 @@ namespace D3\LinkmobilityClient\Tests;
|
||||
use Assert\InvalidArgumentException;
|
||||
use D3\LinkmobilityClient\Client;
|
||||
use D3\LinkmobilityClient\Exceptions\ApiException;
|
||||
use D3\LinkmobilityClient\LoggerHandler;
|
||||
use D3\LinkmobilityClient\Request\RequestInterface;
|
||||
use D3\LinkmobilityClient\Response\Response;
|
||||
use D3\LinkmobilityClient\Response\ResponseInterface;
|
||||
@ -170,13 +171,12 @@ class ClientTest extends ApiTestCase
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @param $useLogger
|
||||
* @param $okStatus
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @dataProvider rawRequestDataProvider
|
||||
*/
|
||||
public function testRawRequest($useLogger, $okStatus)
|
||||
public function testRawRequest($okStatus)
|
||||
{
|
||||
$statusCode = $okStatus ? '200' : '301';
|
||||
|
||||
@ -205,7 +205,7 @@ class ClientTest extends ApiTestCase
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$responseMock->expects($this->atLeastOnce())->method('getStatusCode')->willReturn($statusCode);
|
||||
$responseMock->expects($useLogger && $okStatus ? $this->atLeastOnce() : $this->never())
|
||||
$responseMock->expects($okStatus ? $this->atLeastOnce() : $this->never())
|
||||
->method('getBody')->willReturn($streamMock);
|
||||
|
||||
/** @var GuzzleClient|MockObject $requestClientMock */
|
||||
@ -219,17 +219,21 @@ class ClientTest extends ApiTestCase
|
||||
->onlyMethods(['debug', 'error', 'log'])
|
||||
->getMock();
|
||||
|
||||
/** @var LoggerHandler|MockObject $loggerHandlerMock */
|
||||
$loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class)
|
||||
->onlyMethods(['getLogger'])
|
||||
->getMock();
|
||||
$loggerHandlerMock->method('getLogger')->willReturn($loggerMock);
|
||||
|
||||
/** @var Client|MockObject $clientMock */
|
||||
$clientMock = $this->getMockBuilder(Client::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods([
|
||||
'hasLogger',
|
||||
'getLogger'
|
||||
'getLoggerHandler'
|
||||
])
|
||||
->getMock();
|
||||
$clientMock->method('hasLogger')->willReturn($useLogger);
|
||||
$clientMock->expects($useLogger ? $this->atLeastOnce() : $this->never())
|
||||
->method('getLogger')->willReturn($loggerMock);
|
||||
$clientMock->expects($this->atLeastOnce())
|
||||
->method('getLoggerHandler')->willReturn($loggerHandlerMock);
|
||||
$this->setValue($clientMock, 'requestClient', $requestClientMock);
|
||||
|
||||
if (false === $okStatus) {
|
||||
@ -247,37 +251,8 @@ class ClientTest extends ApiTestCase
|
||||
public function rawRequestDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'has logger, OK status' => [true, true],
|
||||
'has no logger, OK status' => [false, true],
|
||||
'has logger, NOK status' => [true, false],
|
||||
'has no logger, NOK status' => [false, false],
|
||||
'OK status' => [true],
|
||||
'NOK status' => [false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function testLogger()
|
||||
{
|
||||
$this->assertFalse($this->callMethod($this->api, 'hasLogger'));
|
||||
$this->assertNull($this->callMethod($this->api, 'getLogger'));
|
||||
|
||||
/** @var LoggerInterface|MockObject $loggerMock */
|
||||
$loggerMock = $this->getMockBuilder(AbstractLogger::class)
|
||||
->onlyMethods(['debug', 'error', 'log'])
|
||||
->getMock();
|
||||
|
||||
$this->assertSame(
|
||||
$this->api,
|
||||
$this->callMethod($this->api, 'setLogger', [$loggerMock])
|
||||
);
|
||||
|
||||
$this->assertTrue($this->callMethod($this->api, 'hasLogger'));
|
||||
$this->assertSame(
|
||||
$loggerMock,
|
||||
$this->callMethod($this->api, 'getLogger')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
79
Tests/LoggerHandlerTest.php
Normale Datei
79
Tests/LoggerHandlerTest.php
Normale Datei
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\LinkmobilityClient\Tests;
|
||||
|
||||
use D3\LinkmobilityClient\LoggerHandler;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionException;
|
||||
|
||||
class LoggerHandlerTest extends ApiTestCase
|
||||
{
|
||||
/** @var LoggerHandler */
|
||||
public $loggerHandler;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->loggerHandler = new LoggerHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
unset($this->loggerHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function testConstruct()
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
LoggerInterface::class,
|
||||
$this->callMethod($this->loggerHandler, 'getLogger')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function testLogger()
|
||||
{
|
||||
/** @var LoggerInterface|MockObject $loggerMock */
|
||||
$loggerMock = $this->getMockBuilder(AbstractLogger::class)
|
||||
->onlyMethods(['debug', 'error', 'log'])
|
||||
->getMock();
|
||||
|
||||
$this->callMethod( $this->loggerHandler, 'setLogger', [ $loggerMock]);
|
||||
$this->assertSame(
|
||||
$loggerMock,
|
||||
$this->callMethod( $this->loggerHandler, 'getLogger')
|
||||
);
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ declare(strict_types=1);
|
||||
namespace D3\LinkmobilityClient\Tests\RecipientsList;
|
||||
|
||||
use D3\LinkmobilityClient\Client;
|
||||
use D3\LinkmobilityClient\LoggerHandler;
|
||||
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
|
||||
use D3\LinkmobilityClient\Tests\ApiTestCase;
|
||||
use D3\LinkmobilityClient\ValueObject\Recipient;
|
||||
@ -183,13 +184,18 @@ class RecipientsListTest extends ApiTestCase
|
||||
->getMock();
|
||||
$loggerMock->expects($this->atLeastOnce())->method('info')->willReturn(true);
|
||||
|
||||
/** @var LoggerHandler|MockObject $loggerHandlerMock */
|
||||
$loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class)
|
||||
->onlyMethods(['getLogger'])
|
||||
->getMock();
|
||||
$loggerHandlerMock->method('getLogger')->willReturn($loggerMock);
|
||||
|
||||
/** @var Client|MockObject $clientMock */
|
||||
$clientMock = $this->getMockBuilder(Client::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['hasLogger', 'getLogger'])
|
||||
->onlyMethods(['getLoggerHandler'])
|
||||
->getMock();
|
||||
$clientMock->method('hasLogger')->willReturn(true);
|
||||
$clientMock->method('getLogger')->willReturn($loggerMock);
|
||||
$clientMock->method('getLoggerHandler')->willReturn($loggerHandlerMock);
|
||||
|
||||
/** @var RecipientsList|MockObject $recListMock */
|
||||
$recListMock = $this->getMockBuilder(RecipientsList::class)
|
||||
|
@ -30,8 +30,6 @@ class Client
|
||||
public $apiUrl;
|
||||
public $requestClient;
|
||||
|
||||
private $logger;
|
||||
|
||||
public function __construct(string $accessToken, UrlInterface $apiUrl = null, ClientInterface $client = null)
|
||||
{
|
||||
$this->accessToken = $accessToken;
|
||||
@ -69,9 +67,7 @@ class Client
|
||||
{
|
||||
$options['headers']['Authorization'] = 'Bearer '.$this->accessToken;
|
||||
|
||||
if ($this->hasLogger()) {
|
||||
$this->getLogger()->debug('request '.$url, $options);
|
||||
}
|
||||
$this->getLoggerHandler()->getLogger()->debug('request '.$url, $options);
|
||||
|
||||
$response = $this->requestClient->request(
|
||||
$method,
|
||||
@ -81,45 +77,21 @@ class Client
|
||||
|
||||
if ($response->getStatusCode() != 200) {
|
||||
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode());
|
||||
if ($this->hasLogger()) {
|
||||
$this->getLogger()->error($message);
|
||||
}
|
||||
$this->getLoggerHandler()->getLogger()->error($message);
|
||||
throw new ApiException($message);
|
||||
}
|
||||
|
||||
if ($this->hasLogger()) {
|
||||
$response->getBody()->rewind();
|
||||
$this->getLogger()->debug('response', [$response->getBody()->getContents()]);
|
||||
}
|
||||
$response->getBody()->rewind();
|
||||
$this->getLoggerHandler()->getLogger()->debug('response', [$response->getBody()->getContents()]);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $logger
|
||||
*
|
||||
* @return Client
|
||||
* @return LoggerHandler
|
||||
*/
|
||||
public function setLogger(LoggerInterface $logger): Client
|
||||
public function getLoggerHandler(): LoggerHandler
|
||||
{
|
||||
$this->logger = $logger;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasLogger(): bool
|
||||
{
|
||||
return $this->logger instanceof LoggerInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LoggerInterface|null
|
||||
*/
|
||||
public function getLogger()
|
||||
{
|
||||
return $this->hasLogger() ? $this->logger : null;
|
||||
return LoggerHandler::getInstance();
|
||||
}
|
||||
}
|
||||
|
55
src/LoggerHandler.php
Normale Datei
55
src/LoggerHandler.php
Normale Datei
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\LinkmobilityClient;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
class LoggerHandler
|
||||
{
|
||||
private static $instance = null;
|
||||
private $logger;
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->setLogger(new NullLogger());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $logger
|
||||
*/
|
||||
public function setLogger(LoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LoggerInterface
|
||||
*/
|
||||
public function getLogger() : LoggerInterface
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ namespace D3\LinkmobilityClient\RecipientsList;
|
||||
use D3\LinkmobilityClient\Client;
|
||||
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
|
||||
use D3\LinkmobilityClient\Exceptions\RecipientException;
|
||||
use D3\LinkmobilityClient\LoggerHandler;
|
||||
use D3\LinkmobilityClient\ValueObject\Recipient;
|
||||
use Iterator;
|
||||
use libphonenumber\NumberParseException;
|
||||
@ -76,13 +77,9 @@ class RecipientsList implements RecipientsListInterface, Iterator
|
||||
|
||||
$this->recipients[ md5(serialize($recipient)) ] = $recipient;
|
||||
} catch (NumberParseException $e) {
|
||||
if ($this->getClient()->hasLogger()) {
|
||||
$this->getClient()->getLogger()->info($e->getMessage());
|
||||
}
|
||||
$this->client->getLoggerHandler()->getLogger()->info($e->getMessage());
|
||||
} catch (RecipientException $e) {
|
||||
if ($this->getClient()->hasLogger()) {
|
||||
$this->getClient()->getLogger()->info($e->getMessage());
|
||||
}
|
||||
$this->client->getLoggerHandler()->getLogger()->info($e->getMessage());
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren