-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFilterableLifecycleHooksTest.php
More file actions
86 lines (68 loc) · 2.57 KB
/
FilterableLifecycleHooksTest.php
File metadata and controls
86 lines (68 loc) · 2.57 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
<?php
namespace Kettasoft\Filterable\Tests\Feature\Filterable;
use Kettasoft\Filterable\Filterable;
use Kettasoft\Filterable\Tests\TestCase;
use Illuminate\Database\Eloquent\Builder;
use Kettasoft\Filterable\Tests\Models\Post;
class FilterableLifecycleHooksTest extends TestCase
{
public function test_it_can_trigger_before_filtering_hook()
{
$filter = new class extends Filterable {
protected function initially(Builder $builder): Builder
{
return $builder->where('id', '>', 10);
}
};
$invoker = Post::filter($filter);
$this->assertStringContainsString('where "id" > ?', $invoker->toSql());
}
public function test_it_can_trigger_after_filtering_hook()
{
$filter = new class extends Filterable {
protected function finally(Builder $builder): Builder
{
return $builder->where('id', '>', 10);
}
};
$invoker = Post::filter($filter);
$this->assertStringContainsString('where "id" > ?', $invoker->toSql());
}
public function test_it_can_trigger_initially_with_finally_hook()
{
$filter = new class extends Filterable {
protected function initially(Builder $builder): Builder
{
return $builder->where('id', '>', 10);
}
protected function finally(Builder $builder): Builder
{
return $builder->where('status', '=', 'published');
}
};
$invoker = Post::filter($filter);
$this->assertStringContainsString('where "id" > ? and "status" = ?', $invoker->toSql());
}
public function test_it_can_trigger_initially_with_request_filters_and_finally_hook()
{
$filter = new class extends Filterable {
protected $filters = ['title'];
protected function initially(Builder $builder): Builder
{
return $builder->where('id', '>', 10);
}
protected function finally(Builder $builder): Builder
{
return $builder->where('status', '=', 'published');
}
public function title($payload)
{
return $this->builder->where('title', '=', $payload->value);
}
};
// Simulate request filters
$this->app['request']->query->set('title', 'Test Post');
$invoker = Post::filter($filter);
$this->assertStringContainsString('where "id" > ? and "title" = ? and "status" = ?', $invoker->toSql());
}
}