OrmModelsTest::testTeamOrm()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 30
rs 8.8571
cc 2
eloc 23
nc 2
nop 0
1
<?php
2
3
4
/**
5
 * Class OrmModelsTest
6
 */
7
class OrmModelsTest 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 OrmModels
13
     * @group PlayerOrm
14
     */
15
    public function testPlayerOrmGetSet()
16
    {
17
        $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller();
18
        $playerM = $rndFiller->getPlayer();
19
        $arrayPl = $playerM->toArray();
20
        $playerO = \App\Lib\DsManager\Models\Orm\Player::create($arrayPl);
21
        $this->assertNotEmpty($playerO);
22
23
        $newPlayer = \App\Lib\DsManager\Models\Player::fromArray($playerO->toArray());
24
        $this->assertNotEmpty($newPlayer);
25
    }
26
27
    /**
28
     * @group OrmModels
29
     * @group CoachOrm
30
     */
31
    public function testCoachOrmGetSet()
32
    {
33
        $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller();
34
        $coach = $rndFiller->getCoach();
35
        $arrayPl = $coach->toArray();
36
        $coachO = \App\Lib\DsManager\Models\Orm\Coach::create($arrayPl);
37
        $this->assertNotEmpty($coachO);
38
39
        $newCoach = \App\Lib\DsManager\Models\Coach::fromArray($coachO->toArray());
40
        $this->assertNotEmpty($newCoach);
41
    }
42
43
    /**
44
     * @group OrmModels
45
     * @group TeamOrm
46
     */
47
    public function testTeamOrm()
48
    {
49
        $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller();
50
        $team = $rndFiller->getTeam($rndFiller->getLocale());
51
        $teamArray = $team->toArray();
52
        $this->assertNotEmpty($team);
53
        $teamO = \App\Lib\DsManager\Models\Orm\Team::create($teamArray);
54
        $this->assertNotEmpty($teamArray);
55
        $this->assertNotEmpty($teamArray['roster']);
56
        foreach ($teamArray['roster'] as $player) {
57
            $player['team_id'] = $teamO->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Lib\DsManager\Models\Orm\Team>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
            $playerO = \App\Lib\DsManager\Models\Orm\Player::create($player);
59
            $this->assertNotEmpty($playerO);
60
        }
61
        $teamArray['coach']['team_id'] = $teamO->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Lib\DsManager\Models\Orm\Team>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
        $coachO = \App\Lib\DsManager\Models\Orm\Coach::create($teamArray['coach']);
63
        $this->assertNotEmpty($coachO);
64
65
        $this->assertNotEmpty(
66
            \App\Lib\DsManager\Models\Orm\Team::with(
67
                'roster'
68
            )->with(
69
                'coach'
70
            )->where(
71
                [
72
                    'id' => $teamO->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Lib\DsManager\Models\Orm\Team>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
                ]
74
            )->get()->toArray()
75
        );
76
    }
77
78
    /**
79
     * @group Match
80
     * @group createnewmatch
81
     */
82
    public function testCreateNewMatch()
83
    {
84
        $teams = \App\Lib\DsManager\Models\Orm\Team::with(
0 ignored issues
show
Bug introduced by
The method get 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...
85
            'roster',
86
            'coach'
87
        )->get();
88
        $this->assertNotNull($teams);
89
        $teams = $teams->toArray();
90
        $homeIndex = array_rand($teams);
91
        $teamHome = $teams[$homeIndex];
92
        unset($teams[$homeIndex]);
93
        $teamAway = $teams[array_rand($teams)];
94
        $this->assertNotNull($teamHome);
95
        $this->assertNotNull($teamAway);
96
        $matchO = \App\Lib\DsManager\Models\Orm\Match::create(
97
            [
98
                'home_team_id' => $teamHome['id'],
99
                'away_team_id' => $teamAway['id']
100
            ]
101
        );
102
        $this->assertNotNull($matchO);
103
        $matchNew = \App\Lib\DsManager\Models\Orm\Match::complete()->where('id', $matchO->id)->first();
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Lib\DsManager\Models\Orm\Match>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
The method complete() does not exist on App\Lib\DsManager\Models\Orm\Match. Did you maybe mean scopeComplete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
104
        $this->assertNotNull($matchNew);
105
        $match = \App\Lib\DsManager\Models\Match::fromArray($matchNew->toArray());
106
        $this->assertNotNull($match);
107
    }
108
109
    /**
110
     * @group Stats
111
     */
112
    public function testGetStats()
113
    {
114
        $match = \App\Lib\DsManager\Models\Orm\Match::where(
115
            [
116
                'simulated' => false
117
            ]
118
        )->whereNotNull(
119
            'league_round_id'
120
        )->get()->random(1);
121
        $this->assertNotEmpty($match);
122
        $result = \App\Lib\DsManager\Helpers\MatchSimulator::simulateRound($match->league_round_id);
123
        $this->assertNotEmpty($result);
124
        $players = \App\Lib\DsManager\Models\Orm\Player::getBest();
125
        $this->assertNotEmpty($players);
126
        $teams = \App\Lib\DsManager\Models\Orm\Team::getBest();
127
        $this->assertNotEmpty($teams);
128
    }
129
}
130