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

automica's avatar

Testing routes in custom laravel package

I'm developing a package which adds a controller and related routes to my application.

I'm able to register the routes by including the following:

        $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');

within my boot method in my ServiceProvider.

and similarly to register my config

 if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__ . '/../config/language.php' => config_path('language.php'),
            ], 'config');
        }

If i run php artisan route:list I can see the route in my application when the package is installed.

I'm building out the test coverage, and have used Orchestra/Testcase.

The issue i'm facing is that when running my tests i can't seem to access either routes or any of the config i have included. Any suggestions on what i need to do here?

0 likes
4 replies
Niush's avatar

What does your tests look like? Are they extending a TestCase class? And, if yes, what does the TestCase file look like?

My guess would be you are missing something like this in the TestCase class:

// ...
protected function getPackageProviders($app)
{
    return [
       YourAppServiceProvider::class,
   ];
}

https://laravelpackage.com/04-testing.html#directory-structure

1 like
automica's avatar

@Niush this is my TesCase

<?php

namespace Iammikek\LaravelI18nLoader\Tests;

use Iammikek\LaravelI18nLoader\Providers\LaravelI18nLoaderServiceProvider;
use Illuminate\Redis\RedisServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
    public function setUp(): void
    {
        parent::setUp();
        // additional setup
    }

    protected function getPackageProviders($app): array
    {
        return [
            LaravelI18nLoaderServiceProvider::class,
        ];
    }

    protected function getEnvironmentSetUp($app)
    {
        // perform environment setup
    }
}

and I am extending with my tests.

Niush's avatar
Niush
Best Answer
Level 51

@automica Looks fine. I don't see any issue.

May be you accidently extended \Orchestra\Testbench\TestCase instead of extending your \Iammikek\LaravelI18nLoader\Tests\TestCase in your test files ? Other then that, I don't see any problem.

automica's avatar

@Niush

May be you accidently extended \Orchestra\Testbench\TestCase instead of extending your \Iammikek\LaravelI18nLoader\Tests\TestCase in your test files ?

That was it! Thanks.

Please or to participate in this conversation.