1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Tests\Relations; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Nip\Records\Locator\ModelLocator; |
7
|
|
|
use Nip\Records\Record; |
8
|
|
|
use Nip\Records\Relations\Exceptions\ModelNotLoadedInRelation; |
9
|
|
|
use Nip\Records\Relations\MorphTo; |
10
|
|
|
use Nip\Records\Tests\AbstractTest; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MorphToTest |
14
|
|
|
* @package Nip\Records\Tests\Relations |
15
|
|
|
* |
16
|
|
|
* @property MorphTo $object |
17
|
|
|
*/ |
18
|
|
|
class MorphToTest extends AbstractTest |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
View Code Duplication |
public function testMorphDefaultFieldsGeneration() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$relation = new MorphTo(); |
24
|
|
|
self::assertEquals('parent', $relation->getMorphPrefix()); |
25
|
|
|
self::assertEquals('parent_id', $relation->getFK()); |
26
|
|
|
self::assertEquals('parent_type', $relation->getMorphTypeField()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testMorphCustomFieldsGeneration() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$relation = new MorphTo(); |
32
|
|
|
$relation->addParams(['morphPrefix' => 'parent']); |
33
|
|
|
self::assertEquals('parent', $relation->getMorphPrefix()); |
34
|
|
|
self::assertEquals('parent_id', $relation->getFK()); |
35
|
|
|
self::assertEquals('parent_type', $relation->getMorphTypeField()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @throws ModelNotLoadedInRelation |
40
|
|
|
*/ |
41
|
|
|
public function testGetWithClassWithoutItem() |
42
|
|
|
{ |
43
|
|
|
$relation = new MorphTo(); |
44
|
|
|
$relation->setName('test'); |
45
|
|
|
$this->expectException(ModelNotLoadedInRelation::class); |
46
|
|
|
$relation->getWithClass(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws ModelNotLoadedInRelation |
51
|
|
|
*/ |
52
|
|
|
public function testGetWithClass() |
53
|
|
|
{ |
54
|
|
|
$relation = new MorphTo(); |
55
|
|
|
|
56
|
|
|
$article = new Record(); |
57
|
|
|
$relation->setItem($article); |
58
|
|
|
|
59
|
|
|
$article->writeData(['parent_id' => 3, 'parent_type' => 'users']); |
60
|
|
|
self::assertEquals('users', $relation->getWithClass()); |
61
|
|
|
|
62
|
|
|
$article->writeData(['parent_id' => 3, 'parent_type' => 'book']); |
63
|
|
|
self::assertEquals('books', $relation->getWithClass()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetResults() |
67
|
|
|
{ |
68
|
|
|
$this->setUpCompleteRelation(); |
69
|
|
|
|
70
|
|
|
$user = $this->object->getResults(); |
71
|
|
|
|
72
|
|
|
self::assertInstanceOf(Record::class, $user); |
73
|
|
|
self::assertSame(3, $user->id); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
protected function setUpCompleteRelation() |
78
|
|
|
{ |
79
|
|
|
$this->object = new MorphTo(); |
80
|
|
|
$this->object->setName('User'); |
81
|
|
|
|
82
|
|
|
$user = new Record(); |
83
|
|
|
$user->id = 3; |
84
|
|
|
|
85
|
|
|
$users = m::namedMock('Users', 'Nip\Records\RecordManager')->shouldDeferMissing() |
86
|
|
|
->shouldReceive('instance')->andReturnSelf() |
87
|
|
|
->shouldReceive('findOne')->with(3)->andReturn($user) |
88
|
|
|
->getMock(); |
89
|
|
|
|
90
|
|
|
ModelLocator::set('users', $users); |
91
|
|
|
|
92
|
|
|
$article = new Record(); |
93
|
|
|
$article->parent_id = 3; |
|
|
|
|
94
|
|
|
$article->parent_type = 'users'; |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->object->setItem($article); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.