MorphToTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 79
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testMorphDefaultFieldsGeneration() 0 6 1
A testMorphCustomFieldsGeneration() 0 7 1
A testGetEagerQuery() 0 28 2
A testGetEagerQueryType() 0 32 1
A setUpCompleteRelation() 0 20 1
A testGetResults() 0 8 1
A testGetWithClass() 0 12 1
A testGetWithClassWithoutItem() 0 6 1
1
<?php
2
3
namespace Nip\Records\Tests\Relations;
4
5
use Mockery as m;
6
use Nip\Records\Collections\Collection;
7
use Nip\Records\Locator\ModelLocator;
8
use Nip\Records\Record;
9
use Nip\Records\RecordManager;
10
use Nip\Records\Relations\Exceptions\ModelNotLoadedInRelation;
11
use Nip\Records\Relations\MorphTo;
12
use Nip\Records\Tests\AbstractTest;
13
use Nip\Records\Tests\Fixtures\Records\Books\Books;
14
use Nip\Records\Tests\Fixtures\Records\Shelves\Shelves;
15
16
/**
17
 * Class MorphToTest
18
 * @package Nip\Records\Tests\Relations
19
 *
20
 * @property MorphTo $object
21
 */
22
class MorphToTest extends AbstractTest
23
{
24
    public function testMorphDefaultFieldsGeneration()
25
    {
26
        $relation = new MorphTo();
27
        self::assertEquals('parent', $relation->getMorphPrefix());
28
        self::assertEquals('parent_id', $relation->getFK());
29
        self::assertEquals('parent_type', $relation->getMorphTypeField());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $relation->getMorphTypeField() targeting Nip\Records\Relations\MorphTo::getMorphTypeField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
30
    }
31
32
    public function testMorphCustomFieldsGeneration()
33
    {
34
        $relation = new MorphTo();
35
        $relation->addParams(['morphPrefix' => 'item']);
36
        self::assertEquals('item', $relation->getMorphPrefix());
37
        self::assertEquals('item_id', $relation->getFK());
38
        self::assertEquals('item_type', $relation->getMorphTypeField());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $relation->getMorphTypeField() targeting Nip\Records\Relations\MorphTo::getMorphTypeField() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
39
    }
40
41
    /**
42
     * @throws ModelNotLoadedInRelation
43
     */
44
    public function testGetWithClassWithoutItem()
45
    {
46
        $relation = new MorphTo();
47
        $relation->setName('test');
48
        $this->expectException(ModelNotLoadedInRelation::class);
49
        $relation->getWithClass();
50
    }
51
52
    /**
53
     * @throws ModelNotLoadedInRelation
54
     */
55
    public function testGetWithClass()
56
    {
57
        $relation = new MorphTo();
58
59
        $article = new Record();
60
        $relation->setItem($article);
61
62
        $article->writeData(['parent_id' => 3, 'parent_type' => 'users']);
63
        self::assertEquals('users', $relation->getWithClass());
64
65
        $article->writeData(['parent_id' => 3, 'parent_type' => 'book']);
66
        self::assertEquals('books', $relation->getWithClass());
67
    }
68
69
    public function testGetResults()
70
    {
71
        $this->setUpCompleteRelation();
72
73
        $user = $this->object->getResults();
74
75
        self::assertInstanceOf(Record::class, $user);
76
        self::assertSame(3, $user->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Nip\Records\AbstractModels\Record. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The property id does not seem to exist on Nip\Records\Collections\Collection.
Loading history...
77
    }
78
79
    public function testGetEagerQuery()
80
    {
81
        ModelLocator::instance()->getConfiguration()->addNamespace('Nip\Records\Tests\Fixtures\Records');
82
83
        $relation = new MorphTo();
84
        $relation->setName('Book');
85
86
        $books = ModelLocator::get('books');
87
88
        $users = new RecordManager();
89
        $users->setPrimaryKey('id');
90
        $relation->setManager($users);
91
92
        $collection = new Collection();
93
94
        foreach ([3, 4] as $id) {
95
            $user = new Record();
96
            $user->parent_type = 'books';
0 ignored issues
show
Bug Best Practice introduced by
The property parent_type does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
97
            $user->parent_id = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property parent_id does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
98
            $user->setManager($users);
99
            $collection->add($user);
100
        }
101
102
        static::assertEquals('parent_id', $relation->getFK());
103
        static::assertEquals([3, 4], $relation->getEagerFkList($collection));
104
        static::assertEquals(
105
            'SELECT `books`.* FROM `books` WHERE id IN (3, 4)',
106
            $relation->getEagerQueryType($collection, $books)->getString()
0 ignored issues
show
Bug introduced by
The method getString() does not exist on MongoDB\Driver\Query. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $relation->getEagerQueryType($collection, $books)->/** @scrutinizer ignore-call */ getString()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        );
108
    }
109
110
    public function testGetEagerQueryType()
111
    {
112
        ModelLocator::instance()->getConfiguration()->addNamespace('Nip\Records\Tests\Fixtures\Records');
113
114
        $relation = new MorphTo();
115
116
        $users = new RecordManager();
117
        $users->setPrimaryKey('id');
118
        $relation->setManager($users);
119
120
        $collection = new Collection();
121
122
        $user = new Record();
123
        $user->parent_type = 'books';
0 ignored issues
show
Bug Best Practice introduced by
The property parent_type does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
124
        $user->parent_id = 3;
0 ignored issues
show
Bug Best Practice introduced by
The property parent_id does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
125
        $user->setManager($users);
126
        $collection->add($user);
127
128
        $user = new Record();
129
        $user->parent_type = 'shelves';
130
        $user->parent_id = 4;
131
        $user->setManager($users);
132
        $collection->add($user);
133
134
        static::assertEquals(
135
            'SELECT `books`.* FROM `books` WHERE id IN (3)',
136
            $relation->getEagerQueryType($collection, new Books())->getString()
137
        );
138
139
        static::assertEquals(
140
            'SELECT `shelves`.* FROM `shelves` WHERE id IN (4)',
141
            $relation->getEagerQueryType($collection, new Shelves())->getString()
142
        );
143
    }
144
145
146
    protected function setUpCompleteRelation()
147
    {
148
        $this->object = new MorphTo();
149
        $this->object->setName('User');
150
151
        $user = new Record();
152
        $user->id = 3;
153
154
        $users = m::namedMock('Users', 'Nip\Records\RecordManager')->makePartial();
155
        $users->setPrimaryKey('id');
156
        $users->shouldReceive('instance')->andReturnSelf();
157
        $users->shouldReceive('findByField')->andReturn(new Collection([$user]));
158
159
        ModelLocator::set('users', $users);
0 ignored issues
show
Bug introduced by
$users of type Mockery\Mock is incompatible with the type Nip\Records\AbstractModels\RecordManager expected by parameter $entityManager of Nip\Records\Locator\ModelLocator::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        ModelLocator::set('users', /** @scrutinizer ignore-type */ $users);
Loading history...
160
161
        $article = new Record();
162
        $article->parent_id = 3;
0 ignored issues
show
Bug Best Practice introduced by
The property parent_id does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
163
        $article->parent_type = 'users';
0 ignored issues
show
Bug Best Practice introduced by
The property parent_type does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
164
165
        $this->object->setItem($article);
166
    }
167
}
168