Skip to content

Instantly share code, notes, and snippets.

@wildlyinaccurate
Created July 9, 2012 22:00
Show Gist options
  • Save wildlyinaccurate/3079291 to your computer and use it in GitHub Desktop.
Save wildlyinaccurate/3079291 to your computer and use it in GitHub Desktop.
Laravel Controller Test Case
<?php
require_once 'ControllerTestCase.php';
class AccountControllerTest extends ControllerTestCase
{
public function testSignupWithNoDataRedirectsAndHasErrors()
{
$response = $this->post('account@signup', array());
$this->assertEquals('302', $response->foundation->getStatusCode());
$session_errors = Session::instance()->get('errors')->all();
$this->assertNotEmpty($session_errors);
}
public function testSignupWithValidDataRedirectsAndHasNoErrors()
{
$response = $this->post('account@signup', array(
'username' => 'validusername',
'email' => '[email protected]',
'password' => 'passw0rd',
'password_confirm' => 'passw0rd',
));
$this->assertEquals('302', $response->foundation->getStatusCode());
$session_errors = Session::instance()->get('errors');
$this->assertNull($session_errors);
}
}
<?php
/**
* Controller Test Case
*
* Provides some convenience methods for testing Laravel Controllers.
*
* @author Joseph Wynn <[email protected]>
*/
abstract class ControllerTestCase extends PHPUnit_Framework_TestCase
{
/**
* The Laravel session must be re-loaded before each test, otherwise
* the session state is retained across multiple tests.
*/
public function setUp()
{
Session::load();
}
/**
* Call a controller method.
*
* This is basically an alias for Laravel's Controller::call() with the
* option to specify a request method.
*
* @param string $destination
* @param array $parameters
* @param string $method
* @return \Laravel\Response
*/
public function call($destination, $parameters = array(), $method = 'GET')
{
Request::foundation()->server->add(array(
'REQUEST_METHOD' => $method,
));
return Controller::call($destination, $parameters);
}
/**
* Alias for call()
*
* @param string $destination
* @param array $parameters
* @param string $method
* @return \Laravel\Response
*/
public function get($destination, $parameters = array())
{
return $this->call($destination, $parameters, 'GET');
}
/**
* Make a POST request to a controller method
*
* @param string $destination
* @param array $post_data
* @param array $parameters
* @return \Laravel\Response
*/
public function post($destination, $post_data, $parameters = array())
{
Request::foundation()->request->add($post_data);
return $this->call($destination, $parameters, 'POST');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment