-
Notifications
You must be signed in to change notification settings - Fork 1
/
JoinerTest.php
157 lines (129 loc) · 4.78 KB
/
JoinerTest.php
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
<?php
namespace Railken\EloquentMapper\Tests;
use Railken\EloquentMapper\Joiner\Joiner;
use Railken\EloquentMapper\Mapper;
use Railken\EloquentMapper\Tests\Models\Book;
use Railken\EloquentMapper\Tests\Models\Author;
use Railken\EloquentMapper\Contracts\Joiner as JoinerContract;
class JoinerTest extends Base
{
public function testBelongsTo()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'author');
$this->assertQuery('
SELECT *
FROM `books`
LEFT JOIN `authors` AS `author`
ON `books`.`author_id` = `author`.`id`
AND `author`.`deleted_at` is null
WHERE `books`.`deleted_at` is null
', $qb->toSql());
}
public function testBelongsToDouble()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'author');
app(JoinerContract::class)->leftJoin($qb, 'author');
$this->assertQuery('
SELECT *
FROM `books`
LEFT JOIN `authors` AS `author`
ON `books`.`author_id` = `author`.`id`
AND `author`.`deleted_at` is null
WHERE `books`.`deleted_at` is null
', $qb->toSql());
}
public function testHasMany()
{
$qb = (new Author())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'books');
$this->assertQuery('
SELECT *
FROM `authors`
LEFT JOIN `books`
ON `authors`.`id` = `books`.`author_id`
AND `books`.`deleted_at` is null
WHERE `authors`.`deleted_at` is null
', $qb->toSql());
}
public function testMorphMany()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'categories');
$this->assertQuery('
SELECT *
FROM `books`
LEFT JOIN `categories`
ON `books`.`id` = `categories`.`categorizable_id`
AND `categories`.`categorizable_type` = ?
WHERE `books`.`deleted_at` is null
', $qb->toSql());
}
public function testBelongsToAndHasMany()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'author.books');
$this->assertQuery('
SELECT *
FROM `books`
LEFT JOIN `authors` AS `author`
ON `books`.`author_id` = `author`.`id`
AND `author`.`deleted_at` is null
LEFT JOIN `books` AS `author.books`
ON `author`.`id` = `author.books`.`author_id`
AND `author.books`.`deleted_at` is null
WHERE `books`.`deleted_at` is null
', $qb->toSql());
}
public function testMorphToMany()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'tags');
$this->assertQuery('
SELECT *
FROM `books`
LEFT JOIN `relations` AS `tags_pivot`
ON `books`.`id` = `tags_pivot`.`source_id` AND
`tags_pivot`.`source_type` = ?
LEFT JOIN `tags` AS `tags`
ON `tags_pivot`.`target_id` = `tags`.`id` AND
`tags_pivot`.`target_type` = ? AND
`tags_pivot`.`key` = ? AND
`tags`.`deleted_at` is null
WHERE `books`.`deleted_at` is null
', $qb->toSql());
}
public function testBelongsToMany()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'reviews');
$this->assertQuery('
select * from `books`
left join `book_reviews` as `reviews_pivot`
on `books`.`id` = `reviews_pivot`.`book_id`
left join `reviews` as `reviews`
on `reviews_pivot`.`review_id` = `reviews`.`id`
and `reviews`.`deleted_at` is null
where `books`.`deleted_at` is null
', $qb->toSql());
}
public function testBelongsToManyWithOrWhere()
{
$qb = (new Book())->newQuery();
app(JoinerContract::class)->leftJoin($qb, 'worstReviews');
$this->assertQuery('
select * from `books`
left join `book_reviews` as `worstreviews_pivot`
on `books`.`id` = `worstreviews_pivot`.`book_id`
left join `reviews` as `worstreviews`
on `worstreviews_pivot`.`review_id` = `worstreviews`.`id`
and (
`worstreviews`.`rating` <= ?
or `worstreviews`.`content` like ?
)
and `worstreviews`.`deleted_at` is null
where `books`.`deleted_at` is null
', $qb->toSql());
}
}