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()); |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
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'; |
|
|
|
|
97
|
|
|
$user->parent_id = $id; |
|
|
|
|
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() |
|
|
|
|
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'; |
|
|
|
|
124
|
|
|
$user->parent_id = 3; |
|
|
|
|
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); |
|
|
|
|
160
|
|
|
|
161
|
|
|
$article = new Record(); |
162
|
|
|
$article->parent_id = 3; |
|
|
|
|
163
|
|
|
$article->parent_type = 'users'; |
|
|
|
|
164
|
|
|
|
165
|
|
|
$this->object->setItem($article); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
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.