1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Traits; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Tests\Models\Event; |
8
|
|
|
use Tests\TestCase; |
9
|
|
|
|
10
|
|
|
class FilterableTraitTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
parent::setUp(); |
15
|
|
|
$this->initEvents(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function dataProvider() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
// Case 0: Where Filter |
25
|
|
|
[ |
26
|
|
|
3, |
27
|
|
|
[] |
28
|
|
|
], |
29
|
|
|
// Case 1: Where Filter |
30
|
|
|
[ |
31
|
|
|
1, |
32
|
|
|
['id' => 1] |
33
|
|
|
], |
34
|
|
|
|
35
|
|
|
// Case 2: Where Filter with array |
36
|
|
|
[ |
37
|
|
|
2, |
38
|
|
|
['id' => [1, 2]] |
39
|
|
|
], |
40
|
|
|
|
41
|
|
|
// Case 3: Like Filter |
42
|
|
|
[ |
43
|
|
|
2, |
44
|
|
|
['title' => 'Event'] |
45
|
|
|
], |
46
|
|
|
// Case 4: Between Filter |
47
|
|
|
[ |
48
|
|
|
2, |
49
|
|
|
[ |
50
|
|
|
'created_at' => [ |
51
|
|
|
'from' => Carbon::create(2014), |
52
|
|
|
'till' => Carbon::create(2016) |
53
|
|
|
] |
54
|
|
|
] |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $expected |
61
|
|
|
* @param mixed $filter |
62
|
|
|
* |
63
|
|
|
* @dataProvider dataProvider |
64
|
|
|
*/ |
65
|
|
|
public function testFilter($expected, $filter) |
66
|
|
|
{ |
67
|
|
|
/** @var Collection $events */ |
68
|
|
|
$events = Event::filter($filter)->get(); |
69
|
|
|
$this->assertEquals($expected, $events->count()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
*/ |
75
|
|
|
protected function initEvents() |
76
|
|
|
{ |
77
|
|
|
Event::create([ |
78
|
|
|
'title' => 'Party Event', |
79
|
|
|
'created_at' => Carbon::create(2017, 1, 1, 0, 0, 0), |
80
|
|
|
]); |
81
|
|
|
Event::create([ |
82
|
|
|
'title' => 'Sport Event', |
83
|
|
|
'created_at' => Carbon::create(2015, 1, 1, 0, 0, 0), |
84
|
|
|
]); |
85
|
|
|
Event::create([ |
86
|
|
|
'title' => 'Health Check', |
87
|
|
|
'created_at' => Carbon::create(2015, 1, 1, 0, 0, 0), |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
} |