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

Commit

Permalink
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 117 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.4-dev",
"dev-develop": "2.5-dev"
"dev-master": "2.1-dev",
"dev-develop": "2.2-dev"
}
},
"autoload-dev": {
Expand Down
59 changes: 38 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 @@ -105,6 +120,7 @@ public function getApplicationConfig()
/**
* Set the application config
* @param array $applicationConfig
* @return AbstractControllerTestCase
* @throws LogicException
*/
public function setApplicationConfig($applicationConfig)
Expand Down Expand Up @@ -133,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 @@ -186,13 +189,16 @@ public function getResponse()
* Set the request URL
*
* @param string $url
* @param string|null $method
* @param array|null $params
* @return AbstractControllerTestCase
*/
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 @@ -208,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 All @@ -229,6 +244,8 @@ public function url($url, $method = HttpRequest::METHOD_GET, $params = array())
* The URL provided set the request URI in the request object.
*
* @param string $url
* @param string|null $method
* @param array|null $params
* @throws \Exception
*/
public function dispatch($url, $method = HttpRequest::METHOD_GET, $params = array())
Expand Down
45 changes: 23 additions & 22 deletions src/PHPUnit/Controller/AbstractHttpControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function assertNotResponseHeaderContains($header, $match)
*/
public function assertResponseHeaderRegex($header, $pattern)
{
$responseHeader = $this->getResponseHeader($header);;
$responseHeader = $this->getResponseHeader($header);
if (!$responseHeader) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header, header "%s" do not exists', $header
Expand Down Expand Up @@ -170,17 +170,15 @@ public function assertRedirect()
{
$responseHeader = $this->getResponseHeader('Location');
if (false === $responseHeader) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException(
'Failed asserting response is NOT a redirect'
));
);
}
$this->assertNotEquals(false, $responseHeader);
}

/**
* Assert that response is NOT a redirect
*
* @param string $message
*/
public function assertNotRedirect()
{
Expand All @@ -203,9 +201,9 @@ public function assertRedirectTo($url)
{
$responseHeader = $this->getResponseHeader('Location');
if (!$responseHeader) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException(
'Failed asserting response is a redirect'
));
);
}
if ($url != $responseHeader->getFieldValue()) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
Expand All @@ -220,15 +218,14 @@ public function assertRedirectTo($url)
* Assert that response does not redirect to given URL
*
* @param string $url
* @param string $message
*/
public function assertNotRedirectTo($url)
{
$responseHeader = $this->getResponseHeader('Location');
if (!$responseHeader) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException(
'Failed asserting response is a redirect'
));
);
}
if ($url == $responseHeader->getFieldValue()) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
Expand All @@ -247,9 +244,9 @@ public function assertRedirectRegex($pattern)
{
$responseHeader = $this->getResponseHeader('Location');
if (!$responseHeader) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException(
'Failed asserting response is a redirect'
));
);
}
if (!preg_match($pattern, $responseHeader->getFieldValue())) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
Expand All @@ -269,9 +266,9 @@ public function assertNotRedirectRegex($pattern)
{
$responseHeader = $this->getResponseHeader('Location');
if (!$responseHeader) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
throw new PHPUnit_Framework_ExpectationFailedException(
'Failed asserting response is a redirect'
));
);
}
if (preg_match($pattern, $responseHeader->getFieldValue())) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
Expand Down Expand Up @@ -427,7 +424,8 @@ public function assertNotXpathQuery($path)
*/
private function queryCountAssertion($path, $count, $useXpath = false)
{
$match = $this->queryCount($path);
$method = $useXpath ? 'xpathQueryCount' : 'queryCount';
$match = $this->$method($path);
if ($match != $count) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times, actually occurs %d times',
Expand Down Expand Up @@ -468,7 +466,8 @@ public function assertXpathQueryCount($path, $count)
*/
private function notQueryCountAssertion($path, $count, $useXpath = false)
{
$match = $this->queryCount($path);
$method = $useXpath ? 'xpathQueryCount' : 'queryCount';
$match = $this->$method($path);
if ($match == $count) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times',
Expand Down Expand Up @@ -509,7 +508,8 @@ public function assertNotXpathQueryCount($path, $count)
*/
private function queryCountMinAssertion($path, $count, $useXpath = false)
{
$match = $this->queryCount($path);
$method = $useXpath ? 'xpathQueryCount' : 'queryCount';
$match = $this->$method($path);
if ($match < $count) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times, actually occurs %d times',
Expand Down Expand Up @@ -550,7 +550,8 @@ public function assertXpathQueryCountMin($path, $count)
*/
private function queryCountMaxAssertion($path, $count, $useXpath = false)
{
$match = $this->queryCount($path);
$method = $useXpath ? 'xpathQueryCount' : 'queryCount';
$match = $this->$method($path);
if ($match > $count) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times, actually occurs %d times',
Expand Down Expand Up @@ -591,7 +592,7 @@ public function assertXpathQueryCountMax($path, $count)
*/
private function queryContentContainsAssertion($path, $match, $useXpath = false)
{
$result = $this->query($path);
$result = $this->query($path, $useXpath);
if ($result->count() == 0) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s EXISTS', $path
Expand Down Expand Up @@ -637,7 +638,7 @@ public function assertXpathQueryContentContains($path, $match)
*/
private function notQueryContentContainsAssertion($path, $match, $useXpath = false)
{
$result = $this->query($path);
$result = $this->query($path, $useXpath);
if ($result->count() == 0) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s EXISTS', $path
Expand Down Expand Up @@ -683,7 +684,7 @@ public function assertNotXpathQueryContentContains($path, $match)
*/
private function queryContentRegexAssertion($path, $pattern, $useXpath = false)
{
$result = $this->query($path);
$result = $this->query($path, $useXpath);
if ($result->count() == 0) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s EXISTS', $path
Expand Down Expand Up @@ -729,7 +730,7 @@ public function assertXpathQueryContentRegex($path, $pattern)
*/
private function notQueryContentRegexAssertion($path, $pattern, $useXpath = false)
{
$result = $this->query($path);
$result = $this->query($path, $useXpath);
if ($result->count() == 0) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting node DENOTED BY %s EXISTS', $path
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'));
}
}
Loading

0 comments on commit 3afdd4f

Please sign in to comment.