|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @link http://github.com/zendframework/zf2 for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ZendTest\Mvc\Service; |
| 11 | + |
| 12 | +use Interop\Container\ContainerInterface; |
| 13 | +use PHPUnit_Framework_TestCase as TestCase; |
| 14 | +use Zend\Mvc\Service\ConsoleExceptionStrategyFactory; |
| 15 | +use Zend\Mvc\View\Console\ExceptionStrategy; |
| 16 | + |
| 17 | +class ConsoleExceptionStrategyFactoryTest extends TestCase |
| 18 | +{ |
| 19 | + public function createContainer($config = []) |
| 20 | + { |
| 21 | + $container = $this->prophesize(ContainerInterface::class); |
| 22 | + $container->has('config')->willReturn(true); |
| 23 | + $container->get('config')->willReturn($config); |
| 24 | + return $container->reveal(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testReturnsExceptionStrategy() |
| 28 | + { |
| 29 | + $factory = new ConsoleExceptionStrategyFactory(); |
| 30 | + $result = $factory($this->createContainer(), 'ConsoleExceptionStrategy'); |
| 31 | + $this->assertInstanceOf(ExceptionStrategy::class, $result); |
| 32 | + } |
| 33 | + |
| 34 | + public function testIfNotSetShouldUseDefaultExceptionMessageTemplate() |
| 35 | + { |
| 36 | + $factory = new ConsoleExceptionStrategyFactory(); |
| 37 | + $result = $factory($this->createContainer(), 'ConsoleExceptionStrategy'); |
| 38 | + |
| 39 | + $strategy = new ExceptionStrategy(); |
| 40 | + $this->assertEquals($strategy->getMessage(), $result->getMessage()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testExceptionMessageTemplateCanBeChangedInConfig() |
| 44 | + { |
| 45 | + $config = [ |
| 46 | + 'console' => [ |
| 47 | + 'view_manager' => [ |
| 48 | + 'exception_message' => 'Custom template :className :message', |
| 49 | + ] |
| 50 | + ] |
| 51 | + ]; |
| 52 | + $factory = new ConsoleExceptionStrategyFactory(); |
| 53 | + $result = $factory($this->createContainer($config), 'ConsoleExceptionStrategy'); |
| 54 | + |
| 55 | + $this->assertEquals($config['console']['view_manager']['exception_message'], $result->getMessage()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testDisplayExceptionsEnabledByDefault() |
| 59 | + { |
| 60 | + $factory = new ConsoleExceptionStrategyFactory(); |
| 61 | + $result = $factory($this->createContainer(), 'ConsoleExceptionStrategy'); |
| 62 | + |
| 63 | + $this->assertTrue($result->displayExceptions(), 'displayExceptions should be enabled by default.'); |
| 64 | + } |
| 65 | + |
| 66 | + public function testDisplayExceptionsCanBeChangedInConfig() |
| 67 | + { |
| 68 | + $config = [ |
| 69 | + 'console' => [ |
| 70 | + 'view_manager' => [ |
| 71 | + 'display_exceptions' => false, |
| 72 | + ] |
| 73 | + ] |
| 74 | + ]; |
| 75 | + $factory = new ConsoleExceptionStrategyFactory(); |
| 76 | + $result = $factory($this->createContainer($config), 'ConsoleExceptionStrategy'); |
| 77 | + |
| 78 | + $this->assertFalse($result->displayExceptions(), 'displayExceptions should be disabled in config.'); |
| 79 | + } |
| 80 | +} |
0 commit comments