gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:16:14 28 4
gpt4 key购买 nike

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

示例测试.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('/');
}
}

测试用例.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.

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

最佳答案

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

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

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

关于由于未定义索引 : REMOTE_ADDR,Laravel 5 和 PHPUnit 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195480/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com