Completed
Push — master ( 662b17...55cf61 )
by Oliver
10:02
created

MetableTest::testIsDirtyMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace BoxedCode\Tests\Eloquent\Meta;
4
5
use BoxedCode\Eloquent\Meta\HasMeta;
6
use BoxedCode\Eloquent\Meta\MetaItem;
7
use BoxedCode\Tests\Eloquent\Meta\Stubs\MetableModel;
8
9
class MetableTest extends AbstractTestCase
10
{
11
    public function testMeta()
12
    {
13
        $m = $this->getMetableStub();
14
15
        $this->assertInstanceOf(HasMeta::class, $m->meta());
16
    }
17
18
    public function testIsDirtyParent()
19
    {
20
        $m = $this->getMetableStub();
21
22
        $m->foo = 'bar';
0 ignored issues
show
Documentation introduced by
The property foo does not exist on object<BoxedCode\Tests\E...eta\Stubs\MetableModel>. 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...
23
24
        $this->assertTrue($m->isDirty());
25
26
        $this->assertTrue($m->isDirty('foo'));
27
    }
28
29 View Code Duplication
    public function testIsDirtyMeta()
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
        $m = $this->getMetableStub();
32
33
        $item = new MetaItem(['key' => 'foo', 'value' => 'bar']);
34
35
        $m->meta->add($item);
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Tests\E...eta\Stubs\MetableModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
36
37
        $this->assertTrue($m->isDirty());
38
39
        $this->assertTrue($m->isDirty('value'));
40
    }
41
42
    public function testGetMetaItemClassNameDefault()
43
    {
44
        $m = $this->getMetableStub();
45
46
        $this->assertEquals(MetaItem::class, $m->getMetaItemClassName());
47
    }
48
49
    public function testGetMetaItemClassNameCustom()
50
    {
51
        $m = $this->getMetableStub();
52
53
        $m->setProperty('metaItemClassname', 'FooClass');
54
55
        $this->assertEquals('FooClass', $m->getMetaItemClassName());
56
    }
57
58
    public function testGetMetaItemInstance()
59
    {
60
        $m = $this->getMetableStub();
61
62
        $this->assertInstanceOf(MetaItem::class, $m->getMetaItemInstance());
63
    }
64
65
    public function testHasMeta()
66
    {
67
        $m = $this->getMetableStub();
68
69
        $has_meta = $m->hasMeta($m->getMetaItemClassName(), 'model', $m->getMetaItemClassName(), 'id', 'id');
70
71
        $this->assertInstanceOf(HasMeta::class, $has_meta);
72
    }
73
74
    public function testObserveSaveAndCascadeNewItems()
75
    {
76
        $m = $this->getMetableStub();
77
78
        $item = new MetaItem(['key' => 'foo', 'value' => 'bar']);
79
80
        $m->meta->add($item);
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Tests\E...eta\Stubs\MetableModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
81
82
        $m->save();
83
84
        $m = $m::first();
85
86
        $this->assertSame('bar', $m->meta->foo);
87
    }
88
89
    public function testObserveSaveAndCascadeExistingItems()
90
    {
91
        $m = $this->getMetableStub();
92
93
        $item = new MetaItem(['key' => 'foo', 'value' => 'bar']);
94
95
        $m->meta->add($item);
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Tests\E...eta\Stubs\MetableModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
96
97
        $m->save();
98
99
        $m = $m::first();
100
101
        $meta = $m->meta->first();
102
103
        $meta->value = 'baz';
104
105
        $m->save();
106
107
        $m = $m::first();
108
109
        $this->assertSame('baz', $m->meta->foo);
110
    }
111
112
    public function testObserveSaveAndCascadeRemoveItems()
113
    {
114
        $m = $this->getMetableStub();
115
116
        $item = new MetaItem(['key' => 'foo', 'value' => 'bar']);
117
118
        $m->meta->add($item);
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Tests\E...eta\Stubs\MetableModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
119
120
        $m->save();
121
122
        $m = $m::first();
123
124
        unset($m->meta[0]);
125
126
        $m->save();
127
128
        $m = $m::first();
129
130
        $this->assertCount(0, $m->meta);
131
    }
132
133 View Code Duplication
    public function testObserveDeleteAndCascade()
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...
134
    {
135
        $m = $this->getMetableStub();
136
137
        $item = new MetaItem(['key' => 'foo', 'value' => 'bar']);
138
139
        $m->meta->add($item);
0 ignored issues
show
Documentation introduced by
The property meta does not exist on object<BoxedCode\Tests\E...eta\Stubs\MetableModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
140
141
        $m->save();
142
143
        $m->delete();
144
145
        $this->assertNull(MetaItem::first());
146
    }
147
148
    protected function getMetableStub()
149
    {
150
        return new MetableModel;
151
    }
152
}
153