1
+ <?php
2
+
3
+ namespace Behavioral \ChainOfResponsibilities \Tests ;
4
+
5
+ use PHPUnit \Framework \TestCase ;
6
+ use Psr \Http \Message \RequestInterface ;
7
+ use Psr \Http \Message \UriInterface ;
8
+ use Behavioral \ChainOfResponsibilities \Handler ;
9
+ use Behavioral \ChainOfResponsibilities \HttpInMemoryCacheHandler ;
10
+ use Behavioral \ChainOfResponsibilities \SlowDatabaseHandler ;
11
+
12
+ class ChainTest extends TestCase
13
+ {
14
+ /**
15
+ * @var Handler
16
+ */
17
+ private $ chain ;
18
+
19
+ protected function setUp (): void
20
+ {
21
+ $ this ->chain = new HttpInMemoryCacheHandler (
22
+ ['/test?a=1 ' => 'Hello In Memory! ' ],
23
+ new SlowDatabaseHandler ()
24
+ );
25
+ }
26
+
27
+ public function testCanRequestKeyInFastStorage ()
28
+ {
29
+ $ uri = $ this ->createMock (UriInterface::class);
30
+ $ uri ->method ('getPath ' )->willReturn ('/test ' );
31
+ $ uri ->method ('getQuery ' )->willReturn ('a=1 ' );
32
+
33
+ $ request = $ this ->createMock (RequestInterface::class);
34
+ $ request ->method ('getMethod ' )
35
+ ->willReturn ('GET ' );
36
+ $ request ->method ('getUri ' )->willReturn ($ uri );
37
+
38
+ $ this ->assertSame ('Hello In Memory! ' , $ this ->chain ->handle ($ request ));
39
+ }
40
+
41
+ public function testCanRequestKeyInSlowStorage ()
42
+ {
43
+ $ uri = $ this ->createMock (UriInterface::class);
44
+ $ uri ->method ('getPath ' )->willReturn ('/foo ' );
45
+ $ uri ->method ('getQuery ' )->willReturn ('' );
46
+
47
+ $ request = $ this ->createMock (RequestInterface::class);
48
+ $ request ->method ('getMethod ' )
49
+ ->willReturn ('GET ' );
50
+ $ request ->method ('getUri ' )->willReturn ($ uri );
51
+
52
+ $ this ->assertSame ('Hello World! ' , $ this ->chain ->handle ($ request ));
53
+ }
54
+ }
0 commit comments