From 35c6cb9ba3eb5e77f4eb4f1a93d8569d4b291236 Mon Sep 17 00:00:00 2001 From: alex <> Date: Mon, 21 Nov 2005 13:02:49 +0000 Subject: Added unit tests for THttpRequest. Needs some more work... --- tests/UnitTests/framework/Web/utHttpRequest.php | 268 ++++++++++++++++++++++++ tests/UnitTests/framework/common.php | 61 +++++- 2 files changed, 327 insertions(+), 2 deletions(-) create mode 100644 tests/UnitTests/framework/Web/utHttpRequest.php (limited to 'tests') diff --git a/tests/UnitTests/framework/Web/utHttpRequest.php b/tests/UnitTests/framework/Web/utHttpRequest.php new file mode 100644 index 00000000..3da58a69 --- /dev/null +++ b/tests/UnitTests/framework/Web/utHttpRequest.php @@ -0,0 +1,268 @@ +request =& new THttpRequest(); + $this->module =& $this->request; // $this->module is used automatically by the underlying class + $this->initModule($this->defaultConfig); + } + + public function tearDown() { + $this->request = null; + parent::tearDown(); + } + + public function testInit($application,$config) { + // Configuration case 1... + $_SERVER = array( + "REQUEST_URI" => "/foo.php?bar", + "SCRIPT_NAME" => "/foo.php", + "QUEST_STRING" => "bar", + "PATH_INFO" => "/foo.php", + "PHP_SELF" => "/foo.php", + ); + $this->mockApplication->expectOnce("setRequest"); + $this->initModule(); + $this->mockApplication->tally(); + $this->assertIsA($this->request->getItems(), "TMap"); + + $this->assertEqual($this->request->getRequestUri(), "/foo.php?bar"); + $this->assertEqual($this->request->getPathInfo(), "/foo.php"); + + $_SERVER = array( + "REQUEST_URI" => "/aaa/bbb/ccc/foo.php?bar", + "SCRIPT_NAME" => "", + "QUEST_STRING" => "", + "PATH_INFO" => "/aaa/bbb/ccc/foo.php", + "PHP_SELF" => "/aaa/bbb/ccc/foo.php", + ); + $this->initModule(); + $this->assertEqual($this->request->getRequestUri(), "/aaa/bbb/ccc/foo.php?bar"); + $this->assertEqual($this->request->getPathInfo(), "/aaa/bbb/ccc/foo.php"); + + $_SERVER = array( + "SCRIPT_NAME" => "/foo.php", + "QUEST_STRING" => "bar", + "PATH_INFO" => "", + "PHP_SELF" => "/foo.php", + ); + $this->initModule(); + $this->assertEqual($this->request->getRequestUri(), "/foo.php?bar"); + $this->assertEqual($this->request->getPathInfo(), "/foo.php"); + + $_SERVER = array( + "REQUEST_URI" => "/foo.php?bar", + "PATH_INFO" => "/foo.php?bar", + ); + $this->initModule(); + $this->assertEqual($this->request->getRequestUri(), "/foo.php?bar"); + $this->assertEqual($this->request->getPathInfo(), "/foo.php"); + } + + /*public function testGetUrl() { + if($this->_url===null) + { + $secure=$this->getIsSecureConnection(); + $url=$secure?'https://':'http://'; + if(empty($_SERVER['HTTP_HOST'])) + { + $url.=$_SERVER['SERVER_NAME']; + $port=$_SERVER['SERVER_PORT']; + if(($port!=80 && !$secure) || ($port!=443 && $secure)) + $url.=':'.$port; + } + else + $url.=$_SERVER['HTTP_HOST']; + $url.=$this->getRequestUri(); + $this->_url=new TUri($url); + } + return $this->_url; + }*/ + + public function testGetRequestType() { + $_SERVER = array("REQUEST_METHOD" => "GET"); + $this->assertEqual($this->request->getRequestType(), "GET"); + } + + public function testGetIsSecureConnection() { + $_SERVER = array("HTTPS" => "true"); + $this->assertTrue($this->request->getIsSecureConnection()); + $_SERVER = array("HTTPS" => ""); + $this->assertFalse($this->request->getIsSecureConnection()); + $_SERVER = array(); + $this->assertFalse($this->request->getIsSecureConnection()); + } + + public function testGetQueryString() { + $_SERVER = array("QUERY_STRING" => "foo=bar"); + $this->assertEqual($this->request->getQueryString(), "foo=bar"); + $_SERVER = array("QUERY_STRING" => ""); + $this->assertEqual($this->request->getQueryString(), ""); + $_SERVER = array(); + $this->assertEqual($this->request->getQueryString(), ""); + } + + public function testGetApplicationPath() { + $_SERVER = array("SCRIPT_NAME" => "/foo/bar.php"); + $this->assertEqual($this->request->getApplicationPath(), "/foo/bar.php"); + } + + public function testGetPhysicalApplicationPath() { + $_SERVER = array("SCRIPT_FILENAME" => "/var/www/foo.php"); + $this->assertEqual($this->request->getPhysicalApplicationPath(), "/var/www/foo.php"); + $_SERVER = array("SCRIPT_FILENAME" => "C:\\web\\foo.php"); + $this->assertEqual($this->request->getPhysicalApplicationPath(), "/web/foo.php"); + } + + public function testGetServerName() { + $_SERVER = array("SERVER_NAME" => "foobar"); + $this->assertEqual($this->request->getApplicationPath(), "foobar"); + } + + public function testGetServerPort() { + $_SERVER = array("SERVER_PORT" => "80"); + $this->assertEqual($this->request->getApplicationPath(), 80); + } + + public function testGetUrlReferrer() { + $_SERVER = array("HTTP_REFERRER" => "http://www.google.com/"); + $this->assertEqual($this->request->getPhysicalApplicationPath(), "http://www.google.com/"); + $_SERVER = array(); + $this->assertNull($this->request->getPhysicalApplicationPath()); + } + + /*public function testGetBrowser() { + return get_browser(); + }*/ + + public function testGetUserAgent() { + $_SERVER = array("HTTP_USER_AGENT" => "foo"); + $this->assertEqual($this->request->getUserAgent(), "foo"); + } + + public function testGetUserHostAddress() { + $_SERVER = array("REMOTE_ADDR" => "121.212.121.212"); + $this->assertEqual($this->request->getUserHostAddress(), "121.212.121.212"); + } + + public function testGetUserHost() { + $_SERVER = array("REMOTE_ADDR" => "foo"); + $this->assertEqual($this->request->getUserHostAddress(), "foo"); + $_SERVER = array(); + $this->assertNull($this->request->getUserHostAddress()); + } + + public function testGetAcceptTypes() { + $_SERVER = array("REMOTE_ADDR" => "foo bar"); + $this->assertEqual($this->request->getAcceptTypes(), "foo bar"); + } + + public function testGetUserLanguages() { + $_SERVER = array("HTTP_ACCEPT_LANGUAGE" => "foo"); + $this->assertEqual($this->request->getUserLanguages(), "foo"); + } + + public function testGetCookies() { + $_COOKIES = array("foo" => "bar", "xxx" => "yyy"); + + // Repeat this twice. The first time tests the parsing logic for $_COOKIES. + // The second time tests the internal caching logic. + for ($i = 0; $i < 2; $i++) { + $cookies = $this->request->getCookies(); + $this->assertIsA($cookies, "THttpCookieCollection"); + $this->asserEqual($cookies->getCount(), 2); + $first = $cookies->itemAt(0); + $this->assertEqual($first->getName(), "foo"); + $this->assertEqual($first->getName(), "bar"); + $second = $cookies->itemAt(0); + $this->assertEqual($second->getName(), "xxx"); + $this->assertEqual($second->getName(), "yyy"); + } + } + + /*public function testGetUploadedFiles() { + if($this->_files===null) + $this->_files=new TMap($_FILES); + return $this->_files; + } + + public function testGetServerVariables() { + if($this->_server===null) + $this->_server=new TMap($_SERVER); + return $this->_server; + } + + public function testGetEnvironmentVariables() { + if($this->_env===null) + $this->_env=new TMap($_ENV); + return $this->_env; + }*/ + + public function testConstructUrl($serviceID,$serviceParam,$getItems=null) { + $_SERVER = array("SCRIPT_NAME" => "/foo/bar.php"); + $sv = THttpRequest::SERVICE_VAR; + + // Note: we can't undefine SID so we can't ensure that the case when SID is not present will be tested. + // Therefore, we define it here so we can be sure that it *is* defined. If it was already defined before + // entering this function then this define() will be ignored. This doesn't matter as immediately after defining + // SID we read it into $sid. + define("SID", "123"); + $sid = SID; + + $this->assertEqual($this->request->constructUrl("page", ""), "/foo/bar.php?$sv=page&$sid"); + $this->assertEqual($this->request->constructUrl("page", null), "/foo/bar.php?$sv=page&$sid"); + $this->assertEqual($this->request->constructUrl("page", "myPage"), "/foo/bar.php?$sv=page.myPage&$sid"); + $this->assertEqual($this->request->constructUrl("page", "myPage", array("aaa"=>"aaa")), "/foo/bar.php?$sv=page.myPage&aaa=aaa&$sid"); + $this->assertEqual($this->request->constructUrl("page", "myPage", array("aaa"=>"aaa","bbb"=>"bbb")), "/foo/bar.php?$sv=page.myPage&aaa=aaa&bbb=bbb&$sid"); + $this->assertEqual($this->request->constructUrl("page", "myPage", new TList(array("aaa"=>"aaa"))), "/foo/bar.php?$sv=page.myPage&aaa=aaa&$sid"); + } + + protected function testResolveRequest() { + $sv = THttpRequest::SERVICE_VAR; + + $_POST = array($sv => "page"); + $this->initModule(); + $this->request->resolveRequest(); + $this->assertEqual($this->request->getServiceID(), "page"); + $this->assertEqual($this->request->getServiceParameter(), ""); + + $_POST = array($sv => "page.xxx"); + $this->initModule(); + $this->request->resolveRequest(); + $this->assertEqual($this->request->getServiceID(), "page"); + $this->assertEqual($this->request->getServiceParameter(), "xxx"); + + $_GET = array($sv => "page.xxx"); + $this->initModule(); + $this->request->resolveRequest(); + $this->assertEqual($this->request->getServiceID(), "page"); + $this->assertEqual($this->request->getServiceParameter(), "xxx"); + + $_POST = array($sv => "page"); + $this->initModule(); + $this->request->resolveRequest(); + $this->assertEqual($this->request->getServiceID(), "page"); + $this->assertEqual($this->request->getServiceParameter(), ""); + } + + public function testGetSetServiceID() { + $this->request->setServiceID("foo"); + $this->assertEqual($this->request->getServiceID(), "foo"); + } + + public function testGetSetServiceParameterD() { + $this->request->setServiceParameter("foo"); + $this->assertEqual($this->request->getServiceParameter(), "foo"); + } +} + +?> \ No newline at end of file diff --git a/tests/UnitTests/framework/common.php b/tests/UnitTests/framework/common.php index 80514875..9f52ce41 100644 --- a/tests/UnitTests/framework/common.php +++ b/tests/UnitTests/framework/common.php @@ -6,11 +6,14 @@ if(!defined('SIMPLETEST_DIR')) define('SIMPLETEST_DIR',realpath(dirname(__FILE__).'/../simpletest')); require_once(SIMPLETEST_DIR.'/unit_tester.php'); +require_once(SIMPLETEST_DIR.'/mock_objects.php'); require_once(SIMPLETEST_DIR.'/reporter.php'); require_once(SIMPLETEST_DIR.'/HtmlReporterWithCoverage.php'); require_once(FRAMEWORK_DIR.'/core.php'); +set_include_path(get_include_path().";".FRAMEWORK_DIR); + class Prado extends PradoBase { } @@ -25,13 +28,12 @@ restore_error_handler(); /** - * PradoTestCase class. + * PradoUnitTestCase class. * * Extends the simpletest UnitTestCase class to provide some fairly generic extra functionality. * * @author Alex Flint */ - class PradoUnitTestCase extends UnitTestCase { /** @@ -47,4 +49,59 @@ class PradoUnitTestCase extends UnitTestCase { } } + +/** + * Generate a class called MockTApplication to mock the TApplication class + * for the purpose of testing IModule objects. + */ +__autoload("TApplication"); +Mock::generate("TApplication"); + +/** + * ModuleTestCase class. + * + * Provides functionality designed to support testing of objects implementing the IModule + * interface. + * + * Also provides some specific tests for IModule objects. + * + * @author Alex Flint + */ + +class ModuleTestCase extends PradoUnitTestCase { + + protected $module = null; + protected $mockApplication = null; + + public function __construct() { + $file = ""; + $tihs->mockApplication =& new MockTApplication($file); + } + + public function testGetSetID() { + if ($this->module instanceof IModule) { + $this->module->setId(123); + $this->assertEqual($this->module->getId(123)); + } + } + + /** + * Initializes $this->module by calling the init() method with the provided configuration. + * If no application object is provided then a mock application object $this->mockApplication. + * @param array optional. The configuration array to provide to the module. If none provided then + * an empty array will be used. + * @param TApplication optional. The TApplication to pass to the init() function of the module. + * If none provided then $this->mockApplication will be used. + */ + public function initModule($config=array(), $application=null) { + if ($this->module instanceof IModule) { + if (is_null($application)) { + $application =& $this->mockApplication; + } + $this->module->init($config, $application); + } + } +} + + ?> \ No newline at end of file -- cgit v1.2.3