Skip to content

Commit

Permalink
added assertResponse testing method
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Davis committed Jul 2, 2009
1 parent 5be944b commit 9d9a982
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/base.php
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions lib/controller.php
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
}
Expand Down
84 changes: 75 additions & 9 deletions lib/test/phpunit_testcase.php
Expand Up @@ -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;
}


Expand All @@ -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
Expand Down Expand Up @@ -282,27 +318,57 @@ 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
* @param string $name the name of the template with or without .php extension
*/
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);
}


Expand Down

0 comments on commit 9d9a982

Please sign in to comment.