Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 149 additions & 3 deletions Library/Phalcon/Test/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@

abstract class FunctionalTestCase extends ModelTestCase
{
protected $application;

/**
* Sets the test up by loading the DI container and other stuff
*
* @return Phalcon\DI
* @return void
*/
protected function setUp()
{
parent::setUp();

// Set the dispatcher
$this->di->set(
$this->di->setShared(
'dispatcher',
function () {
$dispatcher = new PhDispatcher();
Expand All @@ -54,5 +56,149 @@ function () {
return new PhEscaper();
}
);

if ($this->di instanceof DiInterface) {
$this->application = new PhApplication($this->di);
}

}

/**
* Ensures that each test has it's own DI and all globals are purged
*
* @return void
*/
protected function tearDown()
{
$this->di->reset();
$this->application = null;

$_SESSION = array();
$_GET = array();
$_POST = array();
$_COOKIE = array();
}

/**
* Dispatches a given url and sets the response object accordingly
*
* @param string $url The request url
*
* @return void
*/
protected function dispatch($url)
{
$this->di->setShared('response', $this->application->handle($url));
}

/**
* Assert that the last dispatched controller matches the given controller class name
*
* @param string $expected The expected controller name
*
* @return void
*/
public function assertController($expected)
{
$actual = $this->di->getShared('dispatcher')->getControllerName();
if ($actual != $expected) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Failed asserting Controller name "%s", actual Controller name is "%s"',
$expected,
$actual
)
);
}
$this->assertEquals($expected, $actual);
}

/**
* Assert that the last dispatched action matches the given action name
*
* @param string $expected The expected action name
*
* @return void
*/
public function assertAction($expected)
{
$actual = $this->di->getShared('dispatcher')->getActionName();
if ($actual != $expected) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Failed asserting Action name "%s", actual Action name is "%s"',
$expected,
$actual
)
);
}
$this->assertEquals($expected, $actual);
}

/**
* Assert that the response headers contains the given array
* <code>
* $expected = array('Content-Type' => 'application/json')
* </code>
*
* @param string $expected The expected headers
*
* @return void
*/
public function assertHeader(array $expected)
{
foreach ($expected as $expectedField => $expectedValue) {
$actualValue = $this->di->getShared('response')->getHeaders()->get($expectedField);
if ($actualValue != $expectedValue) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Failed asserting "%s" has a value of "%s", actual "%s" header value is "%s"',
$expectedField,
$expectedValue,
$expectedField,
$actualValue
)
);
}
$this->assertEquals($expectedValue, $actualValue);
}
}

/**
* Asserts that the response code matches the given one
*
* @param string $expected the expected response code
*
* @return void
*/
public function assertResponseCode($expected)
{
$actualValue = $this->di->getShared('response')->getHeaders()->get('Status');
if (empty($actualValue) || stristr($actualValue, $expected)) {
throw new \PHPUnit_Framework_ExpectationFailedException(
sprintf(
'Failed asserting response code "%s", actual response code is "%s"',
$expected,
$actualValue
)
);
}
$this->assertContains($expected, $actualValue);
}

/**
* Asserts that the dispatched url resulted in a redirection
*
* @return void
*/
public function assertRedirection()
{
$actual = $this->di->getShared('dispatcher')->wasForwarded();
if (!$actual) {
throw new \PHPUnit_Framework_ExpectationFailedException(
'Failed asserting response caused a redirect'
);
}
$this->assertTrue($actual);
}
}
}
6 changes: 4 additions & 2 deletions Library/Phalcon/Test/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/

namespace Phalcon\Test;

use Phalcon\DI\FactoryDefault;
use Phalcon\Config;
use Phalcon\DI;
use Phalcon\DiInterface;
Expand Down Expand Up @@ -67,7 +69,7 @@ protected function setUp(DiInterface $di = null, Config $config = null)
DI::reset();

// Instantiate a new DI container
$di = new DI();
$di = new FactoryDefault();

// Set the URL
$di->set(
Expand Down Expand Up @@ -142,4 +144,4 @@ protected function cleanFile($path, $fileName)
unlink($file);
}
}
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ $loader->register();
* [Phalcon\Error](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Error) - Error handler used to centralize the error handling and displaying clean error pages (theDisco)
* [Phalcon\Utils\PrettyExceptions](https://github.com/phalcon/pretty-exceptions) - Pretty Exceptions is an utility to show exceptions/errors/warnings/notices using a nicely visualization. (phalcon/kenjikobe)

### Test
* [Phalcon\Test\FunctionalTestCase](https://github.com/silverbadge/incubator/tree/master/Library/Phalcon/Test) - Mvc app test case wrapper (thecodeassassin)
* [Phalcon\Test\ModelTestCase](https://github.com/silverbadge/incubator/tree/master/Library/Phalcon/Test) - Model test case wrapper (thecodeassassin)
* [Phalcon\Test\UnitTestCase](https://github.com/silverbadge/incubator/tree/master/Library/Phalcon/Test) - Generic test case wrapper (thecodeassassin)

### Translate
* [Phalcon\Translate\Adapter\Gettext](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Translate/Adapter) - Translation adapter for Gettext (phalcon)
* [Phalcon\Translate\Adapter\Database](https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Translate/Adapter) - Translation adapter using relational databases (phalcon)
Expand Down