Skip to content

Commit

Permalink
Merge pull request #13 from giansalex/feature/statuscode-to-error
Browse files Browse the repository at this point in the history
StatusCode 98 to error
  • Loading branch information
giansalex authored Nov 6, 2018
2 parents 901c6b7 + 4a5a242 commit e6a9e53
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
39 changes: 34 additions & 5 deletions src/Ws/Services/ExtService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Greenter\Ws\Services;

use Greenter\Model\Response\Error;
use Greenter\Model\Response\StatusResult;

/**
Expand All @@ -31,15 +32,20 @@ public function getStatus($ticket)
];
$response = $client->call('getStatus', ['parameters' => $params]);
$status = $response->status;
$cdrZip = $status->content;
$code = $status->statusCode;

$result
->setCode($code)
->setSuccess(true);
$result->setCode($code);

if ('0' == $code || '99' == $code) {
if ($this->isPending($code)) {
$result->setError($this->getCustomError($code));

return $result;
}

if ($this->isProcessed($code)) {
$cdrZip = $status->content;
$result
->setSuccess(true)
->setCdrResponse($this->extractResponse($cdrZip))
->setCdrZip($cdrZip);

Expand All @@ -55,4 +61,27 @@ public function getStatus($ticket)

return $result;
}

/**
* @param string $code
* @return Error
*/
private function getCustomError($code)
{
$error = new Error();
$error->setCode($code)
->setMessage('El procesamiento del comprobante aún no ha terminado');

return $error;
}

private function isProcessed($code)
{
return '0' == $code || '99' == $code;
}

private function isPending($code)
{
return '98' == $code;
}
}
12 changes: 12 additions & 0 deletions tests/Ws/Services/FeSunatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ public function testGetStatus()
$this->assertContains('aceptada', $result->getCdrResponse()->getDescription());
}

public function testGetStatusPending()
{
$service = $this->getExtServicePendingProcess();

$result = $service->getStatus('223123123213');

$this->assertFalse($result->isSuccess());
$this->assertNull($result->getCdrResponse());
$this->assertNotNull($result->getError());
$this->assertNotEmpty($result->getError()->getMessage());
}

/**
* @dataProvider codeProvider
* @param $code
Expand Down
36 changes: 25 additions & 11 deletions tests/Ws/Services/FeSunatTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,12 @@ protected function getExtSender()
->getMock();

$stub->method('call')
->will($this->returnCallback(function ($func, $params) {
->will($this->returnCallback(function () {
$zipContent = file_get_contents(__DIR__.'/../../Resources/cdrBaja.zip');
$obj = new \stdClass();
if ($func == 'getStatus') {
$obj->status = new \stdClass();
$obj->status->statusCode = '0';
$obj->status->content = $zipContent;
} elseif ($func == 'getStatusCdr') {
$obj->statusCdr = new \stdClass();
$obj->statusCdr->statusCode = '0';
$obj->statusCdr->statusMessage = 'ACEPTADA';
$obj->statusCdr->content = $zipContent;
}
$obj->status = new \stdClass();
$obj->status->statusCode = '0';
$obj->status->content = $zipContent;

return $obj;
}));
Expand Down Expand Up @@ -212,4 +205,25 @@ private function getErrorCodeProvider()
/**@var $stub ErrorCodeProviderInterface */
return $stub;
}

/**
* @return ExtService
*/
protected function getExtServicePendingProcess()
{
$stub = $this->getMockBuilder(WsClientInterface::class)
->getMock();
$obj = new \stdClass();
$obj->status = new \stdClass();
$obj->status->statusCode = '98';

$stub->method('call')
->willReturn($obj);

/**@var $stub WsClientInterface */
$sunat = new ExtService();
$sunat->setClient($stub);

return $sunat;
}
}

0 comments on commit e6a9e53

Please sign in to comment.