Skip to content

Commit

Permalink
WIP - Unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
burahimu committed Jul 23, 2018
1 parent 4239678 commit a5428ef
Show file tree
Hide file tree
Showing 9 changed files with 546 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gateway/PayPlugPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod('POST')) {
if (!$request->isMethod(Request::METHOD_POST)) {
throw new InvalidPaymentCallbackMethodException('Request method should be POST');
}

Expand Down
2 changes: 1 addition & 1 deletion Gateway/PaypalPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function getResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
if (!$request->isMethod('POST')) {
if (!$request->isMethod(Request::METHOD_POST)) {
throw new InvalidPaymentCallbackMethodException('Request method should be POST');
}

Expand Down
45 changes: 45 additions & 0 deletions Tests/Unit/Gateway/PayPlugPaymentGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace IDCI\Bundle\PaymentBundle\Tests\Unit\Gateway;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use IDCI\Bundle\PaymentBundle\Gateway\PayPlugPaymentGateway;

class PayPlugPaymentGatewayTest extends PaymentGatewayTestCase
{
/**
* UrlGeneratorInterface
*/
private $router;

public function setUp()
{
parent::setUp();

$this->router = $this->getMockBuilder(UrlGeneratorInterface::class)
->disableOriginalConstructor()
->getMock()
;

$this->gateway = new PayPlugPaymentGateway($this->twig, $this->router);
}

/**
* @expectedException IDCI\Bundle\PaymentBundle\Exception\InvalidPaymentCallbackMethodException
*/
public function testInvalidMethod()
{
$request = Request::create('dumy_uri', Request::METHOD_GET);

$this->gateway->getResponse($request, $this->paymentGatewayConfiguration);
}

public function getResponse()
{
$request = Request::create('dumy_uri', Request::METHOD_POST);

$data = $this->gateway->getResponse($request, $this->paymentGatewayConfiguration);
var_dump($data);die;
}
}
56 changes: 56 additions & 0 deletions Tests/Unit/Gateway/PaymentGatewayTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace IDCI\Bundle\PaymentBundle\Tests\Unit\Gateway;

use PHPUnit\Framework\TestCase;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader;
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfiguration;
use IDCI\Bundle\PaymentBundle\Model\Transaction;
use IDCI\Bundle\PaymentBundle\Payment\TransactionFactory;

class PaymentGatewayTestCase extends TestCase
{
/**
* @var \Twig_Environment
*/
protected $twig;

/**
* @var Transaction
*/
protected $transaction;

/**
* @var PaymentGatewayConfiguration
*/
protected $paymentGatewayConfiguration;

/**
* @var PaymentGatewayInterface
*/
protected $gateway;

public function setUp()
{
$loader = new FilesystemLoader();
$loader->addPath(__DIR__ . '/../../..', 'IDCIPaymentBundle');

$this->twig = new TwigEnvironment($loader);

$this->paymentGatewayConfiguration = (new PaymentGatewayConfiguration())
->setAlias('dummy_gateway_alias')
;

$this->transaction = TransactionFactory::getInstance()->create([
'gateway_configuration_alias' => $this->paymentGatewayConfiguration->getAlias(),
'item_id' => 'dummy_item_id',
'customer_id' => 'dummy_customer_id',
'customer_email' => 'dummy_customer_email',
'amount' => 100,
'currency_code' => 'EUR',
'description' => 'Dummy description',
'metadatas' => [],
]);
}
}
42 changes: 42 additions & 0 deletions Tests/Unit/Gateway/PaypalPaymentGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace IDCI\Bundle\PaymentBundle\Tests\Unit\Gateway;

use Symfony\Component\HttpFoundation\Request;
use IDCI\Bundle\PaymentBundle\Gateway\PaypalPaymentGateway;

class PaypalPaymentGatewayTest extends PaymentGatewayTestCase
{
public function setUp()
{
parent::setUp();

$this->gateway = new PaypalPaymentGateway($this->twig);
}

public function testInitialize()
{
$this->paymentGatewayConfiguration
->set('client_id', 'dummy_client_id')
->set('callback_url', 'dummy_callback_url')
->set('environment', 'dummy_environment')
;

$data = $this->gateway->initialize($this->paymentGatewayConfiguration, $this->transaction);

$this->assertEquals($this->paymentGatewayConfiguration->get('client_id'), $data['clientId']);
$this->assertEquals($this->paymentGatewayConfiguration->get('callback_url'), $data['url']);
$this->assertEquals($this->paymentGatewayConfiguration->get('environment'), $data['environment']);
$this->assertEquals($this->transaction, $data['transaction']);
}

/**
* @expectedException IDCI\Bundle\PaymentBundle\Exception\InvalidPaymentCallbackMethodException
*/
public function testInvalidMethod()
{
$request = Request::create('dumy_uri', Request::METHOD_GET);

$this->gateway->getResponse($request, $this->paymentGatewayConfiguration);
}
}
Loading

0 comments on commit a5428ef

Please sign in to comment.