Summer Sale! All accounts are 50% off this week.

shahramatrian's avatar

Error testing routes with Orchestra\Testbench

Hi. I have installed Laravel outside the webserver public folder and it works without errors, however, when I want to test routes with Orchestra\Testbench\BrowserKit I got either error 404 or 500. Here are the environment settings:

  1. Laravel public folder (index.php only) is located at htdocs/xxx/yyy.
  2. Laravel source code is located at /data/laravel
  3. It's a multilingual app using Laravel Localization package (https://github.com/mcamara/laravel-localization)
  4. The authorization is done via 3rd party API. All unauthorized requests are redirected to login page
  5. I use Voyager for admin panel, it's why I need VoyagerServiceProvider

So, I can open login page e.g. at http://laravel.test/xxx/yyy/login, however, I get error 404 when running the following test:

class RoutesTest extends BaseTestCase { public $baseUrl = 'http://laravel.test/xxx/yyy'; protected function getPackageProviders($app) { return [ VoyagerServiceProvider::class, LaravelLocalizationServiceProvider::class, ]; }

protected function defineRoutes($router)
{
    // Define routes.
    require 'routes/web.php';
}

/** @test */
public function it_verifies_login_route()
{
    //$this->withoutExceptionHandling();
    $this->visit('/login');
}

protected function defineEnvironment($app)
{
    $app['config']->set('laravellocalization.hideDefaultLocaleInURL', true);
}

}

Here is part of phpunit.xml:

<php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="DB_CONNECTION" value="sqlite"/>
    <server name="DB_DATABASE" value="/Users/seanatrian/data/laravel/database/test.sqlite3"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
    <server name="APP_DEBUG" value="true"/>
    <server name="APP_URL" value="http://laravel.test/xxx/yyy"/>
    <server name="APP_KEY" value="a_key"/>
</php>

When I change some settings like APP_URL in phpunit.xml: or $baseUrl, I may pass 404 error but then it redirects e.g, to http://laravel.test/en and return error 500.

Can you please let me know what mistake do I make?

BTW, when I comment out $this->withoutExceptionHandling() I get the following error:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

GET http://laravel.test/xxx/yyy/login

at vendor/laravel/browser-kit-testing/src/Concerns/InteractsWithExceptionHandling.php:60

        public function render($request, Throwable $e)
        {
            if ($e instanceof NotFoundHttpException) {
                 throw new NotFoundHttpException(
                     "{$request->method()} {$request->url()}", null, $e->getCode()
                );
             }
0 likes
0 replies

Please or to participate in this conversation.