Completed
Push — master ( 01ad84...898da4 )
by Vincenzo
02:22
created

ModelsTest::testMatchFromExistingTeams()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 36
Code Lines 28

Duplication

Lines 16
Ratio 44.44 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 36
rs 8.439
cc 6
eloc 28
nc 8
nop 0
1
<?php
2
3
4
/**
5
 * Class ModelsTest
6
 */
7
class ModelsTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
10
11
    /**
12
     * @group Player
13
     */
14
    public function testGetRandomPlayer()
15
    {
16
        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller();
17
        $player = $rndF->getPlayer(null, $rndF->getLocale());
18
        $array = $player->toArray();
19
        $this->assertNotEmpty($array);
20
21
        $newPlayer = \App\Lib\DsManager\Models\Player::fromArray($array);
22
        $this->assertNotEmpty($newPlayer->toArray());
23
    }
24
25
    /**
26
     * @group Coach
27
     */
28
    public function testGetRandomCoach()
29
    {
30
        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller();
31
        $coach = $rndF->getCoach();
32
        $this->assertNotEmpty($coach->toArray());
33
    }
34
35
    /**
36
     * @group Coaches
37
     */
38 View Code Duplication
    public function testGetRandomCoaches()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        foreach (\App\Lib\Helpers\Config::get('generic.localesSmall') as $nat) {
0 ignored issues
show
Bug introduced by
The expression \App\Lib\Helpers\Config:...'generic.localesSmall') of type null is not traversable.
Loading history...
41
            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller($nat);
42
            $coach = $rndF->getCoach();
43
            $this->assertNotEmpty($coach->toArray());
44
        }
45
    }
46
47
    /**
48
     * @group Players
49
     */
50 View Code Duplication
    public function testGetRandomPlayers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        foreach (\App\Lib\Helpers\Config::get('generic.localesSmall') as $nat) {
0 ignored issues
show
Bug introduced by
The expression \App\Lib\Helpers\Config:...'generic.localesSmall') of type null is not traversable.
Loading history...
53
            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller($nat);
54
            $player = $rndF->getPlayer();
55
            $this->assertNotEmpty($player->toArray());
56
        }
57
    }
58
59
    /**
60
     * @group Team
61
     */
62
    public function testGetRandomTeam()
63
    {
64
        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
65
        $team = $rndF->getTeam();
66
        $this->assertNotEmpty($team);
67
        $this->assertNotEmpty($team->name);
68
        $this->assertNotEmpty($team->getAvgSkill());
69
70
        //After Adding a player
71
        $player = $rndF->getPlayer();
72
        $this->assertNotEmpty($player->toArray());
73
        $team->roster[] = $player;
74
        $this->assertNotEmpty($team->getAvgSkill());
75
        $this->assertNotEmpty($team->getAvgAge());
76
77
        $this->assertNotEmpty($team->coach->toArray());
78
79
        $teamArray = $team->toArray();
80
        $this->assertNotEmpty($teamArray);
81
82
        $newTeam = \App\Lib\DsManager\Models\Team::fromArray($teamArray);
83
        $this->assertNotEmpty($newTeam->toArray());
84
85
    }
86
87
    /**
88
     * @group Teams
89
     */
90
    public function testGetRandomTeams()
91
    {
92
        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
93
94
        for ($i = 1; $i <= 20; $i++) {
95
            $team = $rndF->getTeam();
96
            $this->assertNotEmpty($team->name);
97
            $this->assertNotEmpty($team->nationality);
98
            $this->assertNotEmpty($team->getAvgSkill());
99
            $this->assertNotEmpty($team->getAvgAge());
100
        }
101
    }
102
103
    /**
104
     * @group Match
105
     */
106
    public function testGetRandomMatch()
107
    {
108
        for ($i = 1; $i <= 30; $i++) {
109
            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
110
            $spanish = $rndF->getTeam();
111
            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
112
            $italian = $rndF->getTeam();
113
            $this->assertNotEmpty($spanish);
114
            $this->assertNotEmpty($italian);
115
            $this->assertNotEmpty($italian->name);
116
            $this->assertNotEmpty($spanish);
117
            $this->assertNotEmpty($spanish->name);
118
119
            $this->assertNotEmpty($italian->getAvgSkill());
120
            $this->assertNotEmpty($spanish->getAvgSkill());
121
            $result = (new \App\Lib\DsManager\Models\Match($italian, $spanish))->simulate()->toArray();
122
            $this->assertNotEmpty($result);
123
            $this->assertGreaterThanOrEqual(0, $result['goalHome']);
124
            $this->assertGreaterThanOrEqual(0, $result['goalAway']);
125
126
        }
127
    }
128
129
    /**
130
     * @group Match
131
     * @group MatchResult
132
     * @group matchresultoutput
133
     */
134
    public function testMatchFromExistingTeams()
135
    {
136
        $teamHome = 1;
137
        $teamAway = 2;
138
        $homeOrm = \App\Lib\DsManager\Models\Orm\Team::with('roster', 'coach')->where(['id' => $teamHome])->first();
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
139
        $awayOrm = \App\Lib\DsManager\Models\Orm\Team::with('roster', 'coach')->where(['id' => $teamAway])->first();
140
        $home = \App\Lib\DsManager\Models\Team::fromArray($homeOrm->toArray());
141
        $away = \App\Lib\DsManager\Models\Team::fromArray($awayOrm->toArray());
142
143
        $match = new \App\Lib\DsManager\Models\Match($home, $away);
144
        $result = $match->simulate()->toArray();
145
        $this->assertNotEmpty($result);
146
        $this->assertGreaterThanOrEqual(0, $result['goalAway']);
147
        $this->assertGreaterThanOrEqual(0, $result['goalHome']);
148 View Code Duplication
        if ($result['goalHome'] > 0) {
149
            $this->assertNotEmpty($result['info']['scorers']['home']);
150
            foreach ($result['info']['scorers']['home'] as $scorerHome) {
151
                $this->assertEquals($scorerHome->team_id, $teamHome);
152
            }
153
        } else {
154
            $this->assertEmpty($result['info']['scorers']['home']);
155
        }
156 View Code Duplication
        if ($result['goalAway'] > 0) {
157
            $this->assertNotEmpty($result['info']['scorers']['away']);
158
            foreach ($result['info']['scorers']['away'] as $scorerAway) {
159
                $this->assertEquals($scorerAway->team_id, $teamAway);
160
            }
161
        } else {
162
            $this->assertEmpty($result['info']['scorers']['away']);
163
        }
164
        if ($result['goalHome'] == $result['goalAway']) {
165
            $this->assertTrue($result['info']['isDraw']);
166
        } else {
167
            $this->assertFalse($result['info']['isDraw']);
168
        }
169
    }
170
171
    /**
172
     * @group Matches
173
     */
174
    public function testGetRandomMatchesOneTeam()
175
    {
176
        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
177
        $myTeam = $rndF->getTeam();
178
        $win = 0;
179
        $lost = 0;
180
        $draw = 0;
181
182
        for ($i = 1; $i <= 2; $i++) {
183
184
            $randomLocale = \App\Lib\Helpers\Config::get('generic.localesSmall');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $randomLocale is correct as \App\Lib\Helpers\Config:...'generic.localesSmall') (which targets App\Lib\Helpers\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
185
            shuffle($randomLocale);
186
            $randomLocale = $randomLocale[0];
187
188
            $rndF = new \App\Lib\DsManager\Helpers\RandomFiller($randomLocale);
189
            $opponent = $rndF->getTeam();
190
            $result = (new \App\Lib\DsManager\Models\Match($opponent, $myTeam))->simulate()->toArray();
191
            $this->assertNotEmpty($result);
192
            $result = $result['info'];
193
            if (!$result['isDraw']) {
194
                if ($result['winner']['name'] == $myTeam->name) {
195
                    $win++;
196
                } else {
197
                    $lost++;
198
                }
199
            } else {
200
                $draw++;
201
            }
202
        }
203
        $this->assertGreaterThanOrEqual(0, $win);
204
        $this->assertGreaterThanOrEqual(0, $lost);
205
        $this->assertGreaterThanOrEqual(0, $draw);
206
    }
207
208
    /**
209
     * @group Module
210
     */
211
    public function testModule()
212
    {
213
        $rndF = new \App\Lib\DsManager\Helpers\RandomFiller("it_IT");
214
        $team = $rndF->getTeam();
215
216
        $modules = \App\Lib\Helpers\Config::get("modules.modules");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $modules is correct as \App\Lib\Helpers\Config::get('modules.modules') (which targets App\Lib\Helpers\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
217
        $modules = array_keys($modules);
218
        foreach ($modules as $mod) {
219
            $module = new \App\Lib\DsManager\Models\Module($mod);
220
            $this->assertNotEmpty($module);
221
            $this->assertNotNull($module->isDefensive());
222
            $this->assertNotNull($module->isBalanced());
223
            $this->assertNotNull($module->isOffensive());
224
            $this->assertTrue(is_array($module->getRoleNeeded()));
225
        }
226
        $this->assertGreaterThan(0, $team->playersPerRoleArray());
227
    }
228
229
}
230