Skip to content

Commit 29cc68a

Browse files
committed
Add tests for the new TransactionIdInterface
1 parent 2d6e3ce commit 29cc68a

9 files changed

+256
-31
lines changed

tests/Action/Api/AbstractApiActionTest.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Ekyna\Component\Payum\Payzen\Action\Api\AbstractApiAction;
88
use Ekyna\Component\Payum\Payzen\Api\Api;
9+
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByDate;
10+
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByFile;
11+
use Ekyna\Component\Payum\Payzen\Api\TransactionIdInterface;
912
use Ekyna\Component\Payum\Payzen\Tests\Action\AbstractActionTest;
1013
use PHPUnit\Framework\MockObject\MockObject;
1114

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

27+
/**
28+
* @var MockObject|TransactionIdInterface
29+
*/
30+
protected $transactionIdInterface;
31+
2432
protected function setUp(): void
2533
{
26-
$this->action = new $this->actionClass();
34+
$this->action = new $this->actionClass($this->getIdGeneratedByFile());
2735
$this->action->setApi($this->getApiMock());
2836
}
2937

@@ -36,12 +44,24 @@ protected function tearDown(): void
3644
/**
3745
* @return MockObject|Api
3846
*/
39-
protected function getApiMock(): MockObject
47+
protected function getApiMock()
4048
{
4149
if ($this->api) {
4250
return $this->api;
4351
}
4452

4553
return $this->api = $this->getMockBuilder(Api::class)->getMock();
4654
}
55+
56+
/**
57+
* @return TransactionIdInterface
58+
*/
59+
protected function getIdGeneratedByFile()
60+
{
61+
if ($this->transactionIdInterface) {
62+
return $this->transactionIdInterface;
63+
}
64+
65+
return $this->transactionIdInterface = new IdGeneratedByFile(dirname(__DIR__, 3) . '/cache/');
66+
}
4767
}

tests/Action/Api/ApiRequestActionTest.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Ekyna\Component\Payum\Payzen\Action\Api\ApiRequestAction;
88
use Ekyna\Component\Payum\Payzen\Request\Request;
9+
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByFile;
10+
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByDate;
911
use Payum\Core\Reply\HttpResponse;
1012

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

2224
/**
2325
* @test
26+
* @throws \Exception
2427
*/
2528
public function should_set_transaction_id_and_date_and_throw_redirect(): void
2629
{
2730
$api = $this->getApiMock();
28-
$api
29-
->expects(static::once())
30-
->method('getTransactionId')
31-
->willReturn('000001');
31+
$this->clearCache();
32+
$transactionIdInterface = $this->getIdGeneratedByFile();
33+
$transactionId = $transactionIdInterface->getTransactionId();
34+
$this->assertEquals('000001', $transactionId['vads_trans_id']);
3235

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

4245
$this->action->execute($request);
4346
}
47+
48+
private function clearCache(): void
49+
{
50+
$path = dirname(__DIR__, 3) . '/cache/transaction_id';
51+
if (file_exists($path)) {
52+
unlink($path);
53+
}
54+
}
4455
}

tests/Action/ConvertPaymentActionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function should_convert_payment_to_array(): void
4848

4949
$result = $request->getResult();
5050

51-
$this->assertEquals('1234', $result['vads_amount']);
51+
$this->assertEquals('123400', $result['vads_amount']);
5252
$this->assertEquals('978', $result['vads_currency']);
5353
$this->assertEquals('O01', $result['vads_order_id']);
5454
$this->assertEquals(123, $result['vads_cust_id']);

tests/Api/ApiTest.php

+2-23
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,6 @@ public function test_valid_config(): void
3939
$this->assertTrue(true);
4040
}
4141

42-
public function test_getTransactionId(): void
43-
{
44-
$this->clearCache();
45-
46-
$api = $this->createApi();
47-
48-
$id = $api->getTransactionId();
49-
$this->assertEquals('000001', $id);
50-
51-
$id = $api->getTransactionId();
52-
$this->assertEquals('000002', $id);
53-
54-
$id = $api->getTransactionId();
55-
$this->assertEquals('000003', $id);
56-
57-
touch(__DIR__ . '/../../cache/transaction_id', time() - 60 * 60 * 24);
58-
59-
$id = $api->getTransactionId();
60-
$this->assertEquals('000001', $id);
61-
}
62-
6342
/**
6443
* @param string $hashMode
6544
* @param array $data
@@ -328,15 +307,15 @@ private function createApi(array $config = []): Api
328307
'site_id' => '123456789',
329308
'certificate' => '987654321',
330309
'ctx_mode' => Api::MODE_PRODUCTION,
331-
'directory' => __DIR__ . '/../../cache',
310+
'directory' => dirname(__DIR__, 2) . '/cache',
332311
], $config));
333312

334313
return $api;
335314
}
336315

337316
private function clearCache(): void
338317
{
339-
$path = __DIR__ . '/../../cache/transaction_id';
318+
$path = dirname(__DIR__, 2) . '/cache/transaction_id';
340319
if (file_exists($path)) {
341320
unlink($path);
342321
}

tests/Api/IdGeneratedByDateTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Ekyna\Component\Payum\Payzen\Tests\Api;
4+
5+
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByDate;
6+
use Ekyna\Component\Payum\Payzen\Api\TransactionIdInterface;
7+
use Ekyna\Component\Payum\Payzen\Tests\Assert\TransactionIdAssertTrait;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class IdGeneratedByDateTest extends TestCase
11+
{
12+
use TransactionIdAssertTrait;
13+
14+
public function test_getTransactionId(): void
15+
{
16+
$transactionIdInterface = $this->createIdGeneratedByDate();
17+
18+
$data = $transactionIdInterface->getTransactionId();
19+
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
20+
}
21+
22+
/**
23+
* Returns the TransactionIdInterface instance.
24+
*
25+
* @return TransactionIdInterface
26+
*/
27+
private function createIdGeneratedByDate(): TransactionIdInterface
28+
{
29+
return new IdGeneratedByDate();
30+
}
31+
}

tests/Api/IdGeneratedByFileTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Ekyna\Component\Payum\Payzen\Tests\Api;
4+
5+
use Ekyna\Component\Payum\Payzen\Api\IdGeneratedByFile;
6+
use Ekyna\Component\Payum\Payzen\Api\TransactionIdInterface;
7+
use Ekyna\Component\Payum\Payzen\Tests\Assert\TransactionIdAssertTrait;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class IdGeneratedByFileTest extends TestCase
11+
{
12+
use TransactionIdAssertTrait;
13+
14+
public function test_getTransactionId(): void
15+
{
16+
$this->clearCache();
17+
18+
$transactionIdInterface = $this->createIdGeneratedByFile();
19+
20+
$data = $transactionIdInterface->getTransactionId();
21+
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
22+
$this->assertEquals('000001', $data['vads_trans_id']);
23+
24+
$data = $transactionIdInterface->getTransactionId();
25+
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
26+
$this->assertEquals('000002', $data['vads_trans_id']);
27+
28+
$data = $transactionIdInterface->getTransactionId();
29+
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
30+
$this->assertEquals('000003', $data['vads_trans_id']);
31+
32+
touch(dirname(__DIR__, 2) . '/cache/transaction_id', time() - 60 * 60 * 24);
33+
34+
$data = $transactionIdInterface->getTransactionId();
35+
$this->assertArrayHasVadsTransIdAndDateKeysTypedString($data);
36+
$this->assertEquals('000001', $data['vads_trans_id']);
37+
}
38+
39+
/**
40+
* Returns the TransactionIdInterface instance.
41+
*
42+
* @return TransactionIdInterface
43+
*/
44+
private function createIdGeneratedByFile(): TransactionIdInterface
45+
{
46+
return new IdGeneratedByFile(dirname(__DIR__, 2) . '/cache/');
47+
}
48+
49+
private function clearCache(): void
50+
{
51+
$path = dirname(__DIR__, 2) . '/cache/transaction_id';
52+
if (file_exists($path)) {
53+
unlink($path);
54+
}
55+
}
56+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Ekyna\Component\Payum\Payzen\Tests\Assert;
4+
5+
use Ekyna\Component\Payum\Payzen\Tests\Constraint\ArrayItem;
6+
use PHPUnit\Framework\Constraint\ArrayHasKey;
7+
use PHPUnit\Framework\Constraint\Constraint;
8+
use PHPUnit\Framework\Constraint\IsType;
9+
use PHPUnit\Framework\Constraint\LogicalAnd;
10+
use PHPUnit\Framework\Constraint\RegularExpression;
11+
12+
trait TransactionIdAssertTrait
13+
{
14+
abstract public static function assertThat($value, Constraint $constraint, string $message = ''): void;
15+
16+
protected function assertArrayHasVadsTransIdAndDateKeysTypedString($array, string $message = '')
17+
{
18+
$this->assertThat(
19+
$array,
20+
LogicalAnd::fromConstraints(
21+
new ArrayHasKey('vads_trans_id'),
22+
new ArrayHasKey('vads_trans_date'),
23+
new ArrayItem('vads_trans_id', new IsType('string')),
24+
new ArrayItem('vads_trans_date', new IsType('string')),
25+
new ArrayItem('vads_trans_id', new RegularExpression('/\d{6}/')),
26+
new ArrayItem('vads_trans_date', new RegularExpression('/\d{8}/'))
27+
),
28+
$message
29+
);
30+
}
31+
}

tests/Constraint/ArrayItem.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Ekyna\Component\Payum\Payzen\Tests\Constraint;
11+
12+
use ArrayAccess;
13+
use PHPUnit\Framework\Constraint\Constraint;
14+
use SebastianBergmann\RecursionContext\InvalidArgumentException;
15+
use function array_key_exists;
16+
use function is_array;
17+
18+
/**
19+
* Constraint that asserts that the array it is evaluated for has a given key and match a constraint type.
20+
*
21+
* Uses array_key_exists() to check if the key is found in the input array, if not found the evaluation fails.
22+
*
23+
* The array key and the constraint type are passed in the constructor.
24+
*/
25+
final class ArrayItem extends Constraint
26+
{
27+
/**
28+
* @var string
29+
*/
30+
private $key;
31+
/**
32+
* @var Constraint
33+
*/
34+
private $constraint;
35+
36+
/**
37+
* @param string $key
38+
* @param Constraint $constraint
39+
*/
40+
public function __construct(string $key, Constraint $constraint)
41+
{
42+
$this->key = $key;
43+
$this->constraint = $constraint;
44+
}
45+
46+
/**
47+
* Returns a string representation of the constraint.
48+
*
49+
* @throws InvalidArgumentException
50+
*/
51+
public function toString(): string
52+
{
53+
return 'has the key ' . $this->exporter()->export($this->key) . $this->constraint->toString();
54+
}
55+
56+
/**
57+
* Evaluates the constraint for parameter $other. Returns true if the
58+
* constraint is met, false otherwise.
59+
*
60+
* @param mixed $other value or object to evaluate
61+
*/
62+
protected function matches($other): bool
63+
{
64+
if (is_array($other)) {
65+
if (!array_key_exists($this->key, $other)) {
66+
return false;
67+
}
68+
69+
return $this->constraint->matches($other[$this->key]);
70+
}
71+
72+
if ($other instanceof ArrayAccess) {
73+
if (!$other->offsetExists($this->key)) {
74+
return false;
75+
}
76+
77+
return $this->constraint->matches($other[$this->key]);
78+
}
79+
80+
return false;
81+
}
82+
83+
/**
84+
* Returns the description of the failure.
85+
*
86+
* The beginning of failure messages is "Failed asserting that" in most
87+
* cases. This method should return the second part of that sentence.
88+
*
89+
* @param mixed $other evaluated value or object
90+
*
91+
* @throws InvalidArgumentException
92+
*/
93+
protected function failureDescription($other): string
94+
{
95+
return 'an array ' . $this->toString();
96+
}
97+
}

tests/PayzenGatewayFactoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function test_create_gateway()
3838
'ctx_mode' => Api::MODE_PRODUCTION,
3939
'site_id' => '123456',
4040
'certificate' => '123456',
41-
'directory' => __DIR__ . '/../cache',
41+
'directory' => dirname(__DIR__, 1) . '/cache',
4242
]);
4343

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

0 commit comments

Comments
 (0)