Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide basic integration testing with Request and Response objects. #41

Open
wants to merge 1 commit into
base: 3.3/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions classes/Kohana/Unittest/Integration/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php defined('SYSPATH') or die('No direct script access.');

/**
* TestCase for testing in integration with Request and Response objects.
*
* This is inspired from the Play framework in which a test case provides
* primitives to test Response objects.
*
* Testing a real application is feasible by self-requesting endpoints and
* asserting pre-conditions and post-conditions.
*
* @package Kohana/UnitTest
* @author Guillaume Poirier-Morency <[email protected]>
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
abstract class Kohana_Unittest_Integration_TestCase extends Unittest_TestCase {

/**
* Assert that a given Response status match the expected value.
*
* @param integer $code expected status code
* @param Response $response Response object
* @param string $message message displayed if the test fail
*/
public function assertStatus($code, Response $response, $message = NULL)
{
if ($message === NULL)
{
$message = $response->body();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this potentially dump an enormous HTML string into your PHPUnit output? Don't think I like that idea.

}

$this->assertEquals($code, $response->status(), $message);
}

public function assertOk(Response $response, $message = NULL)
{
$this->assertStatus(200, $response, $message);
}

public function assertPermanentRedirection($location, Response $response, $message = NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better as a first-class PHPUnit assertion, so that if it fails it can show the whole of the status/location header/etc in a useful failure message that describes the overall assertion and what was incorrect, rather than a chain of assertEquals.

For example, Expected 200 to equal 301 but it did not doesn't tell you whether the Location header was set or not, so you don't know if the issue is that the application never redirected, or it did but the status code went wrong along the way.

{
$this->assertStatus(301, $response, $message);
$this->assertEquals($location, $response->headers('Location'));
}

public function assertTemporaryRedirection($location, Response $response, $message = NULL)
{
$this->assertStatus(302, $response, $message);
$this->assertEquals($location, $response->headers('Location'));
}

public function assertUnauthorized(Response $response, $message = NULL)
{
$this->assertStatus(401, $response, $message);
}

public function assertForbidden(Response $response, $message = NULL)
{
$this->assertStatus(403, $response, $message);
}

public function assertNotFound(Response $response, $message = NULL)
{
$this->assertStatus(404, $response, $message);
}

public function assertServiceUnavailable(Response $response, $message = NULL)
{
$this->assertStatus(503, $response, $message);
}

}
3 changes: 3 additions & 0 deletions classes/Unittest/Integration/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php defined('SYSPATH') or die('No direct script access.');

abstract class Unittest_Integration_TestCase extends Kohana_Unittest_Integration_TestCase {}