Skip to content

Commit

Permalink
Fix: cast value to a decimal (Power-Components#1520)
Browse files Browse the repository at this point in the history
* fix: removed return type forcing for string

* fix: removed return type forcing for string

* feat: add Order model to run the tests

* feat: add method verify value is string

* feat: added Test

* feat: added dropIfExists orders
  • Loading branch information
wandesnet authored Apr 30, 2024
1 parent 3b8d52e commit 2836d50
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/PowerGridFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ final class PowerGridFields
*/
public function add(string $fieldName, Closure $closure = null): PowerGridFields
{
$this->fields[$fieldName] = $closure ?? fn ($model) => e(strval(data_get($model, $fieldName)));
$this->fields[$fieldName] = $closure ?? fn ($model) => $this->valueIsString(data_get($model, $fieldName));

return $this;
}

/**
* @param mixed $value
* @return mixed
*/
private function valueIsString(mixed $value): mixed
{
return is_string($value) ? e($value) : $value;
}
}
7 changes: 7 additions & 0 deletions tests/Concerns/Models/Dish.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Dish extends Model

protected $table = 'dishes';

protected $casts = [
'in_stock' => 'boolean',
'active' => 'boolean',
'price' => 'float',
'produced_at' => 'datetime',
];

public static function servedAt()
{
return self::select('serving_at')->distinct('serving_at')->get();
Expand Down
30 changes: 30 additions & 0 deletions tests/Concerns/Models/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Tests\Concerns\Models;

use Illuminate\Database\Eloquent\{Model, SoftDeletes};
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property string $name
* @property float $price
* @property float $tax
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon $produced_at
*/
class Order extends Model
{
use SoftDeletes;

protected $guarded = [];

protected $table = 'orders';

protected $casts = [
'name' => 'string',
'price' => 'decimal:2',
'tax' => 'float',
];
}
17 changes: 17 additions & 0 deletions tests/Concerns/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static function down(): void
Schema::dropIfExists('chefs');
Schema::dropIfExists('restaurants');
Schema::dropIfExists('category_chef');
Schema::dropIfExists('orders');
}

public static function migrate(): void
Expand Down Expand Up @@ -80,6 +81,15 @@ public static function migrate(): void
$table->foreignId('category_id');
$table->timestamps();
});

Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->double('tax')->nullable();
$table->decimal('price')->nullable();
$table->softDeletes();
$table->timestamps();
});
}

public static function seed(array $dishes = []): void
Expand All @@ -91,6 +101,7 @@ public static function seed(array $dishes = []): void
DB::table('chefs')->truncate();
DB::table('restaurants')->truncate();
DB::table('category_chef')->truncate();
DB::table('orders')->truncate();

Schema::enableForeignKeyConstraints();

Expand All @@ -115,6 +126,12 @@ public static function seed(array $dishes = []): void
['name' => 'Not McDonalds'],
]);

DB::table('orders')->insert([
['name' => 'Order 1', 'price' => 10.00, 'tax' => 127.30],
['name' => 'Order 2', 'price' => 20.00, 'tax' => 259.50],
['name' => 'Order 3', 'price' => null, 'tax' => null],
]);

if (empty($dishes)) {
$dishes = self::generate();
}
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Actions/ListModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Category',
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Chef',
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Dish',
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Order',
'PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Restaurant',
]
);
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/PowerGridFieldsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Order;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

use PowerComponents\LivewirePowerGrid\{Column, PowerGridComponent, PowerGridFields};

$component = new class () extends PowerGridComponent {
public function datasource(): Builder
{
return Order::query();
}

public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('name')
->add('tax')
->add('price')
->add('price_formatted', fn (Order $model) => $model->price * 100);
}

public function columns(): array
{
return [
Column::make('Name', 'name'),
Column::make('Price', 'price_formatted', 'price'),
Column::make('Tax', 'tax'),
];
}
};

it('can add fields', function (string $name, string|float $price, string|float $tax) use ($component) {
$component = livewire($component::class);

$component->assertSee($name)
->assertSee($price)
->assertSee($tax);
})->with([
['Order 1', 1000, 127.30],
['Order 2', 2000, 259.50],
['Order 3', '', ''],
]);

0 comments on commit 2836d50

Please sign in to comment.