Skip to content
Browse files

added assertResponse testing method

  • Loading branch information...
1 parent 5be944b commit 9d9a9826d74689c31b7f5210e02d884aa84b5172 Scott Davis committed
Showing with 86 additions and 14 deletions.
  1. +3 −1 lib/base.php
  2. +8 −4 lib/controller.php
  3. +75 −9 lib/test/phpunit_testcase.php
View
4 lib/base.php
@@ -123,7 +123,9 @@ public function dispatch($test=false)
ob_end_clean();
if (count($this->klass->headers)>0){
foreach($this->klass->headers as $header){
- $this->test_mode ? '' : header($header);
+ if(!$this->test_mode) {
+ header($header[0], true, empty($header[1]) ? null : $header[1]);
+ }
}
}
print $out;
View
12 lib/controller.php
@@ -17,10 +17,11 @@ class Controller {
var $format;
var $layout = true;
var $layout_template;
- var $headers = array('Content-Type: text/html');
+ var $headers = array(array('Content-Type: text/html', 200));
var $filters = array('before' => array(), 'after' => array());
var $has_rendered = false;
var $template = '';
+ var $rendered_partials = array();
/**
* The expected output format for this controller.
* @var string
@@ -141,6 +142,7 @@ public function render($file) {
*/
public function render_partial($file)
{
+ $this->rendered_partials[] = $file;
return $this->open_template(FileUtils::join(Nimble::getInstance()->config['view_path'], $file));
}
@@ -173,7 +175,9 @@ private function open_template($name)
* Add an HTTP header to be included in the output.
* @param string $text The header to add.
*/
- public function header($text){ $this->headers[] = $text; }
+ public function header($text, $code=''){
+ $this->headers[] = array($text, $code);
+ }
/**
* Redirect to another URL.
@@ -183,9 +187,9 @@ public function header($text){ $this->headers[] = $text; }
public function redirect($url, $now=false)
{
if($now && self::nimble()->test_mode === false){
- header("Location: {$url}");
+ header("Location: {$url}", true, 302);
}else{
- $this->header("Location: {$url}");
+ $this->header("Location: {$url}", 302);
$this->has_rendered = true;
}
}
View
84 lib/test/phpunit_testcase.php
@@ -214,8 +214,13 @@ public function delete($action, $action_params = array(), $params = array(), $se
*/
public function assertRedirect($url) {
- $hash = array_flip($this->controller->headers);
- $this->assertTrue(isset($hash["Location: {$url}"]));
+ $test = "Location: {$url}";
+ $this->header_test($test);
+ }
+
+
+ public function headers() {
+ return $this->controller->headers;
}
@@ -225,10 +230,41 @@ public function assertRedirect($url) {
*/
public function assertContentType($type) {
- $hash = array_flip($this->controller->headers);
- $this->assertTrue(isset($hash["Content-Type: {$type}"]));
+ $test = "Content-Type: {$type}";
+ $this->header_test($test);
+ }
+
+
+ private function header_test($test) {
+ $headers = $this->headers();
+ foreach($headers as $header) {
+ if(strtolower($header[0]) == strtolower($test)) {
+ $this->assertTrue(true);
+ return;
+ }
+ }
+ $this->assertTrue(false, "No header found matching " . $test);
}
+
+ private function check_response_code($start, $end=null) {
+ $headers = $this->headers();
+ if($start == $end) {
+ $message = "No response code found for " . $start;
+ }else{
+ $message = "No response code found between " . $start . " and " . $end;
+ }
+ foreach($headers as $header) {
+ $code = $header[1];
+ if($code <= $end && $code >= $start) {
+ $this->assertTrue(true);
+ return;
+ }
+ }
+ $this->assertTrue(false, $message);
+ }
+
+
/**
* Looks for a string match in the response text
* @param string $value Item you wish to look for in the response text
@@ -282,6 +318,16 @@ public function assigns($var) {
return $this->controller->$var;
}
+ /**
+ * Helper function for adding .php to a string
+ * @param string $name string to add .php
+ */
+ private function add_php_extension($name) {
+ if(strpos($name, '.php') === false) {
+ $name = $name . ".php";
+ }
+ return $name;
+ }
/**
* Asserts that the given template has been rendered
@@ -289,20 +335,40 @@ public function assigns($var) {
*/
public function assertTemplate($name) {
$name = basename($name);
- if(strpos($name, '.php') === false) {
- $name = $name . ".php";
- }
+ $name = $this->add_php_extension($name);
$template_rendered = basename($this->controller->template);
$this->assertEquals($name, $template_rendered);
}
+ /**
+ * Asserts that the given partial template has been rendered
+ * @param string $name the name of the partial template with or without .php extension
+ */
+ public function assertPartial($name) {
+ $name = basename($name);
+ $name = $this->add_php_extension($name);
+ $partials = $this->controller->rendered_partials;
+ $base = array();
+ foreach($partials as $partial) {
+ $base[] = basename($partial);
+ }
+ $base = array_flip($partial);
+ $this->assertTrue(isset($base[$name]), "No partial matching $name was found");
+ }
public function assertResponse($code) {
- $shortcuts = array('success' => 200, 'redirect' => range(300,399), 'missing' => 404, 'error' => range(500, 599));
+ $shortcuts = array('success' => 200, 'redirect' => array(300,399), 'missing' => 404, 'error' => array(500, 599));
$message = "Expected response to be a ?, but was ?";
if(is_string($code) && isset($shortcuts[$code])) {
$code = $shortcuts[$code];
}
-
+ if(is_array($code)) {
+ $start = reset($code);
+ $end = end($code);
+ }else{
+ $start = $code;
+ $end = $code;
+ }
+ $this->check_response_code($start, $end);
}

0 comments on commit 9d9a982

Please sign in to comment.
Something went wrong with that request. Please try again.