Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Integration tests for console commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
lakiboy committed Jun 25, 2018
1 parent 7e61fc6 commit acac607
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 4 deletions.
7 changes: 7 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<env name="KERNEL_CLASS" value="Damax\Services\Client\Tests\Functional\AppKernel" />
</php>

<testsuites>
<testsuite name="Damax services client suite">
<directory>tests</directory>
Expand All @@ -16,6 +20,9 @@
<filter>
<whitelist>
<directory>src</directory>
<exclude>
<directory>src/Bridge/Symfony/Bundle/Controller</directory>
</exclude>
</whitelist>
</filter>

Expand Down
6 changes: 5 additions & 1 deletion src/Bridge/Symfony/Console/Command/MvdLookupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

try {
$result = $this->client->checkPassport($input->getArgument('number'))->toArray();
} catch (InvalidRequestException $e) {
return $io->error($e->getMessage());
$io->error($e->getMessage());

return 1;
}

$row = function ($value, string $key): array {
Expand Down
10 changes: 7 additions & 3 deletions src/Bridge/Symfony/Console/Command/RosfinLookupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
$input->getArgument('birthDate')
);
} catch (InvalidRequestException $e) {
return $io->error($e->getMessage());
$io->error($e->getMessage());

return 1;
}

if (!count($check)) {
return $io->success('Not found.');
$io->success('Not found.');

return 0;
}

$format = function (RosfinItem $terrorist): array {
return [
['ID', $terrorist->id()],
['Type', $terrorist->type()],
['Full name', implode("\n", $terrorist->fullName())],
['Birth date', $terrorist->birthDate() ? $terrorist->birthDate()->format('Y-m-d') : '-'],
['Birth date', $terrorist->birthDate() ?: '-'],
['Birth place', $terrorist->birthPlace() ?: '-'],
['Description', $terrorist->description() ? mb_substr($terrorist->description(), 0, 120) : '-'],
['Address', $terrorist->address() ?: '-'],
Expand Down
102 changes: 102 additions & 0 deletions tests/Bridge/Symfony/Console/Command/MvdLookupCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Damax\Services\Client\Tests\Bridge\Symfony\Console\Command;

use Damax\Services\Client\Bridge\Symfony\Console\Command\MvdLookupCommand;
use Damax\Services\Client\Client;
use Damax\Services\Client\InvalidRequestException;
use Damax\Services\Client\PassportCheck;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

/**
* @group integration
* @group console
*/
class MvdLookupCommandTest extends KernelTestCase
{
/**
* @var Client|MockObject
*/
private $client;

/**
* @var CommandTester
*/
private $tester;

protected function setUp()
{
static::bootKernel();

$this->client = $this->createMock(Client::class);

$command = new MvdLookupCommand($this->client);
$command->setApplication(new Application(self::$kernel));

$this->tester = new CommandTester($command);
}

/**
* @test
*/
public function it_checks_passport()
{
$this->client
->expects($this->once())
->method('checkPassport')
->with('0000000001')
->willReturn(new PassportCheck([
'source' => '0000000001',
'code' => 1,
'message' => 'Valid passport',
'ok' => true,
'series' => '0000',
'number' => '000001',
]))
;

$code = $this->tester->execute(['command' => 'damax:mvd:passport:lookup', 'number' => '0000000001']);

$output = <<<CONSOLE
--------- ----------------
Field Value
--------- ----------------
source 0000000001
code 1
message Valid passport
ok +
series 0000
number 000001
--------- ----------------
CONSOLE;

$this->assertSame(0, $code);
$this->assertEquals($output, $this->tester->getDisplay());
}

/**
* @test
*/
public function it_throws_request_exception()
{
$this->client
->expects($this->once())
->method('checkPassport')
->with('0000000001')
->willThrowException(new InvalidRequestException('Connection problem.'))
;

$code = $this->tester->execute(['command' => 'damax:mvd:passport:lookup', 'number' => '0000000001']);

$this->assertSame(1, $code);
$this->assertEquals('[ERROR] Connection problem.', trim($this->tester->getDisplay()));
}
}
145 changes: 145 additions & 0 deletions tests/Bridge/Symfony/Console/Command/RosfinLookupCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

declare(strict_types=1);

namespace Damax\Services\Client\Tests\Bridge\Symfony\Console\Command;

use Damax\Services\Client\Bridge\Symfony\Console\Command\RosfinLookupCommand;
use Damax\Services\Client\Client;
use Damax\Services\Client\InvalidRequestException;
use Damax\Services\Client\RosfinCheck;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class RosfinLookupCommandTest extends KernelTestCase
{
/**
* @var Client|MockObject
*/
private $client;

/**
* @var CommandTester
*/
private $tester;

protected function setUp()
{
static::bootKernel();

$this->client = $this->createMock(Client::class);

$command = new RosfinLookupCommand($this->client);
$command->setApplication(new Application(self::$kernel));

$this->tester = new CommandTester($command);
}

/**
* @test
*/
public function it_throws_request_exception()
{
$this->client
->expects($this->once())
->method('checkRosfin')
->with('John Doe', '1983-01-20')
->willThrowException(new InvalidRequestException('Connection problem.'))
;

$code = $this->tester->execute(['command' => 'damax:rosfin:lookup', 'fullName' => 'John Doe', 'birthDate' => '1983-01-20']);

$this->assertSame(1, $code);
$this->assertEquals('[ERROR] Connection problem.', trim($this->tester->getDisplay()));
}

/**
* @test
*/
public function it_finds_no_results()
{
$this->client
->expects($this->once())
->method('checkRosfin')
->with('John Doe', '1983-01-20')
->willReturn(new RosfinCheck([]))
;

$code = $this->tester->execute(['command' => 'damax:rosfin:lookup', 'fullName' => 'John Doe', 'birthDate' => '1983-01-20']);

$this->assertSame(0, $code);
$this->assertEquals('[OK] Not found.', trim($this->tester->getDisplay()));
}

/**
* @test
*/
public function it_finds_results()
{
$this->client
->expects($this->once())
->method('checkRosfin')
->with('John Doe', '1983-01-20')
->willReturn(new RosfinCheck([
[
'id' => 3302081139,
'type' => 4,
'fullName' => ['СОКОЛОВСКИЙ РУСЛАН ГЕННАДЬЕВИЧ'],
],
[
'id' => 1,
'type' => 3,
'fullName' => ['John Doe', 'Jane Doe'],
'birthDate' => '1983-01-20',
'birthPlace' => 'Riga',
'description' => 'Extremely dangerous.',
'address' => 'Military base.',
'resolution' => 'Wanted by FBI.',
'passport' => '0000000001',
],
]))
;

$code = $this->tester->execute(['command' => 'damax:rosfin:lookup', 'fullName' => 'John Doe', 'birthDate' => '1983-01-20']);

$output = <<<CONSOLE
------------- --------------------------------
Field Value
------------- --------------------------------
ID 3302081139
Type 4
Full name СОКОЛОВСКИЙ РУСЛАН ГЕННАДЬЕВИЧ
Birth date -
Birth place -
Description -
Address -
Resolution -
Passport -
------------- --------------------------------
------------- ----------------------
Field Value
------------- ----------------------
ID 1
Type 3
Full name John Doe
Jane Doe
Birth date 1983-01-20
Birth place Riga
Description Extremely dangerous.
Address Military base.
Resolution Wanted by FBI.
Passport 0000000001
------------- ----------------------
CONSOLE;

$this->assertSame(0, $code);
$this->assertEquals($output, $this->tester->getDisplay());
}
}
30 changes: 30 additions & 0 deletions tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Damax\Services\Client\Tests\Functional;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{
public function getCacheDir()
{
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'cache';
}

public function getLogDir(): string
{
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'logs';
}

public function registerBundles(): array
{
return [];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
}
}

0 comments on commit acac607

Please sign in to comment.