From a5c1d608198f468a955ce04d8a1779383e9e5efa Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 8 May 2025 16:17:26 -0400 Subject: [PATCH] Servicios para Toku --- app/src/Service/MediosPago/Toku/Customer.php | 92 ++++++++ app/src/Service/MediosPago/Toku/Invoice.php | 115 ++++++++++ .../Service/MediosPago/Toku/Subscription.php | 100 +++++++++ .../Service/MediosPago/Toku/CustomerTest.php | 182 ++++++++++++++++ .../Service/MediosPago/Toku/InvoiceTest.php | 200 ++++++++++++++++++ .../MediosPago/Toku/SubscriptionTest.php | 183 ++++++++++++++++ 6 files changed, 872 insertions(+) create mode 100644 app/src/Service/MediosPago/Toku/Customer.php create mode 100644 app/src/Service/MediosPago/Toku/Invoice.php create mode 100644 app/src/Service/MediosPago/Toku/Subscription.php create mode 100644 app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php create mode 100644 app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php create mode 100644 app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php diff --git a/app/src/Service/MediosPago/Toku/Customer.php b/app/src/Service/MediosPago/Toku/Customer.php new file mode 100644 index 0000000..eb40a7b --- /dev/null +++ b/app/src/Service/MediosPago/Toku/Customer.php @@ -0,0 +1,92 @@ +doGetById([$this->customerRepository, 'fetchByRut'], $id, "No existe toku_id para Persona {$id}"); + } + public function get(string $id): array + { + $request_uri = "/customers/{$id}"; + return $this->sendGet($request_uri, [200], [404, 422]); + } + public function add(array $data): bool + { + $request_uri = "/customers"; + return $this->sendAdd($request_uri, $data, [200, 201], [400, 422]); + } + public function edit(string $id, array $data): bool + { + $request_uri = "customers/{$id}"; + return $this->sendEdit($request_uri, $data, [200], [400, 404, 422]); + } + public function delete(string $id): void + { + $request_uri = "/customer/{$id}"; + $this->sendDelete($request_uri, [204], [404, 409]); + } + + protected function save(array $data): bool + { + return $this->doSave($this->customerRepository, $data); + } + protected function mapParams(array $data): array + { + $paramsMap = [ + 'government_id' => 'rut', + 'external_id' => 'rut', + 'email' => 'email', + 'name' => 'nombreCompleto', + 'phone_number' => 'telefono', + 'pac_mandate_id' => null, + 'default_agent' => 'contacto@incoviba.cl', + 'send_mail' => false, + 'agent_phone_number' => null, + 'rfc' => null, + 'tax_zip_code' => null, + 'fiscal_regime' => null, + 'default_receipt_type' => null, + 'secondary_emails' => null, + 'silenced_until' => null, + 'metadata' => null + ]; + + $params = []; + foreach ($paramsMap as $key => $ref) { + if ($ref === null) { + continue; + } + if (array_key_exists($ref, $data)) { + $params[$key] = $data[$ref]; + } + } + + return $params; + } + protected function mapSave(array $data): array + { + $responseMap = [ + 'government_id' => 'rut', + 'id' => 'toku_id' + ]; + $mappedData = []; + foreach ($responseMap as $responseKey => $dataKey) { + if (isset($data[$responseKey])) { + $mappedData[$dataKey] = $data[$responseKey]; + } + } + return $mappedData; + } +} diff --git a/app/src/Service/MediosPago/Toku/Invoice.php b/app/src/Service/MediosPago/Toku/Invoice.php new file mode 100644 index 0000000..0d8237d --- /dev/null +++ b/app/src/Service/MediosPago/Toku/Invoice.php @@ -0,0 +1,115 @@ +doGetById([$this->invoiceRepository, 'fetchByCuota'], $id, "No existe toku_id para Cuota {$id}"); + } + public function get(string $id): array + { + $request_uri = "/invoices/{$id}"; + return $this->sendGet($request_uri, [200], [404]); + } + public function add(array $data): bool + { + $request_uri = "/invoices"; + return $this->sendAdd($request_uri, $data, [200, 201], [400, 409, 422]); + } + public function edit(string $id, array $data): bool + { + $request_uri = "/invoices/{$id}"; + return $this->sendEdit($request_uri, $data, [200], [400, 404, 409, 422]); + } + public function delete(string $id): void + { + $request_uri = "/invoices/{$id}"; + $this->sendDelete($request_uri, [204], [404, 409]); + } + + protected function save(array $data): bool + { + return $this->doSave($this->invoiceRepository, $data); + } + protected function mapParams(array $data): array + { + $paramsMap = [ + 'customer' => 'customer', + 'product_id' => 'product_id', + 'due_date' => 'fecha', + 'subscription' => 'subscription', + 'amount' => 'valor', + 'is_paid' => null, + 'is_void' => null, + 'link_payment' => null, + 'metadata' => 'datosCuota', + 'receipt_type' => null, + 'id_receipt' => null, + 'disable_automatic_payment' => null, + 'currency_code' => 'CLF', + 'invoice_external_id' => 'cuota_id' + ]; + + $params = []; + foreach ($paramsMap as $key => $ref) { + if ($ref === null) { + continue; + } + if ($ref === 'fecha') { + $params[$key] = $data['cuota']->pago->fecha->format('Y-m-d'); + continue; + } + if ($ref === 'valor') { + $params[$key] = $data['cuota']->pago->valor; + continue; + } + if ($ref === 'datosCuota') { + $params[$key] = $this->datosCuota($data['cuota']); + continue; + } + if ($ref === 'cuota_id') { + $params[$key] = $data['cuota']->id; + continue; + } + if (array_key_exists($ref, $data)) { + $params[$key] = $data[$ref]; + } + } + + return $params; + } + protected function mapSave(array $data): array + { + $responseMap = [ + 'invoice_external_id' => 'cuota_id', + 'id' => 'toku_id' + ]; + $mappedData = []; + foreach ($responseMap as $responseKey => $dataKey) { + if (isset($data[$responseKey])) { + $mappedData[$dataKey] = $data[$responseKey]; + } + } + return $mappedData; + } + + protected function datosCuota(Cuota $cuota): string + { + return json_encode([ + 'Numero' => $cuota->numero, + 'valor' => $cuota->pago->valor, + 'UF' => $cuota->pago->valor() + ]); + } +} diff --git a/app/src/Service/MediosPago/Toku/Subscription.php b/app/src/Service/MediosPago/Toku/Subscription.php new file mode 100644 index 0000000..b365a55 --- /dev/null +++ b/app/src/Service/MediosPago/Toku/Subscription.php @@ -0,0 +1,100 @@ +doGetById([$this->subscriptionRepsitory, 'fetchByVenta'], $id, "No existe toku_id para Venta {$id}"); + } + public function get(string $id): array + { + $request_uri = "/subscriptions/{$id}"; + return $this->sendGet($request_uri, [200], [401, 404, 422]); + } + public function add(array $data): bool + { + $request_uri = '/subscriptions'; + return $this->sendAdd($request_uri, $data, [200, 201], [401, 404, 409, 422]); + } + public function edit(string $id, array $data): bool + { + $request_uri = "/subscriptions/{$id}"; + return $this->sendEdit($request_uri, $data, [200], [401, 404, 409, 422]); + } + public function delete(string $id): void + { + $request_uri = "/subscriptions/{$id}"; + $this->sendDelete($request_uri, [204], [404, 409]); + } + + protected function save(array $data): bool + { + return $this->doSave($this->subscriptionRepsitory, $data); + } + protected function mapParams(array $data): array + { + $paramsMap = [ + 'customer' => 'customer', + 'product_id' => 'product_id', + 'pac_mandate_id' => null, + 'is_recurring' => null, + 'due_day' => null, + 'amount' => 'pieValor', + 'receipt_product_code' => null, + 'metadata' => 'datosVenta' + ]; + + $params = []; + foreach ($paramsMap as $key => $ref) { + if ($ref === null) { + continue; + } + if ($ref === 'pieValor') { + $params[$key] = $data['venta']->formaPago()?->pie?->valor ?? 0; + continue; + } + if ($ref === 'datosVenta') { + $params[$key] = $this->datosVenta($data['venta']); + continue; + } + if (array_key_exists($ref, $data)) { + $params[$key] = $data[$ref]; + } + } + + return $params; + } + protected function mapSave(array $data): array + { + $responseMap = [ + 'product_id' => 'venta_id', + 'id' => 'toku_id' + ]; + $mappedData = []; + foreach ($responseMap as $responseKey => $dataKey) { + if (isset($data[$responseKey])) { + $mappedData[$dataKey] = $data[$responseKey]; + } + } + return $mappedData; + } + protected function datosVenta(Venta $venta): string + { + $datos = [ + 'proyecto' => $venta->proyecto()->descripcion, + 'propiedad' => $venta->propiedad()->summary() + ]; + return json_encode($datos); + } +} diff --git a/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php b/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php new file mode 100644 index 0000000..8b31ff8 --- /dev/null +++ b/app/tests/unit/src/Service/MediosPago/Toku/CustomerTest.php @@ -0,0 +1,182 @@ +getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor()->getMock(); + $connection->method('getPDO')->willReturn($pdo); + + $faker = Faker\Factory::create(); + + $datos = $this->getMockBuilder(Model\Persona\Datos::class) + ->disableOriginalConstructor()->getMock(); + $datos->email = $faker->email(); + $datos->telefono = $faker->randomNumber(8); + + $persona = $this->getMockBuilder(Model\Persona::class) + ->disableOriginalConstructor()->getMock(); + $persona->rut = $faker->randomNumber(); + $persona->digito = $faker->randomNumber(); + $persona->nombres = $faker->firstName(); + $persona->apellidoPaterno = $faker->lastName(); + $persona->apellidoMaterno = $faker->lastName(); + $persona->method('datos')->willReturn($datos); + + $this->customer = $this->getMockBuilder(Model\MediosPago\Toku\Customer::class) + ->disableOriginalConstructor()->getMock(); + $this->customer->id = $faker->randomNumber(); + $this->customer->persona = $persona; + $this->customer->toku_id = $faker->ean13(); + $this->customer->method('jsonSerialize')->willReturn([ + 'id' => $this->customer->id, + 'rut' => implode('', [$persona->rut, $persona->digito]), + 'toku_id' => $this->customer->toku_id + ]); + + $this->customerRepository = $this->getMockBuilder(Repository\MediosPago\Toku\Customer::class) + ->disableOriginalConstructor()->getMock(); + $this->customerRepository->method('fetchById')->willReturn($this->customer); + $this->customerRepository->method('fetchByRut')->willReturn($this->customer); + + $this->getData = [ + 'id' => $this->customer->id, + 'external_id' => implode('', [$persona->rut, $persona->digito]), + 'government_id' => implode('', [$persona->rut, $persona->digito]), + 'email' => $persona->datos()->email, + 'name' => implode(' ', [$persona->nombres, $persona->apellidoPaterno, $persona->apellidoMaterno]), + 'phone_number' => $persona->datos()->telefono, + 'silenced_until' => null, + 'default_agent' => null, + 'agent_phone_number' => null, + 'pac_mandate_id' => null, + 'send_mail' => false, + 'default_receipt_type' => 'bill', + 'rfc' => null, + 'tax_zip_code' => null, + 'fiscal_regime' => null, + 'secondary_emails' => [], + 'metadata' => [] + ]; + + $getBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $getBody->method('getContents')->willReturn(json_encode($this->getData)); + + $getResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $getResponse->method('getStatusCode')->willReturn(200); + $getResponse->method('getBody')->willReturn($getBody); + + $this->postData = [ + 'id' => $this->customer->id, + 'external_id' => implode('', [$persona->rut, $persona->digito]), + 'government_id' => implode('', [$persona->rut, $persona->digito]), + 'email' => $persona->datos()->email, + 'name' => implode(' ', [$persona->nombres, $persona->apellidoPaterno, $persona->apellidoMaterno]), + 'phone_number' => $persona->datos()->telefono, + 'silenced_until' => null, + 'default_agent' => null, + 'agent_phone_number' => null, + 'pac_mandate_id' => null, + 'send_mail' => false, + 'default_receipt_type' => null, + 'rfc' => null, + 'tax_zip_code' => null, + 'fiscal_regime' => null, + 'secondary_emails' => [], + 'metadata' => [] + ]; + + $postBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $postBody->method('getContents')->willReturn(json_encode($this->postData)); + + $postResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $postResponse->method('getStatusCode')->willReturn(200); + $postResponse->method('getBody')->willReturn($postBody); + + $deleteBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $deleteBody->method('getContents')->willReturn('Customer Deleted'); + + $deleteResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $deleteResponse->method('getStatusCode')->willReturn(204); + $deleteResponse->method('getBody')->willReturn($deleteBody); + + $this->client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor()->getMock(); + $this->client->method('get')->willReturn($getResponse); + $this->client->method('post')->willReturn($postResponse); + $this->client->method('put')->willReturn($postResponse); + $this->client->method('delete')->willReturn($deleteResponse); + } + + public function testGetById(): void + { + $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); + + $this->assertEquals(json_decode(json_encode($this->customer), true), $service->getById($this->customer->id)); + } + public function testGet(): void + { + $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); + + $this->assertArrayIsEqualToArrayIgnoringListOfKeys($this->getData, $service->get($this->customer->toku_id), []); + } + public function testAdd(): void + { + $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); + + $sendData = [ + 'rut' => $this->postData['government_id'], + 'nombreCompleto' => $this->postData['name'], + 'email' => $this->postData['email'], + 'telefono' => $this->postData['phone_number'] + ]; + $this->assertTrue($service->add($sendData)); + } + public function testEdit(): void + { + $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); + + $sendData = [ + 'rut' => $this->postData['government_id'], + 'nombreCompleto' => $this->postData['name'], + 'email' => $this->postData['email'], + 'telefono' => $this->postData['phone_number'] + ]; + $this->assertTrue($service->edit($this->customer->toku_id, $sendData)); + } + public function testDelete(): void + { + $service = new Service\MediosPago\Toku\Customer($this->client, $this->customerRepository); + + $this->expectNotToPerformAssertions(); + $service->delete($this->customer->toku_id); + } +} diff --git a/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php b/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php new file mode 100644 index 0000000..60bed83 --- /dev/null +++ b/app/tests/unit/src/Service/MediosPago/Toku/InvoiceTest.php @@ -0,0 +1,200 @@ +getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor()->getMock(); + $connection->method('getPDO')->willReturn($pdo); + + $faker = Faker\Factory::create(); + + $customer = $this->getMockBuilder(Model\MediosPago\Toku\Customer::class) + ->disableOriginalConstructor()->getMock(); + $customer->id = $faker->randomNumber(); + $customer->toku_id = $faker->ean13(); + $venta = $this->getMockBuilder(Model\Venta::class) + ->disableOriginalConstructor()->getMock(); + $venta->id = $faker->randomNumber(); + $subscription = $this->getMockBuilder(Model\MediosPago\Toku\Subscription::class) + ->disableOriginalConstructor()->getMock(); + $subscription->id = $faker->randomNumber(); + $subscription->venta = $venta; + $subscription->toku_id = $faker->ean13(); + + $pago = $this->getMockBuilder(Model\Venta\Pago::class) + ->disableOriginalConstructor()->getMock(); + $pago->fecha = $faker->dateTime(); + $pago->valor = $faker->randomNumber(6, true); + $pago->method('valor')->willReturn($faker->randomFloat(3, true)); + $this->cuota = $this->getMockBuilder(Model\Venta\Cuota::class) + ->disableOriginalConstructor()->getMock(); + $this->cuota->id = $faker->randomNumber(); + $this->cuota->numero = $faker->randomNumber(2); + $this->cuota->pago = $pago; + + $this->invoice = $this->getMockBuilder(Model\MediosPago\Toku\Invoice::class) + ->disableOriginalConstructor()->getMock(); + $this->invoice->id = $faker->randomNumber(); + $this->invoice->cuota = $this->cuota; + $this->invoice->toku_id = $faker->ean13(); + $this->invoice->method('jsonSerialize')->willReturn([ + 'id' => $this->invoice->id, + 'cuota_id' => $this->cuota->id, + 'toku_id' => $this->invoice->toku_id + ]); + + $this->invoiceRepository = $this->getMockBuilder(Repository\MediosPago\Toku\Invoice::class) + ->disableOriginalConstructor()->getMock(); + $this->invoiceRepository->method('fetchById')->willReturn($this->invoice); + $this->invoiceRepository->method('fetchByCuota')->willReturn($this->invoice); + + $this->getData = [ + 'customer' => $customer->toku_id, + 'product_id' => $subscription->venta->id, + 'subscription' => $subscription->toku_id, + 'is_paid' => false, + 'due_date' => $this->cuota->pago->fecha->format('Y-m-d'), + 'is_void' => false, + 'amount' => $this->cuota->pago->valor, + 'link_payment' => '', + 'metadata' => [ + 'numero' => $this->cuota->numero, + 'valor' => $this->cuota->pago->valor, + 'UF' => $this->cuota->pago->valor() + ], + 'receipt_type' => null, + 'id_receipt' => null, + 'source' => null, + 'disable_automatic_payment' => false, + 'currency_code' => 'CLP', + 'invoice_external_id' => $this->cuota->id, + 'id' => $this->invoice->toku_id, + ]; + + $getBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $getBody->method('getContents')->willReturn(json_encode($this->getData)); + + $getResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $getResponse->method('getBody')->willReturn($getBody); + $getResponse->method('getStatusCode')->willReturn(200); + + $this->postData = [ + 'customer' => $customer->toku_id, + 'product_id' => $subscription->venta->id, + 'subscription' => $subscription->toku_id, + 'is_paid' => false, + 'due_date' => $this->cuota->pago->fecha->format('Y-m-d'), + 'is_void' => false, + 'amount' => $this->cuota->pago->valor, + 'link_payment' => '', + 'metadata' => [ + 'numero' => $this->cuota->numero, + 'valor' => $this->cuota->pago->valor, + 'UF' => $this->cuota->pago->valor() + ], + 'receipt_type' => null, + 'id_receipt' => null, + 'source' => null, + 'disable_automatic_payment' => false, + 'currency_code' => 'CLF', + 'invoice_external_id' => $this->cuota->id, + 'id' => $this->invoice->toku_id, + ]; + + $postBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $postBody->method('getContents')->willReturn(json_encode($this->postData)); + + $postResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $postResponse->method('getBody')->willReturn($postBody); + $postResponse->method('getStatusCode')->willReturn(200); + + $deleteBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $deleteBody->method('getContents')->willReturn("Invoice deleted"); + + $deleteResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $deleteResponse->method('getBody')->willReturn($deleteBody); + $deleteResponse->method('getStatusCode')->willReturn(204); + + $this->client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor()->getMock(); + $this->client->method('get')->willReturn($getResponse); + $this->client->method('post')->willReturn($postResponse); + $this->client->method('put')->willReturn($postResponse); + $this->client->method('delete')->willReturn($deleteResponse); + } + + public function testGetById(): void + { + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + $this->assertEquals(json_decode(json_encode($this->invoice), true), $service->getById($this->invoice->toku_id)); + } + public function testGet(): void + { + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + + $this->assertEquals($this->getData, $service->get($this->invoice->toku_id)); + } + public function testAdd(): void + { + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + + $sendData = [ + 'customer' => $this->postData['customer'], + 'product_id' => $this->postData['product_id'], + 'subscription' => $this->postData['subscription'], + 'cuota' => $this->cuota + ]; + + $this->assertTrue($service->add($sendData)); + } + public function testEdit(): void + { + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + + $sendData = [ + 'customer' => $this->postData['customer'], + 'product_id' => $this->postData['product_id'], + 'subscription' => $this->postData['subscription'], + 'cuota' => $this->cuota + ]; + + $this->assertTrue($service->edit($this->invoice->toku_id, $sendData)); + } + public function testDelete(): void + { + $service = new Service\MediosPago\Toku\Invoice($this->client, $this->invoiceRepository); + + $this->expectNotToPerformAssertions(); + $service->delete($this->invoice->toku_id); + } +} diff --git a/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php b/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php new file mode 100644 index 0000000..f8974f7 --- /dev/null +++ b/app/tests/unit/src/Service/MediosPago/Toku/SubscriptionTest.php @@ -0,0 +1,183 @@ +getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor()->getMock(); + $connection->method('getPDO')->willReturn($pdo); + + $faker = Faker\Factory::create(); + + $customer = $this->getMockBuilder(Model\MediosPago\Toku\Customer::class) + ->disableOriginalConstructor()->getMock(); + $customer->id = $faker->randomNumber(); + + $pie = $this->getMockBuilder(Model\Venta\Pie::class) + ->disableOriginalConstructor()->getMock(); + $pie->valor = $faker->randomFloat(); + $formaPago = $this->getMockBuilder(Model\Venta\FormaPago::class) + ->disableOriginalConstructor()->getMock(); + $formaPago->pie = $pie; + $proyecto = $this->getMockBuilder(Model\Proyecto::class) + ->disableOriginalConstructor()->getMock(); + $proyecto->descripcion = $faker->sentence(); + $summary = implode(' - ', [ + $faker->randomNumber(4), + 'E' . $faker->randomNumber(3), + 'B' . $faker->randomNumber(3), + ]); + $propiedad = $this->getMockBuilder(Model\Venta\Propiedad::class) + ->disableOriginalConstructor()->getMock(); + $propiedad->method('summary')->willReturn($summary); + $this->venta = $this->getMockBuilder(Model\Venta::class) + ->disableOriginalConstructor()->getMock(); + $this->venta->id = $faker->randomNumber(); + $this->venta->method('formaPago')->willReturn($formaPago); + $this->venta->method('proyecto')->willReturn($proyecto); + $this->venta->method('propiedad')->willReturn($propiedad); + + $this->subscription = $this->getMockBuilder(Model\MediosPago\Toku\Subscription::class) + ->disableOriginalConstructor()->getMock(); + $this->subscription->id = $faker->randomNumber(); + $this->subscription->venta = $this->venta; + $this->subscription->toku_id = $faker->ean13(); + $this->subscription->method('jsonSerialize')->willReturn([ + 'id' => $this->subscription->id, + 'venta_id' => $this->venta->id, + 'toku_id' => $this->subscription->toku_id, + ]); + + $this->subscriptionRepository = $this->getMockBuilder(Repository\MediosPago\Toku\Subscription::class) + ->disableOriginalConstructor()->getMock(); + $this->subscriptionRepository->method('fetchById')->willReturn($this->subscription); + $this->subscriptionRepository->method('fetchByVenta')->willReturn($this->subscription); + + $this->getData = [ + 'id' => $this->subscription->toku_id, + 'customer' => $customer->id, + 'product_id' => $this->venta->id, + 'pac_mandate_id' => null, + 'is_recurring' => false, + 'amount' => $pie->valor, + 'due_day' => null, + 'receipt_product_code' => null, + 'metadata' => [ + 'proyecto' => $proyecto->descripcion, + 'propiedad' => $propiedad->summary(), + ] + ]; + + $getBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $getBody->method('getContents')->willReturn(json_encode($this->getData)); + + $getResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $getResponse->method('getStatusCode')->willReturn(200); + $getResponse->method('getBody')->willReturn($getBody); + + $this->postData = [ + 'id' => $this->subscription->toku_id, + 'customer' => $customer->id, + 'product_id' => $this->venta->id, + 'pac_mandate_id' => null, + 'is_recurring' => false, + 'amount' => $pie->valor, + 'due_day' => null, + 'metadata' => [ + 'proyecto' => $proyecto->descripcion, + 'propiedad' => $propiedad->summary(), + ] + ]; + + $postBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $postBody->method('getContents')->willReturn(json_encode($this->postData)); + + $postResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $postResponse->method('getStatusCode')->willReturn(200); + $postResponse->method('getBody')->willReturn($postBody); + + $deleteBody = $this->getMockBuilder(StreamInterface::class) + ->disableOriginalConstructor()->getMock(); + $deleteBody->method('getContents')->willReturn('Subscription deleted'); + $deleteResponse = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor()->getMock(); + $deleteResponse->method('getStatusCode')->willReturn(204); + $deleteResponse->method('getBody')->willReturn($deleteBody); + + $this->client = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor()->getMock(); + $this->client->method('get')->willReturn($getResponse); + $this->client->method('post')->willReturn($postResponse); + $this->client->method('put')->willReturn($postResponse); + $this->client->method('delete')->willReturn($deleteResponse); + } + + public function testGetById(): void + { + $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); + + $this->assertEquals(json_decode(json_encode($this->subscription), true), $service->getById($this->subscription->toku_id)); + } + public function testGet(): void + { + $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); + + $this->assertArrayIsEqualToArrayIgnoringListOfKeys($this->getData, $service->get($this->subscription->toku_id), []); + } + public function testAdd(): void + { + $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); + + $sendData = [ + 'customer' => $this->postData['customer'], + 'product_id' => $this->postData['product_id'], + 'venta' => $this->venta + ]; + $this->assertTrue($service->add($sendData)); + } + public function testEdit(): void + { + $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); + + $sendData = [ + 'customer' => $this->postData['customer'], + 'product_id' => $this->postData['product_id'], + 'venta' => $this->venta + ]; + $this->assertTrue($service->edit($this->subscription->toku_id, $sendData)); + } + public function testDelete(): void + { + $service = new Service\MediosPago\Toku\Subscription($this->client, $this->subscriptionRepository); + + $this->expectNotToPerformAssertions(); + $service->delete($this->subscription->toku_id); + } +}