Skip to content

Commit

Permalink
validate detail service response
Browse files Browse the repository at this point in the history
  • Loading branch information
adriaroca committed Aug 6, 2019
1 parent 06bd046 commit e72a2b2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/Exceptions/ResponseError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Omatech\SeurDispatchesService\Exceptions;

class ResponseError extends \Exception
{

}
38 changes: 32 additions & 6 deletions src/Services/DetailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Omatech\SeurDispatchesService\Entities\DetailRequest;
use Omatech\SeurDispatchesService\Entities\Dispatch;
use Omatech\SeurDispatchesService\Exceptions\ResponseError;
use Omatech\SeurDispatchesService\Exceptions\ThereAreNotDispatchesWithThisReference;

class DetailService extends Service
{
/**
* @param DetailRequest $request
* @return Dispatch
* @throws ResponseError
* @throws ThereAreNotDispatchesWithThisReference
*/
public function make(DetailRequest $request): Dispatch
Expand All @@ -28,12 +30,7 @@ public function make(DetailRequest $request): Dispatch

$response = $this->call();

if (!is_a($response, \SimpleXMLElement::class)) {
throw new IncorrectCallResponse();
}
if ($response->count() === 0) {
throw new ThereAreNotDispatchesWithThisReference();
}
$this->validate($response);

return Dispatch::fromXML($response->EXPEDICION);
}
Expand All @@ -52,4 +49,33 @@ protected function clean(string $response)

return $response;
}

/**
* @param $response
* @throws ResponseError
* @throws ThereAreNotDispatchesWithThisReference
*/
private function validate($response): void
{
if (!is_a($response, \SimpleXMLElement::class)) {
throw new IncorrectCallResponse();
}

if ($response->count() === 0) {
throw new ThereAreNotDispatchesWithThisReference();
}

$errorCodes = [
'CEXP_0001',
'CEXP_0002',
'CEXP_0003',
'CEXP_0004',
'CEXP_0005',
];

if (isset($response->CODIGO) && in_array($response->CODIGO, $errorCodes)) {
$message = $response->DESCRIPCION ?? 'Response error';
throw new ResponseError((string) $message);
}
}
}
44 changes: 43 additions & 1 deletion tests/Services/DetailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Omatech\SeurDispatchesService\Entities\DetailRequest;
use Omatech\SeurDispatchesService\Entities\Dispatch;
use Omatech\SeurDispatchesService\Exceptions\ResponseError;
use Omatech\SeurDispatchesService\Services\DetailService;
use Omatech\SeurDispatchesService\Tests\BaseTestCase;
use Omatech\SeurDispatchesService\Values\Dispatch\Type;
use Omatech\SeurDispatchesService\Values\Endpoint;

class DetailTest extends BaseTestCase
{
/** @test **/
/** @test * */
public function it_returns_a_dispatch()
{
$type = new Type('S');
Expand All @@ -30,4 +31,45 @@ public function it_returns_a_dispatch()
$this->assertTrue(is_a($service, Dispatch::class));
$this->assertEquals($id, $service->expedicionId());
}

/* @test
* @dataProvider wrongRequestProvider
* @param $detailRequest
* @param $expectedException
* @throws ResponseError
* @throws \Omatech\SeurDispatchesService\Exceptions\ThereAreNotDispatchesWithThisReference
*/
public function it_returns_an_error_when_request_is_wrong($detailRequest, $expectedException)
{
$this->expectException($expectedException);

$endpoint = new Endpoint('ConsultaDetalleExpedicionesStr', getenv('SEUR_MODE'));
$service = (new DetailService($endpoint))->make($detailRequest);

$this->assertTrue(!is_a($service, Dispatch::class));
}

public function wrongRequestProvider()
{
return [
[//CEXP_0001
new DetailRequest(
new Type('S'),
getenv('SEUR_ID_S_EXAMPLE'),
getenv('SEUR_USER'),
'wrongpassword'
),
ResponseError::class,
],
[//CEXP_0001
new DetailRequest(
new Type('S'),
getenv('SEUR_ID_S_EXAMPLE'),
'wronguser',
getenv('SEUR_PASSWORD')
),
ResponseError::class,
],
];
}
}

0 comments on commit e72a2b2

Please sign in to comment.