-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve Tpay transaction channels into an array if an error occurred (#…
…190)
- Loading branch information
Showing
8 changed files
with
219 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Factory; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\GetTpayTransactionsChannels; | ||
use Payum\Core\Model\ArrayObject; | ||
|
||
final class GetTpayTransactionsChannelsFactory implements GetTpayTransactionsChannelsFactoryInterface | ||
{ | ||
public function createNewEmpty(): GetTpayTransactionsChannels | ||
{ | ||
return new GetTpayTransactionsChannels(new ArrayObject()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Payum/Factory/GetTpayTransactionsChannelsFactoryInterface.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Factory; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\GetTpayTransactionsChannels; | ||
|
||
interface GetTpayTransactionsChannelsFactoryInterface | ||
{ | ||
public function createNewEmpty(): GetTpayTransactionsChannels; | ||
} |
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
144 changes: 144 additions & 0 deletions
144
tests/Unit/Tpay/Resolver/TpayTransactionChannelResolverTest.php
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,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Tpay\Resolver; | ||
|
||
use ArrayObject; | ||
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\GetTpayTransactionsChannelsFactoryInterface; | ||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\GetTpayTransactionsChannels; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\TpayTransactionChannelResolver; | ||
use Payum\Core\GatewayInterface; | ||
use Payum\Core\Payum; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Log\LoggerInterface; | ||
use Tpay\OpenApi\Utilities\TpayException; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class TpayTransactionChannelResolverTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private Payum|ObjectProphecy $payum; | ||
|
||
private GetTpayTransactionsChannelsFactoryInterface|ObjectProphecy $getTpayTransactionsChannelsFactory; | ||
|
||
private LoggerInterface|ObjectProphecy $logger; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->payum = $this->prophesize(Payum::class); | ||
$this->getTpayTransactionsChannelsFactory = $this->prophesize(GetTpayTransactionsChannelsFactoryInterface::class); | ||
$this->logger = $this->prophesize(LoggerInterface::class); | ||
} | ||
|
||
public function test_it_resolves_tpay_transaction_channels(): void | ||
{ | ||
$gateway = $this->prophesize(GatewayInterface::class); | ||
$value = $this->prophesize(GetTpayTransactionsChannels::class); | ||
$this->payum->getGateway('tpay')->willReturn($gateway); | ||
$this->getTpayTransactionsChannelsFactory->createNewEmpty()->willReturn($value); | ||
$gateway->execute($value, true)->shouldBeCalled(); | ||
$value->getResult()->willReturn([ | ||
'result' => 'success', | ||
'channels' => [ | ||
['id' => 1, 'name' => 'Bank 1'], | ||
['id' => 2, 'name' => 'Bank 2'], | ||
], | ||
]); | ||
|
||
$result = $this->createTestSubject()->resolve(); | ||
|
||
$this->assertEquals([ | ||
1 => ['id' => 1, 'name' => 'Bank 1'], | ||
2 => ['id' => 2, 'name' => 'Bank 2'], | ||
], $result); | ||
} | ||
|
||
public function test_it_resolves_an_empty_array_when_tpay_exception_is_thrown(): void | ||
{ | ||
$gateway = $this->prophesize(GatewayInterface::class); | ||
$value = $this->prophesize(GetTpayTransactionsChannels::class); | ||
$this->payum->getGateway('tpay')->willReturn($gateway); | ||
$this->getTpayTransactionsChannelsFactory->createNewEmpty()->willReturn($value); | ||
$gateway | ||
->execute($value, true) | ||
->willThrow(new TpayException('Booo! I am a TpayException!')) | ||
; | ||
$this->logger | ||
->critical('Unable to get banks list. TpayException thrown.', Argument::withKey('exceptionMessage')) | ||
->shouldBeCalled() | ||
; | ||
|
||
$result = $this->createTestSubject()->resolve(); | ||
|
||
$this->assertEquals([], $result); | ||
} | ||
|
||
public function test_it_resolves_an_empty_array_when_the_result_is_not_success(): void | ||
{ | ||
$gateway = $this->prophesize(GatewayInterface::class); | ||
$value = $this->prophesize(GetTpayTransactionsChannels::class); | ||
$this->payum->getGateway('tpay')->willReturn($gateway); | ||
$value->getResult()->willReturn(['result' => 'failure']); | ||
$this->getTpayTransactionsChannelsFactory->createNewEmpty()->willReturn($value); | ||
$gateway->execute($value, true)->shouldBeCalled(); | ||
|
||
$this->logger | ||
->critical('Unable to get banks list. The result is not success.', ['responseBody' => '{"result":"failure"}']) | ||
->shouldBeCalled() | ||
; | ||
|
||
$result = $this->createTestSubject()->resolve(); | ||
|
||
$this->assertEquals([], $result); | ||
} | ||
|
||
public function test_it_resolves_an_empty_array_when_the_channels_key_is_missing(): void | ||
{ | ||
$gateway = $this->prophesize(GatewayInterface::class); | ||
$value = $this->prophesize(GetTpayTransactionsChannels::class); | ||
$this->payum->getGateway('tpay')->willReturn($gateway); | ||
$value->getResult()->willReturn(['result' => 'success']); | ||
$this->getTpayTransactionsChannelsFactory->createNewEmpty()->willReturn($value); | ||
$gateway->execute($value, true)->shouldBeCalled(); | ||
|
||
$this->logger | ||
->critical('Unable to get banks list. The channels key is missing.', ['responseBody' => '{"result":"success"}']) | ||
->shouldBeCalled() | ||
; | ||
|
||
$result = $this->createTestSubject()->resolve(); | ||
|
||
$this->assertEquals([], $result); | ||
} | ||
|
||
public function test_it_does_not_log_errors_if_logger_is_null(): void | ||
{ | ||
$gateway = $this->prophesize(GatewayInterface::class); | ||
$value = $this->prophesize(GetTpayTransactionsChannels::class); | ||
$this->payum->getGateway('tpay')->willReturn($gateway); | ||
$value->getResult()->willReturn(['result' => 'failure']); | ||
$this->getTpayTransactionsChannelsFactory->createNewEmpty()->willReturn($value); | ||
$gateway->execute($value, true)->shouldBeCalled(); | ||
|
||
$this->logger->critical(Argument::cetera())->shouldNotBeCalled(); | ||
|
||
$result = $this->createTestSubject(false)->resolve(); | ||
|
||
$this->assertEquals([], $result); | ||
} | ||
|
||
private function createTestSubject(bool $withLogger = true): TpayTransactionChannelResolver | ||
{ | ||
return new TpayTransactionChannelResolver( | ||
$this->payum->reveal(), | ||
$this->getTpayTransactionsChannelsFactory->reveal(), | ||
$withLogger ? $this->logger->reveal() : null, | ||
); | ||
} | ||
} |