Laravel 5和PHPUnit测试由于未定义索引而失败:REMOTE_ADDR

时间:2016-03-24 07:58:16

标签: laravel phpunit undefined

我有一个基于Laravel 5的网站,我希望对它运行一个PHPUnit测试:为此,我在我的测试文件夹中有以下文件:

ExampleTest.php

<?php

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

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->visit('/')
             ->see('Laravel 5');
    }
}

NewTest.php

<?

class Example22Test extends TestCase
{

    /**
     * My test implementation
     */
    public function testEncryptionIsIntelligent()
    {
        $this->visit('/')
        ->see('Laravel 5')
        ->visit('/auth/login')
        ->type('test.admin@', 'email')
        ->type('test.admin@eatlaravel.hu', 'email')
        ->type('admin', 'password')
        ->press('')
        ->seePageIs('/admin')
        ->see('Welcome to the dashboard')
        ->visit('/');
    }
}

TestCase.php

<?php

#class TestCase extends Illuminate\Foundation\Testing\TestCase
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    /**
     * The base URL to use while testing the application.
     *
     * @var string
     */
    protected $baseUrl = 'http://localhost';

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

        return $app;
    }
}

以下是我对PHPUnit测试的结果:

PHPUnit 5.2.12 by Sebastian Bergmann and contributors.


Starting test 'ExampleTest::testBasicExample'.
.
Starting test 'Example22Test::testEncryptionIsIntelligent'.
F                                                                  2 / 2 (100%)

Time: 433 ms, Memory: 21.25Mb

There was 1 failure:

1) Example22Test::testEncryptionIsIntelligent
A request to [http://localhost/admin] failed. Received status code [500].

/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:165
/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:63
/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:109
/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:63
/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:85
/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:688
/var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:675
/var/www/hu.testsite/tests/NewTest.php:17

Caused by
exception 'ErrorException' with message 'Undefined index: REMOTE_ADDR' in /var/www/hu.testsite/app/Http/Middleware/QueryLogAfterMiddleware.php:88
Stack trace:
#0 /var/www/hu.testsite/app/Http/Middleware/QueryLogAfterMiddleware.php(88): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', '/var/www/hu.bit...', 88, Array)
#1 /var/www/hu.testsite/app/Http/Middleware/QueryLogAfterMiddleware.php(23): App\Http\Middleware\QueryLogAfterMiddleware->getIp()
#2 [internal function]: App\Http\Middleware\QueryLogAfterMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#3 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#4 /var/www/hu.testsite/app/Http/Middleware/QueryLogBeforeMiddleware.php(18): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 [internal function]: App\Http\Middleware\QueryLogBeforeMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#6 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#7 /var/www/hu.testsite/app/Http/Middleware/AuthenticateAdmin.php(52): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#8 [internal function]: App\Http\Middleware\AuthenticateAdmin->handle(Object(Illuminate\Http\Request), Object(Closure))
#9 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#10 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#12 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Routing/Router.php(710): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#13 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#14 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#15 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#16 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#17 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#18 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#21 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#22 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#24 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#27 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#28 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#30 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#31 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#33 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
#36 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#37 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#38 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#39 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#40 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php(394): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#41 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php(61): Illuminate\Foundation\Testing\TestCase->call('GET', 'http://localhos...', Array, Array, Array)
#42 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php(109): Illuminate\Foundation\Testing\TestCase->makeRequest('GET', 'http://localhos...')
#43 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php(63): Illuminate\Foundation\Testing\TestCase->followRedirects()
#44 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php(85): Illuminate\Foundation\Testing\TestCase->makeRequest('POST', 'http://localhos...', Array, Array, Array)
#45 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php(688): Illuminate\Foundation\Testing\TestCase->makeRequestUsingForm(Object(Symfony\Component\DomCrawler\Form), Array)
#46 /var/www/hu.testsite/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php(675): Illuminate\Foundation\Testing\TestCase->submitForm('', Array, Array)
#47 /var/www/hu.testsite/tests/NewTest.php(17): Illuminate\Foundation\Testing\TestCase->press('')
#48 [internal function]: Example22Test->testEncryptionIsIntelligent()
#49 /var/www/hu.testsite/vendor/phpunit/phpunit/src/Framework/TestCase.php(984): ReflectionMethod->invokeArgs(Object(Example22Test), Array)
#50 /var/www/hu.testsite/vendor/phpunit/phpunit/src/Framework/TestCase.php(844): PHPUnit_Framework_TestCase->runTest()
#51 /var/www/hu.testsite/vendor/phpunit/phpunit/src/Framework/TestResult.php(686): PHPUnit_Framework_TestCase->runBare()
#52 /var/www/hu.testsite/vendor/phpunit/phpunit/src/Framework/TestCase.php(800): PHPUnit_Framework_TestResult->run(Object(Example22Test))
#53 /var/www/hu.testsite/vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#54 /var/www/hu.testsite/vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#55 /var/www/hu.testsite/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(436): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#56 /var/www/hu.testsite/vendor/phpunit/phpunit/src/TextUI/Command.php(158): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array, true)
#57 /var/www/hu.testsite/vendor/phpunit/phpunit/src/TextUI/Command.php(109): PHPUnit_TextUI_Command->run(Array, true)
#58 /var/www/hu.testsite/vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
#59 {main}
FAILURES!
Tests: 2, Assertions: 6, Failures: 1.

您是否看到过这样的错误,或者您可以看到导致我的测试失败的问题?

2 个答案:

答案 0 :(得分:5)

当php单元在同一台机器上运行并测试单个类或函数时,REMOTE_ADDR属性不会出现在那里。当您通过Internet访问http连接页面时,REMOTE_ADDR属性将存在。因此,无论何时使用REMOTE_ADDR,您都可以进行检查

$remote_addr = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';

**最佳使用方法是在引导程序文件或索引文件中使用它,并在必要时使用$ remote_addr变量

答案 1 :(得分:1)

如果您不介意忽略框架图层,这是最好的方法。它将支持正常环境,负载平衡器,本地命令和测试单元。

    // If the application is executed by command (or unit testing).
    if(php_sapi_name() === 'cli') {
        return "127.0.0.1";
    }

    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
        // Behind balancer
        $clientIpAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']) {
        // Universal PHP headers
        $clientIpAddress = $_SERVER['REMOTE_ADDR'];
    } else {
        // Unusual headers
        $clientIpAddress = $_SERVER['HTTP_CLIENT_IP'];
    }

    return $clientIpAddress;