Skip to content
Browse files

Merge branch 'hotfix/2400' into develop

Forward port #2400
  • Loading branch information...
2 parents b8039b8 + 94ebce6 commit 4aa335a23e2761a105ca570e77f139d338b2810b @weierophinney weierophinney committed Sep 21, 2012
View
23 library/Zend/Console/Console.php
@@ -25,6 +25,13 @@
protected static $instance;
/**
+ * Allow overriding whether or not we're in a console env. If set, and
+ * boolean, returns that value from isConsole().
+ * @var bool
+ */
+ protected static $isConsole;
+
+ /**
* Create and return Adapter\AdapterInterface instance.
*
* @param null|string $forceAdapter Optional adapter class name. Ccan be absolute namespace or class name
@@ -125,14 +132,30 @@ public static function isAnsicon()
/**
* Check if running in a console environment (CLI)
*
+ * By default, returns value of PHP_SAPI global constant. If $isConsole is
+ * set, and a boolean value, that value will be returned.
+ *
* @return bool
*/
public static function isConsole()
{
+ if (null !== static::$isConsole && is_bool(static::$isConsole)) {
+ return static::$isConsole;
+ }
return PHP_SAPI == 'cli';
}
/**
+ * Override the "is console environment" flag
+ *
+ * @param null|bool $flag
+ */
+ public static function overrideIsConsole($flag)
+ {
+ static::$isConsole = $flag;
+ }
+
+ /**
* Try to detect best matching adapter
* @return string|null
*/
View
4 library/Zend/Mvc/Service/ViewHelperManagerFactory.php
@@ -10,6 +10,7 @@
namespace Zend\Mvc\Service;
+use Zend\Console\Console;
use Zend\Mvc\Exception;
use Zend\Mvc\Router\RouteMatch;
use Zend\ServiceManager\ConfigInterface;
@@ -67,7 +68,8 @@ public function createService(ServiceLocatorInterface $serviceLocator)
// Configure URL view helper with router
$plugins->setFactory('url', function($sm) use($serviceLocator) {
$helper = new ViewHelper\Url;
- $helper->setRouter($serviceLocator->get('HttpRouter'));
+ $router = Console::isConsole() ? 'HttpRouter' : 'Router';
+ $helper->setRouter($serviceLocator->get($router));
$match = $serviceLocator->get('application')
->getMvcEvent()
View
130 tests/ZendTest/View/Helper/UrlIntegrationTest.php
@@ -0,0 +1,130 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_View
+ */
+
+namespace ZendTest\View\Helper;
+
+use Zend\Console\Console;
+use Zend\ServiceManager\ServiceManager;
+use Zend\ServiceManager\Config as ServiceManagerConfig;
+use Zend\View\Helper\Url as UrlHelper;
+
+/**
+ * url() helper test -- tests integration with MVC
+ *
+ * @category Zend
+ * @package Zend_View
+ * @subpackage UnitTests
+ * @group Zend_View
+ * @group Zend_View_Helper
+ */
+class UrlIntegrationTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ $config = array(
+ 'router' => array(
+ 'routes' => array(
+ 'test' => array(
+ 'type' => 'Literal',
+ 'options' => array(
+ 'route' => '/test',
+ 'defaults' => array(
+ 'controller' => 'Test\Controller\Test',
+ ),
+ ),
+ ),
+ ),
+ ),
+ 'console' => array(
+ 'router' => array(
+ 'routes' => array(
+ 'test' => array(
+ 'type' => 'Simple',
+ 'options' => array(
+ 'route' => 'test this',
+ 'defaults' => array(
+ 'controller' => 'Test\Controller\TestConsole',
+ ),
+ ),
+ ),
+ ),
+ ),
+ ),
+ );
+ $serviceConfig = array(
+ 'invokables' => array(
+ 'SharedEventManager' => 'Zend\EventManager\SharedEventManager',
+ 'DispatchListener' => 'Zend\Mvc\DispatchListener',
+ 'RouteListener' => 'Zend\Mvc\RouteListener',
+ ),
+ 'factories' => array(
+ 'Application' => 'Zend\Mvc\Service\ApplicationFactory',
+ 'EventManager' => 'Zend\Mvc\Service\EventManagerFactory',
+ 'ViewHelperManager' => 'Zend\Mvc\Service\ViewHelperManagerFactory',
+ 'Request' => 'Zend\Mvc\Service\RequestFactory',
+ 'Response' => 'Zend\Mvc\Service\ResponseFactory',
+ 'Router' => 'Zend\Mvc\Service\RouterFactory',
+ 'ConsoleRouter' => 'Zend\Mvc\Service\RouterFactory',
+ 'HttpRouter' => 'Zend\Mvc\Service\RouterFactory',
+ 'ViewManager' => 'Zend\Mvc\Service\ViewManagerFactory',
+ 'ViewResolver' => 'Zend\Mvc\Service\ViewResolverFactory',
+ 'ViewTemplateMapResolver' => 'Zend\Mvc\Service\ViewTemplateMapResolverFactory',
+ 'ViewTemplatePathStack' => 'Zend\Mvc\Service\ViewTemplatePathStackFactory',
+ ),
+ 'shared' => array(
+ 'EventManager' => false,
+ ),
+ );
+ $serviceConfig = new ServiceManagerConfig($serviceConfig);
+
+ $this->serviceManager = new ServiceManager($serviceConfig);
+ $this->serviceManager->setService('Config', $config);
+ $this->serviceManager->setAlias('Configuration', 'Config');
+ }
+
+ public function testUrlHelperWorksUnderNormalHttpParadigms()
+ {
+ Console::overrideIsConsole(false);
+ $this->serviceManager->get('Application')->bootstrap();
+ $request = $this->serviceManager->get('Request');
+ $this->assertInstanceOf('Zend\Http\Request', $request);
+ $viewHelpers = $this->serviceManager->get('ViewHelperManager');
+ $urlHelper = $viewHelpers->get('url');
+ $test = $urlHelper('test');
+ $this->assertEquals('/test', $test);
+ }
+
+ public function testUrlHelperWorksWithForceCanonicalFlag()
+ {
+ Console::overrideIsConsole(false);
+ $this->serviceManager->get('Application')->bootstrap();
+ $request = $this->serviceManager->get('Request');
+ $this->assertInstanceOf('Zend\Http\Request', $request);
+ $router = $this->serviceManager->get('Router');
+ $router->setRequestUri($request->getUri());
+ $request->setUri('http://example.com/test');
+ $viewHelpers = $this->serviceManager->get('ViewHelperManager');
+ $urlHelper = $viewHelpers->get('url');
+ $test = $urlHelper('test', array(), array('force_canonical' => true));
+ $this->assertContains('/test', $test);
+ }
+
+ public function testUrlHelperUnderConsoleParadigmShouldReturnHttpRoutes()
+ {
+ Console::overrideIsConsole(true);
+ $this->serviceManager->get('Application')->bootstrap();
+ $request = $this->serviceManager->get('Request');
+ $this->assertInstanceOf('Zend\Console\Request', $request);
+ $viewHelpers = $this->serviceManager->get('ViewHelperManager');
+ $urlHelper = $viewHelpers->get('url');
+ $test = $urlHelper('test');
+ $this->assertEquals('/test', $test);
+ }
+}

0 comments on commit 4aa335a

Please sign in to comment.
Something went wrong with that request. Please try again.