Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Phaseolies/Database/Entity/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Phaseolies\Support\Facades\URL;
use Phaseolies\Support\Contracts\Encryptable;
use Phaseolies\Support\Collection;

use Phaseolies\Database\Entity\Model;

class Builder
Expand Down Expand Up @@ -2964,6 +2963,29 @@ public function search(array $attributes, ?string $searchTerm = null): self
return $this->where($searchQuery);
}

/**
* Get records and fix/normalize data
*
* @param callable $fixer
* @param bool $saveChanges
* @return Collection
*/
public function repair(callable $fixer, bool $saveChanges = false): Collection
{
$results = $this->get();

foreach ($results as $item) {
$original = $item->getAttributes();
$fixer($item);

if ($saveChanges && $item->getAttributes() !== $original) {
$item->save();
}
}

return $results;
}

/**
* Convert camelCase to snake_case for column names
*
Expand Down
56 changes: 56 additions & 0 deletions tests/Model/Query/EntityModelQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Phaseolies\DI\Container;
use PHPUnit\Framework\TestCase;
use PDO;
use Tests\Support\Model\MockProduct;

class EntityModelQueryTest extends TestCase
{
Expand Down Expand Up @@ -97,7 +98,20 @@ private function createTestTables(): void
)
");

$this->pdo->exec("
CREATE TABLE product (
price INTEGER
)
");

// Insert test data
$this->pdo->exec("
INSERT INTO product (price) VALUES
(875),
(435),
(999)
");

$this->pdo->exec("
INSERT INTO users (name, email, age, status, created_at) VALUES
('John Doe', 'john@example.com', 30, 'active', '2024-01-01 10:00:00'),
Expand Down Expand Up @@ -2605,4 +2619,46 @@ public function testPostTagRelateMethod()
$this->assertEquals([2], array_keys($changes['detached']));
$this->assertEquals([], $changes['updated']);
}

public function testRepairMethodFetchingRecords()
{
$products = MockProduct::repair(function ($product) {
$product->price = preg_replace('/[^0-9]/', '', $product->price);
}, false);

$this->assertEquals(3, count($products));
}

public function testRepairPreviewsPriceCleanupWithoutPersistingChanges()
{
$products = MockProduct::repair(function ($product) {
$product->price = preg_replace('/[^0-5]/', '', $product->price);
}, false);

foreach ($products[0] as $product) {
$this->assertTrue(str_contains($product->price, 5));
}

// Cause product 3 id price is = 999
foreach ($products[2] as $product) {
$this->assertNull($product->price);
}
}

public function testRepairMethodSaveChanges()
{
$products = MockProduct::query()
->repair(function ($product) {
$product->price = preg_replace('/[^0-9]/', '', $product->price);
}, true);

foreach ($products[0] as $product) {
$this->assertTrue(str_contains($product->price, 5));
}

// Cause product 3 id price is = 999
foreach ($products[2] as $product) {
$this->assertNull($product->price);
}
}
}
18 changes: 18 additions & 0 deletions tests/Support/Model/MockProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Support\Model;

use Phaseolies\Database\Entity\Model;

class MockProduct extends Model
{
protected $table = 'product';

protected $primaryKey = 'id';

protected $connection = 'default';

protected $timeStamps = false;

protected $creatable = ['price'];
}
Loading