-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminModelTest.php
More file actions
237 lines (179 loc) · 5.96 KB
/
AdminModelTest.php
File metadata and controls
237 lines (179 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Javaabu\Helpers\Tests\Feature;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Javaabu\Helpers\AdminModel\AdminModel;
use Javaabu\Helpers\AdminModel\IsAdminModel;
use Javaabu\Helpers\Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;
class CategoryWithDateCast extends Model implements AdminModel
{
use IsAdminModel;
protected $table = 'categories';
protected $casts = [
'published_at' => 'date'
];
protected $guarded = [
];
public function getAdminUrlAttribute(): string
{
return '';
}
}
class CategoryWithDateTimeCast extends Model implements AdminModel
{
use IsAdminModel;
protected $table = 'categories';
protected $casts = [
'published_at' => 'datetime'
];
protected $guarded = [
];
public function getAdminUrlAttribute(): string
{
return '';
}
}
class CategoryWithSearchable extends Model implements AdminModel
{
use IsAdminModel;
protected $table = 'categories';
protected array $searchable = [
'name'
];
protected $guarded = [
];
public function getAdminUrlAttribute(): string
{
return '';
}
}
class CategoryWithStringSearchable extends Model implements AdminModel
{
use IsAdminModel;
protected $table = 'categories';
protected string $searchable = 'name';
protected $guarded = [
];
public function getAdminUrlAttribute(): string
{
return '';
}
}
class CategoryWithMultipleSearchable extends Model implements AdminModel
{
use IsAdminModel;
protected $table = 'categories';
protected array $searchable = [
'slug',
'name'
];
protected $guarded = [
];
public function getAdminUrlAttribute(): string
{
return '';
}
}
class CategoryWithoutSearchable extends Model implements AdminModel
{
use IsAdminModel;
protected $table = 'categories';
protected $guarded = [
];
public function getAdminUrlAttribute(): string
{
return '';
}
}
class AdminModelTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function it_can_get_list_of_date_fields(): void
{
$this->assertEquals([
'published_at' => 'Published At',
'created_at' => 'Created At',
'updated_at' => 'Updated At'
], CategoryWithDateCast::getDateFieldsList());
}
#[Test]
public function it_can_determine_if_an_attribute_is_an_allowed_date_field(): void
{
$this->assertTrue(CategoryWithDateCast::isAllowedDateField('published_at'));
$this->assertTrue(CategoryWithDateCast::isAllowedDateField('created_at'));
$this->assertTrue(CategoryWithDateCast::isAllowedDateField('updated_at'));
$this->assertFalse(CategoryWithDateCast::isAllowedDateField('name'));
}
#[Test]
public function it_can_determine_date_fields_from_date_casts(): void
{
$category = new CategoryWithDateCast();
$this->assertEquals(['published_at', 'created_at', 'updated_at'], $category->getDateAttributes());
}
#[Test]
public function it_can_determine_date_fields_from_datetime_casts(): void
{
$category = new CategoryWithDateTimeCast();
$this->assertEquals(['published_at', 'created_at', 'updated_at'], $category->getDateAttributes());
}
#[Test]
public function it_can_filter_models_by_date_range(): void
{
$category = new CategoryWithDateCast([
'name' => 'Apple',
'slug' => 'some-slug',
'published_at' => '2024-02-11'
]);
$category->save();
$found = CategoryWithDateCast::dateBetween('published_at', '2024-02-10', '2024-02-12')->first();
$this->assertEquals($category->id, $found->id);
}
#[Test]
public function it_can_search_models_using_a_partial_match(): void
{
$category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();
$found = CategoryWithSearchable::search('pple')->first();
$this->assertEquals($category->id, $found->id);
}
#[Test]
public function it_can_search_models_using_a_single_searchable(): void
{
$category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();
$found = CategoryWithSearchable::search('Apple')->first();
$this->assertEquals($category->id, $found->id);
$this->assertNull(CategoryWithSearchable::search('some-slug')->first());
}
#[Test]
public function it_can_search_models_using_a_string_searchable(): void
{
$category = new CategoryWithStringSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();
$found = CategoryWithStringSearchable::search('Apple')->first();
$this->assertEquals($category->id, $found->id);
$this->assertNull(CategoryWithStringSearchable::search('some-slug')->first());
}
#[Test]
public function it_can_search_models_using_multiple_searchables(): void
{
$category = new CategoryWithMultipleSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();
$found = CategoryWithMultipleSearchable::search('Apple')->first();
$this->assertEquals($category->id, $found->id);
$found = CategoryWithMultipleSearchable::search('some-slug')->first();
$this->assertEquals($category->id, $found->id);
}
#[Test]
public function it_can_search_models_without_searchable(): void
{
$category = new CategoryWithoutSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
$category->save();
$found = CategoryWithoutSearchable::search($category->id)->first();
$this->assertEquals($category->id, $found->id);
$this->assertNull(CategoryWithoutSearchable::search('Apple')->first());
$this->assertNull(CategoryWithoutSearchable::search('some-slug')->first());
}
}