Skip to content

Commit

Permalink
Tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Dec 1, 2023
1 parent bee75a7 commit 5ad6f54
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 162 deletions.
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.

32 changes: 19 additions & 13 deletions tests/Cases/DI/CzechPostClientExtensionTest.php
Expand Up @@ -7,34 +7,40 @@
use Contributte\CzechPost\DI\CzechPostExtension;
use Contributte\CzechPost\Http\HttpClient;
use Contributte\CzechPost\Requestor\ConsignmentRequestor;
use Contributte\Tester\Utils\ContainerBuilder;
use GuzzleHttp\Client;
use Nette\DI\Compiler;
use Tester\Assert;
use Tests\Toolkit\ContainerTestCase;
use Tester\TestCase;

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

class CzechPostClientExtensionTest extends ContainerTestCase
class CzechPostClientExtensionTest extends TestCase
{

protected function setUpCompileContainer(Compiler $compiler): void
{
$compiler->addExtension('contributte.czechpost', new CzechPostExtension());
}

public function testServicesRegistration(): void
{
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('contributte.czechpost', new CzechPostExtension());
})->build();

// Basic classes
Assert::type(Client::class, $this->container->getService('contributte.czechpost.guzzle.client'));
Assert::type(HttpClient::class, $this->container->getService('contributte.czechpost.http.client'));
Assert::type(CpostRootquestor::class, $this->container->getService('contributte.czechpost.rootquestor'));
Assert::type(Client::class, $container->getService('contributte.czechpost.guzzle.client'));
Assert::type(HttpClient::class, $container->getService('contributte.czechpost.http.client'));
Assert::type(CpostRootquestor::class, $container->getService('contributte.czechpost.rootquestor'));

// Clients
Assert::type(ConsignmentClient::class, $this->container->getService('contributte.czechpost.client.consignment'));
Assert::type(ConsignmentClient::class, $container->getService('contributte.czechpost.client.consignment'));

// Requestors
Assert::type(ConsignmentRequestor::class, $this->container->getService('contributte.czechpost.requestor.consignment'));
Assert::type(ConsignmentRequestor::class, $this->container->getService('contributte.czechpost.rootquestor')->consignment);
Assert::type(ConsignmentRequestor::class, $container->getService('contributte.czechpost.requestor.consignment'));
Assert::type(ConsignmentRequestor::class, $container->getService('contributte.czechpost.rootquestor')->consignment);
}

protected function setUpCompileContainer(Compiler $compiler): void
{
$compiler->addExtension('contributte.czechpost', new CzechPostExtension());
}

}
Expand Down
68 changes: 33 additions & 35 deletions tests/Cases/UsageTest.php
Expand Up @@ -10,46 +10,21 @@
use Contributte\CzechPost\Http\GuzzleClient;
use Contributte\CzechPost\Requestor\ConsignmentRequestor;
use Contributte\CzechPost\Requestor\ParcelHistoryRequestor;
use Contributte\Tester\Environment;
use DateTimeImmutable;
use GuzzleHttp\Client;
use Ninjify\Nunjuck\TestCase\BaseTestCase;
use Tester\Assert;
use Tester\Environment;
use Tester\TestCase;
use Tests\Cases\XmlRequest\ConsignmentRequestFactoryTest;

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

final class UsageTest extends BaseTestCase
final class UsageTest extends TestCase
{

/** @var ConsignmentRequestor */
private $cpost;
private ConsignmentRequestor $cpost;

/** @var ParcelHistoryRequestor */
private $history;

protected function setUp(): void
{
Environment::skip('This is integration test against Ceska Posta testing environment.');

$config = [
'http' => [
'base_uri' => 'https://online3.postservis.cz/dopisonline/',
'auth' => ['dreplech', 'dreplech'],
],
'config' => [
'tmp_dir' => TEMP_DIR,
],
];

$guzzle = new GuzzleClient(new Client($config['http']));

$cpostClient = new ConsignmentClient($guzzle, $config);
$this->cpost = new ConsignmentRequestor($cpostClient);

$historyClient = new ParcelHistoryClient($guzzle);
$this->history = new ParcelHistoryRequestor($historyClient);
}
private ParcelHistoryRequestor $history;

public function testSend(): Dispatch
{
Expand Down Expand Up @@ -87,15 +62,15 @@ public function testGetConsignmentLabel(Dispatch $dispatch): void
{
$labelData = $this->cpost->printLabel($dispatch->getTrackingNumber());

$fileName = sprintf('%s/%s.pdf', TEMP_DIR, $dispatch->getTrackingNumber());
$fileName = sprintf('%s/%s.pdf', Environment::getTestDir(), $dispatch->getTrackingNumber());
file_put_contents($fileName, $labelData);

Assert::true(filesize($fileName) > 100000);
}

public function testGetConsignmentsOverviewInvalidId(): void
{
Assert::exception(function () {
Assert::exception(function (): void {
$this->cpost->detail('some-invalid-id');
}, ResponseException::class, 'Error: Zakázka č. some-invalid-id neexistuje., Code: -9');
}
Expand All @@ -113,7 +88,7 @@ public function testGetConsignmentByDate(): void

public function testGetConsignmentByDateNoRecords(): void
{
Assert::exception(function () {
Assert::exception(function (): void {
$longTimeAgo = (new DateTimeImmutable())->setDate(1975, 12, 24);
$this->cpost->findByDate($longTimeAgo);
}, ResponseException::class, 'Error: K datu 19751224 neexistuje žádný záznam., Code: -10');
Expand Down Expand Up @@ -165,14 +140,14 @@ public function testFetchIsoCodes(): void

public function testParcelHistoryInvalidTrackingNumber(): void
{
Assert::exception(function () {
Assert::exception(function (): void {
$this->history->history('invalid');
}, ResponseException::class, 'Parcel tracking error. State: -4, Description: "Pro tento druh zásilek Česká pošta informace nezobrazuje."');
}

public function testParcelHistoryParcelNotFound(): void
{
Assert::exception(function () {
Assert::exception(function (): void {
$this->history->history('RR2599903552');
}, ResponseException::class, 'Parcel tracking error. State: -3, Description: "Zásilka tohoto podacího čísla není v evidenci."');
}
Expand Down Expand Up @@ -246,6 +221,29 @@ public function testParcelStatus(): void
Assert::equal('60010', $currentState->getPostCode());
}

protected function setUp(): void
{
Environment::skip('This is integration test against Ceska Posta testing environment.');

$config = [
'http' => [
'base_uri' => 'https://online3.postservis.cz/dopisonline/',
'auth' => ['dreplech', 'dreplech'],
],
'config' => [
'tmp_dir' => Environment::getTestDir(),
],
];

$guzzle = new GuzzleClient(new Client($config['http']));

$cpostClient = new ConsignmentClient($guzzle, $config);
$this->cpost = new ConsignmentRequestor($cpostClient);

$historyClient = new ParcelHistoryClient($guzzle);
$this->history = new ParcelHistoryRequestor($historyClient);
}

private function assertConsignmentDispatch(Dispatch $confirm): void
{
Assert::true(
Expand Down
17 changes: 8 additions & 9 deletions tests/Cases/XmlRequest/ConsignmentRequestFactoryTest.php
Expand Up @@ -7,21 +7,15 @@
use Contributte\CzechPost\Entity\File;
use Contributte\CzechPost\Entity\Person;
use Contributte\CzechPost\XmlRequest\ConsignmentRequestFactory;
use Ninjify\Nunjuck\TestCase\BaseTestCase;
use Tester\Assert;
use Tester\TestCase;

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

final class ConsignmentRequestFactoryTest extends BaseTestCase
final class ConsignmentRequestFactoryTest extends TestCase
{

/** @var ConsignmentRequestFactory */
private $factory;

public function setUp(): void
{
$this->factory = new ConsignmentRequestFactory();
}
private ConsignmentRequestFactory $factory;

public static function createConsignmentWithoutCheque(): Consignment
{
Expand Down Expand Up @@ -78,6 +72,11 @@ public static function createConsignmentWithCheque(): Consignment
return $cons;
}

public function setUp(): void
{
$this->factory = new ConsignmentRequestFactory();
}

public function testCreateWithoutCheque(): void
{
$consignment = self::createConsignmentWithoutCheque();
Expand Down
86 changes: 0 additions & 86 deletions tests/Toolkit/ContainerTestCase.php

This file was deleted.

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 5ad6f54

Please sign in to comment.