Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/3907'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 11, 2013
2 parents 0d92548 + 5e2b902 commit 7a829bf
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 8 deletions.
17 changes: 14 additions & 3 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public static function getInstance($forceAdapter = null, $forceCharset = null)
return static::$instance;
}

/**
* Reset the console instance
*/
public static function resetInstance()
{
static::$instance = null;
}

/**
* Check if currently running under MS Windows
*
Expand Down Expand Up @@ -135,10 +143,10 @@ public static function isAnsicon()
*/
public static function isConsole()
{
if (null !== static::$isConsole && is_bool(static::$isConsole)) {
return static::$isConsole;
if (null === static::$isConsole) {
static::$isConsole = (PHP_SAPI == 'cli');
}
return PHP_SAPI == 'cli';
return static::$isConsole;
}

/**
Expand All @@ -148,6 +156,9 @@ public static function isConsole()
*/
public static function overrideIsConsole($flag)
{
if(null != $flag) {
$flag = (bool)$flag;
}
static::$isConsole = $flag;
}

Expand Down
18 changes: 13 additions & 5 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ public function getParam($name, $default = null)
*/
public function params()
{
if ($this->params === null) {
$this->params = new Parameters();
}

return $this->params;
return $this->getParams();
}

/**
Expand All @@ -139,6 +135,18 @@ public function setEnv(Parameters $env)
return $this;
}

/**
* Return a single parameter container responsible for env parameters
*
* @param string $name Parameter name
* @param string $default (optional) default value in case the parameter does not exist
* @return \Zend\Stdlib\Parameters
*/
public function getEnv($name, $default = null)
{
return $this->env()->get($name, $default);
}

/**
* Return the parameter container responsible for env parameters
*
Expand Down
86 changes: 86 additions & 0 deletions test/ConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Console
*/

namespace ZendTest\Console;

use Zend\Console\Console;
use Zend\Console\Adapter;

/**
* @category Zend
* @package Zend_Console
* @subpackage UnitTests
* @group Zend_Console
*/
class ConsoleTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Console::overrideIsConsole(null);
Console::resetInstance();
}

public function testCanTestIsConsole()
{
$this->assertTrue(Console::isConsole());
$className = Console::detectBestAdapter();
$adpater = new $className;
$this->assertTrue($adpater instanceof Adapter\AdapterInterface);

Console::overrideIsConsole(false);

$this->assertFalse(Console::isConsole());
$this->assertEquals(null, Console::detectBestAdapter());
}

public function testCanOverrideIsConsole()
{
$this->assertEquals(true, Console::isConsole());

Console::overrideIsConsole(true);
$this->assertEquals(true, Console::isConsole());

Console::overrideIsConsole(false);
$this->assertEquals(false, Console::isConsole());

Console::overrideIsConsole(1);
$this->assertEquals(true, Console::isConsole());

Console::overrideIsConsole('false');
$this->assertEquals(true, Console::isConsole());
}

public function testCanGetInstance()
{
$console = Console::getInstance();
$this->assertTrue($console instanceof Adapter\AdapterInterface);
}

public function testCanNotGetInstanceInNoConsoleMode()
{
Console::overrideIsConsole(false);
$this->setExpectedException('Zend\Console\Exception\RuntimeException');
Console::getInstance();
}

public function testCanForceInstance()
{
$console = Console::getInstance('Posix');
$this->assertTrue($console instanceof Adapter\AdapterInterface);
$this->assertTrue($console instanceof Adapter\Posix);

Console::overrideIsConsole(null);
Console::resetInstance();

$console = Console::getInstance('Windows');
$this->assertTrue($console instanceof Adapter\AdapterInterface);
$this->assertTrue($console instanceof Adapter\Windows);
}
}
46 changes: 46 additions & 0 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Console
*/

namespace ZendTest\Console;

use Zend\Console\Request;

/**
* @category Zend
* @package Zend_Console
* @subpackage UnitTests
* @group Zend_Console
*/
class RequestTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (ini_get('register_argc_argv') == false) {
$this->markTestSkipped("Cannot Test Zend\\Console\\Getopt without 'register_argc_argv' ini option true.");
}
}

public function testCanConstructRequestAndGetParams()
{
$_SERVER['argv'] = array('foo.php', 'foo' => 'baz', 'bar');
$_ENV["FOO_VAR"] = "bar";

$request = new Request();
$params = $request->getParams();

$this->assertEquals(2, count($params));
$this->assertEquals($params->toArray(), array('foo' => 'baz', 'bar'));
$this->assertEquals($request->getParam('foo'), 'baz');
$this->assertEquals($request->getScriptName(), 'foo.php');
$this->assertEquals(1, count($request->env()));
$this->assertEquals($request->env()->get('FOO_VAR'), 'bar');
$this->assertEquals($request->getEnv('FOO_VAR'), 'bar');
}
}
78 changes: 78 additions & 0 deletions test/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Console
*/

namespace ZendTest\Console;

use Zend\Console\Response;

/**
* @category Zend
* @package Zend_Console
* @subpackage UnitTests
* @group Zend_Console
*/
class ResponseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Response
*/
protected $response;

public function setUp()
{
$this->response = new Response();
}

public function testInitialisation()
{
$this->assertEquals(false, $this->response->contentSent());
$this->assertEquals(0, $this->response->getErrorLevel());
}

public function testSetContent()
{
$this->response->setContent('foo, bar');
$this->assertEquals(false, $this->response->contentSent());
ob_start();
$this->response->sendContent();
$content = ob_get_clean();
$this->assertEquals('foo, bar', $content);
$this->assertEquals(true, $this->response->contentSent());
$this->assertEquals($this->response, $this->response->sendContent());
}

/*
public function testSetContentWithExit()
{
if (!function_exists('set_exit_overload')) {
$this->markTestSkipped("Install ext/test_helpers to test method with exit : https://github.com/sebastianbergmann/php-test-helpers.");
}
$self = $this;
set_exit_overload(
function($param = null) use ($self) {
if ($param) {
$self->assertEquals($param, 1);
}
return false;
}
);
$this->response->setErrorLevel(1);
$this->response->setContent('foo, bar');
ob_start();
$this->response->send();
$content = ob_get_clean();
$this->assertEquals('foo, bar', $content);
unset_exit_overload();
}
*/
}

0 comments on commit 7a829bf

Please sign in to comment.