PapaHotelPapa
PapaHotelPapa

Reputation: 697

Why can't PHPUnit in Laravel access a route that exists?

I'm having a problem when I run a PHPUnit test but not when I try to test the application manually.

My unit test contains

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserAdminPanelTest extends TestCase
{
    use DatabaseMigrations;

    /**
     * Tests that the user/company admin panel is showing data and able to save changes.
     *
     * @return void
     */
    public function testUserAdminPanel()
    {
        $user = factory(App\User::class)->create();

        $this->actingAs($user)
             ->withSession(['foo' => 'bar'])
             ->visit('/')
             ->click('User')
             ->seePageIs('/user/3')
             ->see('Edit')
             ->click('Edit');   // There's more after this but this is where I get the error
    }
}

and my routes are

Route::group(['middleware' => 'web'], function () {
    Route::auth();

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

    Route::get('/home', 'HomeController@index');

    Route::group(['middleware' => 'auth'], function () {
        Route::resource('user', 'UserController',
            ['except' => ['create', 'store', 'destroy']]);

    });

});

and the error I get when I try to run my test is

There was 1 failure:

1) UserAdminPanelTest::testUserAdminPanel
A request to [http://localhost/usbsinc/public/user/3/edit] failed. Received status code [404].

C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:178
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:72
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:53
C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:706
C:\xampp\htdocs\usbsinc\tests\UserAdminPanelTest.php:27
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129

Caused by
exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:161
Stack trace:
#0 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Router.php(823): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#1 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Router.php(691): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#3 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#4 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#5 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#6 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#7 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(124): call_user_func_array(Array, Array)
#9 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#10 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#11 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#13 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#14 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#15 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php(480): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#16 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php(70): Illuminate\Foundation\Testing\TestCase->call('GET', 'http://localhos...', Array, Array, Array)
#17 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php(53): Illuminate\Foundation\Testing\TestCase->makeRequest('GET', 'http://localhos...')
#18 C:\xampp\htdocs\usbsinc\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php(706): Illuminate\Foundation\Testing\TestCase->visit('http://localhos...')
#19 C:\xampp\htdocs\usbsinc\tests\UserAdminPanelTest.php(27): Illuminate\Foundation\Testing\TestCase->click('Edit')
#20 [internal function]: UserAdminPanelTest->testUserAdminPanel()
#21 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestCase.php(909): ReflectionMethod->invokeArgs(Object(UserAdminPanelTest), Array)
#22 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
#23 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
#24 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(UserAdminPanelTest))
#25 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#26 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\Framework\TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#27 C:\xampp\htdocs\usbsinc\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#28 C:\xampp\php\pear\PHPUnit\TextUI\Command.php(176): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#29 C:\xampp\php\pear\PHPUnit\TextUI\Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#30 C:\xampp\php\phpunit(46): PHPUnit_TextUI_Command::main()
#31 {main}

I've dug and dug but I can't figure out what's happening or even how something like this could happen. Any hints on some debug code or what might be going on?

Upvotes: 1

Views: 2970

Answers (3)

İlter Kağan Öcal
İlter Kağan Öcal

Reputation: 3690

For my case, APP_URL value in .env file should start with http:// otherwise RouteCollection fails to match the request created by MakesHttpRequest

APP_URL=http://myproject.test => Works

APP_URL=myproject.test => Doesn't Work

Upvotes: 1

PapaHotelPapa
PapaHotelPapa

Reputation: 697

The problem turned out to be that I was using the method Click, which will click an anchor tag but my view had an HTML5 button element called "Edit" instead. I switched the method to Press and it worked.

Upvotes: 0

Mike
Mike

Reputation: 421

I had the same issue and fixed it this way:

Goto line 140 of /vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php

Dump and die the $request inside of function match(Request $request) by adding this code and running phpunit from the command line.

dd($request);

Now, check out the "REQUEST_URI" of the dumped array.

I found this was incorrect for me, my app_path appeared to be duplicated and this lead me back to the source of my problem which was in a modification I made to the tests/TestCase.php class.

Make sure $baseUrl is correctly set:

protected $baseUrl = 'http://localhost';

I'd updated this in trying to fix an issue with my .env file and forgot to change it back. Let me know if this fixes your issue!

Upvotes: 3

Related Questions