2016-03-24 18 views
1

私はLaravel 5ベースのウェブサイトを持っていると私はそれにPHPUnitのテストを実行したい:Laravel 5とPHPUnitのテストがあるため、未定義のインデックスで失敗:REMOTE_ADDR

:そのために、私は私のテストフォルダに以下のファイルを持っています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('[email protected]', 'email') 
     ->type('[email protected]', '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 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. 

は、あなたがこのようなエラーを見たことがあり、またはあなたは私のテストは失敗になり、問題を見ることができます:

そしてここでは、PHPUnitのテストのために私の結果ですか?

答えて

3

phpユニットは同じマシン上で動作し、個々のクラスや関数をテストするため、REMOTE_ADDRプロパティは存在しません。 REMOTE_ADDRプロパティは、インターネット経由でhttp接続経由でページにアクセスしているときに表示されます。これまでREMOTE_ADDRが使用されているところだから、使用してのチェック

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

**最適な方法を行うことができますどこ

0

これに必要なブートストラップファイルまたはインデックスファイルにこれを持っていると$ REMOTE_ADDR変数を消費することですあなたがフレームワーク層を無視しても構わないなら、最良の方法です。通常の環境、ロードバランサ、ローカルコマンド、テストユニットをサポートします。

// 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; 
関連する問題