baniol
5/8/2013 - 5:49 PM

php unit, test case

php unit, test case

<?php

class Students_Test extends Controller_TestCase {

    public function __construct()
    {
        Bundle::start('larafactory');
    }

    public function tearDown()
    {
        DB::table('students')->delete();
    }

    public function testActionIndex()
    {
        LaraFactory::create('Student');
        $response = $this->get('students@index');
        $this->assertEquals('students.index', $response->content->view);
        $students = $response->content->data['students'];
        $this->assertCount(1, $students->results);
    }

    public function testActionNew()
    {
        $response = $this->get('students@new');
        $this->assertEquals('students.new', $response->content->view);
        $this->assertNotNull($response->content->data['countries']);
    }

    public function testActionCreate()
    {
        $response = $this->post('students@create', LaraFactory::attributes_for('Student'));
        $this->assertTrue($response->foundation->isRedirect(), 'Should be redirected');
        $this->assertContains('Student successfully saved.', Session::instance()->get('message'));
    }

    public function testActionShow()
    {
        $new_student = LaraFactory::create('Student');
        $response = $this->get('students@show', array('id' => $new_student->id));
        $this->assertEquals('students.show', $response->content->view);
        $this->assertInstanceOf('Student', $response->content->data['student']);
    }

    public function testActionEdit()
    {
        $new_student = LaraFactory::create('Student');
        $response = $this->get('students@edit', array('id' => $new_student->id));
        $this->assertEquals('students.edit', $response->content->view);
        $this->assertInstanceOf('Student', $response->content->data['student']);
    }

    public function testActionUpdate()
    {
        $student = LaraFactory::create('Student');
        $student->forename = 'Something';
        $student->surname  = 'Else';
        assert($student->save());

        $response = $this->put('students@update', (array)$student, array('id' => $student->id));
        $this->assertTrue($response->foundation->isRedirect(), 'Should be redirected');
        $this->assertContains('Student successfully updated.', Session::instance()->get('message'));
    }

    public function testActionDelete()
    {
        $student = LaraFactory::create('Student');
        $response = $this->delete('students@destroy', array('id' => $student->id));
        $this->assertTrue($response->foundation->isRedirect(), 'Should be redirected.');

        $reloaded = Student::find($student->id);

        $this->assertLessThanOrEqual($reloaded->deleted_at, date('Y-m-d h:i:s'));
        $this->assertEquals(0, $reloaded->active);
    }

    public function testAjaxRequest()
    {
        $response = $this->get('students@index', array(), true);
        $this->assertTrue($response->foundation->isOk());
        $this->assertContains('application/json', $response->foundation->headers->get('Content-Type'));
    }
}
<?php

/**
 * Controller Test Case
 *
 * Provides some convenience methods for testing Laravel Controllers.
 *
 * @author Joseph Wynn <joseph@wildlyinaccurate.com>
 */
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
     * @param bool $ajax
     * @return \Laravel\Response
     */
    public function call($destination, $parameters = array(), $method = 'GET', $ajax = false)
    {
        Request::foundation()->setMethod($method);
        if ($ajax) {
            // Set an X-Request-With to the header request so as
            // to mock an Ajax request. This makes Request::ajax() method
            // return true
            Request::foundation()->headers->set('X-Requested-With', 'XMLHttpRequest');
        }
        return Controller::call($destination, $parameters);
    }

    /**
     * Alias for call()
     *
     * @param string $destination
     * @param array $parameters
     * @param string $method
     * @param bool $ajax
     * @return \Laravel\Response
     */
    public function get($destination, $parameters = array(), $ajax = false)
    {
        return $this->call($destination, $parameters, 'GET', $ajax);
    }

    /**
     * Make a POST request to a controller method
     *
     * @param string $destination
     * @param array $post_data
     * @param array $parameters
     * @param bool $ajax
     * @return \Laravel\Response
     */
    public function post($destination, $post_data, $parameters = array(), $ajax = false)
    {
        $this->cleanRequest();
        Request::foundation()->request->add($post_data);
        return $this->call($destination, $parameters, 'POST', $ajax);
    }

    /**
     * Make a PUT request to a controller method
     *
     * @param string $destination
     * @param array $parameters
     * @param bool $ajax
     * @return \Laravel\Response
     */
    public function put($destination, $put_data, $parameters = array(), $ajax = false)
    {
        $this->cleanRequest();
        Request::foundation()->request->add($put_data);
        return $this->call($destination, $parameters, 'PUT', $ajax);
    }

    /**
     * Make a DELETE request to a controller method
     *
     * @param string $destination
     * @param array $parameters
     * @param bool $ajax
     * @return \Laravel\Response
     */
    public function delete($destination, $parameters = array(), $ajax = false)
    {
        return $this->call($destination, $parameters, 'DELETE', $ajax);
    }

    /**
     * Clean data posted on previous request so that the
     * next request wont be merged
     *
     */
    public function cleanRequest()
    {
        $request = Request::foundation()->request;
        $keys = $request->keys();
        foreach ($keys as $key){
            $request->remove($key);
        }
    }

}