From 5ad4fc4038878b410994c216de6b4298c709609a Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 8 May 2025 16:17:14 -0400 Subject: [PATCH] Tests de Repositories --- .../Service/MediosPago/AbstractEndPoint.php | 165 ++++++++++++++++++ app/src/Service/MediosPago/EndPoint.php | 42 +++++ .../MediosPago/Toku/CustomerTest.php | 120 +++++++++++++ .../MediosPago/Toku/InvoiceTest.php | 113 ++++++++++++ .../MediosPago/Toku/SubscriptionTest.php | 112 ++++++++++++ 5 files changed, 552 insertions(+) create mode 100644 app/src/Service/MediosPago/AbstractEndPoint.php create mode 100644 app/src/Service/MediosPago/EndPoint.php create mode 100644 app/tests/unit/src/Repository/MediosPago/Toku/CustomerTest.php create mode 100644 app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php create mode 100644 app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php diff --git a/app/src/Service/MediosPago/AbstractEndPoint.php b/app/src/Service/MediosPago/AbstractEndPoint.php new file mode 100644 index 0000000..6049832 --- /dev/null +++ b/app/src/Service/MediosPago/AbstractEndPoint.php @@ -0,0 +1,165 @@ +getStatusCode(); + $reason = $response->getReasonPhrase(); + $contents = $response->getBody()->getContents(); + + if (in_array($status, $invalidStatus)) { + $exception = new HttpException("{$reason}\n{$contents}", $status); + throw new EmptyResponse($request_uri, $exception); + } + if (!in_array($status, $validStatus)) { + $exception = new HttpException("{$reason}\n{$contents}", $status); + throw new EmptyResponse($request_uri, $exception); + } + } + + /** + * @param string $request_uri + * @param array $validStatus + * @param array $invalidStatus + * @return string + * @throws EmptyResponse + */ + protected function sendGet(string $request_uri, array $validStatus, array $invalidStatus): array + { + try { + $response = $this->client->get($request_uri); + } catch (ClientExceptionInterface $exception) { + throw new EmptyResponse($request_uri, $exception); + } + + $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); + + return json_decode($response->getBody()->getContents(), true); + } + + /** + * @param string $request_uri + * @param array $data + * @param array $validStatus + * @param array $invalidStatus + * @return string + * @throws EmptyResponse + */ + protected function sendAdd(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool + { + $params = $this->mapParams($data); + try { + $response = $this->client->post($request_uri, ['json' => $params]); + } catch (ClientExceptionInterface $exception) { + throw new EmptyResponse($request_uri, $exception); + } + + $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); + + $contents = $response->getBody()->getContents(); + $json = json_decode($contents, true); + return $this->save($json); + } + + /** + * @param string $request_uri + * @param array $data + * @param array $validStatus + * @param array $invalidStatus + * @return string + * @throws EmptyResponse + */ + protected function sendEdit(string $request_uri, array $data, array $validStatus, array $invalidStatus): bool + { + $params = $this->mapParams($data); + try { + $response = $this->client->put($request_uri, ['json' => $params]); + } catch (ClientExceptionInterface $exception) { + throw new EmptyResponse($request_uri, $exception); + } + + $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); + + $contents = $response->getBody()->getContents(); + $json = json_decode($contents, true); + return $this->save($json); + } + + /** + * @param string $request_uri + * @param array $validStatus + * @param array $invalidStatus + * @return void + * @throws EmptyResponse + */ + protected function sendDelete(string $request_uri, array $validStatus, array $invalidStatus): void + { + try { + $response = $this->client->delete($request_uri); + } catch (ClientExceptionInterface $exception) { + throw new EmptyResponse($request_uri, $exception); + } + + $this->validateResponse($response, $request_uri, $validStatus, $invalidStatus); + } + protected function doSave(Repository $repository, array $data): bool + { + try { + $repository->fetchById($data['id']); + return true; + } catch (EmptyResult) { + $mappedData = $this->mapSave($data); + $filteredData = $repository->filterData($mappedData); + $model = $repository->create($filteredData); + try { + $repository->save($model); + return true; + } catch (PDOException $exception) { + $this->logger->warning($exception); + return false; + } + } + } + + /** + * @param callable $callable + * @param string $id + * @param string $message + * @return array + * @throws EmptyResponse + */ + protected function doGetById(callable $callable, string $id, string $message): array + { + try { + $model = $callable($id); + return json_decode(json_encode($model), true); + } catch (EmptyResult $exception) { + throw new EmptyResponse($message, $exception); + } + } + + abstract protected function mapParams(array $data): array; + abstract protected function mapSave(array $data): array; + abstract protected function save(array $data): bool; +} diff --git a/app/src/Service/MediosPago/EndPoint.php b/app/src/Service/MediosPago/EndPoint.php new file mode 100644 index 0000000..bf4c2ea --- /dev/null +++ b/app/src/Service/MediosPago/EndPoint.php @@ -0,0 +1,42 @@ +pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']); + + $this->connection = $this->getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor() + ->getMock(); + $this->connection->method('getPDO')->willReturn($this->pdo); + + $faker = Faker\Factory::create(); + $datos = $this->getMockBuilder(Model\Persona\Datos::class) + ->disableOriginalConstructor()->getMock(); + $datos->email = $faker->email(); + $datos->telefono = $faker->randomNumber(9); + + $this->persona = $this->getMockBuilder(Model\Persona::class) + ->disableOriginalConstructor()->getMock(); + $this->persona->rut = $faker->randomNumber(8); + $digit = $faker->numberBetween(0, 11); + $this->persona->digito = $digit === 11 ? 'k' : $digit; + $this->persona->method('datos')->willReturn($datos); + + $this->personaService = $this->getMockBuilder(Service\Persona::class) + ->disableOriginalConstructor()->getMock(); + $this->personaService->method('getById')->willReturn($this->persona); + } + + public function testCreate(): void + { + $repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService); + + $faker = Faker\Factory::create(); + $data = [ + 'rut' => implode('', [$this->persona->rut, $this->persona->digito]), + 'toku_id' => $faker->iban(), + ]; + + $customer = $repository->create($data); + $this->assertEquals($this->persona, $customer->persona); + $this->assertEquals($data['toku_id'], $customer->toku_id); + } + public function testSave(): Model\MediosPago\Toku\Customer + { + $repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService); + + $faker = Faker\Factory::create(); + $data = [ + 'rut' => implode('', [$this->persona->rut, $this->persona->digito]), + 'toku_id' => $faker->iban(), + ]; + $customer = $repository->create($data); + $customer = $repository->save($customer); + $this->assertNotNull($customer->id); + $this->assertEquals($this->persona, $customer->persona); + $this->assertEquals($data['toku_id'], $customer->toku_id); + + return $customer; + } + #[Depends('testSave')] + public function testFetch(Model\MediosPago\Toku\Customer $reference): void + { + $result = [ + 'id' => $reference->id, + 'rut' => implode('', [$this->persona->rut, $this->persona->digito]), + 'toku_id' => $reference->toku_id, + ]; + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService); + + $customer = $repository->fetchById($reference->id); + $this->assertInstanceOf(Model\MediosPago\Toku\Customer::class, $customer); + $this->assertEquals($reference->id, $customer->id); + $this->assertEquals($this->persona, $customer->persona); + $this->assertEquals($reference->toku_id, $customer->toku_id); + } + #[Depends('testSave')] + public function testFetchByRut(Model\MediosPago\Toku\Customer $reference): void + { + $result = [ + 'id' => $reference->id, + 'rut' => implode('', [$this->persona->rut, $this->persona->digito]), + 'toku_id' => $reference->toku_id, + ]; + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Customer($this->connection, $this->personaService); + + $customer = $repository->fetchByRut($this->persona->rut); + $this->assertEquals($reference->id, $customer->id); + $this->assertEquals($this->persona, $customer->persona); + $this->assertEquals($reference->toku_id, $customer->toku_id); + } +} diff --git a/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php b/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php new file mode 100644 index 0000000..5eda917 --- /dev/null +++ b/app/tests/unit/src/Repository/MediosPago/Toku/InvoiceTest.php @@ -0,0 +1,113 @@ +pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']); + + $this->connection = $this->getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor()->getMock(); + $this->connection->method('getPDO')->willReturn($this->pdo); + + $faker = Faker\Factory::create(); + + $this->cuota = $this->getMockBuilder(Model\Venta\Cuota::class) + ->disableOriginalConstructor()->getMock(); + $this->cuota->id = $faker->randomNumber(); + + $this->cuotaRepository = $this->getMockBuilder(Repository\Venta\Cuota::class) + ->disableOriginalConstructor()->getMock(); + $this->cuotaRepository->method('fetchById')->willReturn($this->cuota); + } + + public function testCreate(): void + { + $repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository); + + $faker = Faker\Factory::create(); + $data = [ + 'cuota_id' => $this->cuota->id, + 'toku_id' => $faker->ean13(), + ]; + + $invoice = $repository->create($data); + + $this->assertEquals($this->cuota->id, $invoice->cuota->id); + $this->assertEquals($data['toku_id'], $invoice->toku_id); + } + public function testSave(): Model\MediosPago\Toku\Invoice + { + $repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository); + + $faker = Faker\Factory::create(); + $data = [ + 'cuota_id' => $this->cuota->id, + 'toku_id' => $faker->ean13(), + ]; + + $invoice = $repository->create($data); + $invoice = $repository->save($invoice); + $this->assertNotNull($invoice->id); + $this->assertEquals($this->cuota->id, $invoice->cuota->id); + $this->assertEquals($data['toku_id'], $invoice->toku_id); + + return $invoice; + } + #[Depends('testSave')] + public function testFetch(Model\MediosPago\Toku\Invoice $reference): void + { + $result = [ + 'id' => $reference->id, + 'cuota_id' => $this->cuota->id, + 'toku_id' => $reference->toku_id, + ]; + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository); + + $invoice = $repository->fetchById($reference->id); + $this->assertInstanceOf(Model\MediosPago\Toku\Invoice::class, $invoice); + $this->assertEquals($reference->id, $invoice->id); + $this->assertEquals($this->cuota->id, $invoice->cuota->id); + $this->assertEquals($reference->toku_id, $invoice->toku_id); + } + #[Depends('testSave')] + public function testFetchByCuota(Model\MediosPago\Toku\Invoice $reference): void + { + $result = [ + 'id' => $reference->id, + 'cuota_id' => $this->cuota->id, + 'toku_id' => $reference->toku_id, + ]; + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Invoice($this->connection, $this->cuotaRepository); + + $invoice = $repository->fetchByCuota($this->cuota->id); + $this->assertEquals($reference->id, $invoice->id); + $this->assertEquals($this->cuota->id, $invoice->cuota->id); + $this->assertEquals($reference->toku_id, $invoice->toku_id); + } +} diff --git a/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php b/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php new file mode 100644 index 0000000..d14f7be --- /dev/null +++ b/app/tests/unit/src/Repository/MediosPago/Toku/SubscriptionTest.php @@ -0,0 +1,112 @@ +pdo = new PDO($dsn, $_ENV['DB_USER'], $_ENV['DB_PASSWORD']); + + $this->connection = $this->getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor() + ->getMock(); + $this->connection->method('getPDO')->willReturn($this->pdo); + + $faker = Faker\Factory::create(); + $this->venta = $this->getMockBuilder(Model\Venta::class) + ->disableOriginalConstructor()->getMock(); + $this->venta->id = $faker->randomNumber(); + + $this->ventaRepository = $this->getMockBuilder(Repository\Venta::class) + ->disableOriginalConstructor()->getMock(); + $this->ventaRepository->method('fetchById')->willReturn($this->venta); + } + + public function testCreate(): void + { + $repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository); + + $faker = Faker\Factory::create(); + $data = [ + 'venta_id' => $this->venta->id, + 'toku_id' => $faker->ean13(), + ]; + + $subscription = $repository->create($data); + $this->assertEquals($this->venta->id, $subscription->venta->id); + $this->assertEquals($data['toku_id'], $subscription->toku_id); + } + public function testSave(): Model\MediosPago\Toku\Subscription + { + $repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository); + + $faker = Faker\Factory::create(); + $data = [ + 'venta_id' => $this->venta->id, + 'toku_id' => $faker->ean13(), + ]; + + $subscription = $repository->create($data); + $subscription = $repository->save($subscription); + $this->assertNotNull($subscription->id); + $this->assertEquals($this->venta->id, $subscription->venta->id); + $this->assertEquals($data['toku_id'], $subscription->toku_id); + + return $subscription; + } + #[Depends('testSave')] + public function testFetch(Model\MediosPago\Toku\Subscription $reference): void + { + $result = [ + 'id' => $reference->id, + 'venta_id' => $this->venta->id, + 'toku_id' => $reference->toku_id, + ]; + + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository); + $subscription = $repository->fetchById($reference->id); + $this->assertInstanceOf(Model\MediosPago\Toku\Subscription::class, $subscription); + $this->assertEquals($reference->id, $subscription->id); + $this->assertEquals($this->venta->id, $subscription->venta->id); + $this->assertEquals($reference->toku_id, $subscription->toku_id); + } + #[Depends('testSave')] + public function testFetchByVenta(Model\MediosPago\Toku\Subscription $reference): void + { + $result = [ + 'id' => $reference->id, + 'venta_id' => $this->venta->id, + 'toku_id' => $reference->toku_id, + ]; + + $resultSet = $this->getMockBuilder(\PDOStatement::class) + ->disableOriginalConstructor()->getMock(); + $resultSet->method('fetch')->willReturn($result); + $this->connection->method('execute')->willReturn($resultSet); + + $repository = new Repository\MediosPago\Toku\Subscription($this->connection, $this->ventaRepository); + $subscription = $repository->fetchByVenta($this->venta->id); + $this->assertEquals($reference->id, $subscription->id); + $this->assertEquals($this->venta->id, $subscription->venta->id); + $this->assertEquals($reference->toku_id, $subscription->toku_id); + } +}