Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Helldar committed Jul 23, 2021
1 parent 7e4d8ff commit dcf26f5
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 2 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"require-dev": {
"andrey-helldar/cashier": "^1.1.1",
"andrey-helldar/support": "^4.1",
"illuminate/database": "^6.0|^7.0|^8.0",
"nesbot/carbon": "^2.0",
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^4.0|^5.0"
},
Expand Down
1 change: 0 additions & 1 deletion src/Helpers/Statuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Statuses extends BaseStatus
public const REFUNDING = [
'AUTHORIZED',
'AUTHORIZING',
'CONFIRMED',
'CONFIRMING',
'REFUNDING',
];
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/Models/CashierDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Tests\Fixtures\Models;

use Helldar\CashierDriver\Tinkoff\QrCode\Resources\Response;
use Illuminate\Database\Eloquent\Model;
use Tests\TestCase;

/**
* @property-read string $payment_id
* @property-read Response $details
*/
class CashierDetail extends Model
{
protected function getPaymentIdAttribute(): string
{
return TestCase::PAYMENT_ID;
}

protected function getDetailsAttribute(): Response
{
return Response::make([
Response::KEY_PAYMENT_ID => TestCase::PAYMENT_ID,
Response::KEY_STATUS => TestCase::STATUS,
], false);
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/Models/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Fixtures\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property-read \Tests\Fixtures\Models\CashierDetail $cashier
*/
class Payment extends Model
{
public function getCashierAttribute(): CashierDetail
{
return new CashierDetail();
}
}
31 changes: 31 additions & 0 deletions tests/Fixtures/Requests/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Fixtures\Requests;

use Carbon\Carbon;
use Helldar\Cashier\Constants\Currency;
use Helldar\CashierDriver\Tinkoff\QrCode\Resources\Request as BaseRequest;
use Tests\TestCase;

class Request extends BaseRequest
{
protected function paymentId(): string
{
return TestCase::PAYMENT_ID;
}

protected function sum(): float
{
return TestCase::PAYMENT_SUM;
}

protected function currency(): int
{
return Currency::RUB;
}

protected function createdAt(): Carbon
{
return Carbon::parse(TestCase::PAYMENT_DATE);
}
}
169 changes: 169 additions & 0 deletions tests/Helpers/StatusesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Tests\Helpers;

use Helldar\CashierDriver\Tinkoff\QrCode\Helpers\Statuses;
use Tests\TestCase;

final class StatusesTest extends TestCase
{
public function testModel()
{
$this->assertTrue($this->statuses()->hasCreated());
$this->assertTrue($this->statuses()->inProgress());

$this->assertFalse($this->statuses()->hasSuccess());
$this->assertFalse($this->statuses()->hasFailed());
$this->assertFalse($this->statuses()->hasRefunding());
$this->assertFalse($this->statuses()->hasRefunded());
}

public function testHasCreated()
{
$this->assertTrue($this->statuses()->hasCreated('FORM_SHOWED'));
$this->assertTrue($this->statuses()->hasCreated('NEW'));

$this->assertFalse($this->statuses()->hasCreated('AUTHORIZED'));
$this->assertFalse($this->statuses()->hasCreated('AUTHORIZING'));
$this->assertFalse($this->statuses()->hasCreated('CONFIRMING'));
$this->assertFalse($this->statuses()->hasCreated('REFUNDING'));

$this->assertFalse($this->statuses()->hasCreated('PARTIAL_REFUNDED'));
$this->assertFalse($this->statuses()->hasCreated('REFUNDED'));
$this->assertFalse($this->statuses()->hasCreated('REVERSED'));

$this->assertFalse($this->statuses()->hasCreated('ATTEMPTS_EXPIRED'));
$this->assertFalse($this->statuses()->hasCreated('CANCELED'));
$this->assertFalse($this->statuses()->hasCreated('DEADLINE_EXPIRED'));
$this->assertFalse($this->statuses()->hasCreated('REJECTED'));

$this->assertFalse($this->statuses()->hasCreated('CONFIRMED'));

$this->assertFalse($this->statuses()->hasCreated('UNKNOWN'));
}

public function testHasSuccess()
{
$this->assertFalse($this->statuses()->hasSuccess('FORM_SHOWED'));
$this->assertFalse($this->statuses()->hasSuccess('NEW'));

$this->assertFalse($this->statuses()->hasSuccess('AUTHORIZED'));
$this->assertFalse($this->statuses()->hasSuccess('AUTHORIZING'));
$this->assertFalse($this->statuses()->hasSuccess('CONFIRMING'));
$this->assertFalse($this->statuses()->hasSuccess('REFUNDING'));

$this->assertFalse($this->statuses()->hasSuccess('PARTIAL_REFUNDED'));
$this->assertFalse($this->statuses()->hasSuccess('REFUNDED'));
$this->assertFalse($this->statuses()->hasSuccess('REVERSED'));

$this->assertFalse($this->statuses()->hasSuccess('ATTEMPTS_EXPIRED'));
$this->assertFalse($this->statuses()->hasSuccess('CANCELED'));
$this->assertFalse($this->statuses()->hasSuccess('DEADLINE_EXPIRED'));
$this->assertFalse($this->statuses()->hasSuccess('REJECTED'));

$this->assertTrue($this->statuses()->hasSuccess('CONFIRMED'));

$this->assertFalse($this->statuses()->hasSuccess('UNKNOWN'));
}

public function testHasFailed()
{
$this->assertFalse($this->statuses()->hasFailed('FORM_SHOWED'));
$this->assertFalse($this->statuses()->hasFailed('NEW'));

$this->assertFalse($this->statuses()->hasFailed('AUTHORIZED'));
$this->assertFalse($this->statuses()->hasFailed('AUTHORIZING'));
$this->assertFalse($this->statuses()->hasFailed('CONFIRMING'));
$this->assertFalse($this->statuses()->hasFailed('REFUNDING'));

$this->assertFalse($this->statuses()->hasFailed('PARTIAL_REFUNDED'));
$this->assertFalse($this->statuses()->hasFailed('REFUNDED'));
$this->assertFalse($this->statuses()->hasFailed('REVERSED'));

$this->assertTrue($this->statuses()->hasFailed('ATTEMPTS_EXPIRED'));
$this->assertTrue($this->statuses()->hasFailed('CANCELED'));
$this->assertTrue($this->statuses()->hasFailed('DEADLINE_EXPIRED'));
$this->assertTrue($this->statuses()->hasFailed('REJECTED'));

$this->assertFalse($this->statuses()->hasFailed('CONFIRMED'));

$this->assertFalse($this->statuses()->hasFailed('UNKNOWN'));
}

public function testHasRefunding()
{
$this->assertFalse($this->statuses()->hasRefunding('FORM_SHOWED'));
$this->assertFalse($this->statuses()->hasRefunding('NEW'));

$this->assertTrue($this->statuses()->hasRefunding('AUTHORIZED'));
$this->assertTrue($this->statuses()->hasRefunding('AUTHORIZING'));
$this->assertTrue($this->statuses()->hasRefunding('CONFIRMING'));
$this->assertTrue($this->statuses()->hasRefunding('REFUNDING'));

$this->assertFalse($this->statuses()->hasRefunding('PARTIAL_REFUNDED'));
$this->assertFalse($this->statuses()->hasRefunding('REFUNDED'));
$this->assertFalse($this->statuses()->hasRefunding('REVERSED'));

$this->assertFalse($this->statuses()->hasRefunding('ATTEMPTS_EXPIRED'));
$this->assertFalse($this->statuses()->hasRefunding('CANCELED'));
$this->assertFalse($this->statuses()->hasRefunding('DEADLINE_EXPIRED'));
$this->assertFalse($this->statuses()->hasRefunding('REJECTED'));

$this->assertFalse($this->statuses()->hasRefunding('CONFIRMED'));

$this->assertFalse($this->statuses()->hasRefunding('UNKNOWN'));
}

public function testHasRefunded()
{
$this->assertFalse($this->statuses()->hasRefunded('FORM_SHOWED'));
$this->assertFalse($this->statuses()->hasRefunded('NEW'));

$this->assertFalse($this->statuses()->hasRefunded('AUTHORIZED'));
$this->assertFalse($this->statuses()->hasRefunded('AUTHORIZING'));
$this->assertFalse($this->statuses()->hasRefunded('CONFIRMING'));
$this->assertFalse($this->statuses()->hasRefunded('REFUNDING'));

$this->assertTrue($this->statuses()->hasRefunded('PARTIAL_REFUNDED'));
$this->assertTrue($this->statuses()->hasRefunded('REFUNDED'));
$this->assertTrue($this->statuses()->hasRefunded('REVERSED'));

$this->assertFalse($this->statuses()->hasRefunded('ATTEMPTS_EXPIRED'));
$this->assertFalse($this->statuses()->hasRefunded('CANCELED'));
$this->assertFalse($this->statuses()->hasRefunded('DEADLINE_EXPIRED'));
$this->assertFalse($this->statuses()->hasRefunded('REJECTED'));

$this->assertFalse($this->statuses()->hasRefunded('CONFIRMED'));

$this->assertFalse($this->statuses()->hasRefunded('UNKNOWN'));
}

public function testInProgress()
{
$this->assertTrue($this->statuses()->inProgress('FORM_SHOWED'));
$this->assertTrue($this->statuses()->inProgress('NEW'));

$this->assertTrue($this->statuses()->inProgress('AUTHORIZED'));
$this->assertTrue($this->statuses()->inProgress('AUTHORIZING'));
$this->assertTrue($this->statuses()->inProgress('CONFIRMING'));
$this->assertTrue($this->statuses()->inProgress('REFUNDING'));

$this->assertFalse($this->statuses()->inProgress('PARTIAL_REFUNDED'));
$this->assertFalse($this->statuses()->inProgress('REFUNDED'));
$this->assertFalse($this->statuses()->inProgress('REVERSED'));

$this->assertFalse($this->statuses()->inProgress('ATTEMPTS_EXPIRED'));
$this->assertFalse($this->statuses()->inProgress('CANCELED'));
$this->assertFalse($this->statuses()->inProgress('DEADLINE_EXPIRED'));
$this->assertFalse($this->statuses()->inProgress('REJECTED'));

$this->assertFalse($this->statuses()->inProgress('CONFIRMED'));

$this->assertTrue($this->statuses()->inProgress('UNKNOWN'));
}

protected function statuses(): Statuses
{
return Statuses::make()->model($this->model());
}
}
80 changes: 80 additions & 0 deletions tests/Resources/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Tests\Resources;

use Helldar\Cashier\Constants\Currency;
use Tests\Fixtures\Requests\Request;
use Tests\TestCase;

class RequestTest extends TestCase
{
public function testParams()
{
$this->assertSame(TestCase::PAYMENT_ID, $this->request()->getPaymentId());
$this->assertSame(TestCase::PAYMENT_SUM_FORMATTED, $this->request()->getSum());
$this->assertSame((string) Currency::RUB, $this->request()->getCurrency());
}

public function testCreatedAt()
{
$this->assertSame(TestCase::PAYMENT_DATE_FORMATTED, $this->request()->getCreatedAt());

$this->assertMatchesRegularExpression('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/', $this->request()->getCreatedAt());
}

public function testNow()
{
$this->assertMatchesRegularExpression('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/', $this->request()->getCreatedAt());
$this->assertMatchesRegularExpression('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/', $this->request()->getCreatedAt());
}

public function testUniqueId()
{
$request = $this->request();

$this->assertMatchesRegularExpression('/^(([0-9]|[a-f]|[A-F]){32})$/', $request->getUniqueId());
$this->assertMatchesRegularExpression('/^(([0-9]|[a-f]|[A-F]){32})$/', $request->getUniqueId());
}

public function testSameUniqueId()
{
$request = $this->request();

$unique_id = $request->getUniqueId();

$this->assertSame($unique_id, $request->getUniqueId());
$this->assertSame($unique_id, $request->getUniqueId());
}

public function testRandomUniqueId()
{
$request = $this->request();

$unique_id = $request->getUniqueId();

$this->assertSame($unique_id, $request->getUniqueId());
$this->assertSame($unique_id, $request->getUniqueId());

$this->assertNotSame($unique_id, $request->getUniqueId(true));
$this->assertNotSame($unique_id, $request->getUniqueId(true));
$this->assertNotSame($unique_id, $request->getUniqueId());
}

public function testToArray()
{
$expected = [
'OrderId' => TestCase::PAYMENT_ID,

'Amount' => TestCase::PAYMENT_SUM_FORMATTED,

'Currency' => (string) Currency::RUB,
];

$this->assertSame($expected, $this->request()->toArray());
}

protected function request(): Request
{
return Request::make($this->model());
}
}
Loading

0 comments on commit dcf26f5

Please sign in to comment.