From 9853333215b80b35933314efec55e54f14c7d127 Mon Sep 17 00:00:00 2001 From: Eric COURTIAL <57905107+ecourtial@users.noreply.github.com> Date: Tue, 1 Mar 2022 15:19:53 +0100 Subject: [PATCH] fix: add strict comparison between current and row in the db loaders (#108) --- changelog.MD | 5 ++++ src/Loaders/InsertUpdate.php | 2 +- tests/Loaders/InsertUpdateTest.php | 46 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/changelog.MD b/changelog.MD index 1b475f9e..3a82787d 100644 --- a/changelog.MD +++ b/changelog.MD @@ -1,5 +1,10 @@ # Changelog +## 2.1.1 + +**Bugfixes** +* Added a strict comparison in the DB Loaders. Without the fix, since with a loose comparison 'false' == true, some updates were skipped. + ## 2.1 **New features** diff --git a/src/Loaders/InsertUpdate.php b/src/Loaders/InsertUpdate.php index 796082b8..68c79736 100644 --- a/src/Loaders/InsertUpdate.php +++ b/src/Loaders/InsertUpdate.php @@ -185,7 +185,7 @@ protected function update(array $row, array $current): void $this->prepareUpdate($row); } - if ($row == array_intersect_key($current, $row)) { + if ($row === array_intersect_key($current, $row)) { return; } diff --git a/tests/Loaders/InsertUpdateTest.php b/tests/Loaders/InsertUpdateTest.php index b32b28c7..95f64959 100644 --- a/tests/Loaders/InsertUpdateTest.php +++ b/tests/Loaders/InsertUpdateTest.php @@ -334,6 +334,52 @@ public function updateDataUsingTimestamps(): void $this->execute($this->loader, [$this->row]); } + /** + * @test + * + * This test has been added to check strict comparison. + * Before the fix, we had a loose comparison making skip updates because in PHP 'false' == true. + */ + public function updateWithTrickyTypesComparison(): void + { + $row = $this->createMock('Wizaplace\Etl\Row'); + $row->expects(static::any())->method('toArray') + ->willReturn( + [ + 'name' => 'Jane', + 'birth_date' => '1962-06-18', + 'active' => true, + ] + ); + + $this->select->expects(static::once())->method('fetch')->willReturn( + [ + 'name' => 'Jane', + 'birth_date' => '1962-06-18', + 'active' => 'false', + ] + ); + + // Updates + + $this->statement->expects(static::once())->method('update')->with( + 'table', + [ + 'name', + 'birth_date', + 'active', + ] + ); + + $this->update->expects(static::once())->method('execute')->with([ + 'name' => 'Jane', + 'birth_date' => '1962-06-18', + 'active' => true, + ]); + + $this->execute($this->loader, [$row]); + } + /** @test */ public function insertDataIntoDatabaseWithoutTransactions(): void {