Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from brianpando/dni_jne_json
Browse files Browse the repository at this point in the history
Dni jne json, close #22
  • Loading branch information
giansalex authored Oct 19, 2019
2 parents cd86599 + b2b8034 commit 48d00e7
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 23 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
colors="true"
stopOnFailure="true">
<php>
<env name="MOCK_URL" value="http://localhost"/>
<env name="MOCK_URL" value=""/>
</php>
<testsuites>
<testsuite name="Unit Tests">
Expand Down
11 changes: 11 additions & 0 deletions src/Peru/Http/Async/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ interface ClientInterface
* @return PromiseInterface
*/
public function getAsync(string $url, array $headers = []): PromiseInterface;

/**
* Post Request.
*
* @param string $url
* @param mixed $data
* @param array $headers
*
* @return PromiseInterface
*/
public function postAsync(string $url, $data, array $headers = []): PromiseInterface;
}
33 changes: 27 additions & 6 deletions src/Peru/Http/Async/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@ class HttpClient extends Client implements ClientInterface
* @return PromiseInterface
*/
public function getAsync(string $url, array $headers = []): PromiseInterface
{
return $this->requestAsync('GET', $url, null, $headers);
}

/**
* Post Request.
*
* @param string $url
* @param mixed $data
* @param array $headers
*
* @return PromiseInterface
*/
public function postAsync(string $url, $data, array $headers = []): PromiseInterface
{
return $this->requestAsync('POST', $url, $data, $headers);
}

private function requestAsync($method, $url, $data, $headers)
{
$deferred = new Deferred();
$headers = $this->buildHeaders();
$request = $this->request('GET', $url, $headers);
$headers = $this->buildHeaders($headers);

$request = $this->request($method, $url, $headers);
$request->on('response', function (Response $response) use ($deferred) {
$this->saveCookies($response->getHeaders());

Expand All @@ -48,7 +67,7 @@ public function getAsync(string $url, array $headers = []): PromiseInterface
$request->on('error', function ($e) use ($deferred) {
$deferred->reject($e);
});
$request->end();
$request->end($data);

return $deferred->promise();
}
Expand All @@ -70,12 +89,14 @@ private function saveCookies(array $headers)
}, $responseCookies);
}

private function buildHeaders()
private function buildHeaders(array $headers)
{
if (empty($this->cookies)) {
return [];
return $headers;
}

return ['Cookie' => implode('; ', $this->cookies)];
$headers['Cookie'] = implode('; ', $this->cookies);

return $headers;
}
}
2 changes: 1 addition & 1 deletion src/Peru/Http/ContextClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ContextClient implements ClientInterface
{
private const FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded';
private const USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1';

/**
* stream_context extra options.
*
Expand Down
15 changes: 12 additions & 3 deletions src/Peru/Jne/Async/Dni.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Dni
{
private const URL_CONSULT_FORMAT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/Afiliado/GetNombresCiudadano?DNI=%s';
private const URL_CONSULT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/api/AfiliadoApi/GetNombresCiudadano';

/**
* @var ClientInterface
Expand Down Expand Up @@ -41,11 +41,20 @@ public function __construct(ClientInterface $client, DniParser $parser)
*/
public function get(string $dni): PromiseInterface
{
$url = sprintf(self::URL_CONSULT_FORMAT, $dni);
$url = self::URL_CONSULT;
$payload = json_encode(['CODDNI' => $dni]);

return $this->client
->getAsync($url)
->postAsync(
$url,
$payload,
[
'Content-Type' => 'application/json;chartset=utf-8',
'Content-Length' => strlen($payload),
'Requestverificationtoken' => '30OB7qfO2MmL2Kcr1z4S0ttQcQpxH9pDUlZnkJPVgUhZOGBuSbGU4qM83JcSu7DZpZw-IIIfaDZgZ4vDbwE5-L9EPoBIHOOC1aSPi4FS_Sc1:clDOiaq7mKcLTK9YBVGt2R3spEU8LhtXEe_n5VG5VLPfG9UkAQfjL_WT9ZDmCCqtJypoTD26ikncynlMn8fPz_F_Y88WFufli38cUM-24PE1',
])
->then(function ($raw) use ($dni) {
$raw = json_decode($raw)->data;
return $this->parser->parse($dni, $raw);
});
}
Expand Down
15 changes: 11 additions & 4 deletions src/Peru/Jne/Dni.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
use Peru\Http\ClientInterface;
use Peru\Reniec\Person;
use Peru\Services\DniInterface;

/**
* Class Dni.
*/
class Dni implements DniInterface
{
private const URL_CONSULT_FORMAT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/Afiliado/GetNombresCiudadano?DNI=%s';
private const URL_CONSULT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/api/AfiliadoApi/GetNombresCiudadano';
/**
* @var ClientInterface
*/
Expand Down Expand Up @@ -48,8 +47,16 @@ public function __construct(ClientInterface $client, DniParser $parser)
*/
public function get(string $dni): ?Person
{
$url = sprintf(self::URL_CONSULT_FORMAT, $dni);
$raw = $this->client->get($url);
$url = self::URL_CONSULT;

$json = $this->client->post(
$url,
json_encode(['CODDNI' => $dni]),
[
'Content-Type' => 'application/json;chartset=utf-8',
'Requestverificationtoken' => '30OB7qfO2MmL2Kcr1z4S0ttQcQpxH9pDUlZnkJPVgUhZOGBuSbGU4qM83JcSu7DZpZw-IIIfaDZgZ4vDbwE5-L9EPoBIHOOC1aSPi4FS_Sc1:clDOiaq7mKcLTK9YBVGt2R3spEU8LhtXEe_n5VG5VLPfG9UkAQfjL_WT9ZDmCCqtJypoTD26ikncynlMn8fPz_F_Y88WFufli38cUM-24PE1',
]);
$raw = json_decode($json)->data;

return $this->parser->parse($dni, $raw);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Peru/Http/Async/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ public function testGetWithError()
await($this->client->getAsync('http://http323bin.org'), $this->loop);
}

/**
* @throws \Exception
*/
public function testPost()
{
$data = json_encode(['result' => 42]);
$result = await($this->client->postAsync('https://httpbin.org/post', $data, [
'Content-Type' => 'application/json',
'Content-Length' => strlen($data)
]), $this->loop);

$obj = json_decode($result);

$this->assertEquals(42, $obj->json->result);
}

/**
* @expectedException \RuntimeException
* @throws \Exception
*/
public function testPostWithError()
{
await($this->client->postAsync('https://httpbin323.org/post', ''), $this->loop);
}

protected function tearDown()
{
$this->loop->run();
Expand Down
13 changes: 7 additions & 6 deletions tests/Peru/Jne/Async/DniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
use Tests\Peru\Sunat\Async\HttpClientStub;

class DniTest extends TestCase
{
Expand All @@ -27,20 +28,20 @@ class DniTest extends TestCase
protected function setUp()
{
$this->loop = Factory::create();
$this->consult = new Dni(new HttpClient($this->loop), new DniParser());
$this->consult = new Dni(new HttpClientStub(new HttpClient($this->loop)), new DniParser());
}

/**
* @throws \Exception when the promise is rejected
*/
public function testGetDni()
{
$promise = $this->consult->get('48004836');
/**@var $person Person */
$person = await($promise, $this->loop);
$promise = $this->consult->get('48004836');
/**@var $person Person */
$person = await($promise, $this->loop);

$this->assertNotNull($person);
$this->assertEquals('48004836', $person->dni);
$this->assertNotNull($person);
$this->assertEquals('48004836', $person->dni);
}

protected function tearDown()
Expand Down
3 changes: 2 additions & 1 deletion tests/Peru/Jne/DniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Peru\{Http\ContextClient, Http\EmptyResponseDecorator, Jne\Dni, Jne\DniParser};
use PHPUnit\Framework\TestCase;
use Tests\Peru\Sunat\ClientStubDecorator;

/**
* Class DniTest
Expand All @@ -33,7 +34,7 @@ public function setUp()
]
];

$this->cs = new Dni(new EmptyResponseDecorator($client), new DniParser());
$this->cs = new Dni(new ClientStubDecorator(new EmptyResponseDecorator($client)), new DniParser());
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Peru/Sunat/Async/HttpClientStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ public function getAsync(string $url, array $headers = []): PromiseInterface
{
return $this->client->getAsync(ClientStubDecorator::getNewUrl($url), $headers);
}

/**
* Post Request.
*
* @param string $url
* @param mixed $data
* @param array $headers
*
* @return PromiseInterface
*/
public function postAsync(string $url, $data, array $headers = []): PromiseInterface
{
return $this->client->postAsync(ClientStubDecorator::getNewUrl($url), $data, $headers);
}
}
6 changes: 5 additions & 1 deletion tests/Peru/Sunat/ClientStubDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public function post(string $url, $data, array $headers = [])

public static function getNewUrl($url)
{
$urlBase = getenv('MOCK_URL');
$urlBase = $_ENV['MOCK_URL'];
if (empty($urlBase)) {
return $url;
}

$u = parse_url($url);

return $urlBase.$u['path'].(isset($u['query']) ? "?$u[query]" : '');
Expand Down

0 comments on commit 48d00e7

Please sign in to comment.