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