1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Tests\Relations; |
4
|
|
|
|
5
|
|
|
use Nip\Records\Locator\ModelLocator; |
6
|
|
|
use Nip\Records\Record; |
7
|
|
|
use Nip\Records\RecordManager; |
8
|
|
|
use Nip\Records\Relations\MorphMany; |
9
|
|
|
use Nip\Records\Tests\AbstractTest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class MorphManyTest |
13
|
|
|
* @package Nip\Records\Tests\Relations |
14
|
|
|
*/ |
15
|
|
|
class MorphManyTest extends AbstractTest |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function testGetMorphClassWithGenericManager() |
19
|
|
|
{ |
20
|
|
|
$relation = new MorphMany(); |
21
|
|
|
$manager = new RecordManager(); |
22
|
|
|
$relation->setManager($manager); |
23
|
|
|
|
24
|
|
|
self::assertEquals('nip_records', $relation->getMorphValue()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testGetQuery() |
28
|
|
|
{ |
29
|
|
|
ModelLocator::instance()->getConfiguration()->addNamespace('Nip\Records\Tests\Fixtures\Records'); |
30
|
|
|
|
31
|
|
|
$relation = new MorphMany(); |
32
|
|
|
$relation->setName('Books'); |
33
|
|
|
|
34
|
|
|
$users = new RecordManager(); |
35
|
|
|
$users->setPrimaryKey('id'); |
36
|
|
|
|
37
|
|
|
$user = new Record(); |
38
|
|
|
$user->id = 3; |
39
|
|
|
$user->setManager($users); |
40
|
|
|
$relation->setItem($user); |
41
|
|
|
|
42
|
|
|
self::assertEquals( |
43
|
|
|
"SELECT `books`.* FROM `books` WHERE parent_type = 'nip_records' AND `parent_id` = 3", |
44
|
|
|
$relation->getQuery()->getString() |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
View Code Duplication |
public function testMorphDefaultFieldsGeneration() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$relation = new MorphMany(); |
52
|
|
|
self::assertEquals('parent', $relation->getMorphPrefix()); |
53
|
|
|
self::assertEquals('parent_id', $relation->getFK()); |
54
|
|
|
self::assertEquals('parent_type', $relation->getMorphTypeField()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function testMorphCustomFieldsGeneration() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$relation = new MorphMany(); |
60
|
|
|
$relation->addParams(['morphPrefix' => 'item']); |
61
|
|
|
self::assertEquals('item', $relation->getMorphPrefix()); |
62
|
|
|
self::assertEquals('item_id', $relation->getFK()); |
63
|
|
|
self::assertEquals('item_type', $relation->getMorphTypeField()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
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.