99
1010namespace ZendTest \Expressive \Container ;
1111
12+ use Closure ;
1213use PHPUnit \Framework \TestCase ;
1314use Prophecy \Prophecy \ObjectProphecy ;
1415use Psr \Container \ContainerInterface ;
1516use Psr \Http \Message \ResponseInterface ;
17+ use RuntimeException ;
18+ use TypeError ;
1619use Zend \Expressive \Container \ErrorHandlerFactory ;
1720use Zend \Expressive \Middleware \ErrorResponseGenerator ;
1821use Zend \Stratigility \Middleware \ErrorHandler ;
@@ -28,15 +31,45 @@ public function setUp()
2831 $ this ->container = $ this ->prophesize (ContainerInterface::class);
2932 }
3033
34+ public function testFactoryFailsIfResponseServiceIsMissing ()
35+ {
36+ $ exception = new RuntimeException ();
37+ $ this ->container ->has (ErrorResponseGenerator::class)->willReturn (false );
38+ $ this ->container ->get (ErrorResponseGenerator::class)->shouldNotBeCalled ();
39+ $ this ->container ->get (ResponseInterface::class)->willThrow ($ exception );
40+
41+ $ factory = new ErrorHandlerFactory ();
42+
43+ $ this ->expectException (RuntimeException::class);
44+ $ factory ($ this ->container ->reveal ());
45+ }
46+
47+ public function testFactoryFailsIfResponseServiceReturnsResponse ()
48+ {
49+ $ response = $ this ->prophesize (ResponseInterface::class)->reveal ();
50+ $ this ->container ->has (ErrorResponseGenerator::class)->willReturn (false );
51+ $ this ->container ->get (ErrorResponseGenerator::class)->shouldNotBeCalled ();
52+ $ this ->container ->get (ResponseInterface::class)->willReturn ($ response );
53+
54+ $ factory = new ErrorHandlerFactory ();
55+
56+ $ this ->expectException (TypeError::class);
57+ $ factory ($ this ->container ->reveal ());
58+ }
59+
3160 public function testFactoryCreatesHandlerWithStratigilityGeneratorIfNoGeneratorServiceAvailable ()
3261 {
3362 $ this ->container ->has (ErrorResponseGenerator::class)->willReturn (false );
63+ $ this ->container ->get (ErrorResponseGenerator::class)->shouldNotBeCalled ();
64+
65+ $ this ->container ->get (ResponseInterface::class)->willReturn (function () {
66+ });
3467
3568 $ factory = new ErrorHandlerFactory ();
3669 $ handler = $ factory ($ this ->container ->reveal ());
3770
3871 $ this ->assertInstanceOf (ErrorHandler::class, $ handler );
39- $ this ->assertAttributeInstanceOf (ResponseInterface ::class, 'responsePrototype ' , $ handler );
72+ $ this ->assertAttributeInstanceOf (Closure ::class, 'responseFactory ' , $ handler );
4073 $ this ->assertAttributeInstanceOf (StratigilityGenerator::class, 'responseGenerator ' , $ handler );
4174 }
4275
@@ -46,11 +79,14 @@ public function testFactoryCreatesHandlerWithGeneratorIfGeneratorServiceAvailabl
4679 $ this ->container ->has (ErrorResponseGenerator::class)->willReturn (true );
4780 $ this ->container ->get (ErrorResponseGenerator::class)->willReturn ($ generator );
4881
82+ $ this ->container ->get (ResponseInterface::class)->willReturn (function () {
83+ });
84+
4985 $ factory = new ErrorHandlerFactory ();
5086 $ handler = $ factory ($ this ->container ->reveal ());
5187
5288 $ this ->assertInstanceOf (ErrorHandler::class, $ handler );
53- $ this ->assertAttributeInstanceOf (ResponseInterface ::class, 'responsePrototype ' , $ handler );
89+ $ this ->assertAttributeInstanceOf (Closure ::class, 'responseFactory ' , $ handler );
5490 $ this ->assertAttributeSame ($ generator , 'responseGenerator ' , $ handler );
5591 }
5692}
0 commit comments