1 | <?php |
||
11 | class AttributesTest extends TestCase |
||
12 | { |
||
13 | /** |
||
14 | * @return Attributes |
||
15 | */ |
||
16 | protected function attributeFactory() |
||
17 | { |
||
18 | return $this->app->makeWith(Attributes::class, [ |
||
19 | 'attributes' => $this->attributesStub(), |
||
20 | ]); |
||
21 | } |
||
22 | |||
23 | protected function serializeStub() |
||
24 | { |
||
25 | return 'C:31:"Malezha\Menu\Support\Attributes":56:{a:2:{s:5:"class";s:10:"some-style";s:2:"id";s:4:"test";}}'; |
||
26 | } |
||
27 | |||
28 | protected function attributesStub() |
||
29 | { |
||
30 | return [ |
||
31 | 'class' => 'some-style', |
||
32 | 'id' => 'test', |
||
33 | ]; |
||
34 | } |
||
35 | |||
36 | public function testGet() |
||
37 | { |
||
38 | $attributes = $this->attributeFactory(); |
||
39 | |||
40 | $this->assertEquals('some-style', $attributes->get('class')); |
||
41 | $this->assertEquals('some-style', $attributes['class']); |
||
42 | } |
||
43 | |||
44 | public function testSet() |
||
45 | { |
||
46 | $attributes = $this->attributeFactory(); |
||
47 | $subset = ['class' => 'new-style', 'id' => 'link']; |
||
48 | |||
49 | $attributes->set($subset); |
||
50 | |||
51 | $this->assertArraySubset($subset, $attributes->all()); |
||
52 | } |
||
53 | |||
54 | public function testForget() |
||
55 | { |
||
56 | $attributes = $this->attributeFactory(); |
||
57 | |||
58 | $attributes->forget('id'); |
||
59 | unset($attributes['class']); |
||
60 | |||
61 | $this->assertEquals(null, $attributes->get('id')); |
||
62 | $this->assertEquals(null, $attributes->get('class')); |
||
63 | } |
||
64 | |||
65 | public function testPut() |
||
66 | { |
||
67 | $attributes = $this->attributeFactory(); |
||
68 | |||
69 | $attributes->put('data-test', 'value'); |
||
70 | $attributes['class'] = 'test'; |
||
71 | |||
72 | $this->assertEquals('value', $attributes->get('data-test')); |
||
73 | $this->assertEquals('test', $attributes->get('class')); |
||
74 | } |
||
75 | |||
76 | public function testPush() |
||
77 | { |
||
78 | $attributes = $this->attributeFactory(); |
||
79 | |||
80 | $subset = $attributes->all(); |
||
81 | $push = ['data-value' => 'some']; |
||
82 | |||
83 | $attributes->push($push); |
||
84 | |||
85 | $this->assertArraySubset(array_merge($subset, $push), $attributes->all()); |
||
86 | } |
||
87 | |||
88 | public function testHas() |
||
89 | { |
||
90 | $attributes = $this->attributeFactory(); |
||
91 | |||
92 | $this->assertTrue($attributes->has('class')); |
||
93 | $this->assertTrue(isset($attributes['class'])); |
||
94 | } |
||
95 | |||
96 | public function testMerge() |
||
97 | { |
||
98 | $attributes = $this->attributeFactory(); |
||
99 | |||
100 | $attributes->merge([ |
||
101 | 'class' => 'another-style', |
||
102 | ]); |
||
103 | |||
104 | $this->assertArraySubset([ |
||
105 | 'class' => 'some-style another-style', |
||
106 | 'id' => 'test', |
||
107 | ], $attributes->all()); |
||
108 | } |
||
109 | |||
110 | public function testBuild() |
||
111 | { |
||
112 | $attributes = $this->attributeFactory(); |
||
113 | $expected = ' class="some-style active" id="test"'; |
||
114 | $expectedString = ' class="some-style" id="test"'; |
||
115 | |||
116 | $this->assertEquals($expected, $attributes->build(['class' => 'active'])); |
||
117 | $this->assertEquals($expectedString, (string) $attributes); |
||
118 | } |
||
119 | |||
120 | public function testMergeAttributesEmptyConstructor() |
||
121 | { |
||
122 | $this->assertEquals([], (new MergeAttributes())->merge()); |
||
123 | } |
||
124 | |||
125 | public function testToArray() |
||
126 | { |
||
127 | $attributes = $this->attributeFactory(); |
||
128 | $this->assertEquals($this->attributesStub(), $attributes->toArray()); |
||
129 | } |
||
130 | } |