Skip to content

Commit

Permalink
Add phpspec
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick477 committed May 18, 2020
1 parent a31354a commit a33092f
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 0 deletions.
119 changes: 119 additions & 0 deletions spec/Action/CaptureActionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusMultiSafepayPlugin\Action;

use BitBag\SyliusMultiSafepayPlugin\Action\CaptureAction;
use BitBag\SyliusMultiSafepayPlugin\ApiClient\MultiSafepayApiClientInterface;
use MultiSafepayAPI\Object\Orders;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayInterface;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Capture;
use Payum\Core\Security\GenericTokenFactory;
use Payum\Core\Security\TokenInterface;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;

final class CaptureActionSpec extends ObjectBehavior
{
function let(LoggerInterface $logger): void
{
$this->beConstructedWith($logger);
}

function it_is_initializable(): void
{
$this->shouldHaveType(CaptureAction::class);
}

function it_implements_action_interface(): void
{
$this->shouldHaveType(ActionInterface::class);
}

function it_implements_api_aware_interface(): void
{
$this->shouldHaveType(ApiAwareInterface::class);
}

function it_implements_gateway_aware_interface(): void
{
$this->shouldHaveType(GatewayAwareInterface::class);
}

function it_executes(
Capture $request,
ArrayObject $arrayObject,
TokenInterface $token,
GatewayInterface $gateway,
MultiSafepayApiClientInterface $multiSafepayApiClient,
GenericTokenFactory $genericTokenFactory,
TokenInterface $notifyToken,
Orders $orders
): void {
$orders->getPaymentLink()->willReturn('redirect_url');

$orders->data = (object) [
'order_id' => 'test',
];

$this->setGateway($gateway);
$this->setApi($multiSafepayApiClient);
$this->setGenericTokenFactory($genericTokenFactory);
$arrayObject->getArrayCopy()->willReturn([]);
$request->getModel()->willReturn($arrayObject);
$request->getToken()->willReturn($token);
$token->getGatewayName()->willReturn('test');
$token->getDetails()->willReturn([]);
$multiSafepayApiClient->createPayment([
'payment_options' => [
'notification_url' => 'url',
'redirect_url' => 'url',
'cancel_url' => sprintf('%s?type=%s', 'url', MultiSafepayApiClientInterface::STATUS_CANCEL),
],
])->willReturn($orders);
$token->getTargetUrl()->willReturn('url');
$notifyToken->getTargetUrl()->willReturn('url');
$notifyToken->getHash()->willReturn('test');
$genericTokenFactory->createNotifyToken('test', [])->willReturn($notifyToken);

$arrayObject->offsetExists('orderId')->shouldBeCalled();
$arrayObject->offsetGet('paymentData')->shouldBeCalled();
$arrayObject->offsetSet('status', 'initialized')->shouldBeCalled();
$arrayObject->offsetSet('paymentLink', 'redirect_url')->shouldBeCalled();
$arrayObject->offsetSet('orderId', 'test')->shouldBeCalled();
$arrayObject->offsetSet('paymentData', [
'payment_options' => [
'notification_url' => 'url',
'redirect_url' => 'url',
'cancel_url' => 'url?type=cancel',
],
])->shouldBeCalled();

$this
->shouldThrow(HttpRedirect::class)
->during('execute', [$request])
;
}

function it_supports_only_capture_request_and_array_access(
Capture $request,
\ArrayAccess $arrayAccess
): void {
$request->getModel()->willReturn($arrayAccess);
$this->supports($request)->shouldReturn(true);
}
}
110 changes: 110 additions & 0 deletions spec/Action/ConvertPaymentActionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on mikolaj.krol@bitbag.pl.
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusMultiSafepayPlugin\Action;

use BitBag\SyliusMultiSafepayPlugin\Action\ConvertPaymentAction;
use BitBag\SyliusMultiSafepayPlugin\ApiClient\MultiSafepayApiClientInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\GatewayInterface;
use Payum\Core\Request\Convert;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;

final class ConvertPaymentActionSpec extends ObjectBehavior
{
function let(PaymentDescriptionProviderInterface $paymentDescriptionProvider): void
{
$this->beConstructedWith($paymentDescriptionProvider);
}

function it_is_initializable(): void
{
$this->shouldHaveType(ConvertPaymentAction::class);
}

function it_implements_action_interface(): void
{
$this->shouldHaveType(ActionInterface::class);
}

function it_executes(
Convert $request,
PaymentInterface $payment,
GatewayInterface $gateway,
OrderInterface $order,
CustomerInterface $customer,
AddressInterface $address,
MultiSafepayApiClientInterface $multiSafepayApiClient,
PaymentDescriptionProviderInterface $paymentDescriptionProvider
): void
{
$this->setGateway($gateway);
$this->setApi($multiSafepayApiClient);

$multiSafepayApiClient->getType()->willReturn(MultiSafepayApiClientInterface::REDIRECT_ORDER_TYPE);
$order->getId()->willReturn(21);
$request->getSource()->willReturn($payment);
$request->getTo()->willReturn('array');
$order->getCustomer()->willReturn($customer);
$order->getCurrencyCode()->willReturn('EUR');
$order->getLocaleCode()->willReturn('US');
$order->getCustomerIp()->willReturn('127.11.22.22');
$payment->getOrder()->willReturn($order);
$payment->getDetails()->willReturn([]);
$payment->getId()->willReturn(17);
$payment->getAmount()->willReturn(170);
$payment->getCurrencyCode()->willReturn('EUR');
$order->getBillingAddress()->willReturn($address);
$order->getShippingAddress()->willReturn($address);
$paymentDescriptionProvider->getPaymentDescription($payment)->willReturn('Payment Description');

$request->setResult([
'paymentData' => [
'type' => 'redirect',
'order_id' => '21-17-',
'currency' => 'EUR',
'amount' => 170,
'description' => 'Payment Description',
'customer' => [
'locale' => 'US',
'ip_address' => '127.11.22.22',
'first_name' => null,
'last_name' => null,
'address1' => null,
'zip_code' => null,
'city' => null,
'country' => null,
'phone' => null,
'email' => null
]
]
])->shouldBeCalled();


$this->execute($request);
}

function it_supports_only_convert_request_payment_source_and_array_to(
Convert $request,
PaymentInterface $payment
): void
{
$request->getSource()->willReturn($payment);
$request->getTo()->willReturn('array');
$this->supports($request)->shouldReturn(true);
}
}

0 comments on commit a33092f

Please sign in to comment.