BelongsToTest::testGetEagerQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 31
rs 9.584
c 0
b 0
f 0
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\BelongsTo;
11
use Nip\Records\Tests\Fixtures\Records\Books\Books;
12
13
/**
14
 * Class BelongsToTest
15
 * @package Nip\Records\Tests\Relations
16
 */
17
class BelongsToTest extends \Nip\Records\Tests\AbstractTest
18
{
19
20
    /**
21
     * @var BelongsTo
22
     */
23
    protected $object;
24
25
    public function testInitResults()
26
    {
27
        static::assertSame($this->_user, $this->object->getResults());
28
    }
29
30
    public function testGetEagerQuery()
31
    {
32
        ModelLocator::instance()->getConfiguration()->addNamespace('Nip\Records\Tests\Fixtures\Records');
33
34
        $relation = new BelongsTo();
35
        $relation->setName('Books');
36
37
        static::assertInstanceOf(Books::class, $relation->getWith());
38
        $collection = new Collection();
39
40
        static::assertEquals(
41
            'SELECT `books`.* FROM `books` WHERE id IN ()',
42
            $relation->getEagerQuery($collection)->getString()
43
        );
44
45
        $users = new RecordManager();
46
        $users->setPrimaryKey('id');
47
        $relation->setManager($users);
48
49
        foreach ([3, 4] as $id) {
50
            $user = new Record();
51
            $user->id_book = $id;
0 ignored issues
show
Bug Best Practice introduced by
The property id_book does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
52
            $user->setManager($users);
53
            $collection->add($user);
54
        }
55
56
        static::assertEquals('id_book', $relation->getFK());
57
        static::assertEquals([3, 4], $relation->getEagerFkList($collection));
58
        static::assertEquals(
59
            'SELECT `books`.* FROM `books` WHERE id IN (3, 4)',
60
            $relation->getEagerQuery($collection)->getString()
61
        );
62
    }
63
64
    protected function setUp(): void
65
    {
66
        parent::setUp();
67
68
        $this->object = new BelongsTo();
69
        $this->object->setName('User');
70
        $this->object->addParams(['withPK' => 'id_custom']);
71
72
        $this->_user = new Record();
0 ignored issues
show
Bug Best Practice introduced by
The property _user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
74
        $users = m::namedMock('Users', 'Nip\Records\RecordManager')->makePartial();
75
        $users->setPrimaryKey('id');
76
        $users->setPrimaryFK('id_user');
77
78
        $users->shouldReceive('instance')->andReturnSelf();
79
        $users->shouldReceive('findByField')
80
            ->withArgs(['id_custom', 3])
81
            ->andReturn(new Collection([$this->_user]));
82
83
//        m::namedMock('User', 'Record');
84
85
        $this->object->setWith($users);
86
        $article = new Record();
87
        $article->id_user = 3;
0 ignored issues
show
Bug Best Practice introduced by
The property id_user does not exist on Nip\Records\Record. Since you implemented __set, consider adding a @property annotation.
Loading history...
88
        $this->object->setItem($article);
89
    }
90
}
91