diff --git a/src/Exceptions/ResponseError.php b/src/Exceptions/ResponseError.php new file mode 100644 index 0000000..610e4b4 --- /dev/null +++ b/src/Exceptions/ResponseError.php @@ -0,0 +1,8 @@ +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); } @@ -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); + } + } } \ No newline at end of file diff --git a/tests/Services/DetailTest.php b/tests/Services/DetailTest.php index 066df4a..2004694 100644 --- a/tests/Services/DetailTest.php +++ b/tests/Services/DetailTest.php @@ -4,6 +4,7 @@ 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; @@ -11,7 +12,7 @@ class DetailTest extends BaseTestCase { - /** @test **/ + /** @test * */ public function it_returns_a_dispatch() { $type = new Type('S'); @@ -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, + ], + ]; + } }