lundi 30 novembre 2015

PHPUnit and Laravel's Middleware

Currently I am supplying constants to views where the user is logged in by using the below Middleware:

<?php

namespace App\Http\Middleware;

use Closure;

/*
 * Middleware has replaced what used to be "filters"
 */
class AddConstantsFilesToAllRoutes
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        require_once app_path('constants') . '/constants2013andearlier.php';
        require_once app_path('constants') . '/constants20142015.php';
        require_once app_path('constants') . '/constants20152016.php';

        return $next($request);
    }
}

This works well for my views. However, when I try testing a user logging in with PHPUnit, I get an error that my constants are undefined, specifically in a service provider that I am using to supply the same sets of data to multiple views (which also works fine when not testing). Here is the error that PHPUnit gives me:

1) ExampleTest::testToSeeIfLoginLoads
A request to [http://ift.tt/1Q9CBqk] failed. Received status code [500].

C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:166
C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:64
C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:110
C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:64
C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:86
C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:653
C:\xampp\htdocs\BuddoBotLaravel\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:640
C:\xampp\htdocs\BuddoBotLaravel\tests\ExampleTest.php:35
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129

Caused by
exception 'ErrorException' with message 'Use of undefined constant NUM_TOTAL_ALB_BADGES - assumed 'NUM_TOTAL_ALB_BADGES'' in C:\xampp\htdocs\BuddoBotLaravel\app\Providers\ViewComposerServiceProvider.php:131
Stack trace:
#0 C:\xampp\htdocs\BuddoBotLaravel\app\Providers\ViewComposerServiceProvider.php(131): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Use of undefine...', 'C:\\xampp\\htdocs...', 131, Array)
#1 C:\xampp\htdocs\BuddoBotLaravel\app\Providers\ViewComposerServiceProvider.php(80): App\Providers\ViewComposerServiceProvider->provideALBTrackerData(Object(Illuminate\View\View))

If anyone can help me figure out how to get PHPUnit to recognize my constants, I'd appreciate it. Here is my PHPUnit test:

     /**
     * Tests to see if the login page loads 
     */
    public function testToSeeIfLoginLoads()
    {
        $this->visit('/login')
            ->see('Login')->see('Email Address')->see('Password')
            ->type('email@email.com', 'email')->type('mypassword', 'password')
            ->press('Log In')->see('Complete Course Summary');
    }

The problem is NOT that I have disabled the middleware in the PHPUnit test by using the withoutMiddleware() method or class. However, the problem I have described above does act like I have disabled the middleware, at least the middleware that my constants are being supplied in.

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire