From 6bb410ff217c6da69447d8a49b74edb0c07479e7 Mon Sep 17 00:00:00 2001 From: Rami H Date: Mon, 21 Oct 2013 23:32:23 +0100 Subject: [PATCH] Added http and controller related assertions Updated the FunctionalTestCase class to support standard MVC app tests ranging from controller to reponse Updated the readme markdown --- Library/Phalcon/Test/FunctionalTestCase.php | 152 +++++++++++++++++++- Library/Phalcon/Test/UnitTestCase.php | 6 +- README.md | 5 + 3 files changed, 158 insertions(+), 5 deletions(-) diff --git a/Library/Phalcon/Test/FunctionalTestCase.php b/Library/Phalcon/Test/FunctionalTestCase.php index 7595276bd..c893c2e4f 100755 --- a/Library/Phalcon/Test/FunctionalTestCase.php +++ b/Library/Phalcon/Test/FunctionalTestCase.php @@ -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(); @@ -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 + * + * $expected = array('Content-Type' => 'application/json') + * + * + * @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); } -} \ No newline at end of file +} diff --git a/Library/Phalcon/Test/UnitTestCase.php b/Library/Phalcon/Test/UnitTestCase.php index 310f31042..9d3a99059 100755 --- a/Library/Phalcon/Test/UnitTestCase.php +++ b/Library/Phalcon/Test/UnitTestCase.php @@ -22,6 +22,8 @@ */ namespace Phalcon\Test; + +use Phalcon\DI\FactoryDefault; use Phalcon\Config; use Phalcon\DI; use Phalcon\DiInterface; @@ -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( @@ -142,4 +144,4 @@ protected function cleanFile($path, $fileName) unlink($file); } } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 5578c526c..8a6144307 100644 --- a/README.md +++ b/README.md @@ -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)