Laravel, PHPUnit, Require_Once on Routes

Posted: 2016-03-22 12:14:30

Going crazy running PHPUnit all and getting fail but you run the one route and it works!

The difference is require versus require_once in PHPUnit tests.

For example

require __DIR__ . '/../ConfigurationExamples/routes.php';

Route::get('/', function () {
    return view('welcome');
});

Now when I run my tests I get 100% pass

/**
     * @test
     */
    public function should_auth_user()
    {
        $user = factory(\App\User::class)->create();

        $results = $this->call('GET', sprintf("/example/auth_token?api_token=%s", $user->api_token));

        $this->assertEquals(200, $results->status());

        $this->assertEquals("foo", $results->getContent());

    }

    /**
     * @test
     */
    public function should_fail_user()
    {
        $results = $this->call('GET', '/example/auth_token');

        $this->assertEquals(302, $results->status());
    }

    /**
     * @test
     */
    public function should_pass_auth_with_no_token()
    {
        $user = factory(\App\User::class)->create();

        $this->be($user);

        $results = $this->call('GET', '/example/auth_token');

        $this->assertEquals(200, $results->status());

        $this->assertEquals("foo", $results->getContent());
    }

The moment I change it to require_once I get fails on the last or second to last one, it is not consistant except that it will fail. I immediately run that one and it passes.

It is not the database and I can change the order of these tests to have the bottom one fail.

So require is your friend!!