#Cannot call `Route::getRoutes()` in test

5 messages · Page 1 of 1 (latest)

mild creek
#

Hi,
I have two files:

  • ApiTest.pxp:
<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Route;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;

class ApiTest extends TestCase
{
    public static function getData()
    {
        Route::getRoutes();
    }

    #[DataProvider("getData")]
    public function testExample() {}
}
  • TestCase.php:
<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    public function createApplication()
    {
        $app = require __DIR__ . '/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

Running php artisan test returns this error:

FAILED  Tests\Feature\ApiTest > example
  The data provider specified for Tests\Feature\ApiTest::testExample is invalid
  A facade root has not been set.

I would like to automate testing by getting every route my API consist of.
However, this error confuses me. Searching through the internet didn't help.
What is the problem and how can I solve this issue?
Does somebody have more experience with testing and may help me?

Thank you in advance!
~ Maschga

shy orchid
#

Data providers are called before setup(), so before Laravel has booted. I've tried something similar myself, but haven't found a solution - other than looping through the routes inside a single test method.

mild creek
shy orchid
mild creek
#

Then I'll have to think about another plan. Thanks for your help!