From 02318901291b92594a250cd032e78aac8bf5065d Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 18 Jan 2023 09:10:48 +0100 Subject: [PATCH] remove unused ApiException, check for non status 200 errors --- src/Application/Model/MessageTypes/Sms.php | 4 +- .../integration/LMIntegrationTestCase.php | 12 +++ src/tests/integration/adminOrderTest.php | 78 ++++++++++++++++++- .../Model/MessageTypes/SmsTest.php | 9 ++- 4 files changed, 94 insertions(+), 9 deletions(-) diff --git a/src/Application/Model/MessageTypes/Sms.php b/src/Application/Model/MessageTypes/Sms.php index 452cdd9..7da96e8 100644 --- a/src/Application/Model/MessageTypes/Sms.php +++ b/src/Application/Model/MessageTypes/Sms.php @@ -22,7 +22,6 @@ use D3\Linkmobility4OXID\Application\Model\OrderRecipients; use D3\Linkmobility4OXID\Application\Model\RequestFactory; use D3\Linkmobility4OXID\Application\Model\UserRecipients; use D3\LinkmobilityClient\Client; -use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\Exceptions\RecipientException; use D3\LinkmobilityClient\LoggerHandler; use D3\LinkmobilityClient\RecipientsList\RecipientsList; @@ -144,7 +143,7 @@ class Sms extends AbstractMessage $this->response = $response = $this->submitMessage($recipientsList); return $response->isSuccessful(); - } catch (GuzzleException|ApiException|InvalidArgumentException $e) { + } catch (GuzzleException|InvalidArgumentException $e) { $this->getLogger()->error($e->getMessage()); // Oxid does not accept throwable interface only exceptions according to definition /** @var UtilsView $utilsView */ @@ -204,7 +203,6 @@ class Sms extends AbstractMessage /** * @param RecipientsListInterface $recipientsList * @return ResponseInterface - * @throws ApiException * @throws GuzzleException * @throws NumberParseException * @throws RecipientException diff --git a/src/tests/integration/LMIntegrationTestCase.php b/src/tests/integration/LMIntegrationTestCase.php index fb478b6..fdf781f 100644 --- a/src/tests/integration/LMIntegrationTestCase.php +++ b/src/tests/integration/LMIntegrationTestCase.php @@ -18,6 +18,7 @@ namespace D3\Linkmobility4OXID\tests\integration; use D3\Linkmobility4OXID\Application\Model\MessageClient; use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase; use D3\LinkmobilityClient\Client; +use D3\LinkmobilityClient\LoggerHandler; use Doctrine\DBAL\Exception; use Doctrine\DBAL\Query\QueryBuilder; use GuzzleHttp\Client as GuzzleClient; @@ -31,9 +32,20 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Log\NullLogger; abstract class LMIntegrationTestCase extends LMUnitTestCase { + /** + * @throws \Exception + */ + public function setUp(): void + { + parent::setUp(); + + d3GetOxidDIC()->get(LoggerHandler::class)->setLogger(new NullLogger()); + } + /** * @param $userId * diff --git a/src/tests/integration/adminOrderTest.php b/src/tests/integration/adminOrderTest.php index 2142d8e..a29f54a 100644 --- a/src/tests/integration/adminOrderTest.php +++ b/src/tests/integration/adminOrderTest.php @@ -20,6 +20,7 @@ use D3\Linkmobility4OXID\Application\Model\Configuration; use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Query\QueryBuilder; use Exception; +use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Request; @@ -73,7 +74,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @te__st + * @test * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -139,7 +140,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @te__st + * @test * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -206,7 +207,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @te__st + * @test * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -267,6 +268,75 @@ class adminOrderTest extends LMIntegrationTestCase $this->deleteAllRemarksFrom($this->userId); } + /** + * @test + * @throws DoctrineException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function serverError() + { + $container = []; + $history = Middleware::history($container); + + $this->deleteAllRemarksFrom($this->userId); + + $this->setClientResponse( + new Response( + 500, + [], + '{"statusCode": 4019, "statusMessage": "parameter \"messageContent\" invalid", "clientMessageId": null, "transferId": null, "smsCount": 0}' + ), + $history + ); + + $_POST['messagebody'] = 'testMessage'; + $_POST['oxid'] = $this->orderId; + + /** @var AdminOrder $controller */ + try{ + $controller = oxNew(AdminOrder::class); + $controller->send(); + + } catch (Exception $e) { + dumpvar(get_class($e)); + } + + // check requests + $this->assertCount( + 1, + $container + ); + /** @var RequestInterface $request */ + $request = $container[0]['request']; + $this->assertTrue( + (bool) strpos(serialize($request->getBody()->getContents()), 'testMessage') + ); + + // check return message + $search = 'no response'; + + $this->assertTrue( + (bool) strpos(serialize(Registry::getSession()->getVariable('Errors')), $search) + ); + + // check remark + /** @var QueryBuilder $queryBuilder */ + $queryBuilder = ContainerFactory::getInstance()->getContainer()->get(QueryBuilderFactoryInterface::class)->create(); + $queryBuilder->select('oxid') + ->from(oxNew(Remark::class)->getViewName()) + ->where( + $queryBuilder->expr()->eq( + 'oxparentid', + $queryBuilder->createNamedParameter($this->userId) + ) + ); + $remarkIds = $queryBuilder->execute()->fetchAll(); + $this->assertEmpty($remarkIds); + + $this->deleteAllRemarksFrom($this->userId); + } + /** * @test * @throws DoctrineException @@ -332,7 +402,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @te__st + * @test * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface diff --git a/src/tests/unit/Application/Model/MessageTypes/SmsTest.php b/src/tests/unit/Application/Model/MessageTypes/SmsTest.php index 7a1b180..4bf015a 100644 --- a/src/tests/unit/Application/Model/MessageTypes/SmsTest.php +++ b/src/tests/unit/Application/Model/MessageTypes/SmsTest.php @@ -23,13 +23,13 @@ use D3\Linkmobility4OXID\Application\Model\RequestFactory; use D3\Linkmobility4OXID\Application\Model\UserRecipients; use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase; use D3\LinkmobilityClient\Client; -use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\RecipientsList\RecipientsList; use D3\LinkmobilityClient\SMS\BinaryRequest; use D3\LinkmobilityClient\SMS\Response; use D3\LinkmobilityClient\ValueObject\Recipient; use D3\LinkmobilityClient\ValueObject\Sender; use D3\TestingTools\Development\CanAccessRestricted; +use GuzzleHttp\Exception\ServerException; use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\Remark; @@ -554,6 +554,11 @@ class SmsTest extends LMUnitTestCase $utilsViewMock->expects($this->exactly((int) $throwException))->method('addErrorToDisplay'); d3GetOxidDIC()->set('d3ox.linkmobility.'.UtilsView::class, $utilsViewMock); + /** @var ServerException|MockObject $serverExceptionMock */ + $serverExceptionMock = $this->getMockBuilder(ServerException::class) + ->disableOriginalConstructor() + ->getMock(); + /** @var Sms|MockObject $sut */ $sut = $this->getMockBuilder(Sms::class) ->onlyMethods(['submitMessage', 'getLogger']) @@ -561,7 +566,7 @@ class SmsTest extends LMUnitTestCase ->getMock(); $sut->method('submitMessage')->will( $throwException ? - $this->throwException(oxNew(ApiException::class)) : + $this->throwException($serverExceptionMock) : $this->returnValue($smsResponseMock) ); $sut->method('getLogger')->willReturn($loggerMock);