From 186cd0f5b8caee5a36c53931a45fa83d73f97d89 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 5 May 2025 19:01:15 -0400 Subject: [PATCH] Reparaciones con Prueba --- app/src/Service/Money/SII.php | 16 +++-- app/tests/unit/src/Service/Money/SIITest.php | 62 ++++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 app/tests/unit/src/Service/Money/SIITest.php diff --git a/app/src/Service/Money/SII.php b/app/src/Service/Money/SII.php index b76d0f7..721b61a 100644 --- a/app/src/Service/Money/SII.php +++ b/app/src/Service/Money/SII.php @@ -61,7 +61,11 @@ class SII implements Define\Money\Provider throw new EmptyResponse($request_uri); } $body = $response->getBody(); - $domHandler = HTMLDocument::createFromString($body->getContents()); + $contents = $body->getContents(); + if (trim($contents) === '') { + throw new EmptyResponse($request_uri); + } + $domHandler = HTMLDocument::createFromString($contents); $table = $domHandler->querySelector('div#mes_all'); $tbody = $table->querySelector('tbody'); $trs = $tbody->querySelectorAll('tr'); @@ -70,17 +74,17 @@ class SII implements Define\Money\Provider * [th 1, td #, #, #, #, #, #, #, #, #, #, #, #] */ $year = []; + $trim = mb_chr(160); foreach ($trs as $i => $tr) { $tds = $tr->querySelectorAll('td'); foreach ($tds as $j => $td) { - $value = $td->nodeValue; - if (trim($value) === '') { + $value = $td->textContent; + if (trim($value, "{$trim} ") === '') { continue; } - $d = str_pad($j + 1, 2, '0', STR_PAD_LEFT); - $m = str_pad($i, 2, '0', STR_PAD_LEFT); + $m = str_pad($j + 1, 2, '0', STR_PAD_LEFT); + $d = str_pad($i + 1, 2, '0', STR_PAD_LEFT); $date = "{$dateTime->format('Y')}-{$m}-{$d}"; - trigger_error($date); $value = $this->valorService->clean($value); $year[$date] = $value; } diff --git a/app/tests/unit/src/Service/Money/SIITest.php b/app/tests/unit/src/Service/Money/SIITest.php new file mode 100644 index 0000000..4acb57d --- /dev/null +++ b/app/tests/unit/src/Service/Money/SIITest.php @@ -0,0 +1,62 @@ +client = new Client(['base_uri' => 'https://www.sii.cl/valores_y_fechas/']); + + $this->ufService = $this->getMockBuilder(Service\UF::class) + ->disableOriginalConstructor()->getMock(); + $this->ufService->method('get')->willReturn(1.0); + + $this->valorService = new Service\Valor($this->ufService); + + $this->pdo = $this->getMockBuilder(PDO::class) + ->disableOriginalConstructor()->getMock(); + $this->pdo->method('beginTransaction')->willReturn(true); + $this->pdo->method('commit')->willReturn(true); + #$this->pdo->method('rollBack')->willReturn(null); + + $this->statement = $this->getMockBuilder(PDOStatement::class)->getMock(); + $this->statement->method('fetchAll')->willReturn([]); + + $this->connection = $this->getMockBuilder(Define\Connection::class) + ->disableOriginalConstructor()->getMock(); + $this->connection->method('getPDO')->willReturn($this->pdo); + $this->connection->method('query')->willReturn($this->statement); + $this->connection->method('execute')->willReturn($this->statement); + + $this->ufRepository = $this->getMockBuilder(Repository\UF::class) + ->disableOriginalConstructor()->getMock(); + $this->ufRepository->method('getConnection')->willReturn($this->connection); + $this->ufRepository->method('getTable')->willReturn('uf'); + } + + public function testGet(): void + { + $provider = new Service\Money\SII($this->client, $this->valorService, $this->ufRepository); + + $date = new \DateTimeImmutable('2025-05-05'); + $expected = 39107.9; + + $this->assertEquals($expected, $provider->get(Service\Money::UF, $date)); + } +}