forked from mollie/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvement: Do not log validation error mollie#839
- Loading branch information
1 parent
edfeaba
commit 0fc0d49
Showing
6 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/* | ||
* Copyright Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mollie\Payment\Service\Mollie; | ||
|
||
use Mollie\Payment\Config; | ||
|
||
class LogException | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
private $messagesToSkip = [ | ||
'The \'billingAddress.familyName\' field contains characters that are not allowed' | ||
]; | ||
|
||
public function __construct( | ||
Config $config, | ||
array $messagesToSkip = [] | ||
) { | ||
$this->config = $config; | ||
$this->messagesToSkip = array_merge($this->messagesToSkip, $messagesToSkip); | ||
} | ||
|
||
public function execute(\Exception $exception): void | ||
{ | ||
$message = method_exists($exception, 'getPlainMessage') ? | ||
$exception->getPlainMessage() : | ||
$exception->getMessage(); | ||
|
||
if (in_array($message, $this->messagesToSkip)) { | ||
return; | ||
} | ||
|
||
$this->config->addTolog('error', $message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/* | ||
* Copyright Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mollie\Payment\Test\Fakes; | ||
|
||
use Mollie\Payment\Config; | ||
|
||
class ConfigFake extends Config | ||
{ | ||
private $loggedMessages = []; | ||
|
||
public function addTolog($type, $message) | ||
{ | ||
parent::addToLog($type, $message); | ||
|
||
$this->loggedMessages[] = [ | ||
'type' => $type, | ||
'message' => $message, | ||
]; | ||
} | ||
|
||
public function getLoggedMessages(): array | ||
{ | ||
return $this->loggedMessages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/* | ||
* Copyright Magmodules.eu. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mollie\Payment\Test\Integration\Service\Mollie; | ||
|
||
use Mollie\Api\Exceptions\ApiException; | ||
use Mollie\Payment\Service\Mollie\LogException; | ||
use Mollie\Payment\Test\Fakes\ConfigFake; | ||
use Mollie\Payment\Test\Integration\IntegrationTestCase; | ||
|
||
class LogExceptionTest extends IntegrationTestCase | ||
{ | ||
public function testLogsMessages(): void | ||
{ | ||
$exception = new ApiException('This is a test exception triggered in ' . __FILE__); | ||
|
||
$configFake = $this->objectManager->create(ConfigFake::class); | ||
|
||
/** @var LogException $instance */ | ||
$instance = $this->objectManager->create(LogException::class, [ | ||
'config' => $configFake, | ||
]); | ||
$instance->execute($exception); | ||
|
||
$this->assertCount(1, $configFake->getLoggedMessages()); | ||
} | ||
|
||
public function testSkipsSomeMessages(): void | ||
{ | ||
$exception = new ApiException('The \'billingAddress.familyName\' field contains characters that are not allowed'); | ||
|
||
$configFake = $this->objectManager->create(ConfigFake::class); | ||
|
||
/** @var LogException $instance */ | ||
$instance = $this->objectManager->create(LogException::class, [ | ||
'config' => $configFake, | ||
]); | ||
$instance->execute($exception); | ||
|
||
$this->assertCount(0, $configFake->getLoggedMessages()); | ||
} | ||
|
||
public function testCanAddOwnExceptions(): void | ||
{ | ||
$exception = new ApiException('Some random message'); | ||
|
||
$configFake = $this->objectManager->create(ConfigFake::class); | ||
|
||
/** @var LogException $instance */ | ||
$instance = $this->objectManager->create(LogException::class, [ | ||
// Normally you would do this through di.xml, but when testing we can do it like this. | ||
'messagesToSkip' => [ | ||
'Some random message', | ||
], | ||
'config' => $configFake, | ||
]); | ||
$instance->execute($exception); | ||
|
||
$this->assertCount(0, $configFake->getLoggedMessages()); | ||
} | ||
} |