Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a transaction id generation by date #7

Open
wants to merge 2 commits into
base: 1.6
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add tests for the new TransactionIdInterface
  • Loading branch information
JoMessina committed Feb 20, 2023
commit 42d1074654ee1e549aeb812dc583b9def3651306
24 changes: 22 additions & 2 deletions tests/Action/Api/AbstractApiActionTest.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@

use Ekyna\Component\Payum\Payzen\Action\Api\AbstractApiAction;
use Ekyna\Component\Payum\Payzen\Api\Api;
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByDate;
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByFile;
use Ekyna\Component\Payum\Payzen\Api\TransactionIdInterface;
use Ekyna\Component\Payum\Payzen\Tests\Action\AbstractActionTest;
use PHPUnit\Framework\MockObject\MockObject;

@@ -21,9 +24,14 @@ abstract class AbstractApiActionTest extends AbstractActionTest
/** @var MockObject|Api */
protected $api;

/**
* @var MockObject|TransactionIdInterface
*/
protected $transactionIdInterface;

protected function setUp(): void
{
$this->action = new $this->actionClass();
$this->action = new $this->actionClass($this->getIdGeneratedByFile());
$this->action->setApi($this->getApiMock());
}

@@ -36,12 +44,24 @@ protected function tearDown(): void
/**
* @return MockObject|Api
*/
protected function getApiMock(): MockObject
protected function getApiMock()
{
if ($this->api) {
return $this->api;
}

return $this->api = $this->getMockBuilder(Api::class)->getMock();
}

/**
* @return TransactionIdInterface
*/
protected function getIdGeneratedByFile()
{
if ($this->transactionIdInterface) {
return $this->transactionIdInterface;
}

return $this->transactionIdInterface = new IdGeneratedByFile(dirname(__DIR__, 3) . '/cache/');
}
}
19 changes: 15 additions & 4 deletions tests/Action/Api/ApiRequestActionTest.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@

use Ekyna\Component\Payum\Payzen\Action\Api\ApiRequestAction;
use Ekyna\Component\Payum\Payzen\Request\Request;
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByFile;
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByDate;
use Payum\Core\Reply\HttpResponse;

/**
@@ -21,14 +23,15 @@ class ApiRequestActionTest extends AbstractApiActionTest

/**
* @test
* @throws \Exception
*/
public function should_set_transaction_id_and_date_and_throw_redirect(): void
{
$api = $this->getApiMock();
$api
->expects(static::once())
->method('getTransactionId')
->willReturn('000001');
$this->clearCache();
$transactionIdInterface = $this->getIdGeneratedByFile();
$transactionId = $transactionIdInterface->getTransactionId();
$this->assertEquals('000001', $transactionId['vads_trans_id']);

$api
->expects(static::once())
@@ -41,4 +44,12 @@ public function should_set_transaction_id_and_date_and_throw_redirect(): void

$this->action->execute($request);
}

private function clearCache(): void
{
$path = dirname(__DIR__, 3) . '/cache/transaction_id';
if (file_exists($path)) {
unlink($path);
}
}
}
2 changes: 1 addition & 1 deletion tests/Action/ConvertPaymentActionTest.php
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ public function should_convert_payment_to_array(): void

$result = $request->getResult();

$this->assertEquals('1234', $result['vads_amount']);
$this->assertEquals('123400', $result['vads_amount']);
$this->assertEquals('978', $result['vads_currency']);
$this->assertEquals('O01', $result['vads_order_id']);
$this->assertEquals(123, $result['vads_cust_id']);
25 changes: 2 additions & 23 deletions tests/Api/ApiTest.php
Original file line number Diff line number Diff line change
@@ -39,27 +39,6 @@ public function test_valid_config(): void
$this->assertTrue(true);
}

public function test_getTransactionId(): void
{
$this->clearCache();

$api = $this->createApi();

$id = $api->getTransactionId();
$this->assertEquals('000001', $id);

$id = $api->getTransactionId();
$this->assertEquals('000002', $id);

$id = $api->getTransactionId();
$this->assertEquals('000003', $id);

touch(__DIR__ . '/../../cache/transaction_id', time() - 60 * 60 * 24);

$id = $api->getTransactionId();
$this->assertEquals('000001', $id);
}

/**
* @param string $hashMode
* @param array $data
@@ -328,15 +307,15 @@ private function createApi(array $config = []): Api
'site_id' => '123456789',
'certificate' => '987654321',
'ctx_mode' => Api::MODE_PRODUCTION,
'directory' => __DIR__ . '/../../cache',
'directory' => dirname(__DIR__, 2) . '/cache',
], $config));

return $api;
}

private function clearCache(): void
{
$path = __DIR__ . '/../../cache/transaction_id';
$path = dirname(__DIR__, 2) . '/cache/transaction_id';
if (file_exists($path)) {
unlink($path);
}
31 changes: 31 additions & 0 deletions tests/Api/IdGeneratedByDateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Ekyna\Component\Payum\Payzen\Tests\Api;

use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByDate;
use Ekyna\Component\Payum\Payzen\Api\TransactionIdInterface;
use Ekyna\Component\Payum\Payzen\Tests\Assert\TransactionIdAssertTrait;
use PHPUnit\Framework\TestCase;

class IdGeneratedByDateTest extends TestCase
{
use TransactionIdAssertTrait;

public function test_getTransactionId(): void
{
$transactionIdInterface = $this->createIdGeneratedByDate();

$data = $transactionIdInterface->getTransactionId();
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
}

/**
* Returns the TransactionIdInterface instance.
*
* @return TransactionIdInterface
*/
private function createIdGeneratedByDate(): TransactionIdInterface
{
return new IdGeneratedByDate();
}
}
56 changes: 56 additions & 0 deletions tests/Api/IdGeneratedByFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Ekyna\Component\Payum\Payzen\Tests\Api;

use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByFile;
use Ekyna\Component\Payum\Payzen\Api\TransactionIdInterface;
use Ekyna\Component\Payum\Payzen\Tests\Assert\TransactionIdAssertTrait;
use PHPUnit\Framework\TestCase;

class IdGeneratedByFileTest extends TestCase
{
use TransactionIdAssertTrait;

public function test_getTransactionId(): void
{
$this->clearCache();

$transactionIdInterface = $this->createIdGeneratedByFile();

$data = $transactionIdInterface->getTransactionId();
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
$this->assertEquals('000001', $data['vads_trans_id']);

$data = $transactionIdInterface->getTransactionId();
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
$this->assertEquals('000002', $data['vads_trans_id']);

$data = $transactionIdInterface->getTransactionId();
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
$this->assertEquals('000003', $data['vads_trans_id']);

touch(dirname(__DIR__, 2) . '/cache/transaction_id', time() - 60 * 60 * 24);

$data = $transactionIdInterface->getTransactionId();
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
$this->assertEquals('000001', $data['vads_trans_id']);
}

/**
* Returns the TransactionIdInterface instance.
*
* @return TransactionIdInterface
*/
private function createIdGeneratedByFile(): TransactionIdInterface
{
return new IdGeneratedByFile(dirname(__DIR__, 2) . '/cache/');
}

private function clearCache(): void
{
$path = dirname(__DIR__, 2) . '/cache/transaction_id';
if (file_exists($path)) {
unlink($path);
}
}
}
31 changes: 31 additions & 0 deletions tests/Assert/TransactionIdAssertTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Ekyna\Component\Payum\Payzen\Tests\Assert;

use Ekyna\Component\Payum\Payzen\Tests\Constraint\ArrayItem;
use PHPUnit\Framework\Constraint\ArrayHasKey;
use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsType;
use PHPUnit\Framework\Constraint\LogicalAnd;
use PHPUnit\Framework\Constraint\RegularExpression;

trait TransactionIdAssertTrait
{
abstract public static function assertThat($value, Constraint $constraint, string $message = ''): void;

protected function assertArrayHasVadsTransIdAndDateKeysTypedString($array, string $message = '')
{
$this->assertThat(
$array,
LogicalAnd::fromConstraints(
new ArrayHasKey('vads_trans_id'),
new ArrayHasKey('vads_trans_date'),
new ArrayItem('vads_trans_id', new IsType('string')),
new ArrayItem('vads_trans_date', new IsType('string')),
new ArrayItem('vads_trans_id', new RegularExpression('/\d{6}/')),
new ArrayItem('vads_trans_date', new RegularExpression('/\d{8}/'))
),
$message
);
}
}
97 changes: 97 additions & 0 deletions tests/Constraint/ArrayItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ekyna\Component\Payum\Payzen\Tests\Constraint;

use ArrayAccess;
use PHPUnit\Framework\Constraint\Constraint;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use function array_key_exists;
use function is_array;

/**
* Constraint that asserts that the array it is evaluated for has a given key and match a constraint type.
*
* Uses array_key_exists() to check if the key is found in the input array, if not found the evaluation fails.
*
* The array key and the constraint type are passed in the constructor.
*/
final class ArrayItem extends Constraint
{
/**
* @var string
*/
private $key;
/**
* @var Constraint
*/
private $constraint;

/**
* @param string $key
* @param Constraint $constraint
*/
public function __construct(string $key, Constraint $constraint)
{
$this->key = $key;
$this->constraint = $constraint;
}

/**
* Returns a string representation of the constraint.
*
* @throws InvalidArgumentException
*/
public function toString(): string
{
return 'has the key ' . $this->exporter()->export($this->key) . $this->constraint->toString();
}

/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other value or object to evaluate
*/
protected function matches($other): bool
{
if (is_array($other)) {
if (!array_key_exists($this->key, $other)) {
return false;
}

return $this->constraint->matches($other[$this->key]);
}

if ($other instanceof ArrayAccess) {
if (!$other->offsetExists($this->key)) {
return false;
}

return $this->constraint->matches($other[$this->key]);
}

return false;
}

/**
* Returns the description of the failure.
*
* The beginning of failure messages is "Failed asserting that" in most
* cases. This method should return the second part of that sentence.
*
* @param mixed $other evaluated value or object
*
* @throws InvalidArgumentException
*/
protected function failureDescription($other): string
{
return 'an array ' . $this->toString();
}
}
2 changes: 1 addition & 1 deletion tests/PayzenGatewayFactoryTest.php
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ public function test_create_gateway()
'ctx_mode' => Api::MODE_PRODUCTION,
'site_id' => '123456',
'certificate' => '123456',
'directory' => __DIR__ . '/../cache',
'directory' => dirname(__DIR__, 1) . '/cache',
]);

$this->assertInstanceOf('Payum\Core\Gateway', $gateway);