Completed
Push — master ( c5047e...3a177f )
by Gabriel
03:52
created

MorphToTest::setUpCompleteRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
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()
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...
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()
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...
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;
0 ignored issues
show
Documentation introduced by
The property parent_id does not exist on object<Nip\Records\Record>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
        $article->parent_type = 'users';
0 ignored issues
show
Documentation introduced by
The property parent_type does not exist on object<Nip\Records\Record>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
96
        $this->object->setItem($article);
97
    }
98
}
99