-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssertTest.php
More file actions
162 lines (129 loc) · 4.49 KB
/
Copy pathAssertTest.php
File metadata and controls
162 lines (129 loc) · 4.49 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Assert;
use BadMethodCallException;
use DateTimeImmutable;
use LogicException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\Test\Utils\TestClass;
use SimpleSAML\Test\Utils\TestEnum;
use stdClass;
use function opendir;
use function sys_get_temp_dir;
/**
* Class \SimpleSAML\Assert\AssertTest
*
* @package simplesamlphp/assert
*/
#[CoversClass(Assert::class)]
final class AssertTest extends TestCase
{
#[DoesNotPerformAssertions]
public function testAssertionPassing(): void
{
Assert::integer(1);
}
/**
*/
public function testAssertionFailingThrowsException(): void
{
$this->expectException(AssertionFailedException::class);
Assert::nullOrSame('a', 'b');
}
/**
*/
public function testAssertionFailingWithCustomExceptionThrowsCustomException(): void
{
$this->expectException(LogicException::class);
Assert::allSame(['a', 'b', 'c'], 'b', LogicException::class);
}
/**
*/
public function testUnknownAssertionRaisesBadMethodCallException(): void
{
$this->expectException(BadMethodCallException::class);
// @phpstan-ignore staticMethod.notFound
Assert::thisAssertionDoesNotExist('a', 'b', LogicException::class); // @phpstan-ignore-line
}
/**
*/
public function testUnknownNullOrAssertionRaisesBadMethodCallException(): void
{
$this->expectException(BadMethodCallException::class);
// @phpstan-ignore staticMethod.notFound
Assert::nullOrThisAssertionDoesNotExist('a', 'b', LogicException::class); // @phpstan-ignore-line
}
/**
*/
public function testUnknownAllAssertionRaisesBadMethodCallException(): void
{
$this->expectException(BadMethodCallException::class);
// @phpstan-ignore staticMethod.notFound
Assert::allThisAssertionDoesNotExist('a', 'b', LogicException::class); // @phpstan-ignore-line
}
/**
*/
public function testNullOrCustomAssertionWorks(): void
{
Assert::nullOrValidBase64('U2ltcGxlU0FNTHBocA==');
Assert::nullOrValidBase64(null);
// Also make sure it keeps working for Webmozart's native assertions
Assert::nullOrString(null);
Assert::nullOrString('test');
// Test a failure for coverage
$this->expectException(AssertionFailedException::class);
Assert::nullOrValidBase64('U2ltcGxlU0FNTHocA==');
}
/**
*/
public function testAllCustomAssertionWorks(): void
{
Assert::allValidBase64(['U2ltcGxlU0FNTHBocA==', 'dGVzdA==']);
// Also make sure it keeps working for Webmozart's native assertions
Assert::allString(['test', 'phpunit']);
// Test a failure for coverage
$this->expectException(AssertionFailedException::class);
Assert::allValidBase64(['U2ltcGxlU0FNTHocA==', null]);
}
/**
* @param mixed $value
* @param string $expected
*/
#[DataProvider('provideValue')]
public function testValueToString(mixed $value, string $expected): void
{
$assert = new Assert();
$method = new ReflectionMethod(Assert::class, 'valueToString');
$this->assertEquals($expected, $method->invoke($assert, $value));
}
/**
* @return array<string, array{0: mixed, 1: string}>
*/
public static function provideValue(): array
{
$stringable = new TestClass('phpunit');
$dateTime = new DateTimeImmutable('2000-01-01T00:00:00+00:00');
$otherObject = new stdClass();
$resource = opendir(sys_get_temp_dir());
$enum = TestEnum::PHPUnit;
return [
'null' => [null, 'null'],
'true' => [true, 'true'],
'false' => [false, 'false'],
'array' => [[], 'array'],
'Stringable' => [$stringable, 'SimpleSAML\Test\Utils\TestClass: "phpunit"'],
'DateTime' => [$dateTime, 'DateTimeImmutable: "2000-01-01T00:00:00+00:00"'],
'Enum' => [$enum, 'SimpleSAML\Test\Utils\TestEnum::PHPUnit'],
'object' => [$otherObject, 'stdClass'],
'resource' => [$resource, 'resource'],
'string' => ['string', '"string"'],
'other' => [1, '1'],
];
}
}