15
15
use PHPUnit \Framework \ExpectationFailedException ;
16
16
use PHPUnit \Framework \NativeType ;
17
17
use PHPUnit \Framework \TestCase ;
18
+ use stdClass ;
18
19
19
20
#[CoversClass(TraversableContainsOnly::class)]
20
21
#[CoversClass(Constraint::class)]
21
22
#[Small]
22
23
final class TraversableContainsOnlyTest extends TestCase
23
24
{
24
- public static function provider (): array
25
+ public static function nativeTypeProvider (): array
25
26
{
26
27
return [
27
28
[
@@ -46,8 +47,31 @@ public static function provider(): array
46
47
];
47
48
}
48
49
49
- #[DataProvider('provider ' )]
50
- public function testCanBeEvaluated (bool $ result , string $ failureDescription , NativeType $ expected , mixed $ actual ): void
50
+ public static function classOrInterfaceProvider (): array
51
+ {
52
+ return [
53
+ [
54
+ true ,
55
+ '' ,
56
+ stdClass::class,
57
+ [new stdClass , new stdClass ],
58
+ ],
59
+
60
+ [
61
+ false ,
62
+ <<<'EOT'
63
+ Failed asserting that Array &0 [
64
+ 0 => null,
65
+ ] contains only values of type "stdClass".
66
+ EOT,
67
+ stdClass::class,
68
+ [null ],
69
+ ],
70
+ ];
71
+ }
72
+
73
+ #[DataProvider('nativeTypeProvider ' )]
74
+ public function testCanBeEvaluatedForNativeType (bool $ result , string $ failureDescription , NativeType $ expected , mixed $ actual ): void
51
75
{
52
76
$ constraint = TraversableContainsOnly::forNativeType ($ expected );
53
77
@@ -63,11 +87,36 @@ public function testCanBeEvaluated(bool $result, string $failureDescription, Nat
63
87
$ constraint ->evaluate ($ actual );
64
88
}
65
89
66
- public function testCanBeRepresentedAsString (): void
90
+ /**
91
+ * @param class-string $expected
92
+ */
93
+ #[DataProvider('classOrInterfaceProvider ' )]
94
+ public function testCanBeEvaluatedForClassOrInterface (bool $ result , string $ failureDescription , string $ expected , mixed $ actual ): void
95
+ {
96
+ $ constraint = TraversableContainsOnly::forClassOrInterface ($ expected );
97
+
98
+ $ this ->assertSame ($ result , $ constraint ->evaluate ($ actual , returnResult: true ));
99
+
100
+ if ($ result ) {
101
+ return ;
102
+ }
103
+
104
+ $ this ->expectException (ExpectationFailedException::class);
105
+ $ this ->expectExceptionMessage ($ failureDescription );
106
+
107
+ $ constraint ->evaluate ($ actual );
108
+ }
109
+
110
+ public function testCanBeRepresentedAsStringForNativeType (): void
67
111
{
68
112
$ this ->assertSame ('contains only values of type "int" ' , TraversableContainsOnly::forNativeType (NativeType::Int)->toString ());
69
113
}
70
114
115
+ public function testCanBeRepresentedAsStringForClassOrInterface (): void
116
+ {
117
+ $ this ->assertSame ('contains only values of type "stdClass" ' , TraversableContainsOnly::forClassOrInterface (stdClass::class)->toString ());
118
+ }
119
+
71
120
public function testIsCountable (): void
72
121
{
73
122
$ this ->assertCount (1 , TraversableContainsOnly::forNativeType (NativeType::Int));
0 commit comments