From f7418e06b0748d0e07c1a81c77d2a1c1c9dbd8d3 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Fri, 6 Jan 2023 23:42:17 +0100 Subject: [PATCH] add tests for MessageClient class --- src/Application/Model/MessageClient.php | 16 ++++- src/Config/linkmobilityphpclient.yaml | 9 +++ src/Config/oxid.yaml | 17 ++++- .../Application/Model/MessageClientTest.php | 65 +++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 src/tests/unit/Application/Model/MessageClientTest.php diff --git a/src/Application/Model/MessageClient.php b/src/Application/Model/MessageClient.php index 9d16f2d..38bd7e5 100644 --- a/src/Application/Model/MessageClient.php +++ b/src/Application/Model/MessageClient.php @@ -17,7 +17,7 @@ namespace D3\Linkmobility4OXID\Application\Model; use D3\LinkmobilityClient\Client; use D3\LinkmobilityClient\LoggerHandler; -use OxidEsales\Eshop\Core\Registry; +use Psr\Log\LoggerInterface; class MessageClient { @@ -26,8 +26,18 @@ class MessageClient */ public function getClient(): Client { - $client = oxNew(Client::class, oxNew(Configuration::class)->getApiToken()); - LoggerHandler::getInstance()->setLogger(Registry::getLogger()); + /** @var Configuration $configuration */ + $configuration = d3GetOxidDIC()->get(Configuration::class); + + d3GetOxidDIC()->setParameter(Client::class.'.args.accesstoken', $configuration->getApiToken()); + /** @var Client $client */ + $client = d3GetOxidDIC()->get(Client::class); + + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + /** @var LoggerInterface $logger */ + $logger = d3GetOxidDIC()->get('d3ox.linkmobility.'.LoggerInterface::class); + $loggerHandler->setLogger($logger); return $client; } diff --git a/src/Config/linkmobilityphpclient.yaml b/src/Config/linkmobilityphpclient.yaml index f97ef66..2444d1c 100644 --- a/src/Config/linkmobilityphpclient.yaml +++ b/src/Config/linkmobilityphpclient.yaml @@ -8,6 +8,7 @@ # @link https://www.oxidmodule.com parameters: + D3\LinkmobilityClient\Client.args.accesstoken: D3\LinkmobilityClient\ValueObject\Recipient.args.number: D3\LinkmobilityClient\ValueObject\Recipient.args.iso2countrycode: D3\LinkmobilityClient\ValueObject\Sender.args.number: @@ -25,6 +26,14 @@ services: - 'getInstance' shared: true + D3\LinkmobilityClient\Client: + class: D3\LinkmobilityClient\Client + factory: 'oxNew' + shared: false + arguments: + - D3\LinkmobilityClient\Client + - '%D3\LinkmobilityClient\Client.args.accesstoken%' + D3\LinkmobilityClient\ValueObject\Recipient: class: D3\LinkmobilityClient\ValueObject\Recipient factory: 'oxNew' diff --git a/src/Config/oxid.yaml b/src/Config/oxid.yaml index c716851..8d2912d 100644 --- a/src/Config/oxid.yaml +++ b/src/Config/oxid.yaml @@ -16,6 +16,14 @@ services: autowire: false public: true + # config + d3ox.linkmobility.OxidEsales\Eshop\Core\Config: + class: 'OxidEsales\Eshop\Core\Config' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getConfig' + shared: true + # lang d3ox.linkmobility.OxidEsales\Eshop\Core\Language: class: 'OxidEsales\Eshop\Core\Session' @@ -56,4 +64,11 @@ services: factory: 'oxNew' arguments: - 'OxidEsales\Eshop\Application\Model\Country' - shared: false \ No newline at end of file + shared: false + + d3ox.linkmobility.Psr\Log\LoggerInterface: + class: Psr\Log\LoggerInterface + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getLogger' + shared: true \ No newline at end of file diff --git a/src/tests/unit/Application/Model/MessageClientTest.php b/src/tests/unit/Application/Model/MessageClientTest.php new file mode 100644 index 0000000..6dbab22 --- /dev/null +++ b/src/tests/unit/Application/Model/MessageClientTest.php @@ -0,0 +1,65 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Linkmobility4OXID\tests\unit\Application\Model; + +use D3\Linkmobility4OXID\Application\Model\Configuration; +use D3\Linkmobility4OXID\Application\Model\MessageClient; +use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase; +use D3\LinkmobilityClient\Client; +use D3\LinkmobilityClient\LoggerHandler; +use D3\TestingTools\Development\CanAccessRestricted; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use ReflectionException; + +class MessageClientTest extends LMUnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageClient::getClient + */ + public function canGetClient() + { + /** @var Configuration|MockObject $configurationMock */ + $configurationMock = $this->getMockBuilder(Configuration::class) + ->onlyMethods(['getApiToken']) + ->getMock(); + $configurationMock->method('getApiToken')->willReturn('apiTokenFixture'); + d3GetOxidDIC()->set(Configuration::class, $configurationMock); + + /** @var MessageClient $sut */ + $sut = oxNew(MessageClient::class); + + $this->assertInstanceOf( + Client::class, + $this->callMethod( + $sut, + 'getClient' + ) + ); + + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $this->assertInstanceOf( + LoggerInterface::class, + $loggerHandler->getLogger() + ); + } +} \ No newline at end of file