|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpCfdi\CfdiSatScraper\Tests\Unit\Internal; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use GuzzleHttp\Exception\RequestException; |
| 9 | +use GuzzleHttp\Psr7\Response; |
| 10 | +use PhpCfdi\CfdiSatScraper\Contracts\ResourceDownloadHandlerInterface; |
| 11 | +use PhpCfdi\CfdiSatScraper\Exceptions\ResourceDownloadResponseError; |
| 12 | +use PhpCfdi\CfdiSatScraper\Internal\ResourceDownloaderPromiseHandler; |
| 13 | +use PhpCfdi\CfdiSatScraper\ResourceType; |
| 14 | +use PhpCfdi\CfdiSatScraper\Tests\TestCase; |
| 15 | +use RuntimeException; |
| 16 | + |
| 17 | +final class ResourceDownloaderPromiseHandlerTest extends TestCase |
| 18 | +{ |
| 19 | + public function testValidateResponseWithInvalidStatusCode(): void |
| 20 | + { |
| 21 | + $resourceDownloadHandler = $this->createMock(ResourceDownloadHandlerInterface::class); |
| 22 | + $response = new Response(400); |
| 23 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 24 | + |
| 25 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 26 | + |
| 27 | + $this->expectException(ResourceDownloadResponseError::class); |
| 28 | + $this->expectExceptionMessage( |
| 29 | + sprintf('Download of CFDI %s return an invalid status code %d', $uuid, $response->getStatusCode()), |
| 30 | + ); |
| 31 | + |
| 32 | + $handler->validateResponse($response, $uuid); |
| 33 | + } |
| 34 | + |
| 35 | + public function testValidateResponseWithEmptyBody(): void |
| 36 | + { |
| 37 | + $resourceDownloadHandler = $this->createMock(ResourceDownloadHandlerInterface::class); |
| 38 | + $response = new Response(200, body: ''); |
| 39 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 40 | + |
| 41 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 42 | + |
| 43 | + $this->expectException(ResourceDownloadResponseError::class); |
| 44 | + $this->expectExceptionMessage( |
| 45 | + sprintf('Download of CFDI %s return an empty body', $uuid), |
| 46 | + ); |
| 47 | + |
| 48 | + $handler->validateResponse($response, $uuid); |
| 49 | + } |
| 50 | + |
| 51 | + public function testValidateResponseIsXmlButWithoutUuid(): void |
| 52 | + { |
| 53 | + $resourceDownloadHandler = $this->createMock(ResourceDownloadHandlerInterface::class); |
| 54 | + $response = new Response(200, body: '<root/>'); |
| 55 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 56 | + |
| 57 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 58 | + |
| 59 | + $this->expectException(ResourceDownloadResponseError::class); |
| 60 | + $this->expectExceptionMessage( |
| 61 | + sprintf('Download of CFDI %s return something that is not a CFDI', $uuid), |
| 62 | + ); |
| 63 | + |
| 64 | + $handler->validateResponse($response, $uuid); |
| 65 | + } |
| 66 | + |
| 67 | + public function testValidateResponseIsPdfButMimeTypeDoesNotMatch(): void |
| 68 | + { |
| 69 | + $resourceDownloadHandler = $this->createMock(ResourceDownloadHandlerInterface::class); |
| 70 | + $mimeType = 'text/plain'; |
| 71 | + $response = new Response(200, body: 'text'); |
| 72 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 73 | + |
| 74 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::pdf(), $resourceDownloadHandler); |
| 75 | + |
| 76 | + $this->expectException(ResourceDownloadResponseError::class); |
| 77 | + $this->expectExceptionMessage( |
| 78 | + sprintf('Download of CFDI %s return something that is not a PDF (mime %s)', $uuid, $mimeType), |
| 79 | + ); |
| 80 | + |
| 81 | + $handler->validateResponse($response, $uuid); |
| 82 | + } |
| 83 | + |
| 84 | + public function testPromiseFullfilledWithValidResponse(): void |
| 85 | + { |
| 86 | + $resourceDownloadHandler = new ResourceDownloadHandlerSpy(); |
| 87 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 88 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 89 | + $cfdiContent = <<< XML |
| 90 | + <?xml version="1.0" encoding="UTF-8"?> |
| 91 | + <cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4"> |
| 92 | + <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" UUID="$uuid"/> |
| 93 | + </cfdi:Comprobante> |
| 94 | + XML; |
| 95 | + $response = new Response(200, body: $cfdiContent); |
| 96 | + $handler->promiseFulfilled($response, $uuid); |
| 97 | + |
| 98 | + $this->assertSame($uuid, $resourceDownloadHandler->lastUuid); |
| 99 | + $this->assertSame($cfdiContent, $resourceDownloadHandler->lastContent); |
| 100 | + $this->assertSame($response, $resourceDownloadHandler->lastResponse); |
| 101 | + $this->assertSame([$uuid], $handler->downloadedUuids()); |
| 102 | + } |
| 103 | + |
| 104 | + public function testPromiseFullfilledWithErrorResponse(): void |
| 105 | + { |
| 106 | + $resourceDownloadHandler = new ResourceDownloadHandlerSpy(); |
| 107 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 108 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 109 | + $response = new Response(500); |
| 110 | + $handler->promiseFulfilled($response, $uuid); |
| 111 | + |
| 112 | + $this->assertSame($uuid, $resourceDownloadHandler->lastError->getUuid()); |
| 113 | + } |
| 114 | + |
| 115 | + public function testPromiseFullfilledWithException(): void |
| 116 | + { |
| 117 | + $resourceDownloadHandler = new ResourceDownloadHandlerSpy(); |
| 118 | + $resourceDownloadHandler->onSuccessException = new Exception('Dummy exception'); |
| 119 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 120 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 121 | + $cfdiContent = <<< XML |
| 122 | + <?xml version="1.0" encoding="UTF-8"?> |
| 123 | + <cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4"> |
| 124 | + <tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" UUID="$uuid"/> |
| 125 | + </cfdi:Comprobante> |
| 126 | + XML; |
| 127 | + $response = new Response(200, body: $cfdiContent); |
| 128 | + $handler->promiseFulfilled($response, $uuid); |
| 129 | + |
| 130 | + $this->assertSame($uuid, $resourceDownloadHandler->lastError->getUuid()); |
| 131 | + } |
| 132 | + |
| 133 | + public function testPromiseRejected(): void |
| 134 | + { |
| 135 | + $resourceDownloadHandler = new ResourceDownloadHandlerSpy(); |
| 136 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 137 | + $reason = $this->createMock(RuntimeException::class); |
| 138 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 139 | + $handler->promiseRejected($reason, $uuid); |
| 140 | + |
| 141 | + $handledError = $resourceDownloadHandler->lastError; |
| 142 | + $this->assertSame($uuid, $handledError->getUuid()); |
| 143 | + $this->assertSame($reason, $handledError->getReason()); |
| 144 | + } |
| 145 | + |
| 146 | + public function testPromiseRejectedReasonIsRequestException(): void |
| 147 | + { |
| 148 | + $resourceDownloadHandler = new ResourceDownloadHandlerSpy(); |
| 149 | + $handler = new ResourceDownloaderPromiseHandler(ResourceType::xml(), $resourceDownloadHandler); |
| 150 | + $reason = $this->createMock(RequestException::class); |
| 151 | + $uuid = 'b01017a7-e1b8-4a25-9e31-85ab56526f54'; |
| 152 | + $handler->promiseRejected($reason, $uuid); |
| 153 | + |
| 154 | + $handledError = $resourceDownloadHandler->lastError; |
| 155 | + $this->assertSame($uuid, $handledError->getUuid()); |
| 156 | + $this->assertSame($reason, $handledError->getReason()); |
| 157 | + } |
| 158 | +} |
0 commit comments