33
44use ArrayObject ;
55use InvalidArgumentException ;
6+ use Psr \Http \Message \ResponseInterface ;
7+ use Psr \Http \Message \ServerRequestInterface ;
8+ use RuntimeException ;
69use TypeError ;
710use Zend \Diactoros \ServerRequestFactory ;
811use Zend \Diactoros \Response ;
@@ -60,7 +63,7 @@ public function testTraversableQueue()
6063 public function testBadQueue ()
6164 {
6265 $ this ->expectException (TypeError::CLASS );
63- $ relay = new Relay ('bad ' );
66+ new Relay ('bad ' );
6467 }
6568
6669 public function testEmptyQueue ()
@@ -71,6 +74,17 @@ public function testEmptyQueue()
7174 new Relay ([]);
7275 }
7376
77+ public function testQueueWithInvalidEntry ()
78+ {
79+ $ this ->expectException (RuntimeException::CLASS );
80+ $ this ->expectExceptionMessage (
81+ "Invalid middleware queue entry: bad. Middleware must either be callable or implement Psr\Http\Server\MiddlewareInterface. "
82+ );
83+
84+ $ relay = new Relay (['bad ' ]);
85+ $ relay ->handle (ServerRequestFactory::fromGlobals ());
86+ }
87+
7488 public function testResolverEntries ()
7589 {
7690 $ queue = [
@@ -84,6 +98,7 @@ public function testResolverEntries()
8498
8599 $ this ->assertRelay (new Relay ($ queue , $ resolver ));
86100 }
101+
87102
88103 public function testRequestHandlerInQueue ()
89104 {
@@ -96,4 +111,26 @@ public function testRequestHandlerInQueue()
96111 $ requestHandler = new Relay ($ queue );
97112 $ this ->assertRelay (new Relay ([$ requestHandler ]));
98113 }
114+
115+ public function testCallableMiddleware ()
116+ {
117+ $ queue = [
118+ function (
119+ ServerRequestInterface $ request ,
120+ callable $ next
121+ ) : ResponseInterface {
122+ $ response = $ next ($ request );
123+
124+ $ response ->getBody ()->write ('Hello, callable world! ' );
125+
126+ return $ response ;
127+ },
128+ $ this ->responder
129+ ];
130+
131+ $ relay = new Relay ($ queue );
132+ $ response = $ relay ->handle (ServerRequestFactory::fromGlobals ());
133+
134+ $ this ->assertEquals ('Hello, callable world! ' , (string ) $ response ->getBody ());
135+ }
99136}
0 commit comments