Passed
Push — main ( 9cb66d...0deb5f )
by Sergey
02:19
created

EagerloadTest::testManyMany()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 15
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Gurucomkz\EagerLoading\Tests;
4
5
use Gurucomkz\EagerLoading\ProxyDBCounterExtension;
6
use Gurucomkz\EagerLoading\Tests\Models\Music;
7
use Gurucomkz\EagerLoading\Tests\Models\Origin;
8
use Gurucomkz\EagerLoading\Tests\Models\Player;
9
use Gurucomkz\EagerLoading\Tests\Models\Team;
10
use SilverStripe\Dev\SapphireTest;
11
12
class EagerloadTest extends SapphireTest
13
{
14
15
    protected static $fixture_file = 'fixtures.yml';
16
17
    protected static $extra_dataobjects = [
18
        Origin::class,
19
        Music::class,
20
        Team::class,
21
        Player::class,
22
    ];
23
24
    public function testHasOne()
25
    {
26
        $expectedQueries = 4;
27
        ProxyDBCounterExtension::resetQueries();
28
        $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
29
30
        $players = Player::get()->with('Team');
31
32
        foreach($players as $player) {
33
            $team = $player->Team->Title;
0 ignored issues
show
Unused Code introduced by
The assignment to $team is dead and can be removed.
Loading history...
34
        }
35
        $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
36
37
        $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
38
    }
39
40
    public function testHasMany()
41
    {
42
        $expectedQueries = 3;
43
        ProxyDBCounterExtension::resetQueries();
44
        $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
45
46
        $teams = Team::get()->with('Players');
47
48
        foreach($teams as $team) {
49
            $players = $team->Players()->map()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $players is dead and can be removed.
Loading history...
50
        }
51
        $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
52
53
        $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
54
    }
55
56
    public function testManyMany()
57
    {
58
        $expectedQueries = 4;
59
        ProxyDBCounterExtension::resetQueries();
60
        $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
61
62
        $players = Player::get()->with('Listens');
63
64
        foreach($players as $player) {
65
            $music = $player->Listens()->map()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $music is dead and can be removed.
Loading history...
66
            // print_r($music);
67
        }
68
        $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
69
70
        $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
71
    }
72
73
    // public function testBelongsToManyMany()
74
    // {
75
    //     $expectedQueries = 4;
76
    //     ProxyDBCounterExtension::resetQueries();
77
    //     $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
78
79
    //     $music = Music::get()->with('Players');
80
81
    //     foreach($music as $genre) {
82
    //         $players = $genre->Players()->map()->toArray();
83
    //     }
84
    //     $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
85
86
    //     // print_r(ProxyDBCounterExtension::getQueries());
87
88
    //     $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
89
    // }
90
91
    public function test2HasOne()
92
    {
93
        $expectedQueries = 5;
94
        ProxyDBCounterExtension::resetQueries();
95
        $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
96
97
        $players = Player::get()->with(['Team','Origin']);
98
99
        foreach($players as $player) {
100
            $team = $player->Team->Title;
0 ignored issues
show
Unused Code introduced by
The assignment to $team is dead and can be removed.
Loading history...
101
            $origin = $player->Origin->Title;
0 ignored issues
show
Unused Code introduced by
The assignment to $origin is dead and can be removed.
Loading history...
102
        }
103
        $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
104
105
        // print_r(ProxyDBCounterExtension::getQueries());
106
107
        $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
108
    }
109
110
    public function testHasOneChain()
111
    {
112
        $expectedQueries = 7;
113
        ProxyDBCounterExtension::resetQueries();
114
        $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
115
116
        $players = Player::get()->with(['Team.Origin']);
117
118
        foreach($players as $player) {
119
            $team = $player->Team->Origin->Title;
0 ignored issues
show
Unused Code introduced by
The assignment to $team is dead and can be removed.
Loading history...
120
        }
121
        $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
122
123
        // print_r(ProxyDBCounterExtension::getQueries());
124
125
        $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
126
    }
127
128
    public function testChainOneMany()
129
    {
130
        $expectedQueries = 6;
131
        ProxyDBCounterExtension::resetQueries();
132
        $pre_fetch_count = ProxyDBCounterExtension::getQueriesCount();
133
134
        $players = Player::get()->with(['Team.Players']);
135
136
        foreach($players as $player) {
137
            $team = $player->Team->Players()->map()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $team is dead and can be removed.
Loading history...
138
        }
139
        $post_fetch_count = ProxyDBCounterExtension::getQueriesCount();
140
141
        $this->assertEquals($pre_fetch_count + $expectedQueries,$post_fetch_count);
142
    }
143
144
}
145