diff --git a/src/Contracts/HttpClientInterface.php b/src/Contracts/HttpClientInterface.php index 09847ef8..8e0cc63f 100644 --- a/src/Contracts/HttpClientInterface.php +++ b/src/Contracts/HttpClientInterface.php @@ -3,6 +3,7 @@ namespace Transbank\Contracts; use Psr\Http\Message\ResponseInterface; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; interface HttpClientInterface { @@ -12,7 +13,7 @@ interface HttpClientInterface * @param array|null $payload * @param array|null $options * - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return ResponseInterface */ diff --git a/src/Contracts/RequestService.php b/src/Contracts/RequestService.php index 0a7097bd..bdb57f0e 100644 --- a/src/Contracts/RequestService.php +++ b/src/Contracts/RequestService.php @@ -3,6 +3,7 @@ namespace Transbank\Contracts; use Transbank\Webpay\Options; +use Transbank\Webpay\Exceptions\WebpayRequestException; use Psr\Http\Message\ResponseInterface; use Transbank\Utils\TransbankApiRequest; @@ -14,7 +15,7 @@ interface RequestService * @param array $payload * @param Options $options * - * @throws \Transbank\Webpay\Exceptions\WebpayRequestException + * @throws WebpayRequestException * * @return array Response from the API as json. */ diff --git a/src/PatpassComercio/Inscription.php b/src/PatpassComercio/Inscription.php index 053a49d0..0973db44 100644 --- a/src/PatpassComercio/Inscription.php +++ b/src/PatpassComercio/Inscription.php @@ -17,6 +17,7 @@ use Transbank\Utils\RequestServiceTrait; use Transbank\Contracts\RequestService; use Transbank\PatpassComercio\Options; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; class Inscription { @@ -62,7 +63,7 @@ public function __construct( * @param string $city * * @throws InscriptionStartException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return InscriptionStartResponse */ @@ -122,7 +123,7 @@ public function start( * @param string $token * * @throws InscriptionStatusException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return InscriptionStatusResponse */ diff --git a/src/Utils/Curl/MessageTrait.php b/src/Utils/Curl/MessageTrait.php index 9a77ffaa..bb137204 100644 --- a/src/Utils/Curl/MessageTrait.php +++ b/src/Utils/Curl/MessageTrait.php @@ -3,7 +3,6 @@ namespace Transbank\Utils\Curl; use Psr\Http\Message\StreamInterface; -use Transbank\Utils\Curl\Exceptions\CurlRequestException; trait MessageTrait { @@ -61,35 +60,4 @@ public function withBody(StreamInterface $body): static $new->body = $body; return $new; } - - public function withHeader($name, $value): static - { - $new = clone $this; - $new->headers[$name] = (array) $value; - return $new; - } - - public function withAddedHeader($name, $value): static - { - $new = clone $this; - $new->headers[$name][] = $value; - return $new; - } - - private function createBody($body = ''): StreamInterface - { - $resource = fopen('php://temp', 'rw+'); - if ($resource === false) { - throw new CurlRequestException('Unable to open stream'); - } - - if (!empty($body)) { - $writtenBytes = fwrite($resource, $body); - if ($writtenBytes === false) { - throw new CurlRequestException('Unable to write to stream'); - } - } - - return new Stream($resource); - } } diff --git a/src/Utils/Curl/Request.php b/src/Utils/Curl/Request.php index 9f470e3a..4e86e25b 100644 --- a/src/Utils/Curl/Request.php +++ b/src/Utils/Curl/Request.php @@ -69,4 +69,24 @@ public function withUri(UriInterface $uri, $preserveHost = false): RequestInterf return $new; } + + public function withHeader($name, $value): RequestInterface + { + return clone $this; + } + + public function withAddedHeader($name, $value): RequestInterface + { + return clone $this; + } + + private function createBody($body = ''): StreamInterface + { + $resource = fopen('php://temp', 'rw+'); + if (!empty($body)) { + fwrite($resource, $body); + } + + return new Stream($resource); + } } diff --git a/src/Utils/Curl/Response.php b/src/Utils/Curl/Response.php index 3fe21ec6..3148e78d 100644 --- a/src/Utils/Curl/Response.php +++ b/src/Utils/Curl/Response.php @@ -46,4 +46,28 @@ public function getReasonPhrase(): string { return $this->reasonPhrase; } + + public function withHeader($name, $value): ResponseInterface + { + $new = clone $this; + $new->headers[$name] = (array) $value; + return $new; + } + + public function withAddedHeader($name, $value): ResponseInterface + { + $new = clone $this; + $new->headers[$name][] = $value; + return $new; + } + + private function createBody($body = ''): StreamInterface + { + $resource = fopen('php://temp', 'rw+'); + if (!empty($body)) { + fwrite($resource, $body); + } + + return new Stream($resource); + } } diff --git a/src/Utils/HttpClientRequestService.php b/src/Utils/HttpClientRequestService.php index f04a9c2c..2c896af7 100644 --- a/src/Utils/HttpClientRequestService.php +++ b/src/Utils/HttpClientRequestService.php @@ -8,6 +8,7 @@ use Transbank\Utils\Curl\HttpCurlClient; use Transbank\Webpay\Exceptions\WebpayRequestException; use Transbank\Webpay\Options; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; use Psr\Http\Message\ResponseInterface; class HttpClientRequestService implements RequestService @@ -56,8 +57,7 @@ public function setHttpClient(HttpClientInterface $httpClient): void * @param array $payload * @param Options $options * - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException - + * @throws CurlRequestException * @throws WebpayRequestException * * @return array diff --git a/src/Utils/RequestServiceTrait.php b/src/Utils/RequestServiceTrait.php index c34e94f9..a58bb488 100644 --- a/src/Utils/RequestServiceTrait.php +++ b/src/Utils/RequestServiceTrait.php @@ -3,6 +3,7 @@ namespace Transbank\Utils; use Transbank\Contracts\RequestService; +use Transbank\Webpay\Exceptions\WebpayRequestException; /** * Trait RequestServiceTrait . @@ -20,7 +21,7 @@ trait RequestServiceTrait * @param string $endpoint * @param array $payload * - * @throws \Transbank\Webpay\Exceptions\WebpayRequestException + * @throws WebpayRequestException * * @return array */ diff --git a/src/Webpay/Oneclick/MallInscription.php b/src/Webpay/Oneclick/MallInscription.php index 5430b992..92381ffc 100644 --- a/src/Webpay/Oneclick/MallInscription.php +++ b/src/Webpay/Oneclick/MallInscription.php @@ -9,6 +9,7 @@ use Transbank\Webpay\Oneclick\Exceptions\InscriptionStartException; use Transbank\Webpay\Oneclick\Responses\InscriptionFinishResponse; use Transbank\Webpay\Oneclick\Responses\InscriptionStartResponse; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; class MallInscription { @@ -26,7 +27,7 @@ class MallInscription * @return InscriptionStartResponse * * @throws InscriptionStartException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function start(string $username, string $email, string $responseUrl): InscriptionStartResponse { @@ -61,7 +62,7 @@ public function start(string $username, string $email, string $responseUrl): Ins * @return InscriptionFinishResponse * * @throws InscriptionFinishException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function finish(string $token): InscriptionFinishResponse { @@ -91,7 +92,7 @@ public function finish(string $token): InscriptionFinishResponse * @return bool * * @throws InscriptionDeleteException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function delete(string $tbkUser, string $username): bool { diff --git a/src/Webpay/Oneclick/MallTransaction.php b/src/Webpay/Oneclick/MallTransaction.php index 3a57d317..51a8734c 100644 --- a/src/Webpay/Oneclick/MallTransaction.php +++ b/src/Webpay/Oneclick/MallTransaction.php @@ -12,6 +12,7 @@ use Transbank\Webpay\Oneclick\Responses\MallTransactionCaptureResponse; use Transbank\Webpay\Oneclick\Responses\MallTransactionRefundResponse; use Transbank\Webpay\Oneclick\Responses\MallTransactionStatusResponse; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; class MallTransaction { @@ -30,7 +31,7 @@ class MallTransaction * @return MallTransactionAuthorizeResponse * * @throws MallTransactionAuthorizeException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function authorize( string $userName, @@ -73,7 +74,7 @@ public function authorize( * @return MallTransactionCaptureResponse * * @throws MallTransactionCaptureException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function capture( string $childCommerceCode, @@ -113,7 +114,7 @@ public function capture( * @return MallTransactionStatusResponse * * @throws MallTransactionStatusException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function status(string $buyOrder): MallTransactionStatusResponse { @@ -145,7 +146,7 @@ public function status(string $buyOrder): MallTransactionStatusResponse * @return MallTransactionRefundResponse * * @throws MallRefundTransactionException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException */ public function refund( string $buyOrder, diff --git a/src/Webpay/TransaccionCompleta/MallTransaction.php b/src/Webpay/TransaccionCompleta/MallTransaction.php index 26f95d25..f17b2508 100644 --- a/src/Webpay/TransaccionCompleta/MallTransaction.php +++ b/src/Webpay/TransaccionCompleta/MallTransaction.php @@ -21,6 +21,7 @@ use Transbank\Webpay\TransaccionCompleta\Responses\MallTransactionStatusResponse; use Transbank\Webpay\TransaccionCompleta\Responses\MallTransactionCaptureResponse; use Transbank\Utils\InteractsWithWebpayApi; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; use Transbank\Webpay\Exceptions\WebpayRequestException; class MallTransaction @@ -223,7 +224,7 @@ public function status(string $token) * @param int|float $captureAmount * * @throws MallTransactionCaptureException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return MallTransactionCaptureResponse */ diff --git a/src/Webpay/TransaccionCompleta/Transaction.php b/src/Webpay/TransaccionCompleta/Transaction.php index 5b399e6a..6d27ef59 100644 --- a/src/Webpay/TransaccionCompleta/Transaction.php +++ b/src/Webpay/TransaccionCompleta/Transaction.php @@ -16,6 +16,7 @@ use Transbank\Webpay\TransaccionCompleta\Responses\TransactionCaptureResponse; use Transbank\Utils\InteractsWithWebpayApi; use Transbank\Webpay\Exceptions\WebpayRequestException; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; /** * Class Transaction. @@ -41,7 +42,7 @@ class Transaction * @param string|null $cvv * * @throws TransactionCreateException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionCreateResponse */ @@ -82,7 +83,7 @@ public function create( * @param int $installmentsNumber * * @throws TransactionInstallmentsException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionInstallmentsResponse */ @@ -118,7 +119,7 @@ public function installments( * @param bool|null $gracePeriod * * @throws TransactionCommitException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionCommitResponse */ @@ -156,7 +157,7 @@ public function commit( * @param int|float $amount * * @throws TransactionRefundException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionRefundResponse */ @@ -187,7 +188,7 @@ public function refund(string $token, int|float $amount) * @param string $token * * @throws TransactionStatusException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionStatusResponse */ @@ -217,7 +218,7 @@ public function status($token) * @param int|float $captureAmount * * @throws TransactionCaptureException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionCaptureResponse */ diff --git a/src/Webpay/WebpayPlus/MallTransaction.php b/src/Webpay/WebpayPlus/MallTransaction.php index b9b21ac4..8f20dd8f 100644 --- a/src/Webpay/WebpayPlus/MallTransaction.php +++ b/src/Webpay/WebpayPlus/MallTransaction.php @@ -4,6 +4,8 @@ use Transbank\Utils\InteractsWithWebpayApi; use Transbank\Webpay\Exceptions\WebpayRequestException; +use Transbank\Webpay\Options; +use Transbank\Webpay\WebpayPlus; use Transbank\Webpay\WebpayPlus\Exceptions\MallTransactionCaptureException; use Transbank\Webpay\WebpayPlus\Exceptions\MallTransactionCommitException; use Transbank\Webpay\WebpayPlus\Exceptions\MallTransactionCreateException; @@ -14,6 +16,7 @@ use Transbank\Webpay\WebpayPlus\Responses\MallTransactionCreateResponse; use Transbank\Webpay\WebpayPlus\Responses\MallTransactionRefundResponse; use Transbank\Webpay\WebpayPlus\Responses\MallTransactionStatusResponse; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; class MallTransaction { @@ -33,7 +36,7 @@ class MallTransaction * @param array $details * * @throws MallTransactionCreateException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return MallTransactionCreateResponse */ @@ -69,7 +72,7 @@ public function create(string $buyOrder, string $sessionId, string $returnUrl, a * @param string $token * * @throws MallTransactionCommitException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return MallTransactionCommitResponse */ @@ -105,7 +108,7 @@ public function commit(string $token) * @param int|float $amount * * @throws MallTransactionRefundException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return MallTransactionRefundResponse */ @@ -140,7 +143,7 @@ public function refund(string $token, string $buyOrder, string $childCommerceCod * @param string $token * * @throws MallTransactionStatusException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return MallTransactionStatusResponse */ @@ -173,7 +176,7 @@ public function status(string $token) * @param int|float $captureAmount * * @throws MallTransactionCaptureException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return MallTransactionCaptureResponse */ diff --git a/src/Webpay/WebpayPlus/Transaction.php b/src/Webpay/WebpayPlus/Transaction.php index e6679e78..f4d0db6c 100644 --- a/src/Webpay/WebpayPlus/Transaction.php +++ b/src/Webpay/WebpayPlus/Transaction.php @@ -3,6 +3,7 @@ namespace Transbank\Webpay\WebpayPlus; use Transbank\Utils\InteractsWithWebpayApi; +use Transbank\Utils\Curl\Exceptions\CurlRequestException; use Transbank\Webpay\Exceptions\WebpayRequestException; use Transbank\Webpay\WebpayPlus\Exceptions\TransactionCaptureException; use Transbank\Webpay\WebpayPlus\Exceptions\TransactionCommitException; @@ -42,7 +43,7 @@ class Transaction * @param string $returnUrl * * @throws TransactionCreateException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionCreateResponse */ @@ -78,7 +79,7 @@ public function create( * @param string $token * * @throws TransactionCommitException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionCommitResponse */ @@ -112,7 +113,7 @@ public function commit(string $token): TransactionCommitResponse * @param int|float $amount * * @throws TransactionRefundException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionRefundResponse */ @@ -141,7 +142,7 @@ public function refund(string $token, int|float $amount): TransactionRefundRespo * @param string $token * * @throws TransactionStatusException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionStatusResponse */ @@ -173,7 +174,7 @@ public function status(string $token): TransactionStatusResponse * @param int|float $captureAmount * * @throws TransactionCaptureException - * @throws \Transbank\Utils\Curl\Exceptions\CurlRequestException + * @throws CurlRequestException * * @return TransactionCaptureResponse */ diff --git a/tests/Utils/Curl/RequestTest.php b/tests/Utils/Curl/RequestTest.php index 88eabfea..aefc5bfe 100644 --- a/tests/Utils/Curl/RequestTest.php +++ b/tests/Utils/Curl/RequestTest.php @@ -3,6 +3,7 @@ use PHPUnit\Framework\TestCase; use Transbank\Utils\Curl\Request; use Transbank\Utils\Curl\Uri; +use Transbank\Utils\Curl\Stream; class RequestTest extends TestCase @@ -59,12 +60,10 @@ public function it_can_set_class_properties(): void $this->assertEquals([], $newRequest->getHeader('Accept')); $this->assertNotSame($newRequest, $this->request); - $secondRequest = - new Request('GET', 'https://www.transbank.cl:443/webpay/1.2/transactions/token?param1=123¶m2=222', [ - 'Accept' => 'text/plain', - 'api_key' => 'fakeApiKey' - ], 'this is a new body', '1.2'); - $newRequest = $this->request->withBody($secondRequest->getBody()); + + $resource = fopen('php://temp', 'rw+'); + fwrite($resource, 'testData'); + $newRequest = $this->request->withBody(new Stream($resource)); $this->assertFalse($newRequest->getBody() == $this->request->getBody()); $this->assertNotSame($newRequest, $this->request); } diff --git a/tests/Utils/Curl/ResponseTest.php b/tests/Utils/Curl/ResponseTest.php index 2a0643ac..b5cbbc30 100644 --- a/tests/Utils/Curl/ResponseTest.php +++ b/tests/Utils/Curl/ResponseTest.php @@ -2,6 +2,7 @@ use PHPUnit\Framework\TestCase; use Transbank\Utils\Curl\Response; +use Transbank\Utils\Curl\Stream; class ResponseTest extends TestCase @@ -51,11 +52,10 @@ public function it_can_set_class_properties(): void $this->assertEquals([], $newResponse->getHeader('Accept')); $this->assertNotSame($newResponse, $this->response); - $secondResponse = new Response(200, [ - 'Accept' => 'text/plain', - 'api_key' => 'fakeApiKey' - ], 'a text for body'); - $newResponse = $this->response->withBody($secondResponse->getBody()); + + $resource = fopen('php://temp', 'rw+'); + fwrite($resource, 'testData'); + $newResponse = $this->response->withBody(new Stream($resource)); $this->assertFalse($newResponse->getBody() == $this->response->getBody()); $this->assertNotSame($newResponse, $this->response); } diff --git a/tests/Utils/Curl/StreamTest.php b/tests/Utils/Curl/StreamTest.php index 1b42274e..16b1c756 100644 --- a/tests/Utils/Curl/StreamTest.php +++ b/tests/Utils/Curl/StreamTest.php @@ -2,7 +2,6 @@ use PHPUnit\Framework\TestCase; use Transbank\Utils\Curl\Stream; -use Transbank\Utils\Curl\Response; use Transbank\Utils\Curl\Exceptions\StreamException; class StreamTest extends TestCase @@ -11,9 +10,9 @@ class StreamTest extends TestCase public function setUp(): void { - $response = new Response(200, [], 'this is a test data for stream'); - $stream = $response->getBody(); - $this->stream = $stream; + $resource = fopen('php://temp', 'rw+'); + fwrite($resource, 'this is a test data for stream'); + $this->stream = new Stream($resource); } /** @test */ @@ -27,7 +26,7 @@ public function it_throws_exception_on_constructor_failure() public function it_gets_empty_string_on_failure() { $mockStream = $this->getMockBuilder(Stream::class) - ->disableOriginalConstructor() + ->setConstructorArgs([fopen('php://temp', 'r')]) ->onlyMethods(['isSeekable']) ->getMock(); $mockStream->method('isSeekable')->willThrowException(new Exception('test Exception'));