PHPUnit和Laravel的中间件

时间:2015-11-30 23:51:15

标签: php phpunit laravel-5.1 middleware service-provider

目前,我正在使用以下中间件向用户登录的视图提供常量:

<?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);
    }
}

这适用于我的观点。但是,当我尝试测试用PHPUnit登录的用户时,我得到一个错误,我的常量是未定义的,特别是在我用来向多个视图提供相同数据集的服务提供者中(在不测试时也可以正常工作) )。这是PHPUnit给我的错误:

1) ExampleTest::testToSeeIfLoginLoads
A request to [http://buddobot.localhost/timelines] 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))

如果有人可以帮我弄清楚如何让PHPUnit识别我的常量,我会很感激。这是我的PHPUnit测试:

     /**
     * 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');
    }

问题是 NOT 我使用withoutMiddleware()方法或类在PHPUnit测试中禁用了中间件。但是,我上面描述的问题确实像我已经禁用了中间件,至少是我的常量所提供的中间件。

提前致谢。

0 个答案:

没有答案