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

Commit

Permalink
Support for multiple passports check.
Browse files Browse the repository at this point in the history
  • Loading branch information
lakiboy committed Aug 15, 2018
1 parent 7d7c782 commit 2580184
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 22 deletions.
19 changes: 19 additions & 0 deletions examples/check_multiple_passports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

// Usage:
// $ php examples/check_multiple_passports.php

declare(strict_types=1);

use Damax\Services\Client\Configuration;

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

$config = new Configuration('https://api.damax.solutions/services', 'token');

$result = $config
->getClient()
->checkMultiplePassports(['74 05 558551', '46 05 685971'])
;

dump($result);
24 changes: 22 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Http\Client\Common\HttpMethodsClient;
use Http\Client\HttpClient;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;

class Client
Expand All @@ -29,7 +30,26 @@ public function checkPassport(string $input): PassportCheck
{
$response = $this->httpClient->get('/mvd/passports?input=' . urlencode($input), ['accept' => 'application/json']);

return new PassportCheck($this->parseResponse($response));
return PassportCheck::fromArray($this->parseResponse($response));
}

/**
* @return PassportCheck[]
*
* @throws InvalidArgumentException
* @throws InvalidRequestException
*/
public function checkMultiplePassports(array $inputs): array
{
if (!count($inputs)) {
throw new InvalidArgumentException('At least one element required.');
}

$input = implode(',', array_map('urlencode', $inputs));

$response = $this->httpClient->get('/mvd/passports/multiple?input=' . $input, ['accept' => 'application/json']);

return array_map([PassportCheck::class, 'fromArray'], $this->parseResponse($response));
}

/**
Expand All @@ -56,7 +76,7 @@ public function checkRosfin(string $fullName, string $birthDate = null): RosfinC
'birthDate' => $birthDate,
]));

return new RosfinCheck($this->parseResponse($response));
return RosfinCheck::fromArray($this->parseResponse($response));
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/PassportCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ final class PassportCheck

private $data;

public function __construct(array $data)
public static function fromArray(array $data): self
{
$this->data = $data;
return new self($data);
}

public function source(): string
Expand Down Expand Up @@ -61,4 +61,9 @@ public function toArray(): array
{
return $this->data;
}

private function __construct(array $data)
{
$this->data = $data;
}
}
10 changes: 5 additions & 5 deletions src/RosfinCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ final class RosfinCheck implements IteratorAggregate, Countable
{
private $data;

public function __construct(array $data)
public static function fromArray(array $data): self
{
$this->data = $data;
return new self($data);
}

public function count(): int
Expand All @@ -25,16 +25,16 @@ public function count(): int

public function getIterator(): Iterator
{
return new ArrayIterator(array_map([$this, 'itemFactory'], $this->data));
return new ArrayIterator(array_map([RosfinItem::class, 'fromArray'], $this->data));
}

public function toArray(): array
{
return $this->data;
}

private function itemFactory(array $item): RosfinItem
private function __construct(array $data)
{
return new RosfinItem($item);
$this->data = $data;
}
}
9 changes: 7 additions & 2 deletions src/RosfinItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ final class RosfinItem

private $data;

public function __construct(array $data)
public static function fromArray(array $data): self
{
$this->data = $data;
return new self($data);
}

public function id(): int
Expand Down Expand Up @@ -69,4 +69,9 @@ public function toArray(): array
{
return $this->data;
}

private function __construct(array $data)
{
$this->data = $data;
}
}
4 changes: 2 additions & 2 deletions tests/Bridge/Symfony/Console/Command/MvdLookupCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function it_checks_passport()
->expects($this->once())
->method('checkPassport')
->with('0000000001')
->willReturn(new PassportCheck([
->willReturn(PassportCheck::fromArray([
'source' => '0000000001',
'code' => 1,
'message' => 'Valid passport',
Expand Down Expand Up @@ -85,7 +85,7 @@ public function it_checks_passport()
/**
* @test
*/
public function it_throws_request_exception()
public function it_fails_to_perform_check()
{
$this->client
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function setUp()
/**
* @test
*/
public function it_throws_request_exception()
public function it_fails_to_perform_check()
{
$this->client
->expects($this->once())
Expand All @@ -64,7 +64,7 @@ public function it_finds_no_results()
->expects($this->once())
->method('checkRosfin')
->with('John Doe', '1983-01-20')
->willReturn(new RosfinCheck([]))
->willReturn(RosfinCheck::fromArray([]))
;

$code = $this->tester->execute(['command' => 'damax:rosfin:lookup', 'fullName' => 'John Doe', 'birthDate' => '1983-01-20']);
Expand All @@ -82,7 +82,7 @@ public function it_finds_results()
->expects($this->once())
->method('checkRosfin')
->with('John Doe', '1983-01-20')
->willReturn(new RosfinCheck([
->willReturn(RosfinCheck::fromArray([
[
'id' => 3302081139,
'type' => 4,
Expand Down
61 changes: 59 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Damax\Services\Client\Client;
use Damax\Services\Client\InvalidRequestException;
use Damax\Services\Client\PassportCheck;
use Damax\Services\Client\RosfinItem;
use Http\Client\Common\HttpMethodsClient;
use Http\Message\MessageFactory;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Http\Mock\Client as MockClient;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;

Expand Down Expand Up @@ -97,7 +99,7 @@ public function it_downloads_passport_check_result()
/**
* @test
*/
public function it_throws_exception_when_downloading_passport_check_result()
public function it_fails_downloading_passport_check_result()
{
$stream = $this->createMock(StreamInterface::class);

Expand Down Expand Up @@ -148,7 +150,7 @@ public function it_checks_rosfin()
/**
* @test
*/
public function it_throws_exception_on_invalid_rosfin_check()
public function it_fails_to_perform_rosfin_check()
{
$response = $this->messageFactory->createResponse(400, 'Bad request', [], json_encode([
'message' => 'Empty full name.',
Expand All @@ -161,4 +163,59 @@ public function it_throws_exception_on_invalid_rosfin_check()

$this->client->checkRosfin('');
}

/**
* @test
*/
public function it_fails_to_perform_multiple_passports_check_on_empty_input()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('At least one element required.');

$this->client->checkMultiplePassports([]);
}

/**
* @test
*/
public function it_checks_multiple_passports()
{
$response = $this->messageFactory->createResponse(200, 'OK', [], json_encode([
[
'source' => '01 23 456789',
'code' => 2,
'message' => 'Invalid passport',
'ok' => false,
'series' => '0123',
'number' => '456789',
],
[
'source' => '98 76 543210',
'code' => 1,
'message' => 'Valid passport',
'ok' => true,
'series' => '9876',
'number' => '543210',
],
]));

$this->httpClient->addResponse($response);

$result = $this->client->checkMultiplePassports(['01 23 456789', '98 76 543210']);

$this->assertCount(2, $result);
$this->assertContainsOnlyInstancesOf(PassportCheck::class, $result);

$this->assertEquals('01 23 456789', $result[0]->source());
$this->assertEquals(2, $result[0]->code());
$this->assertTrue($result[0]->failed());

$this->assertEquals('98 76 543210', $result[1]->source());
$this->assertEquals(1, $result[1]->code());
$this->assertTrue($result[1]->passed());

$request = $this->httpClient->getLastRequest();
$this->assertEquals('/mvd/passports/multiple', $request->getUri()->getPath());
$this->assertEquals('input=01+23+456789,98+76+543210', $request->getUri()->getQuery());
}
}
4 changes: 2 additions & 2 deletions tests/PassportCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PassportCheckTest extends TestCase
*/
public function it_creates_passport_check_result()
{
$result = new PassportCheck([
$result = PassportCheck::fromArray([
'source' => '74 05 558551',
'code' => 2,
'message' => 'Invalid passport',
Expand All @@ -36,7 +36,7 @@ public function it_creates_passport_check_result()
*/
public function it_creates_malformed_passport_check_result()
{
$result = new PassportCheck([
$result = PassportCheck::fromArray([
'source' => '743',
'code' => 3,
'message' => 'Malformed passport',
Expand Down
2 changes: 1 addition & 1 deletion tests/RosfinCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RosfinCheckTest extends TestCase
*/
public function it_creates_rosfin_check_result()
{
$result = new RosfinCheck([
$result = RosfinCheck::fromArray([
[
'id' => 123,
'type' => 4,
Expand Down
2 changes: 1 addition & 1 deletion tests/RosfinItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RosfinItemTest extends TestCase
*/
public function it_creates_rosfin_item()
{
$result = new RosfinItem([
$result = RosfinItem::fromArray([
'id' => 123,
'type' => 4,
'fullName' => ['John Doe', 'Jane Doe'],
Expand Down

0 comments on commit 2580184

Please sign in to comment.