Skip to content

Commit 2eefab9

Browse files
committed
Add tests for Deprecated attribute
1 parent 98dd286 commit 2eefab9

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

tests/Attribute/DeprecatedTest.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tests\Attribute;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Html\Attribute\Deprecated;
9+
10+
final class DeprecatedTest extends TestCase
11+
{
12+
private function make(?string $message, ?string $since): Deprecated
13+
{
14+
$errors = [];
15+
set_error_handler(static function (int $errno, string $errstr) use (&$errors): bool {
16+
$errors[] = $errstr;
17+
return true;
18+
}, E_USER_DEPRECATED);
19+
20+
$attribute = new Deprecated($message, $since);
21+
22+
restore_error_handler();
23+
24+
return $attribute;
25+
}
26+
27+
public function testMessageProperty(): void
28+
{
29+
$attribute = $this->make('Test message', null);
30+
31+
$this->assertSame('Test message', $attribute->message);
32+
}
33+
34+
public function testSinceProperty(): void
35+
{
36+
$attribute = $this->make('Test message', '1.0.0');
37+
38+
$this->assertSame('1.0.0', $attribute->since);
39+
}
40+
41+
public function testNullMessageProperty(): void
42+
{
43+
$attribute = $this->make(null, null);
44+
45+
$this->assertNull($attribute->message);
46+
}
47+
48+
public function testNullSinceProperty(): void
49+
{
50+
$attribute = $this->make('Test message', null);
51+
52+
$this->assertNull($attribute->since);
53+
}
54+
55+
public function testTriggersDeprecationError(): void
56+
{
57+
$triggered = false;
58+
$triggeredMessage = null;
59+
60+
set_error_handler(static function (int $errno, string $errstr) use (&$triggered, &$triggeredMessage): bool {
61+
$triggered = true;
62+
$triggeredMessage = $errstr;
63+
return true;
64+
}, E_USER_DEPRECATED);
65+
66+
new Deprecated('Use something else.', null);
67+
68+
restore_error_handler();
69+
70+
$this->assertTrue($triggered);
71+
$this->assertSame('Use something else.', $triggeredMessage);
72+
}
73+
74+
public function testNullMessageTriggersEmptyError(): void
75+
{
76+
$triggeredMessage = null;
77+
78+
set_error_handler(static function (int $errno, string $errstr) use (&$triggeredMessage): bool {
79+
$triggeredMessage = $errstr;
80+
return true;
81+
}, E_USER_DEPRECATED);
82+
83+
new Deprecated(null, null);
84+
85+
restore_error_handler();
86+
87+
$this->assertSame('', $triggeredMessage);
88+
}
89+
90+
public function testClassAlias(): void
91+
{
92+
$this->assertTrue(class_exists('Deprecated'));
93+
94+
$attribute = $this->make('Test message', null);
95+
96+
$this->assertInstanceOf(Deprecated::class, $attribute);
97+
}
98+
}

0 commit comments

Comments
 (0)