Permalink
Browse files

various app manager, request and mainly controller tests

  • Loading branch information...
makeusabrew committed May 28, 2012
1 parent ee519b2 commit e1242f1b9e9959849fbfce4478a4a2cfe50e15b9
View
@@ -2,14 +2,16 @@
<project name="Jaoss Library" default="build">
<property name="tests.dir" value="tests" />
<property name="reports.dir" value="reports" />
+ <property name="coverage.dir" value="coverage" />
<target name="test" description="Test">
<exec dir="${tests.dir}" checkreturn="true" passthru="true" command="phpunit" />
</target>
<target name="build" description="Build">
<exec dir="${tests.dir}" checkreturn="true" passthru="true" command="phpunit --log-junit ${reports.dir}/test-results.xml
- --coverage-clover ${reports.dir}/phpunit.coverage.xml" />
+ --coverage-clover ${reports.dir}/phpunit.coverage.xml
+ --coverage-html ${reports.dir}/${coverage.dir}" />
</target>
</project>
View
@@ -36,10 +36,6 @@ public function setPathsFile($file) {
}
public function loadPaths() {
- if ($this->getLoaded()) {
- return false;
- }
-
$path = $this->paths_file;
Log::verbose("Looking for [".$path."]");
if (file_exists($path)) {
View
@@ -39,5 +39,6 @@ public static function isAppInstalled($app) {
public static function reset() {
self::$installed_apps = array();
+ self::$appFolders = array();
}
}
View
@@ -378,7 +378,7 @@ public function getErrors() {
return $this->errors;
}
- protected function filterRequest() {
+ public function filterRequest() {
$final = array();
foreach (func_get_args() as $key) {
if ($this->request->getVar($key) !== null) {
@@ -388,7 +388,7 @@ protected function filterRequest() {
return $final;
}
- protected function filterRequestStrict() {
+ public function filterRequestStrict() {
$final = array();
foreach (func_get_args() as $key) {
$final[$key] = $this->request->getVar($key);
@@ -14,7 +14,7 @@ public function setProperties($params = array()) {
Log::debug("attempting to set request properties via non CLI server API!");
return false;
}
- $allowed_params = array("folder_base", "base_href", "url", "query_string", "method", "ajax", "referer");
+ $allowed_params = array("folder_base", "base_href", "url", "query_string", "method", "ajax", "pjax", "referer");
foreach ($allowed_params as $param) {
if (isset($params[$param])) {
$this->$param = $params[$param];
@@ -43,6 +43,23 @@ public function testGetInstalledAppPaths() {
$this->assertEquals(array("testapp"), AppManager::getAppPaths());
}
+ public function testGetInstalledAppsHash() {
+ if (!is_dir(PROJECT_ROOT."apps")) {
+ mkdir(PROJECT_ROOT."apps");
+ $appDirCreated = true;
+ }
+ $this->createDir(PROJECT_ROOT."apps/testapp");
+
+ AppManager::installApp("testapp");
+
+ $this->removeDir(PROJECT_ROOT."apps/testapp");
+ if (isset($appDirCreated)) {
+ $this->removeDir(PROJECT_ROOT."apps/");
+ }
+
+ $this->assertEquals(sha1("testapp"), AppManager::getInstalledAppsHash());
+ }
+
protected function createDir($dir) {
if (!is_dir($dir)) {
mkdir($dir);
@@ -262,18 +262,198 @@ public function testRedirectToInternalUrlFromSubfolderBase() {
$this->assertEquals("http://localhost/my/subfolder/foo", $response->getRedirectUrl());
}
+
+ public function testRedirectToInternalUrlForAjaxRequestAssignsUrlToBody() {
+ $this->stub = new ConcreteController(array(
+ 'ajax' => true,
+ ));
+
+
+ $this->stub->redirect("/foo");
+ $body = $this->stub->getResponse()->getBody();
+ $data = json_decode($body, true);
+
+ $this->assertEquals("/foo", $data["redirect"]);
+ }
+
+ public function testRedirectToInternalUrlForAjaxRequestAssignsMessageToBody() {
+ $this->stub = new ConcreteController(array(
+ 'ajax' => true,
+ ));
+
+
+ $this->stub->redirect("/foo", "Test Redirect");
+ $body = $this->stub->getResponse()->getBody();
+ $data = json_decode($body, true);
+
+ $this->assertEquals("Test Redirect", $data["message"]);
+ }
+
+ public function testRenderTemplateAddsHtmlContentTypeIfNotSet() {
+ //
+ $stub = $this->getMock("ConcreteController", array("fetchTemplate"));
+ $stub->expects($this->any())
+ ->method('fetchTemplate')
+ ->will($this->returnValue("FooBar"));
+
+ $response = $stub->getResponse();
+
+ $this->assertEquals(null, $response->getHeader("Content-Type"));
+
+ $stub->renderTemplate("foo");
+
+ $this->assertEquals("text/html; charset=utf-8", $response->getHeader("Content-Type"));
+ }
+
+ public function testRenderTemplateDoesNotAlterContentTypeIfNotSet() {
+ //
+ $stub = $this->getMock("ConcreteController", array("fetchTemplate"));
+ $stub->expects($this->any())
+ ->method('fetchTemplate')
+ ->will($this->returnValue("FooBar"));
+
+ $response = $stub->getResponse();
+
+ $response->addHeader("Content-Type", "foo/bar");
+
+ $stub->renderTemplate("foo");
+
+ $this->assertEquals("foo/bar", $response->getHeader("Content-Type"));
+ }
+
+ public function testFilterRequest() {
+ $stub = new ConcreteController(array(), array(
+ "foo" => "bar",
+ "baz" => "test",
+ "invalid" => "invalid",
+ "ignore" => "ignore",
+ ));
+
+ $this->assertEquals(array(
+ "foo" => "bar",
+ "baz" => "test",
+ ), $stub->filterRequest("foo", "baz", "notfound"));
+ }
+
+ public function testFilterRequestStrict() {
+ $stub = new ConcreteController(array(), array(
+ "foo" => "bar",
+ "baz" => "test",
+ "invalid" => "invalid",
+ "ignore" => "ignore",
+ ));
+
+ $this->assertEquals(array(
+ "foo" => "bar",
+ "baz" => "test",
+ "notfound" => null,
+ ), $stub->filterRequestStrict("foo", "baz", "notfound"));
+ }
+
+ public function testRenderPjaxAssignsPjaxVariable() {
+ $stub = $this->getMock("ConcreteController", array("renderTemplate"));
+ $stub->expects($this->any())
+ ->method('renderTemplate')
+ ->will($this->returnValue("FooBar"));
+
+ $stub->renderPjax("fooBar");
+
+ $this->assertTrue($stub->getAssignVar("_pjax"));
+ }
+
+ public function testUnassignAllEmptiesVarStack() {
+ $this->stub->assign("foo", "bar");
+ $this->stub->assign("baz", "test");
+
+ $this->assertEquals(2, count($this->stub->getVarStack()));
+
+ $this->stub->unassignAll();
+
+ $this->assertEquals(0, count($this->stub->getVarStack()));
+ }
+
+ public function testRenderCallsRenderTemplateForNormalRequest() {
+ $stub = $this->getMock("ConcreteController", array("renderTemplate"));
+ $stub->expects($this->any())
+ ->method('renderTemplate')
+ ->will($this->returnArgument(0));
+
+ $result = $stub->render("mock string");
+
+ $this->assertEquals("mock string", $result);
+ }
+
+ public function testRenderAssignsErrorsVarIfAnyPresent() {
+ $stub = $this->getMock("ConcreteController", array("renderTemplate"));
+ $stub->expects($this->any())
+ ->method('renderTemplate')
+ ->will($this->returnArgument(0));
+
+ $stub->setErrors(array(
+ "foo" => "bar",
+ ));
+
+ $stub->render("mock string");
+
+ $this->assertEquals(array(
+ "foo" => "bar",
+ ), $stub->getAssignVar("_errors"));
+ }
+
+ public function testRenderCallsRenderPjaxForPjaxRequest() {
+ $stub = $this->getMock("ConcreteController", array("renderPjax"));
+ $stub->expects($this->any())
+ ->method('renderPjax')
+ ->will($this->returnArgument(0));
+
+ $stub->setRequestProperties(array(
+ "pjax" => true,
+ ));
+
+ $result = $stub->render("mock string");
+
+ $this->assertEquals("mock string", $result);
+ }
+
+ public function testRenderCallsRenderJsonForajaxRequest() {
+ $this->stub->setRequestProperties(array(
+ "ajax" => true,
+ ));
+
+ $this->stub->assign("foo", "bar");
+
+ $this->stub->render("dummy");
+
+ $result = $this->stub->getResponse()->getBody();
+
+ $this->assertEquals('{"foo":"bar","msg":"OK"}', $result);
+ }
}
// I'd much rather use a mock here but for some reason code coverage doesn't
// seem to work properly. Look at fixing this later.
class ConcreteController extends Controller {
- public function __construct($properties = array()) {
+ public function __construct($properties = array(), $params = array()) {
// don't want abstract controller construct firing cos it throws an exception
// could look at moving stuff out of construct?
$this->request = new TestRequest();
- $this->request->setProperties($properties);
+
+ $this->setRequestProperties($properties);
+ $this->request->setParams($params);
$this->response = new JaossResponse();
$this->session = Session::getInstance();
}
+
+ public function getAssignVar($key) {
+ return $this->var_stack[$key];
+ }
+
+ public function getVarStack() {
+ return $this->var_stack;
+ }
+
+ public function setRequestProperties($properties) {
+ $this->request->setProperties($properties);
+ }
}
@@ -157,6 +157,11 @@ public function testGetSapi() {
public function testGetQueryString() {
$request = new JaossRequest($this->reqData);
$this->assertEquals("foo=bar", $request->getQueryString());
+
+ unset($this->reqData["QUERY_STRING"]);
+
+ $request = new JaossRequest($this->reqData);
+ $this->assertEquals(null, $request->getQueryString());
}
public function testCacheDisabled() {
@@ -221,4 +226,9 @@ public function testGetBaseHrefHttpsNonStandardPort() {
$request = new JaossRequest($this->reqData);
$this->assertEquals('https://myproject.build:4444/', $request->getBaseHref());
}
+
+ public function testGetFullUrl() {
+ $request = new JaossRequest($this->reqData);
+ $this->assertEquals("http://myproject.build/my/url", $request->getFullUrl());
+ }
}
View
@@ -5,6 +5,7 @@
<filter>
<blacklist>
<directory suffix=".php">../library/Smarty</directory>
+ <directory suffix=".php">../library/test</directory>
</blacklist>
</filter>
</phpunit>

0 comments on commit e1242f1

Please sign in to comment.