Skip to content

Commit

Permalink
Tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 16, 2023
1 parent dc0f5e4 commit 5de7bff
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 143 deletions.
49 changes: 41 additions & 8 deletions src/Entity/AbstractPayment.php
Expand Up @@ -3,59 +3,88 @@
namespace Contributte\Comgate\Entity;

use Contributte\Comgate\Entity\Codes\CountryCode;
use Contributte\Comgate\Exceptions\LogicException;

class AbstractPayment extends AbstractEntity
{

protected int $price;
protected ?int $price = null;

/** @var string ISO 4217 */
protected string $curr;
protected ?string $curr = null;

protected string $label;
protected ?string $label = null;

protected string $refId;
protected ?string $refId = null;

protected string $email;
protected ?string $email = null;

protected string $country = CountryCode::ALL;
protected ?string $country = CountryCode::ALL;

protected string $account;
protected ?string $account = null;

protected string $name;
protected ?string $name = null;

public function getPrice(): int
{
if ($this->price === null) {
throw new LogicException('Price is required');
}

return $this->price;
}

public function getCurr(): string
{
if ($this->curr === null) {
throw new LogicException('Curr is required');
}

return $this->curr;
}

public function getLabel(): string
{
if ($this->label === null) {
throw new LogicException('Label is required');
}

return $this->label;
}

public function getRefId(): string
{
if ($this->refId === null) {
throw new LogicException('RefId is required');
}

return $this->refId;
}

public function getEmail(): string
{
if ($this->email === null) {
throw new LogicException('Email is required');
}

return $this->email;
}

public function getCountry(): string
{
if ($this->country === null) {
throw new LogicException('Country is required');
}

return $this->country;
}

public function getAccount(): string
{
if ($this->account === null) {
throw new LogicException('Account is required');
}

return $this->account;
}

Expand All @@ -66,6 +95,10 @@ public function setAccount(string $account): void

public function getName(): string
{
if ($this->name === null) {
throw new LogicException('Name is required');
}

return $this->name;
}

Expand Down
4 changes: 0 additions & 4 deletions tests/.coveralls.yml

This file was deleted.

10 changes: 0 additions & 10 deletions tests/.gitignore

This file was deleted.

28 changes: 28 additions & 0 deletions tests/Cases/DI/ComgateExtension.phpt
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\Unit\DI;

use Contributte\Comgate\DI\ComgateExtension;
use Contributte\Comgate\Http\HttpClient;
use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Nette\DI\Compiler;
use Tester\Assert;

require_once __DIR__ . '/../../bootstrap.php';

Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('comgate', new ComgateExtension())
->addConfig([
'comgate' => [
'merchant' => '123456',
'secret' => 'top',
'test' => true,
],
]);
})->build();

Assert::type(HttpClient::class, $container->getService('comgate.http.client'));
});
29 changes: 12 additions & 17 deletions tests/cases/Manual/payments.php → tests/Cases/E2E/payments.php
Expand Up @@ -6,30 +6,25 @@
use Contributte\Comgate\Entity\Payment;
use Contributte\Comgate\Entity\PaymentStatus;
use Contributte\Comgate\Gateway\PaymentService;
use Contributte\Tester\Utils\ContainerBuilder;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;

require __DIR__ . '/../../../vendor/autoload.php';

function createContainer(): Container
{
$loader = new ContainerLoader(__DIR__ . '/../../tmp/cache', false);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('comgate', new ComgateExtension())
->addConfig([
'comgate' => [
'merchant' => '***',
'secret' => '***',
'test' => true,
],
]);
}, __FILE__);

/** @var Container $container */
$container = new $class();

return $container;
return ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('comgate', new ComgateExtension())
->addConfig([
'comgate' => [
'merchant' => '***',
'secret' => '***',
'test' => true,
],
]);
})->build();
}

function createPayment(): void
Expand Down
Expand Up @@ -7,11 +7,12 @@ use Contributte\Comgate\Entity\Codes\CountryCode;
use Contributte\Comgate\Entity\Codes\LangCode;
use Contributte\Comgate\Entity\Codes\PaymentMethodCode;
use Contributte\Comgate\Entity\Payment;
use Contributte\Tester\Toolkit;
use Tester\Assert;

require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../../bootstrap.php';

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand Down Expand Up @@ -40,7 +41,7 @@ test(function (): void {
], $payment->toArray());
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -51,7 +52,7 @@ test(function (): void {
Assert::equal('true', $payment->toArray()['embedded']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -62,7 +63,7 @@ test(function (): void {
Assert::equal('true', $payment->toArray()['initRecurring']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -73,7 +74,7 @@ test(function (): void {
Assert::equal('true', $payment->toArray()['preauth']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -84,7 +85,7 @@ test(function (): void {
Assert::equal('false', $payment->toArray()['prepareOnly']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -95,7 +96,7 @@ test(function (): void {
Assert::equal('true', $payment->toArray()['verification']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -106,7 +107,7 @@ test(function (): void {
Assert::equal('item101', $payment->toArray()['name']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -117,7 +118,7 @@ test(function (): void {
Assert::equal('acc1', $payment->toArray()['account']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand Down
Expand Up @@ -6,11 +6,12 @@ use Brick\Money\Money;
use Contributte\Comgate\Entity\Codes\CountryCode;
use Contributte\Comgate\Entity\Payment;
use Contributte\Comgate\Entity\RecurringPayment;
use Contributte\Tester\Toolkit;
use Tester\Assert;

require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../../bootstrap.php';

test(function (): void {
Toolkit::test(function (): void {
$payment = RecurringPayment::of(
'123-ABC-123',
Money::of(50, 'CZK'),
Expand All @@ -33,7 +34,7 @@ test(function (): void {
], $payment->toArray());
});

test(function (): void {
Toolkit::test(function (): void {
$payment = Payment::of(
Money::of(50, 'CZK'),
'Test item',
Expand All @@ -44,7 +45,7 @@ test(function (): void {
Assert::equal('item101', $payment->toArray()['name']);
});

test(function (): void {
Toolkit::test(function (): void {
$payment = RecurringPayment::of(
'123-ABC-123',
Money::of(50, 'CZK'),
Expand Down
Expand Up @@ -16,16 +16,17 @@ use Contributte\Comgate\Entity\Response\StornoResponse;
use Contributte\Comgate\Entity\Storno;
use Contributte\Comgate\Gateway\PaymentService;
use Contributte\Comgate\Http\HttpClient;
use Contributte\Tester\Toolkit;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Tester\Assert;

require_once __DIR__ . '/../../../bootstrap.php';
require_once __DIR__ . '/../../bootstrap.php';

test(function (): void {
Toolkit::test(function (): void {
$transactions = [];
$mockHandler = new MockHandler();
$handlerStack = HandlerStack::create(HandlerStack::create($mockHandler));
Expand All @@ -48,7 +49,7 @@ test(function (): void {
Assert::equal('create', (string) $transactions[0]['request']->getUri()->getPath());
});

test(function (): void {
Toolkit::test(function (): void {
$transactions = [];
$mockHandler = new MockHandler();
$handlerStack = HandlerStack::create(HandlerStack::create($mockHandler));
Expand All @@ -72,7 +73,7 @@ test(function (): void {
Assert::equal('recurring', (string) $transactions[0]['request']->getUri()->getPath());
});

test(function (): void {
Toolkit::test(function (): void {
$transactions = [];
$mockHandler = new MockHandler();
$handlerStack = HandlerStack::create(HandlerStack::create($mockHandler));
Expand All @@ -90,7 +91,7 @@ test(function (): void {
Assert::equal('status', (string) $transactions[0]['request']->getUri()->getPath());
});

test(function (): void {
Toolkit::test(function (): void {
$transactions = [];
$mockHandler = new MockHandler();
$handlerStack = HandlerStack::create(HandlerStack::create($mockHandler));
Expand All @@ -111,7 +112,7 @@ test(function (): void {
Assert::equal('refund', (string) $transactions[0]['request']->getUri()->getPath());
});

test(function (): void {
Toolkit::test(function (): void {
$transactions = [];
$mockHandler = new MockHandler();
$handlerStack = HandlerStack::create(HandlerStack::create($mockHandler));
Expand All @@ -129,7 +130,7 @@ test(function (): void {
Assert::equal('cancel', (string) $transactions[0]['request']->getUri()->getPath());
});

test(function (): void {
Toolkit::test(function (): void {
$transactions = [];
$mockHandler = new MockHandler();
$handlerStack = HandlerStack::create(HandlerStack::create($mockHandler));
Expand Down
7 changes: 2 additions & 5 deletions tests/bootstrap.php
@@ -1,13 +1,10 @@
<?php declare(strict_types = 1);

use Ninjify\Nunjuck\Environment;
use Contributte\Tester\Environment;

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}

// Configure environment
Environment::setupTester();
Environment::setupTimezone();
Environment::setupVariables(__DIR__);
Environment::setup(__DIR__);

0 comments on commit 5de7bff

Please sign in to comment.