-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPostQueryBuilderTest.php
More file actions
80 lines (65 loc) · 2.26 KB
/
PostQueryBuilderTest.php
File metadata and controls
80 lines (65 loc) · 2.26 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
<?php
namespace Rareloop\Lumberjack\Test;
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;
class PostQueryBuilderTest 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 can_create_a_builder()
{
$builder = Post::builder();
$this->assertInstanceOf(ScopedQueryBuilder::class, $builder);
$this->assertArraySubset([
'post_type' => Post::getPostType(),
], $builder->getParameters());
}
/** @test */
public function can_create_a_builder_from_static_functions()
{
$this->assertQueryBuilder('whereStatus', ['publish'], QueryBuilderTestPost::class);
$this->assertQueryBuilder('whereIdIn', [[1, 2, 3]], QueryBuilderTestPost::class);
$this->assertQueryBuilder('whereIdNotIn', [[1, 2, 3]], QueryBuilderTestPost::class);
}
/**
* @test
* @runInSeparateProcess
* @expectedException \PHPUnit\Framework\Error\Error
*/
public function throw_error_on_missing_static_function()
{
Post::missingStaticFunction();
}
private function assertQueryBuilder($function, $params, $postType)
{
$builder = Mockery::mock(ScopedQueryBuilder::class.'['.$function.']', [$postType]);
$builder->shouldReceive($function)->withArgs($params)->once();
// Inject the mock builder
call_user_func([$postType, 'setCreateBuilderResponse'], $builder);
// Call the static function e.g. $postType::$function($params)
call_user_func_array([$postType, $function], $params);
}
}
class QueryBuilderTestPost extends Post
{
private static $injectedBuilder;
public static function setCreateBuilderResponse($builder)
{
static::$injectedBuilder = $builder;
}
public static function builder() : ScopedQueryBuilder
{
return static::$injectedBuilder;
}
}