|
1 | 1 | <?php |
2 | 2 |
|
3 | | - use Workbench\App\Models\BlogPost; |
4 | | - use Workbench\App\Models\Product; |
| 3 | +use Workbench\App\Models\BlogPost; |
5 | 4 |
|
6 | | - test('retrieve blog posts with specific comments', function () { |
| 5 | +test('retrieve blog posts with specific comments', function () { |
7 | 6 | BlogPost::factory()->create([ |
8 | | - 'comments' => [ |
9 | | - ['name' => 'John Doe', 'country' => 'Peru', 'likes' => 5], |
10 | | - ['name' => 'Jane Smith', 'country' => 'USA', 'likes' => 3] |
11 | | - ] |
12 | | - ]); |
| 7 | + 'comments' => [ |
| 8 | + ['name' => 'John Doe', 'country' => 'Peru', 'likes' => 5], |
| 9 | + ['name' => 'Jane Smith', 'country' => 'USA', 'likes' => 3], |
| 10 | + ], |
| 11 | + ]); |
13 | 12 |
|
14 | 13 | $posts = BlogPost::whereNestedObject('comments', function ($query) { |
15 | | - $query->where('country', 'Peru')->where('likes', 5); |
| 14 | + $query->where('country', 'Peru')->where('likes', 5); |
16 | 15 | })->get(); |
17 | 16 |
|
18 | 17 | expect($posts)->toHaveCount(1) |
19 | | - ->and($posts->first()->comments[0]['country'])->toEqual('Peru') |
20 | | - ->and($posts->first()->comments[0]['likes'])->toEqual(5); |
21 | | - }); |
| 18 | + ->and($posts->first()->comments[0]['country'])->toEqual('Peru') |
| 19 | + ->and($posts->first()->comments[0]['likes'])->toEqual(5); |
| 20 | +}); |
22 | 21 |
|
23 | | - test('exclude blog posts with comments from a specific country', function () { |
| 22 | +test('exclude blog posts with comments from a specific country', function () { |
24 | 23 | BlogPost::factory()->create([ |
25 | | - 'comments' => [ |
26 | | - ['name' => 'John Doe', 'country' => 'Peru', 'likes' => 5] |
27 | | - ] |
28 | | - ]); |
| 24 | + 'comments' => [ |
| 25 | + ['name' => 'John Doe', 'country' => 'Peru', 'likes' => 5], |
| 26 | + ], |
| 27 | + ]); |
29 | 28 |
|
30 | 29 | $posts = BlogPost::whereNotNestedObject('comments', function ($query) { |
31 | | - $query->where('country', 'Peru'); |
| 30 | + $query->where('country', 'Peru'); |
32 | 31 | })->get(); |
33 | 32 |
|
34 | 33 | expect($posts->isNotEmpty())->toBeTrue(); |
35 | | - }); |
| 34 | +}); |
36 | 35 |
|
37 | | - test('order blog posts by comments likes descending', function () { |
| 36 | +test('order blog posts by comments likes descending', function () { |
38 | 37 | BlogPost::factory()->create([ |
39 | | - 'status' => 1, |
40 | | - 'comments' => [ |
41 | | - ['name' => 'John Doe', 'country' => 'Peru', 'likes' => 5], |
42 | | - ['name' => 'Jane Smith', 'country' => 'USA', 'likes' => 8] |
43 | | - ] |
44 | | - ]); |
| 38 | + 'status' => 1, |
| 39 | + 'comments' => [ |
| 40 | + ['name' => 'John Doe', 'country' => 'Peru', 'likes' => 5], |
| 41 | + ['name' => 'Jane Smith', 'country' => 'USA', 'likes' => 8], |
| 42 | + ], |
| 43 | + ]); |
45 | 44 |
|
46 | 45 | // FIXME: @pdphilip I can't get this to sort for the life of me not sure what I am doing wrong. |
47 | 46 | $posts = BlogPost::where('status', 1)->orderByNested('comments.likes', 'desc', 'sum')->get(); |
48 | 47 | expect($posts->first()->comments[0]['likes'])->toEqual(8); |
49 | | - })->todo(); |
| 48 | +})->todo(); |
50 | 49 |
|
51 | | - test('filter blog posts by comments from Switzerland ordered by likes', function () { |
| 50 | +test('filter blog posts by comments from Switzerland ordered by likes', function () { |
52 | 51 | BlogPost::factory()->create([ |
53 | | - 'status' => 5, |
54 | | - 'comments' => [ |
55 | | - ['name' => 'April Von', 'country' => 'Switzerland', 'likes' => 10], |
56 | | - ['name' => 'Mabelle Schinner', 'country' => 'Switzerland', 'likes' => 7] |
57 | | - ] |
58 | | - ]); |
| 52 | + 'status' => 5, |
| 53 | + 'comments' => [ |
| 54 | + ['name' => 'April Von', 'country' => 'Switzerland', 'likes' => 10], |
| 55 | + ['name' => 'Mabelle Schinner', 'country' => 'Switzerland', 'likes' => 7], |
| 56 | + ], |
| 57 | + ]); |
59 | 58 |
|
60 | 59 | $post = BlogPost::where('status', 5)->queryNested('comments', function ($query) { |
61 | | - $query->where('country', 'Switzerland')->orderBy('likes'); |
| 60 | + $query->where('country', 'Switzerland')->orderBy('likes'); |
62 | 61 | })->first(); |
63 | 62 |
|
64 | 63 | expect($post->comments[0]['name'])->toEqual('Mabelle Schinner') |
65 | | - ->and($post->comments[0]['likes'])->toEqual(7) |
66 | | - ->and($post->comments[1]['likes'])->toEqual(10); |
67 | | - }); |
| 64 | + ->and($post->comments[0]['likes'])->toEqual(7) |
| 65 | + ->and($post->comments[1]['likes'])->toEqual(10); |
| 66 | +}); |
68 | 67 |
|
69 | | - test('filter comments with likes greater than or equal to 5, limit 2', function () { |
| 68 | +test('filter comments with likes greater than or equal to 5, limit 2', function () { |
70 | 69 | BlogPost::factory()->create([ |
71 | | - 'status' => 5, |
72 | | - 'comments' => [ |
73 | | - ['name' => 'Damaris Ondricka', 'country' => 'Peru', 'likes' => 5], |
74 | | - ['name' => 'April Von', 'country' => 'Switzerland', 'likes' => 10], |
75 | | - ['name' => 'Third Comment', 'country' => 'USA', 'likes' => 2] |
76 | | - ] |
77 | | - ]); |
| 70 | + 'status' => 5, |
| 71 | + 'comments' => [ |
| 72 | + ['name' => 'Damaris Ondricka', 'country' => 'Peru', 'likes' => 5], |
| 73 | + ['name' => 'April Von', 'country' => 'Switzerland', 'likes' => 10], |
| 74 | + ['name' => 'Third Comment', 'country' => 'USA', 'likes' => 2], |
| 75 | + ], |
| 76 | + ]); |
78 | 77 |
|
79 | 78 | $post = BlogPost::where('status', 5)->queryNested('comments', function ($query) { |
80 | | - $query->where('likes', '>=', 5)->limit(2); |
| 79 | + $query->where('likes', '>=', 5)->limit(2); |
81 | 80 | })->first(); |
82 | 81 |
|
83 | 82 | expect($post->comments)->toHaveCount(2) |
84 | | - ->and($post->comments[0]['likes'])->toBeGreaterThanOrEqual(5); |
85 | | - }); |
| 83 | + ->and($post->comments[0]['likes'])->toBeGreaterThanOrEqual(5); |
| 84 | +}); |
0 commit comments