|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Paweł Jędrzejewski |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace spec\Sylius\PayPalPlugin\Enabler; |
| 15 | + |
| 16 | +use Doctrine\Persistence\ObjectManager; |
| 17 | +use GuzzleHttp\Client; |
| 18 | +use Payum\Core\Model\GatewayConfigInterface; |
| 19 | +use PhpSpec\ObjectBehavior; |
| 20 | +use Psr\Http\Message\ResponseInterface; |
| 21 | +use Psr\Http\Message\StreamInterface; |
| 22 | +use Sylius\Component\Core\Model\PaymentMethodInterface; |
| 23 | +use Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface; |
| 24 | +use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException; |
| 25 | + |
| 26 | +final class PayPalPaymentMethodEnablerSpec extends ObjectBehavior |
| 27 | +{ |
| 28 | + function let( |
| 29 | + Client $client, |
| 30 | + ObjectManager $paymentMethodManager |
| 31 | + ): void { |
| 32 | + $this->beConstructedWith($client, 'http://base-url.com', $paymentMethodManager); |
| 33 | + } |
| 34 | + |
| 35 | + function it_implements_payment_method_enabler_interface(): void |
| 36 | + { |
| 37 | + $this->shouldImplement(PaymentMethodEnablerInterface::class); |
| 38 | + } |
| 39 | + |
| 40 | + function it_enables_payment_method_if_it_has_proper_credentials_set( |
| 41 | + Client $client, |
| 42 | + ObjectManager $paymentMethodManager, |
| 43 | + PaymentMethodInterface $paymentMethod, |
| 44 | + GatewayConfigInterface $gatewayConfig, |
| 45 | + ResponseInterface $response, |
| 46 | + StreamInterface $body |
| 47 | + ): void { |
| 48 | + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); |
| 49 | + $gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123']); |
| 50 | + |
| 51 | + $client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response); |
| 52 | + $response->getBody()->willReturn($body); |
| 53 | + $body->getContents()->willReturn('{ "permissionsGranted": true }'); |
| 54 | + |
| 55 | + $paymentMethod->setEnabled(true)->shouldBeCalled(); |
| 56 | + $paymentMethodManager->flush()->shouldBeCalled(); |
| 57 | + |
| 58 | + $this->enable($paymentMethod); |
| 59 | + } |
| 60 | + |
| 61 | + function it_throws_exception_if_payment_method_credentials_are_not_granted( |
| 62 | + Client $client, |
| 63 | + ObjectManager $paymentMethodManager, |
| 64 | + PaymentMethodInterface $paymentMethod, |
| 65 | + GatewayConfigInterface $gatewayConfig, |
| 66 | + ResponseInterface $response, |
| 67 | + StreamInterface $body |
| 68 | + ): void { |
| 69 | + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); |
| 70 | + $gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123']); |
| 71 | + |
| 72 | + $client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response); |
| 73 | + $response->getBody()->willReturn($body); |
| 74 | + $body->getContents()->willReturn('{ "permissionsGranted": false }'); |
| 75 | + |
| 76 | + $paymentMethod->setEnabled(true)->shouldNotBeCalled(); |
| 77 | + $paymentMethodManager->flush()->shouldNotBeCalled(); |
| 78 | + |
| 79 | + $this |
| 80 | + ->shouldThrow(PaymentMethodCouldNotBeEnabledException::class) |
| 81 | + ->during('enable', [$paymentMethod]) |
| 82 | + ; |
| 83 | + } |
| 84 | +} |
0 commit comments