-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAttributeBagTest.php
104 lines (84 loc) · 2.75 KB
/
AttributeBagTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Sofa\Eloquence\Tests;
use PHPUnit\Framework\TestCase;
use Sofa\Eloquence\Metable\Attribute;
use Sofa\Eloquence\Metable\AttributeBag;
class AttributeBagTest extends TestCase
{
/** @test */
public function it_replicates()
{
$original = $this->getBag();
$copy = $original->replicate();
$this->assertEquals($original, $copy);
$this->assertNotSame($original, $copy);
$this->assertEquals($original->first(), $copy->first());
$this->assertNotSame($original->first(), $copy->first());
}
/** @test */
public function it_handles_magic_calls()
{
$bag = $this->getBag();
$this->assertTrue(isset($bag->foo));
$this->assertFalse(isset($bag->wrong));
$this->assertEquals('bar', $bag->foo);
$this->assertNull($bag->wrong);
$bag->color = 'red';
$this->assertEquals('red', $bag->color);
unset($bag->color);
$this->assertNull($bag->color);
}
/** @test */
public function it_sets_value_to_null_when_unsetting()
{
$bag = $this->getBag()->set('key', 'value');
$bag->set('key', null);
$bag->forget('baz');
unset($bag['foo']);
$this->assertNull($bag->getValue('key'));
$this->assertNull($bag->getValue('baz'));
$this->assertNull($bag->getValue('foo'));
}
/** @test */
public function it_updates_existing_attribute()
{
$bag = $this->getbag()->set('foo', 'new_bar');
$bag->set(new Attribute('baz', 'new_bax'));
$bag->add(new Attribute('key', 'value'));
$bag['key'] = 'new_value';
$this->assertEquals('new_bar', $bag->getValue('foo'));
$this->assertEquals('new_bax', $bag->getValue('baz'));
$this->assertEquals('new_value', $bag->getValue('key'));
}
/** @test */
public function it_gets_null_for_non_existent_attributes()
{
$bag = $this->getBag();
$this->assertNull($bag->getValue('bad'));
$this->assertNull($bag->getValue('wrong'));
}
/** @test */
public function it_gets_raw_attribute_value()
{
$bag = $this->getBag();
$this->assertEquals('bar', $bag->getValue('foo'));
$this->assertEquals('bax', $bag->getValue('baz'));
}
/** @test */
public function it_gets_attributes_by_group()
{
$bag = $this->getBag();
$this->assertEquals([
'baz' => $bag->get('baz'),
'bar' => $bag->get('bar'),
], $bag->getMetaByGroup('group')->toArray());
}
protected function getBag()
{
return new AttributeBag([
new Attribute('foo', 'bar'),
new Attribute('baz', 'bax', 'group'),
new Attribute('bar', 'baz', 'group'),
]);
}
}