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'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
$this->assertTrue($m->isDirty()); |
25
|
|
|
|
26
|
|
|
$this->assertTrue($m->isDirty('foo')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function testIsDirtyMeta() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$m = $this->getMetableStub(); |
32
|
|
|
|
33
|
|
|
$item = new MetaItem(['key' => 'foo', 'value' => 'bar']); |
34
|
|
|
|
35
|
|
|
$m->meta->add($item); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$m = $this->getMetableStub(); |
136
|
|
|
|
137
|
|
|
$item = new MetaItem(['key' => 'foo', 'value' => 'bar']); |
138
|
|
|
|
139
|
|
|
$m->meta->add($item); |
|
|
|
|
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
|
|
|
|
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.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.