@@ -37,9 +37,42 @@ public function testTouchMethodUpdatesRelatedTimestamps()
3737 $ relation ->touch ();
3838 }
3939
40+ /**
41+ * Testing to ensure loop does not occur during relational queries in global scopes
42+ *
43+ * Executing parent model's global scopes could result in an infinite loop when the
44+ * parent model's global scope utilizes a relation in a query like has or whereHas
45+ */
46+ public function testDonNotRunParentModelGlobalScopes ()
47+ {
48+ /** @var Mockery\MockInterface $parent */
49+ $ eloquentBuilder = m::mock ('Illuminate\Database\Eloquent\Builder ' );
50+ $ queryBuilder = m::mock ('Illuminate\Database\Query\Builder ' );
51+ $ parent = m::mock ('EloquentRelationResetModelStub ' )->makePartial ();
52+ $ grammar = m::mock ('\Illuminate\Database\Grammar ' );
53+
54+ $ eloquentBuilder ->shouldReceive ('getModel ' )->andReturn ($ related = m::mock ('StdClass ' ));
55+ $ eloquentBuilder ->shouldReceive ('getQuery ' )->andReturn ($ queryBuilder );
56+ $ queryBuilder ->shouldReceive ('getGrammar ' )->andReturn ($ grammar );
57+ $ grammar ->shouldReceive ('wrap ' );
58+ $ parent ->shouldReceive ('newQueryWithoutScopes ' )->andReturn ($ eloquentBuilder );
59+
60+ //Test Condition
61+ $ parent ->shouldReceive ('applyGlobalScopes ' )->andReturn ($ eloquentBuilder )->never ();
62+
63+ $ relation = new EloquentRelationStub ($ eloquentBuilder , $ parent );
64+ $ relation ->wrap ('test ' );
65+ }
66+
4067}
4168
42- class EloquentRelationResetModelStub extends Illuminate \Database \Eloquent \Model {}
69+ class EloquentRelationResetModelStub extends Illuminate \Database \Eloquent \Model {
70+ //Override method call which would normally go through __call()
71+ public function getQuery ()
72+ {
73+ return $ this ->newQuery ()->getQuery ();
74+ }
75+ }
4376
4477
4578class EloquentRelationResetStub extends Illuminate \Database \Eloquent \Builder {
@@ -51,3 +84,11 @@ public function getModel() { return new EloquentRelationResetModelStub; }
5184class EloquentRelationQueryStub extends Illuminate \Database \Query \Builder {
5285 public function __construct () {}
5386}
87+
88+ class EloquentRelationStub extends \Illuminate \Database \Eloquent \Relations \Relation {
89+ public function addConstraints () {}
90+ public function addEagerConstraints (array $ models ) {}
91+ public function initRelation (array $ models , $ relation ) {}
92+ public function match (array $ models , \Illuminate \Database \Eloquent \Collection $ results , $ relation ) {}
93+ public function getResults () {}
94+ }
0 commit comments