1- <?php
1+ <?php declare (strict_types= 1 );
22
33namespace ProtoneMedia \LaravelEloquentScopeAsSelect \Tests ;
44
55class ScopeAsSelectTest extends TestCase
66{
7+ private function prepareFourPosts (): array
8+ {
9+ $ postA = Post::create (['title ' => 'foo ' ]);
10+ $ postB = Post::create (['title ' => 'foo ' ]);
11+ $ postC = Post::create (['title ' => 'bar ' ]);
12+ $ postD = Post::create (['title ' => 'bar ' ]);
13+
14+ foreach (range (1 , 5 ) as $ i ) {
15+ $ postA ->comments ()->create (['body ' => 'ok ' ]);
16+ $ postC ->comments ()->create (['body ' => 'ok ' ]);
17+ }
18+
19+ foreach (range (1 , 10 ) as $ i ) {
20+ $ postB ->comments ()->create (['body ' => 'ok ' ]);
21+ $ postD ->comments ()->create (['body ' => 'ok ' ]);
22+ }
23+
24+ return [$ postA , $ postB , $ postC , $ postD ];
25+ }
26+
727 /** @test */
828 public function it_can_add_a_scope_as_a_select ()
929 {
@@ -19,6 +39,71 @@ public function it_can_add_a_scope_as_a_select()
1939 $ this ->assertFalse ($ posts ->get (1 )->title_is_foo );
2040 }
2141
42+ /** @test */
43+ public function it_can_add_a_scope_as_a_select_and_cast_inversed ()
44+ {
45+ $ postA = Post::create (['title ' => 'foo ' ]);
46+ $ postB = Post::create (['title ' => 'bar ' ]);
47+
48+ $ posts = Post::query ()
49+ ->addScopeAsSelect ('title_is_foo ' , fn ($ query ) => $ query ->titleIsFoo (), false )
50+ ->orderBy ('id ' )
51+ ->get ();
52+
53+ $ this ->assertFalse ($ posts ->get (0 )->title_is_foo );
54+ $ this ->assertTrue ($ posts ->get (1 )->title_is_foo );
55+ }
56+
57+ /** @test */
58+ public function it_can_add_a_scope_by_using_the_name ()
59+ {
60+ $ postA = Post::create (['title ' => 'foo ' ]);
61+ $ postB = Post::create (['title ' => 'bar ' ]);
62+
63+ $ posts = Post::query ()
64+ ->addScopeAsSelect ('title_is_foo ' , 'titleIsFoo ' )
65+ ->orderBy ('id ' )
66+ ->get ();
67+
68+ $ this ->assertTrue ($ posts ->get (0 )->title_is_foo );
69+ $ this ->assertFalse ($ posts ->get (1 )->title_is_foo );
70+ }
71+
72+ /** @test */
73+ public function it_can_add_multiple_scopes_by_using_an_array ()
74+ {
75+ [$ postA , $ postB , $ postC , $ postD ] = $ this ->prepareFourPosts ();
76+
77+ $ posts = Post::query ()
78+ ->addScopeAsSelect ('title_is_foo_and_has_six_comments_or_more ' , ['titleIsFoo ' , 'hasSixOrMoreComments ' ])
79+ ->orderBy ('id ' )
80+ ->get ();
81+
82+ $ this ->assertFalse ($ posts ->get (0 )->title_is_foo_and_has_six_comments_or_more );
83+ $ this ->assertTrue ($ posts ->get (1 )->title_is_foo_and_has_six_comments_or_more );
84+ $ this ->assertFalse ($ posts ->get (2 )->title_is_foo_and_has_six_comments_or_more );
85+ $ this ->assertFalse ($ posts ->get (3 )->title_is_foo_and_has_six_comments_or_more );
86+ }
87+
88+ /** @test */
89+ public function it_can_add_multiple_dynamic_scopes_by_using_an_array ()
90+ {
91+ [$ postA , $ postB , $ postC , $ postD ] = $ this ->prepareFourPosts ();
92+
93+ $ posts = Post::query ()
94+ ->addScopeAsSelect ('title_is_foo_and_has_more_than_five_comments ' , [
95+ 'titleIsFoo ' ,
96+ 'hasMoreCommentsThan ' => 5 ,
97+ ])
98+ ->orderBy ('id ' )
99+ ->get ();
100+
101+ $ this ->assertFalse ($ posts ->get (0 )->title_is_foo_and_has_more_than_five_comments );
102+ $ this ->assertTrue ($ posts ->get (1 )->title_is_foo_and_has_more_than_five_comments );
103+ $ this ->assertFalse ($ posts ->get (2 )->title_is_foo_and_has_more_than_five_comments );
104+ $ this ->assertFalse ($ posts ->get (3 )->title_is_foo_and_has_more_than_five_comments );
105+ }
106+
22107 /** @test */
23108 public function it_can_add_multiple_and_has_relation_scopes ()
24109 {
@@ -53,20 +138,7 @@ public function it_can_add_multiple_and_has_relation_scopes()
53138 /** @test */
54139 public function it_can_do_inline_contraints_as_well ()
55140 {
56- $ postA = Post::create (['title ' => 'foo ' ]);
57- $ postB = Post::create (['title ' => 'foo ' ]);
58- $ postC = Post::create (['title ' => 'bar ' ]);
59- $ postD = Post::create (['title ' => 'bar ' ]);
60-
61- foreach (range (1 , 5 ) as $ i ) {
62- $ postA ->comments ()->create (['body ' => 'ok ' ]);
63- $ postC ->comments ()->create (['body ' => 'ok ' ]);
64- }
65-
66- foreach (range (1 , 10 ) as $ i ) {
67- $ postB ->comments ()->create (['body ' => 'ok ' ]);
68- $ postD ->comments ()->create (['body ' => 'ok ' ]);
69- }
141+ [$ postA , $ postB , $ postC , $ postD ] = $ this ->prepareFourPosts ();
70142
71143 $ posts = Post::query ()
72144 ->addScopeAsSelect ('title_is_foo_and_has_six_comments_or_more ' , function ($ query ) {
@@ -84,20 +156,7 @@ public function it_can_do_inline_contraints_as_well()
84156 /** @test */
85157 public function it_can_mix_scopes_outside_of_the_closure ()
86158 {
87- $ postA = Post::create (['title ' => 'foo ' ]);
88- $ postB = Post::create (['title ' => 'foo ' ]);
89- $ postC = Post::create (['title ' => 'bar ' ]);
90- $ postD = Post::create (['title ' => 'bar ' ]);
91-
92- foreach (range (1 , 5 ) as $ i ) {
93- $ postA ->comments ()->create (['body ' => 'ok ' ]);
94- $ postC ->comments ()->create (['body ' => 'ok ' ]);
95- }
96-
97- foreach (range (1 , 10 ) as $ i ) {
98- $ postB ->comments ()->create (['body ' => 'ok ' ]);
99- $ postD ->comments ()->create (['body ' => 'ok ' ]);
100- }
159+ [$ postA , $ postB , $ postC , $ postD ] = $ this ->prepareFourPosts ();
101160
102161 $ posts = Post::query ()
103162 ->where ('title ' , 'foo ' )
0 commit comments