Skip to content

Commit ca196f7

Browse files
committed
chore: completing partial tests
1 parent 56b33db commit ca196f7

10 files changed

Lines changed: 389 additions & 100 deletions

File tree

clover.xml

Lines changed: 95 additions & 96 deletions
Large diffs are not rendered by default.

src/Trait/GlobalAttribute/ClassTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function setClass(string|array $className): static
1919
/** @todo be more permissive here? or stricter (as is) for max css compatbility */
2020
$className = array_filter($className, 'strlen'); // remove empty values
2121
foreach ($className as &$name) {
22-
$name = preg_replace('/[^a-zA-Z0-9_-]+/', '', $name);
23-
$className = preg_replace('/[^a-zA-Z0-9_-]+/', ' ', $className);
24-
$className = preg_replace('/\s+/', ' ', $className);
25-
$className = trim($className);
22+
// sanitize individual class name and normalize whitespace
23+
$name = preg_replace('/[^a-zA-Z0-9_-]+/', ' ', $name);
24+
$name = preg_replace('/\s+/', ' ', $name);
25+
$name = trim($name);
2626
}
2727
$className = implode(' ', $className);
2828
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\AutocorrectTrait;
4+
use Html\Enum\AutocorrectEnum;
5+
6+
class TestAutocorrect
7+
{
8+
use AutocorrectTrait;
9+
10+
public array $attributes = [];
11+
public $delegated;
12+
13+
public function __construct()
14+
{
15+
$this->delegated = $this;
16+
}
17+
18+
public function setAttribute(string $name, string $value)
19+
{
20+
$this->attributes[$name] = $value;
21+
return $this;
22+
}
23+
}
24+
25+
test('setAutocorrect accepts boolean true and sets on', function () {
26+
$obj = new TestAutocorrect();
27+
28+
$obj->setAutocorrect(true);
29+
30+
expect($obj->getAutocorrect())
31+
->toBeInstanceOf(AutocorrectEnum::class)
32+
->and($obj->getAutocorrect()->value)->toBe(AutocorrectEnum::ON->value)
33+
->and($obj->attributes['autocorrect'])->toBe(AutocorrectEnum::ON->value);
34+
});
35+
36+
test('setAutocorrect accepts string "off" and sets off', function () {
37+
$obj = new TestAutocorrect();
38+
39+
$obj->setAutocorrect('off');
40+
41+
expect($obj->getAutocorrect()->value)->toBe(AutocorrectEnum::OFF->value)
42+
->and($obj->attributes['autocorrect'])->toBe(AutocorrectEnum::OFF->value);
43+
});
44+
45+
test('setAutocorrect throws for invalid string', function () {
46+
$obj = new TestAutocorrect();
47+
48+
expect(fn () => $obj->setAutocorrect('maybe'))
49+
->toThrow(InvalidArgumentException::class);
50+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\AutofocusTrait;
4+
5+
class TestAutofocus
6+
{
7+
use AutofocusTrait;
8+
9+
public array $attributes = [];
10+
public $delegated;
11+
12+
public function __construct()
13+
{
14+
$this->delegated = $this;
15+
}
16+
17+
public function setAttribute(string $name, string $value)
18+
{
19+
$this->attributes[$name] = $value;
20+
return $this;
21+
}
22+
}
23+
24+
test('setAutofocus true sets delegated attribute and getAutofocus returns true', function () {
25+
$obj = new TestAutofocus();
26+
27+
$obj->setAutofocus(true);
28+
29+
expect($obj->getAutofocus())->toBeTrue()
30+
->and($obj->attributes['autofocus'])->toBe('true');
31+
});
32+
33+
test('setAutofocus with "true" string also sets attribute', function () {
34+
$obj = new TestAutofocus();
35+
36+
$obj->setAutofocus('true');
37+
38+
expect($obj->getAutofocus())->toBeTrue()
39+
->and($obj->attributes['autofocus'])->toBe('true');
40+
});
41+
42+
test('setAutofocus false does not set attribute', function () {
43+
$obj = new TestAutofocus();
44+
45+
$obj->setAutofocus(false);
46+
47+
expect(array_key_exists('autofocus', $obj->attributes))->toBeFalse();
48+
});
49+
50+
test('setAutofocus throws on invalid string', function () {
51+
$obj = new TestAutofocus();
52+
53+
expect(fn () => $obj->setAutofocus('maybe'))
54+
->toThrow(InvalidArgumentException::class);
55+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\ClassTrait;
4+
5+
class TestClassTrait
6+
{
7+
use ClassTrait;
8+
9+
public ?string $className = null;
10+
public $delegated;
11+
12+
public function __construct()
13+
{
14+
$this->delegated = $this;
15+
}
16+
}
17+
18+
test('setClass with string sets className and delegated className', function () {
19+
$obj = new TestClassTrait();
20+
21+
$obj->setClass('foo bar');
22+
23+
expect($obj->getClass())->toBe('foo bar')
24+
->and($obj->delegated->className)->toBe('foo bar');
25+
});
26+
27+
test('setClass with array sets sanitized className', function () {
28+
$obj = new TestClassTrait();
29+
30+
$obj->setClass(['one', 'two']);
31+
32+
expect($obj->getClass())->toBe('one two')
33+
->and($obj->delegated->className)->toBe('one two');
34+
});
35+
36+
test('setClass with empty string does not set className', function () {
37+
$obj = new TestClassTrait();
38+
39+
$obj->setClass('');
40+
41+
expect($obj->getClass())->toBeNull();
42+
});
43+
44+
test('aliases getClassName/getClasses return the same value', function () {
45+
$obj = new TestClassTrait();
46+
47+
$obj->setClass('alpha');
48+
49+
expect($obj->getClassName())->toBe('alpha')
50+
->and($obj->getClasses())->toBe('alpha');
51+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\HiddenTrait;
4+
5+
class TestHidden
6+
{
7+
use HiddenTrait;
8+
9+
public array $attributes = [];
10+
public $delegated;
11+
12+
public function __construct()
13+
{
14+
$this->delegated = $this;
15+
}
16+
17+
public function setAttribute(string $name, string $value)
18+
{
19+
$this->attributes[$name] = $value;
20+
return $this;
21+
}
22+
}
23+
24+
test('setHidden true sets delegated attribute and getHidden returns true', function () {
25+
$obj = new TestHidden();
26+
27+
$obj->setHidden(true);
28+
29+
expect($obj->getHidden())->toBeTrue()
30+
->and($obj->attributes['hidden'])->toBe('true');
31+
});
32+
33+
test('setHidden false does not set attribute', function () {
34+
$obj = new TestHidden();
35+
36+
$obj->setHidden(false);
37+
38+
expect(array_key_exists('hidden', $obj->attributes))->toBeFalse();
39+
});
40+
41+
test('setHidden throws on invalid string', function () {
42+
$obj = new TestHidden();
43+
44+
expect(fn () => $obj->setHidden('maybe'))
45+
->toThrow(InvalidArgumentException::class);
46+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\InertTrait;
4+
5+
class TestInert
6+
{
7+
use InertTrait;
8+
9+
public array $attributes = [];
10+
public $delegated;
11+
12+
public function __construct()
13+
{
14+
$this->delegated = $this;
15+
}
16+
17+
public function setAttribute(string $name, string $value)
18+
{
19+
$this->attributes[$name] = $value;
20+
return $this;
21+
}
22+
}
23+
24+
test('setInert true sets delegated attribute and getInert returns true', function () {
25+
$obj = new TestInert();
26+
27+
$obj->setInert(true);
28+
29+
expect($obj->getInert())->toBeTrue()
30+
->and($obj->attributes['inert'])->toBe('true');
31+
});
32+
33+
test('setInert false sets delegated attribute and getInert returns false', function () {
34+
$obj = new TestInert();
35+
36+
$obj->setInert(false);
37+
38+
expect($obj->getInert())->toBeFalse()
39+
->and($obj->attributes['inert'])->toBe('false');
40+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\IsTrait;
4+
5+
class TestIs
6+
{
7+
use IsTrait;
8+
}
9+
10+
test('setIs stores the value and getIs returns it', function () {
11+
$obj = new TestIs();
12+
13+
$obj->setIs('custom-element');
14+
15+
expect($obj->getIs())->toBe('custom-element');
16+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\PartTrait;
4+
5+
class TestPart
6+
{
7+
use PartTrait;
8+
}
9+
10+
test('setPart stores the value and getPart returns it', function () {
11+
$obj = new TestPart();
12+
13+
$obj->setPart('header');
14+
15+
expect($obj->getPart())->toBe('header');
16+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Html\Trait\GlobalAttribute\RoleTrait;
4+
5+
class TestRole
6+
{
7+
use RoleTrait;
8+
}
9+
10+
test('setRole stores the value and getRole returns it', function () {
11+
$obj = new TestRole();
12+
13+
$obj->setRole('button');
14+
15+
expect($obj->getRole())->toBe('button');
16+
});

0 commit comments

Comments
 (0)