| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Gurucomkz\EagerLoading\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Gurucomkz\EagerLoading\EagerLoadingException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Gurucomkz\EagerLoading\Tests\Models\Drink; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Gurucomkz\EagerLoading\Tests\Models\Music; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Gurucomkz\EagerLoading\Tests\Models\Origin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Gurucomkz\EagerLoading\Tests\Models\Player; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Gurucomkz\EagerLoading\Tests\Models\Supporter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Gurucomkz\EagerLoading\Tests\Models\Team; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Gurucomkz\EagerLoading\Tests\Models\TeamSupporter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class EagerloadTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected static $fixture_file = 'fixtures.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected static $extra_dataobjects = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         Origin::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         Music::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         Team::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         Player::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         Drink::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         Supporter::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         TeamSupporter::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testNoTrait() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assertFalse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             method_exists(Drink::class, 'addEagerRelation'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'Drink Class should not have "addEagerRelation" method' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $drinks = Drink::get()->with('Players')->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             foreach ($drinks as $drink) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $drink->Players()->map()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } catch (EagerLoadingException $th) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->fail('No EagerLoadingException raised'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function testWrongNames() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $record = Drink::get()->with('Bubbles')->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $this->assertNotNull($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } catch (\Exception $th) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->fail('Wrong names should fail silently'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function testHasOne() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $expectedQueries = 4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $players = Player::get()->with('Team'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         foreach ($players as $player) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $player->Team->Title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function testHasMany() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $expectedQueries = 3; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $teams = Team::get()->with('Players'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         foreach ($teams as $team) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $team->Players()->map()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     public function testManyManyPlain() | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $expectedQueries = 4; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $players = Player::get()->with('Listens'); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         foreach ($players as $player) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             $player->Listens()->map()->toArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             // print_r($music); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function testManyManyThrough() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $expectedQueries = 4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $teams = Team::get()->with('Supporters'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         foreach ($teams as $team) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $supporters = $team->Supporters()->map()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             // print_r(ProxyDBCounterExtension::getQueries()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             switch($team->Title){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 case 'The Hurricanes': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     $this->assertCount(3, $supporters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $this->assertTrue(in_array('Supporter 1', $supporters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     $this->assertTrue(in_array('Supporter 3', $supporters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     $this->assertTrue(in_array('Supporter 5', $supporters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 case 'The Crusaders': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     $this->assertCount(1, $supporters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     $this->assertTrue(in_array('Supporter 1', $supporters)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 case 'The Bears': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     $this->assertCount(5, $supporters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function testBelongsToManyMany() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $expectedQueries = 4; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $music = Music::get()->with('Players'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         foreach ($music as $genre) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $genre->Players()->map()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         // print_r(ProxyDBCounterExtension::getQueries()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     public function test2HasOne() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $expectedQueries = 5; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $players = Player::get()->with(['Team', 'Origin']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         foreach ($players as $player) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             $player->Team->Title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $player->Origin->Title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         // print_r(ProxyDBCounterExtension::getQueries()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     public function testHasOneChain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $expectedQueries = 7; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $players = Player::get()->with(['Team.Origin']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         foreach ($players as $player) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $player->Team->Origin->Title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         // print_r(ProxyDBCounterExtension::getQueries()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     public function testChainOneMany() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         $expectedQueries = 6; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         ProxyDBCounterExtension::resetQueries(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $players = Player::get()->with(['Team.Players']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         foreach ($players as $player) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             $player->Team->Players()->map()->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         $post_fetch_count = ProxyDBCounterExtension::getQueriesCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $this->assertEquals($pre_fetch_count + $expectedQueries, $post_fetch_count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  |