@@ -52,6 +52,36 @@ public function testMiddlewareStackIsTraversed()
5252 $ delegate ->process ($ firstRequestProphecy ->reveal ());
5353 }
5454
55+ public function testMiddlewareStackIsTraversedUsingContainer ()
56+ {
57+ $ firstRequestProphecy = $ this ->prophesize (ServerRequestInterface::class);
58+ $ secondRequestProphecy = $ this ->prophesize (ServerRequestInterface::class);
59+ $ thirdRequestMock = $ this ->prophesize (ServerRequestInterface::class)->reveal ();
60+
61+ $ secondRequestProphecy ->getAttribute ('total ' )->shouldBeCalled (1 )->willReturn (2 );
62+ $ secondRequestProphecy ->withAttribute ('total ' , 4 )->shouldBeCalled (1 )->willReturn ($ thirdRequestMock );
63+
64+ $ firstRequestProphecy ->getAttribute ('total ' )->shouldBeCalled (1 )->willReturn (1 );
65+ $ firstRequestProphecy ->withAttribute ('total ' , 2 )->shouldBeCalled (1 )->willReturn ($ secondRequestProphecy ->reveal ());
66+
67+ $ responseMock = $ this ->createMock (ResponseInterface::class);
68+ $ assertion = function (ServerRequestInterface $ request ) use ($ thirdRequestMock , $ responseMock ) {
69+ $ this ->assertSame ($ thirdRequestMock , $ request );
70+ return $ responseMock ;
71+ };
72+
73+ $ containerProphecy = $ this ->prophesize (ContainerInterface::class);
74+ $ containerProphecy ->has ('one ' )->shouldBeCalled (1 )->willReturn (true );
75+ $ containerProphecy ->has ('two ' )->shouldBeCalled (1 )->willReturn (true );
76+ $ containerProphecy ->get ('one ' )->shouldBeCalled (1 )->willReturn (new PlusOneMiddleware ());
77+ $ containerProphecy ->get ('two ' )->shouldBeCalled (1 )->willReturn (new PlusTwoMiddleware ());
78+ $ containerMock = $ containerProphecy ->reveal ();
79+
80+
81+ $ delegate = new Delegate (['one ' , 'two ' ], $ assertion , $ containerMock );
82+ $ delegate ->process ($ firstRequestProphecy ->reveal ());
83+ }
84+
5585 public function testMiddlewareStackStop ()
5686 {
5787 $ requestMock = $ this ->prophesize (ServerRequestInterface::class)->reveal ();
@@ -63,20 +93,26 @@ public function testMiddlewareStackStop()
6393 $ this ->assertSame ($ responseMock , $ delegate ->process ($ requestMock ));
6494 }
6595
66- public function testInvalidLazyLoadingMiddlewareFromContainer ()
96+ public function testInvalidMiddlewareisPassedToInvalidArgumentException ()
6797 {
6898 $ this ->expectException (InvalidArgumentException::class);
69-
99+ $ invalidMiddleware = new \ SplStack ();
70100 $ requestMock = $ this ->prophesize (ServerRequestInterface::class)->reveal ();
71101 $ containerProphecy = $ this ->prophesize (ContainerInterface::class);
72102 $ containerProphecy ->has ('InvalidMiddleware ' )->shouldBeCalled (1 )->willReturn (true );
73- $ containerProphecy ->get ('InvalidMiddleware ' )->shouldBeCalled (1 )->willReturn (new \SplStack ());
103+ $ containerProphecy ->has (Argument::any ())->shouldBeCalled (2 )->willReturn (false );
104+ $ containerProphecy ->get ('InvalidMiddleware ' )->shouldBeCalled (1 )->willReturn ($ invalidMiddleware );
74105 $ containerMock = $ containerProphecy ->reveal ();
75106
76107 $ delegate = new Delegate (['InvalidMiddleware ' ], function () {
77108 }, $ containerMock );
78109
79- $ delegate ->process ($ requestMock );
110+ try {
111+ $ delegate ->process ($ requestMock );
112+ } catch (InvalidArgumentException $ e ) {
113+ $ this ->assertSame ($ e ->getInvalidMiddleware (), $ invalidMiddleware );
114+ throw $ e ;
115+ }
80116 }
81117
82118 public function testLazyLoadingMiddlewareFromContainer ()
0 commit comments