Skip to content

Commit 60dabda

Browse files
committed
Add AttributesTest for TypeRocket
A new test suite, named AttributesTest, has been added to the project. This tests the functionality of the Attribute trait in the TypeRocket engine, including attribute addition, removal, and modification methods. It also ensures the correct handling of different attribute arguments or conditions.
1 parent d82e010 commit 60dabda

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

tests/Traits/AttributesTest.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php
2+
namespace Traits;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use TypeRocket\Engine7\Elements\Traits\Attributes;
6+
7+
class AttributesTest extends TestCase
8+
{
9+
private $attributes;
10+
11+
protected function setUp(): void
12+
{
13+
$this->attributes = new class {
14+
use Attributes;
15+
};
16+
}
17+
18+
public function testAttrWithNoArguments(): void
19+
{
20+
$actual = $this->attributes->attr();
21+
$this->assertEquals([], $actual);
22+
}
23+
24+
public function testAttrWithArrayArgument(): void
25+
{
26+
$array = ['key' => 'value'];
27+
$this->attributes->attr($array);
28+
29+
$actual = $this->attributes->attr();
30+
$expected = $array;
31+
$this->assertEquals($expected, $actual);
32+
}
33+
34+
public function testAttrWithKeyValuePair(): void
35+
{
36+
$key = 'key2';
37+
$value = 'value2';
38+
$this->attributes->attr($key, $value);
39+
40+
$actual = $this->attributes->attr($key);
41+
$expected = $value;
42+
$this->assertEquals($expected, $actual);
43+
}
44+
45+
public function testAttrWithKeyOnly(): void
46+
{
47+
$key = 'key3';
48+
$value = null;
49+
$this->attributes->attr($key, $value);
50+
51+
$actual = $this->attributes->attr($key);
52+
$expected = $value;
53+
$this->assertNull($actual);
54+
}
55+
56+
public function testAttrReset(): void
57+
{
58+
$this->attributes->attrExtend(['class' => 'container', 'id' => 'containerId']);
59+
60+
$this->assertEquals(['class' => 'container', 'id' => 'containerId'], $this->attributes->getAttributes());
61+
62+
$this->attributes->attrReset(['style' => 'color:red']);
63+
64+
$this->assertEquals(['style' => 'color:red'], $this->attributes->getAttributes());
65+
}
66+
67+
public function testPopAllAttributes(): void
68+
{
69+
$testData = ['foo' => 'bar', 'baz' => 'qux'];
70+
$this->attributes->attrReset($testData);
71+
$poppedAttributes = $this->attributes->popAllAttributes();
72+
73+
$this->assertSame($testData, $poppedAttributes);
74+
}
75+
76+
/**
77+
* Ensure popAllAttributes resets attributes to empty array on the object itself.
78+
*/
79+
public function testPopAllAttributesResetsArray(): void
80+
{
81+
$this->attributes->attrReset(['foo' => 'bar', 'baz' => 'qux']);
82+
$this->attributes->popAllAttributes();
83+
84+
$this->assertEquals([], $this->attributes->getAttributes());
85+
}
86+
87+
public function testPopAttribute(): void
88+
{
89+
//Arranging
90+
$attributeToBePopped = "value2";
91+
$attributes = ["attr1" => "value1", "attr2" => $attributeToBePopped];
92+
$this->attributes->attrReset($attributes);
93+
94+
//Acting
95+
$poppedAttribute = $this->attributes->popAttribute();
96+
97+
//Asserting
98+
$this->assertEquals($attributeToBePopped, $poppedAttribute);
99+
$this->assertArrayNotHasKey("attr2", $this->attributes->getAttributes());
100+
}
101+
102+
public function testShiftAttribute(): void
103+
{
104+
// Setting Initial attributes
105+
$this->attributes->attrReset(['attr1' => 'value1', 'attr2' => 'value2', 'attr3' => 'value3']);
106+
107+
// Use Case #1: Shifting the first attribute
108+
$shifted = $this->attributes->shiftAttribute();
109+
$remaining = $this->attributes->getAttributes();
110+
111+
$this->assertEquals('value1', $shifted, 'The return value does not match the first attribute value.');
112+
$this->assertArrayNotHasKey('attr1', $remaining, 'Shifted attribute still exists in the remaining attributes');
113+
}
114+
115+
public function testRemoveAttributeMethod(): void
116+
{
117+
// Setup test data
118+
$key = 'attributeKey';
119+
$value = 'attributeValue';
120+
121+
// Set an attribute
122+
$this->attributes->setAttribute($key, $value);
123+
124+
// Assert that the attribute was set
125+
$this->assertEquals($value, $this->attributes->getAttribute($key));
126+
127+
// Call `removeAttribute` method
128+
$this->attributes->removeAttribute($key);
129+
130+
// Assert that the attribute was removed
131+
$this->assertNull($this->attributes->getAttribute($key));
132+
}
133+
134+
public function testAttrClass(): void
135+
{
136+
// Create a new Attributes instance
137+
$attributes = $this->attributes;
138+
139+
// Define class attribute
140+
$classValue = 'my-class';
141+
142+
// Set the class attribute
143+
$attributes->attrClass($classValue);
144+
145+
// Check if the correct class attribute is set
146+
$this->assertEquals($classValue, $attributes->getAttribute('class'), "Class attribute does not match expected value");
147+
148+
// Append a class to the class attribute
149+
$appendedClassValue = 'additional-class';
150+
$attributes->attrClass($appendedClassValue);
151+
152+
// Check if the class attribute is updated with append value
153+
$this->assertEquals($classValue . ' ' . $appendedClassValue, $attributes->getAttribute('class'), "Appended class attribute does not match expected value");
154+
155+
// Check if attrClass with no parameters returns the proper class string
156+
$this->assertEquals($classValue . ' ' . $appendedClassValue, $attributes->attrClass(), "Returned class attribute does not match expected value");
157+
}
158+
159+
public function testAttrClassIfTrueCondition() : void
160+
{
161+
$this->attributes->attrClassIf(true, 'test');
162+
163+
// Check that the 'class' key has the correct value
164+
$this->assertEquals('test', $this->attributes->getAttribute('class'));
165+
}
166+
167+
public function testAttrClassIfFalseCondition() : void
168+
{
169+
$this->attributes->attrClassIf(false, 'test');
170+
171+
// Check that the 'class' key has not been set
172+
$this->assertNull($this->attributes->getAttribute('class'));
173+
}
174+
175+
public function testMaybeSetAttributeWithNonexistentAttribute(): void
176+
{
177+
$this->attributes->maybeSetAttribute('data-test', 'value');
178+
$this->assertEquals('value', $this->attributes->getAttribute('data-test'));
179+
}
180+
181+
public function testMaybeSetAttributeWithExistingAttribute(): void
182+
{
183+
$this->attributes->attr('data-test', 'original');
184+
$this->attributes->maybeSetAttribute('data-test', 'new-value');
185+
$this->assertEquals('original', $this->attributes->getAttribute('data-test'));
186+
}
187+
}

0 commit comments

Comments
 (0)