@@ -18,11 +18,16 @@ public function test_closure_scopes_can_be_applied()
1818 {
1919 $ b = new Builder (new Entry , new QueryBuilder (new Connection ));
2020
21- $ b ->withGlobalScope ('foo ' , function ($ query ) use ($ b ) {
22- $ this ->assertSame ($ b , $ query );
21+ $ scoped = null ;
22+
23+ $ b ->withGlobalScope ('foo ' , function ($ query ) use (&$ scoped ) {
24+ $ scoped = $ query ;
2325 });
2426
25- $ b ->applyScopes ();
27+ $ applied = $ b ->applyScopes ();
28+
29+ $ this ->assertNotSame ($ b , $ applied );
30+ $ this ->assertSame ($ applied , $ scoped );
2631 }
2732
2833 public function test_class_scopes_can_be_applied ()
@@ -33,11 +38,54 @@ public function test_class_scopes_can_be_applied()
3338
3439 $ b ->withGlobalScope ('foo ' , new TestModelScope );
3540
36- // Scopes are wrapped in an AndGroup to prevent negation
37- $ this ->assertEquals ('(&(foo=LdapRecord\Models\Entry)) ' , $ b ->getUnescapedQuery ());
41+ // Scope filters are composed onto the scoped query builder.
42+ $ scoped = $ b ->applyScopes ();
43+
44+ $ this ->assertEquals ('(foo=LdapRecord\Models\Entry) ' , $ scoped ->getUnescapedQuery ());
45+
46+ $ this ->assertEmpty ($ b ->appliedScopes ());
47+ $ this ->assertCount (1 , $ scoped ->appliedScopes ());
48+ $ this ->assertArrayHasKey ('foo ' , $ scoped ->appliedScopes ());
49+ }
50+
51+ public function test_scope_or_filters_are_preserved_when_grouped ()
52+ {
53+ $ b = new Builder (new Entry , new QueryBuilder (new Connection ));
54+
55+ $ b ->where ('cn ' , '= ' , 'John Doe ' );
56+
57+ $ b ->withGlobalScope ('foo ' , function ($ query ) {
58+ $ query ->orWhere ('foo ' , '= ' , 'bar ' );
59+ $ query ->orWhere ('bar ' , '= ' , 'baz ' );
60+ });
61+
62+ $ this ->assertEquals (
63+ '(&(cn=John Doe)(|(foo=bar)(bar=baz))) ' ,
64+ $ b ->toBase ()->getUnescapedQuery ()
65+ );
66+ }
67+
68+ public function test_complex_scope_filters_cannot_be_negated_by_complex_queries ()
69+ {
70+ $ b = new Builder (new Entry , new QueryBuilder (new Connection ));
71+
72+ $ b ->where ('department ' , '= ' , 'Sales ' )
73+ ->orWhere ('department ' , '= ' , 'Support ' )
74+ ->where ('enabled ' , '= ' , 'true ' );
75+
76+ $ b ->withGlobalScope ('foo ' , function ($ query ) {
77+ $ query ->orFilter (function ($ query ) {
78+ $ query ->where ('type ' , '= ' , 'person ' );
79+ $ query ->where ('type ' , '= ' , 'contact ' );
80+ });
81+
82+ $ query ->where ('tenant ' , '= ' , 'acme ' );
83+ });
3884
39- $ this ->assertCount (1 , $ b ->appliedScopes ());
40- $ this ->assertArrayHasKey ('foo ' , $ b ->appliedScopes ());
85+ $ this ->assertEquals (
86+ '(&(|(department=Sales)(department=Support))(enabled=true)(&(|(type=person)(type=contact))(tenant=acme))) ' ,
87+ $ b ->toBase ()->getUnescapedQuery ()
88+ );
4189 }
4290
4391 public function test_scopes_can_be_removed_after_being_added ()
0 commit comments