2
\$\begingroup\$

Is this Test Case overkill?

I am new to Unit Testing so I am learning and I'd love to hear your opinion about it.

use Application\Tests\TestCase as TestCase;

class RoutesSubmissionsTest extends TestCase {

    public function setUp()
    {
        parent::setUp();
        $this->app['router']->enableFilters();
    }

    /**
     * Tests submissions/welcome route as a Guest.
     *
     * @return void
     */
    public function testRouteToSubmissionsWelcomeAsGuest()
    {
        $this->route('GET', 'submissions.welcome');
        $this->assertRedirectedToRoute('users.login');
    }

    /**
     * Tests submissions/welcome route as an User with GET method.
     *
     * @return void
     */
    public function testRouteToSubmissionsWelcomeAsUserWithGetMethod()
    {
        $this->authenticateAsUser();
        $this->route('GET', 'submissions.welcome');
        $this->assertResponseOk();
    }

    /**
     * Tests submissions/welcome route as an User with POST method without CSRF token.
     *
     * @expectedException Illuminate\Session\TokenMismatchException
     * @return void
     */
    public function testRouteToSubmissionsWelcomeAsUserWithPostMethodWithoutCSRFToken()
    {
        $this->authenticateAsUser();
        $this->route('POST', 'submissions.welcome');
    }

    /**
     * Tests submissions/welcome route as an User with POST method with CSRF token without AJAX.
     *
     * @return void
     */
    public function testRouteToSubmissionsWelcomeAsUserWithPostMethodWithCSRFTokenWithoutAjax()
    {       
        Session::start();
        $this->authenticateAsUser();
        $this->route('POST', 'submissions.welcome', [], ['_token' => csrf_token()]);
        $this->assertResponseStatus(403);
    }

    /**
     * Tests submissions/welcome route as an User with POST method with CSRF token with AJAX.
     *
     * @expectedException ErrorException
     * @expectedExceptionMessage Undefined index: username
     * @return void
     */
    public function testRouteToSubmissionsWelcomeAsUserWithPostMethodWithCSRFTokenWithAjax()
    {
        Session::start();
        $this->authenticateAsUser();
        $this->route('POST', 'submissions.welcome', [], [], [], ['HTTP_X-CSRF-Token' => csrf_token(), 'HTTP_X-Requested-With' => 'XMLHttpRequest']);
    }   

    /**
     * Tests submissions/registration route as a Guest.
     *
     * @return void
     */
    public function testRouteToSubmissionsRegistrationAsGuest()
    {
        $this->route('GET', 'submissions.registration');
        $this->assertRedirectedToRoute('users.login');
    }

    /**
     * Tests submissions/registration route as an User with GET method.
     *
     * @return void
     */
    public function testRouteToSubmissionsRegistrationAsUserWithGetMethod()
    {
        $this->authenticateAsUser();
        $this->route('GET', 'submissions.registration');
        $this->assertResponseOk();
    }

    /**
     * Tests submissions/registration route as an User with POST method.
     *
     * @expectedException Illuminate\Session\TokenMismatchException
     * @return void
     */
    public function testRouteToSubmissionsRegistrationAsUserWithPostMethodWithoutCSRFToken()
    {
        $this->authenticateAsUser();
        $this->route('POST', 'submissions.registration');
    }   

    /**
     * Tests submissions/registration route as an User with POST method with CSRF token without AJAX.
     *
     * @return void
     */
    public function testRouteToSubmissionsRegistrationAsUserWithPostMethodWithCSRFTokenWithoutAjax()
    {       
        Session::start();
        $this->authenticateAsUser();
        $this->route('POST', 'submissions.registration', [], ['_token' => csrf_token()]);
        $this->assertResponseStatus(403);
    }

    /**
     * Tests submissions/registration route as an User with POST method with CSRF token with AJAX.
     *
     * @expectedException ErrorException
     * @expectedExceptionMessage Undefined index: username
     * @return void
     */
    public function testRouteToSubmissionsRegistrationAsUserWithPostMethodWithCSRFTokenWithAjax()
    {
        Session::start();
        $this->authenticateAsUser();
        $this->route('POST', 'submissions.registration', [], [], [], ['HTTP_X-CSRF-Token' => csrf_token(), 'HTTP_X-Requested-With' => 'XMLHttpRequest']);
    }

    /**
     * Authenticates as a User.
     *
     * @return void
     */
    protected function authenticateAsUser()
    {
        $user = User::where('username', '=', 'n.cognome')->first();
        $this->be($user);
    }
}
\$\endgroup\$
1
\$\begingroup\$

I could be wrong (and my answer down voted)

but when it comes to routing or submission of forms consider using Behat or a behavior test framework rather than PhpUnit testing.

The reason why I'm stating this is because you have sessions, and submission and cause/effect returning that you want to test. Those ARE valid things you want to test but its more user behavior test rather than what PHPUnit is built for which is functional testing.

A lot of time when doing unit testing when relying on other classes your able to "mock" or substitute classes that normally function in production to be isolated which is only to initialize a class or a function that you are running a test on.

What you are doing instead is testing if the routing mechanism is behaving with set inputs.

\$\endgroup\$
  • 1
    \$\begingroup\$ I don't really use mocks because I seed a SQLite database in :memory: for testing. Is that fine as well? \$\endgroup\$ – GiamPy Jan 2 '15 at 14:32
  • 2
    \$\begingroup\$ @GiamPy yes seeding SQLite to quickly test DB calls where you boot up the SQLite and destroy it at the end of the test is fine (as long as your DB is not complicated since SQLite is restrictive). Mocking is important to testing as you don't want to chain up your classes and make too much of a dependency of the classes involved (which will indicate a code smell) \$\endgroup\$ – azngunit81 Jan 2 '15 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.