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

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 91 deletions.
54 changes: 33 additions & 21 deletions src/PHPUnit/Controller/AbstractControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_ExpectationFailedException;
use Zend\Console\Console;
use Zend\EventManager\StaticEventManager;
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\SendResponseListener;
use Zend\Stdlib\Exception\LogicException;
use Zend\Stdlib\Parameters;
use Zend\Stdlib\ResponseInterface;
Expand All @@ -39,6 +39,12 @@ abstract class AbstractControllerTestCase extends PHPUnit_Framework_TestCase
*/
protected $useConsoleRequest = false;

/**
* Flag console used before tests
* @var boolean
*/
private $usedConsoleBackup;

/**
* Trace error when exception is throwed in application
* @var boolean
Expand All @@ -50,9 +56,18 @@ abstract class AbstractControllerTestCase extends PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->usedConsoleBackup = Console::isConsole();
$this->reset();
}

/**
* Restore params
*/
public function tearDown()
{
Console::overrideIsConsole($this->usedConsoleBackup);
}

/**
* Get the trace error flag
* @return boolean
Expand Down Expand Up @@ -134,25 +149,12 @@ public function getApplication()
return $this->application;
}
$appConfig = $this->applicationConfig;
if (!$this->useConsoleRequest) {
$consoleServiceConfig = array(
'service_manager' => array(
'factories' => array(
'ServiceListener' => 'Zend\Test\PHPUnit\Mvc\Service\ServiceListenerFactory',
),
),
);
$appConfig = array_replace_recursive($appConfig, $consoleServiceConfig);
}
Console::overrideIsConsole($this->getUseConsoleRequest());
$this->application = Application::init($appConfig);

$events = $this->application->getEventManager();
foreach ($events->getListeners(MvcEvent::EVENT_FINISH) as $listener) {
$callback = $listener->getCallback();
if (is_array($callback) && $callback[0] instanceof SendResponseListener) {
$events->detach($listener);
}
}
$events->detach($this->application->getServiceManager()->get('SendResponseListener'));

return $this->application;
}

Expand Down Expand Up @@ -195,7 +197,8 @@ public function url($url, $method = HttpRequest::METHOD_GET, $params = array())
{
$request = $this->getRequest();
if ($this->useConsoleRequest) {
$params = preg_split('#\s+#', $url);
preg_match_all('/(--\S+[= ]"\S*\s*\S*")|(--\S+=\S+|--\S+\s\S+|\S+)/', $url, $matches);
$params = str_replace(array(' "', '"'), array('=', ''), $matches[0]);
$request->params()->exchangeArray($params);
return $this;
}
Expand All @@ -211,10 +214,19 @@ public function url($url, $method = HttpRequest::METHOD_GET, $params = array())

if ($method == HttpRequest::METHOD_POST) {
$post = $params;
}

if ($method == HttpRequest::METHOD_GET) {
} elseif ($method == HttpRequest::METHOD_GET) {
$query = array_merge($query, $params);
} elseif ($method == HttpRequest::METHOD_PUT) {
array_walk($params,
function(&$item, $key) { $item = $key . '=' . $item; }
);
$content = implode('&', $params);
$request->setContent($content);
} elseif ($params) {
trigger_error(
'Additional params is only supported by GET, POST and PUT HTTP method',
E_USER_NOTICE
);
}

$request->setMethod($method);
Expand Down
31 changes: 0 additions & 31 deletions src/PHPUnit/Mvc/Service/RouterFactory.php

This file was deleted.

37 changes: 0 additions & 37 deletions src/PHPUnit/Mvc/Service/ServiceListenerFactory.php

This file was deleted.

18 changes: 18 additions & 0 deletions test/PHPUnit/Controller/AbstractConsoleControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,22 @@ public function testNotAssertConsoleOutputContains()
$this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');
$this->assertNotConsoleOutputContains('foo');
}

public function testAssertMatchedArgumentsWithValue()
{
$this->dispatch('filter --date="2013-03-07 00:00:00" --id=10 --text="custom text"');
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
$this->assertEquals("2013-03-07 00:00:00", $routeMatch->getParam('date'));
$this->assertEquals("10", $routeMatch->getParam('id'));
$this->assertEquals("custom text", $routeMatch->getParam('text'));
}

public function testAssertMatchedArgumentsWithValueWithoutEqualsSign()
{
$this->dispatch('filter --date "2013-03-07 00:00:00" --id=10 --text="custom text"');
$routeMatch = $this->getApplication()->getMvcEvent()->getRouteMatch();
$this->assertEquals("2013-03-07 00:00:00", $routeMatch->getParam('date'));
$this->assertEquals("10", $routeMatch->getParam('id'));
$this->assertEquals("custom text", $routeMatch->getParam('text'));
}
}
21 changes: 21 additions & 0 deletions test/PHPUnit/Controller/AbstractControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace ZendTest\Test\PHPUnit\Controller;

use Zend\Console\Console;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\RequestInterface;
Expand Down Expand Up @@ -61,6 +62,26 @@ public function testApplicationClass()
$this->assertEquals($applicationClass, 'Zend\Mvc\Application');
}

public function testApplicationClassAndTestRestoredConsoleFlag()
{
$this->assertTrue(Console::isConsole());
$this->getApplication();
$this->assertFalse(Console::isConsole());
$this->tearDown();
$this->assertTrue(Console::isConsole());

Console::overrideIsConsole(false);
parent::setUp();

$this->assertFalse(Console::isConsole());
$this->getApplication();
$this->assertFalse(Console::isConsole());

parent::tearDown();

$this->assertFalse(Console::isConsole());
}

public function testApplicationServiceLocatorClass()
{
$smClass = get_class($this->getApplicationServiceLocator());
Expand Down
10 changes: 10 additions & 0 deletions test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,22 @@ public function testAssertQueryWithDynamicPostParams()
public function testAssertQueryWithDynamicPostParamsInDispatchMethod()
{
$this->dispatch('/tests', 'POST', array('num_post' => 5));
$request = $this->getRequest();
$this->assertEquals($request->getMethod(), 'POST');
$this->assertQueryCount('div.post', 5);
$this->assertXpathQueryCount('//div[@class="post"]', 5);
$this->assertQueryCount('div.get', 0);
$this->assertXpathQueryCount('//div[@class="get"]', 0);
}

public function testAssertQueryWithDynamicPutParamsInDispatchMethod()
{
$this->dispatch('/tests', 'PUT', array('num_post' => 5, 'foo' => 'bar'));
$request = $this->getRequest();
$this->assertEquals($request->getMethod(), 'PUT');
$this->assertEquals('num_post=5&foo=bar', $request->getContent());
}
/*
public function testAssertUriWithHostname()
{
$this->dispatch('http://my.domain.tld:443');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ModuleDependenciesTest extends AbstractHttpControllerTestCase
public function testDependenciesModules()
{
$this->setApplicationConfig(
include __DIR__ . '/../../_files/application.config.with.dependencies.php'
include __DIR__ . '/../_files/application.config.with.dependencies.php'
);
$sm = $this->getApplicationServiceLocator();
$this->assertEquals(true, $sm->has('FooObject'));
Expand All @@ -36,7 +36,7 @@ public function testDependenciesModules()
public function testBadDependenciesModules()
{
$this->setApplicationConfig(
include __DIR__ . '/../../_files/application.config.with.dependencies.disabled.php'
include __DIR__ . '/../_files/application.config.with.dependencies.disabled.php'
);
$sm = $this->getApplicationServiceLocator();
$this->assertEquals(false, $sm->has('FooObject'));
Expand Down
10 changes: 10 additions & 0 deletions test/_files/Baz/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
),
),
),
'arguments' => array(
'type' => 'simple',
'options' => array(
'route' => 'filter --date= --id= --text=',
'defaults' => array(
'controller' => 'baz_index',
'action' => 'console',
),
),
),
),
),
),
Expand Down

0 comments on commit 9b75cec

Please sign in to comment.