-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathScopedQueryBuilderTest.php
More file actions
175 lines (146 loc) · 4.82 KB
/
ScopedQueryBuilderTest.php
File metadata and controls
175 lines (146 loc) · 4.82 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
<?php
namespace Rareloop\Lumberjack\Test;
use Illuminate\Support\Collection;
use Mockery;
use PHPUnit\Framework\TestCase;
use Rareloop\Lumberjack\Application;
use Rareloop\Lumberjack\Contracts\QueryBuilder as QueryBuilderContract;
use Rareloop\Lumberjack\Post;
use Rareloop\Lumberjack\QueryBuilder;
use Rareloop\Lumberjack\ScopedQueryBuilder;
use Timber\Timber;
class ScopedQueryBuilderTest extends TestCase
{
use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
protected function setUp(): void
{
$this->app = new Application;
$this->app->bind(QueryBuilderContract::class, QueryBuilder::class);
parent::setUp();
}
/** @test */
public function correct_post_type_is_set()
{
$builder = new ScopedQueryBuilder(Post::class);
$params = $builder->getParameters();
$this->assertArraySubset([
'post_type' => Post::getPostType(),
], $params);
}
/**
* @test
* @expectedException Rareloop\Lumberjack\Exceptions\CannotRedeclarePostTypeOnQueryException
*/
public function cannot_overwrite_post_type()
{
$builder = new ScopedQueryBuilder(PostWithQueryScope::class);
$builder->wherePostType('test_post_type');
}
/**
* @test
* @expectedException Rareloop\Lumberjack\Exceptions\CannotRedeclarePostClassOnQueryException
*/
public function cannot_overwrite_post_class()
{
$builder = new ScopedQueryBuilder(PostWithQueryScope::class);
$builder->as(Post::class);
}
/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function get_retrieves_list_of_posts_of_correct_type()
{
$posts = [new PostWithQueryScope(1, true), new PostWithQueryScope(2, true)];
$timber = Mockery::mock('alias:' . Timber::class);
$timber->shouldReceive('get_posts')->withArgs([
Mockery::subset([
'post_type' => PostWithQueryScope::getPostType(),
'post_status' => 'publish',
'offset' => 10,
]),
PostWithQueryScope::class,
])->once()->andReturn($posts);
$builder = new ScopedQueryBuilder(PostWithQueryScope::class);
$returnedPosts = $builder->whereStatus('publish')->offset(10)->get();
$this->assertInstanceOf(Collection::class, $returnedPosts);
$this->assertSame($posts, $returnedPosts->toArray());
}
/** @test */
public function can_call_a_query_scope_on_post_object()
{
$builder = new ScopedQueryBuilder(PostWithQueryScope::class);
$chainedBuilder = $builder->inDraft();
$params = $builder->getParameters();
$this->assertSame($builder, $chainedBuilder);
$this->assertArraySubset([
'post_status' => 'draft',
], $params);
}
/** @test */
public function can_pass_params_into_a_query_scope_on_post_object()
{
$builder = new ScopedQueryBuilder(PostWithQueryScope::class);
$chainedBuilder = $builder->without(1, 2);
$params = $builder->getParameters();
$this->assertSame($builder, $chainedBuilder);
$this->assertArraySubset([
'post__not_in' => [1, 2],
], $params);
}
/**
* @test
* @runInSeparateProcess
* @expectedException \PHPUnit\Framework\Error\Error
*/
public function missing_query_scope_throws_an_error()
{
$builder = new ScopedQueryBuilder(PostWithQueryScope::class);
$builder->nonExistentScope();
}
/** @test */
public function can_use_a_different_query_builder_implementation()
{
$this->app->bind(QueryBuilderContract::class, CustomQueryBuilder::class);
$builder = new ScopedQueryBuilder(Post::class);
$this->assertSame('it works', $builder->nonStandardMethod());
}
/** @test */
public function can_call_a_function_added_to_querybuilder_via_a_macro()
{
QueryBuilder::macro('testFunctionAddedByMacro', function () {
$this->params['foo'] = 'bar';
return $this;
});
$builder = new ScopedQueryBuilder(Post::class);
$chainedBuilder = $builder->testFunctionAddedByMacro();
$params = $builder->getParameters();
$this->assertSame($builder, $chainedBuilder);
$this->assertArraySubset([
'foo' => 'bar',
], $params);
}
}
class CustomQueryBuilder extends QueryBuilder
{
public function nonStandardMethod()
{
return 'it works';
}
}
class PostWithQueryScope extends Post
{
public static function getPostType()
{
return 'post_with_query_scope';
}
public function scopeInDraft($query)
{
return $query->whereStatus('draft');
}
public function scopeWithout($query, $id1, $id2)
{
return $query->whereIdNotIn([$id1, $id2]);
}
}