8
8
use Psr \Http \Message \ServerRequestInterface ;
9
9
use React \Http \Message \Response ;
10
10
use React \Http \Message \ServerRequest ;
11
+ use ReflectionProperty ;
11
12
use WyriHaximus \AsyncTestUtilities \AsyncTestCase ;
12
13
use WyriHaximus \React \Http \Middleware \Header ;
13
14
use WyriHaximus \React \Http \Middleware \WithRandomHeadersMiddleware ;
14
15
16
+ use function array_key_exists ;
17
+ use function array_map ;
18
+ use function array_values ;
19
+ use function count ;
15
20
use function React \Async \await ;
21
+ use function strlen ;
22
+ use function strtoupper ;
16
23
17
24
final class WithRandomHeadersMiddlewareTest extends AsyncTestCase
18
25
{
19
- public function testWithRandomHeaders (): void
26
+ /** @test */
27
+ public function withRandomHeaders (): void
20
28
{
21
- $ headers = [
22
- new Header ( ' X-Hamsterred-By ' , ' ReactPHP 7 ' ),
23
- new Header ('X-Foo ' , ' Bar ' ),
24
- new Header ( ' X-Bar ' , ' Foo ' ),
25
- ];
26
- $ middleware = new WithRandomHeadersMiddleware (...$ headers );
29
+ $ headers = [];
30
+ for ( $ char = ' a ' ; strlen ( $ char ) === 1 ; $ char ++) {
31
+ $ headers [ ' X- ' . strtoupper ( $ char )] = new Header ('X- ' . strtoupper ( $ char ), $ char );
32
+ }
33
+
34
+ $ middleware = ( new WithRandomHeadersMiddleware (...$ headers))-> withMinimum ( count ( $ headers ) );
27
35
$ request = new ServerRequest ('GET ' , 'https://example.com/ ' );
28
36
$ requestWithHeaders = await ($ middleware ($ request , static fn (ServerRequestInterface $ request ): ResponseInterface => new Response ()));
29
37
30
- $ count = 0 ;
31
-
32
- foreach ($ headers as $ header ) {
33
- if (! $ requestWithHeaders ->hasHeader ($ header ->header )) {
38
+ $ requestHeaders = [];
39
+ foreach ($ requestWithHeaders ->getHeaders () as $ headerName => $ value ) {
40
+ if (! array_key_exists ($ headerName , $ headers )) {
34
41
continue ;
35
42
}
36
43
37
- $ count ++ ;
44
+ $ requestHeaders [] = $ headerName ;
38
45
}
39
46
40
- self ::assertSame (2 , $ count );
47
+ self ::assertCount (count ($ headers ), $ requestHeaders );
48
+ self ::assertNotSame (
49
+ [
50
+ ...array_map (
51
+ static fn (Header $ header ): string => $ header ->header ,
52
+ array_values ($ headers ),
53
+ ),
54
+ ],
55
+ $ requestHeaders ,
56
+ );
41
57
}
42
58
43
59
/** @test */
@@ -51,14 +67,43 @@ public function immutability(): void
51
67
self ::assertNotSame ($ b , $ middleware );
52
68
}
53
69
54
- /** @test */
55
- public function minMaxMath (): void
70
+ /**
71
+ * @test
72
+ * @dataProvider minMaxMAthDataProvider
73
+ */
74
+ public function minMaxMath (int $ min , int $ max , int $ expectedMin , int $ expectedMax ): void
56
75
{
57
- $ a = new WithRandomHeadersMiddleware (new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ));
58
- $ b = $ a ->withMinimum (5 );
59
- $ middleware = $ b ->withMaximum (1 );
76
+ $ a = new WithRandomHeadersMiddleware (new Header ('A ' , 'a ' ), new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ), new Header ('Foo ' , 'bar ' ));
77
+ $ b = $ a ->withMinimum ($ min );
78
+
79
+ self ::assertSame ($ expectedMin , self ::getPropertyValue ($ b , 'minimum ' ));
80
+
81
+ $ middleware = $ b ->withMaximum ($ max );
60
82
self ::assertNotSame ($ a , $ b );
61
83
self ::assertNotSame ($ a , $ middleware );
62
84
self ::assertNotSame ($ b , $ middleware );
85
+
86
+ self ::assertSame ($ expectedMin , self ::getPropertyValue ($ middleware , 'minimum ' ));
87
+ self ::assertSame ($ expectedMax , self ::getPropertyValue ($ middleware , 'maximum ' ));
88
+ }
89
+
90
+ /** @return iterable<array<int>> */
91
+ public static function minMaxMAthDataProvider (): iterable
92
+ {
93
+ yield 'Same ' => [1 , 1 , 1 , 1 ];
94
+ yield 'Max higher than Min ' => [1 , 2 , 1 , 2 ];
95
+ yield 'Same but with 2 instead of 1 ' => [2 , 2 , 2 , 2 ];
96
+ yield 'Min higher than Max so they are both pulled to Max ' => [2 , 1 , 2 , 2 ];
97
+ yield 'Min and Max can \'t be higher than the 5 headers we put into it, but with Max lower than Min ' => [13 , 1 , 5 , 5 ];
98
+ yield 'Min and Max can \'t be higher than the 5 headers we put into it ' => [6 , 13 , 5 , 5 ];
99
+ }
100
+
101
+ private static function getPropertyValue (WithRandomHeadersMiddleware $ middleware , string $ propertyName ): int
102
+ {
103
+ $ property = (new ReflectionProperty (WithRandomHeadersMiddleware::class, $ propertyName ));
104
+ $ property ->setAccessible (true );
105
+
106
+ /** @phpstan-ignore-next-line */
107
+ return $ property ->getValue ($ middleware );
63
108
}
64
109
}
0 commit comments