Completed
Push — master ( 0ba58c...dd341e )
by Gabriel
07:12
created

MorphManyTest::testSaveResult()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 24
rs 8.9713
c 1
b 1
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Nip\Records\Tests\Relations;
4
5
use Nip\Records\Collections\Collection;
6
use Nip\Records\Locator\ModelLocator;
7
use Nip\Records\Record;
8
use Nip\Records\RecordManager;
9
use Nip\Records\Relations\MorphMany;
10
use Nip\Records\Tests\AbstractTest;
11
use mockery as m;
12
use Nip\Records\Tests\Fixtures\Records\Books\Book;
13
14
/**
15
 * Class MorphManyTest
16
 * @package Nip\Records\Tests\Relations
17
 */
18
class MorphManyTest extends AbstractTest
19
{
20
21
    public function testGetMorphClassWithGenericManager()
22
    {
23
        $relation = new MorphMany();
24
        $manager = new RecordManager();
25
        $relation->setManager($manager);
26
27
        self::assertEquals('nip_records', $relation->getMorphValue());
28
    }
29
30
    public function testGetQuery()
31
    {
32
        ModelLocator::instance()->getConfiguration()->addNamespace('Nip\Records\Tests\Fixtures\Records');
33
34
        $relation = new MorphMany();
35
        $relation->setName('Books');
36
37
        $users = new RecordManager();
38
        $users->setPrimaryKey('id');
39
40
        $user = new Record();
41
        $user->id = 3;
42
        $user->setManager($users);
43
        $relation->setItem($user);
44
45
        self::assertEquals(
46
            "SELECT `books`.* FROM `books` WHERE parent_type = 'nip_records' AND `parent_id` = 3",
47
            $relation->getQuery()->getString()
48
        );
49
    }
50
51
    public function testGetEagerQuery()
52
    {
53
        ModelLocator::instance()->getConfiguration()->addNamespace('Nip\Records\Tests\Fixtures\Records');
54
55
        $relation = new MorphMany();
56
        $relation->setName('Books');
57
58
        $collection = new Collection();
59
60
        self::assertEquals(
61
            "SELECT `books`.* FROM `books` WHERE parent_type = '' AND parent_id IN ()",
62
            $relation->getEagerQuery($collection)->getString()
63
        );
64
65
        $users = new RecordManager();
66
        $users->setPrimaryKey('id');
67
        $relation->setManager($users);
68
69 View Code Duplication
        foreach ([3, 4] as $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
70
            $user = new Record();
71
            $user->id = $id;
72
            $user->setManager($users);
73
            $collection->add($user);
74
        }
75
76
        self::assertEquals(
77
            "SELECT `books`.* FROM `books` WHERE parent_type = 'nip_records' AND parent_id IN (3, 4)",
78
            $relation->getEagerQuery($collection)->getString()
79
        );
80
    }
81
82
    public function testSaveResult()
83
    {
84
        $relation = new MorphMany();
85
86
        $relation->setName('Books');
87
88
        $users = new RecordManager();
89
        $users->setPrimaryKey('id');
90
91
        $user = new Record();
92
        $user->id = 3;
93
        $user->setManager($users);
94
        $relation->setItem($user);
95
        $relation->setManager($users);
96
97
        $book = m::mock(Book::class)->shouldDeferMissing()
98
            ->shouldReceive('saveRecord')->andReturn(true)
99
            ->getMock();
100
101
        $relation->saveResult($book);
102
103
        self::assertEquals(3, $book->parent_id);
104
        self::assertEquals('nip_records', $book->parent_type);
105
    }
106
107
108 View Code Duplication
    public function testMorphDefaultFieldsGeneration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
109
    {
110
        $relation = new MorphMany();
111
        self::assertEquals('parent', $relation->getMorphPrefix());
112
        self::assertEquals('parent_id', $relation->getFK());
113
        self::assertEquals('parent_type', $relation->getMorphTypeField());
114
    }
115
116 View Code Duplication
    public function testMorphCustomFieldsGeneration()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
117
    {
118
        $relation = new MorphMany();
119
        $relation->addParams(['morphPrefix' => 'item']);
120
        self::assertEquals('item', $relation->getMorphPrefix());
121
        self::assertEquals('item_id', $relation->getFK());
122
        self::assertEquals('item_type', $relation->getMorphTypeField());
123
    }
124
}
125