Skip to content

Commit c0d4357

Browse files
tests (#2034)
1 parent 433d3f2 commit c0d4357

File tree

7 files changed

+382
-774
lines changed

7 files changed

+382
-774
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
namespace PowerComponents\LivewirePowerGrid\Tests\Concerns\Components;
4+
5+
use Carbon\Carbon;
6+
use NumberFormatter;
7+
use PowerComponents\LivewirePowerGrid\{Column,
8+
Components\SetUp\Exportable,
9+
Facades\PowerGrid,
10+
PowerGridComponent,
11+
PowerGridFields};
12+
13+
abstract class BaseDishesTable extends PowerGridComponent
14+
{
15+
public array $eventId = [];
16+
17+
public array $testFilters = [];
18+
19+
protected function getListeners()
20+
{
21+
return array_merge(
22+
parent::getListeners(),
23+
[
24+
'deletedEvent',
25+
]
26+
);
27+
}
28+
29+
public function openModal(array $params)
30+
{
31+
$this->eventId = $params;
32+
}
33+
34+
public function deletedEvent(array $params)
35+
{
36+
$this->eventId = $params;
37+
}
38+
39+
public function setUp(): array
40+
{
41+
$this->showCheckBox();
42+
43+
return [
44+
PowerGrid::exportable('export')
45+
->striped()
46+
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
47+
48+
PowerGrid::header()
49+
->showToggleColumns()
50+
->showSearchInput(),
51+
52+
PowerGrid::footer()
53+
->showPerPage()
54+
->showRecordCount(),
55+
];
56+
}
57+
58+
public function filters(): array
59+
{
60+
return $this->testFilters;
61+
}
62+
63+
public function setTestThemeClass(string $themeClass): void
64+
{
65+
config(['livewire-powergrid.theme' => $themeClass]);
66+
}
67+
68+
public function fields(): PowerGridFields
69+
{
70+
$fmt = new NumberFormatter('ca_ES', NumberFormatter::CURRENCY);
71+
72+
return PowerGrid::fields()
73+
->add('id')
74+
->add('name')
75+
->add('storage_room')
76+
->add('chef_name')
77+
->add('serving_at')
78+
->add('calories')
79+
->add('calories', function ($dish) {
80+
return $dish->calories.' kcal';
81+
})
82+
->add('category_id', function ($dish) {
83+
return $dish->category_id;
84+
})
85+
->add('category_name', fn ($dish) => $this->getCategoryName($dish))
86+
->add('price')
87+
->add('price_EUR', function ($dish) use ($fmt) {
88+
return $fmt->formatCurrency($dish->price, 'EUR');
89+
})
90+
->add('price_BRL', function ($dish) {
91+
return 'R$ '.number_format($dish->price, 2, ',', '.');
92+
})
93+
->add('sales_price')
94+
->add('sales_price_BRL', function ($dish) {
95+
$sales_price = $dish->price + ($dish->price * 0.15);
96+
97+
return 'R$ '.number_format($sales_price, 2, ',', '.');
98+
})
99+
->add('in_stock')
100+
->add('in_stock_label', function ($dish) {
101+
return $dish->in_stock ? 'yes' : 'no';
102+
})
103+
->add('produced_at')
104+
->add('produced_at_formatted', function ($dish) {
105+
return Carbon::parse($dish->produced_at)->format('d/m/Y');
106+
});
107+
}
108+
109+
public function columns(): array
110+
{
111+
return [
112+
Column::add()
113+
->title('ID')
114+
->field('id')
115+
->searchable()
116+
->sortable(),
117+
118+
Column::add()
119+
->title('Stored at')
120+
->field('storage_room')
121+
->sortable(),
122+
123+
Column::add()
124+
->title('Dish')
125+
->field('name')
126+
->searchable()
127+
->placeholder('Dish placeholder')
128+
->sortable(),
129+
130+
Column::add()
131+
->title('Serving at')
132+
->field('serving_at')
133+
->sortable(),
134+
135+
Column::add()
136+
->title('Chef')
137+
->field('chef_name')
138+
->searchable()
139+
->placeholder('Chef placeholder')
140+
->sortable(),
141+
142+
Column::add()
143+
->title('Category')
144+
->field('category_name')
145+
->placeholder('Category placeholder'),
146+
147+
Column::add()
148+
->title('Price')
149+
->field('price_BRL'),
150+
151+
Column::add()
152+
->title('Sales Price')
153+
->field('sales_price_BRL'),
154+
155+
Column::add()
156+
->title('Calories')
157+
->field('calories')
158+
->sortable(),
159+
160+
Column::add()
161+
->title('In Stock')
162+
->toggleable(true, 'sim', 'não')
163+
->field('in_stock'),
164+
165+
Column::add()
166+
->title('Production Date')
167+
->field('produced_at_formatted'),
168+
169+
Column::add()
170+
->title('Date')
171+
->field('produced_at')
172+
->sortable(),
173+
174+
Column::action('Action'),
175+
];
176+
}
177+
178+
protected function getCategoryName($dish): string
179+
{
180+
return $dish->category->name ?? $dish->category_name ?? '';
181+
}
182+
183+
abstract public function datasource();
184+
}

tests/Concerns/Components/DishesIterableTable.php

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,15 @@
22

33
namespace PowerComponents\LivewirePowerGrid\Tests\Concerns\Components;
44

5-
use Illuminate\Support\{Carbon};
6-
use PowerComponents\LivewirePowerGrid\{Button,
7-
Column,
8-
Components\SetUp\Exportable,
9-
Facades\PowerGrid,
10-
PowerGridComponent,
11-
PowerGridFields};
12-
13-
class DishesIterableTable extends PowerGridComponent
5+
use Carbon\Carbon;
6+
use PowerComponents\LivewirePowerGrid\{Button, Column, Facades\PowerGrid, PowerGridFields};
7+
8+
class DishesIterableTable extends BaseDishesTable
149
{
1510
public string $tableName = 'testing-dishes-iterable-table';
1611

17-
public array $eventId = [];
18-
19-
public array $testFilters = [];
20-
2112
public string $iterableType = 'array';
2213

23-
protected function getListeners()
24-
{
25-
return array_merge(
26-
parent::getListeners(),
27-
[
28-
'deletedEvent',
29-
]
30-
);
31-
}
32-
33-
public function openModal(array $params)
34-
{
35-
$this->eventId = $params;
36-
}
37-
3814
public function datasource()
3915
{
4016
$data = [
@@ -87,25 +63,6 @@ public function datasource()
8763
return $data;
8864
}
8965

90-
public function setUp(): array
91-
{
92-
$this->showCheckBox();
93-
94-
return [
95-
PowerGrid::exportable('export')
96-
->striped()
97-
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
98-
99-
PowerGrid::header()
100-
->showToggleColumns()
101-
->showSearchInput(),
102-
103-
PowerGrid::footer()
104-
->showPerPage()
105-
->showRecordCount(),
106-
];
107-
}
108-
10966
public function fields(): PowerGridFields
11067
{
11168
return PowerGrid::fields()
@@ -115,7 +72,7 @@ public function fields(): PowerGridFields
11572
->add('price')
11673
->add('in_stock')
11774
->add('in_stock_label', function ($entry) {
118-
return $entry->in_stock ? 'sim' : 'não';
75+
return $entry->in_stock ? 'yes' : 'no';
11976
})
12077
->add('created_at_formatted', function ($entry) {
12178
return Carbon::parse($entry->created_at)->format('d/m/Y');
@@ -126,35 +83,35 @@ public function columns(): array
12683
{
12784
return [
12885
Column::add()
129-
->title(__('ID'))
86+
->title('ID')
13087
->field('id')
13188
->searchable()
13289
->sortable(),
13390

13491
Column::add()
135-
->title(__('Name'))
92+
->title('Name')
13693
->field('name')
13794
->searchable()
13895
->sortable(),
13996

14097
Column::add()
141-
->title(__('Chef'))
98+
->title('Chef')
14299
->field('chef_name')
143100
->searchable()
144101
->sortable(),
145102

146103
Column::add()
147-
->title(__('Price'))
104+
->title('Price')
148105
->field('price')
149106
->sortable(),
150107

151108
Column::add()
152-
->title(__('In Stock'))
109+
->title('In Stock')
153110
->toggleable(true, 'sim', 'não')
154111
->field('in_stock'),
155112

156113
Column::add()
157-
->title(__('Created At'))
114+
->title('Created At')
158115
->field('created_at_formatted'),
159116

160117
Column::action('Action'),
@@ -170,14 +127,4 @@ public function actions($row): array
170127
->openModal('edit-stock', ['dishId' => 'id']),
171128
];
172129
}
173-
174-
public function filters(): array
175-
{
176-
return $this->testFilters;
177-
}
178-
179-
public function setTestThemeClass(string $themeClass): void
180-
{
181-
config(['livewire-powergrid.theme' => $themeClass]);
182-
}
183130
}

0 commit comments

Comments
 (0)