简体   繁体   English

Laravel 5.1中的PHPUnit刷新请求()?

[英]PHPUnit in Laravel 5.1 flush request()?

Looks like I don't know some basic things about PHPUnit. 看来我对PHPUnit不了解一些基本知识。 My problem: 我的问题:

In AppServiceProvider.php AppServiceProvider.php中

class AppServiceProvider extends ServiceProvider{
   public function boot() {
      request()->country_id = 100;
   }
}

In IndexController.php IndexController.php中

public function index(Request $request) {
   dd(request()->country_id);
}

Controller here for example, it could be used in views, controllers, everywhere... 例如,这里的控制器,可以在视图,控制器,任何地方使用...

Basic test from box ExampleTest.php 从框的基本测试ExampleTest.php

<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    public function testBasicExample()
    {
        $this->visit('/')
             ->see('Laravel 5');
    }
}

In browser it will display 100 , when I'm using in CLI "phpunit tests/ExampleTest" it will return null . 在浏览器中它将显示100 ,当我在CLI中使用“ phpunit tests / ExampleTest”时,它将返回null Why? 为什么?

And is it right to use request() this way? 以这种方式使用request()是否正确? Because each country have own domain, and domain (host) is a part or request. 因为每个国家/地区都有自己的域,所以域(主机)是一部分或要求。 So I decide to make country part of request too, it have a sense in my logic. 因此,我决定也将国家/地区作为请求的一部分,这在我的逻辑中是有道理的。

I found solution. 我找到了解决方案。 It was my mistake. 是我的错 It should be in RouteServiceProvider instead of AppServiceProvider . 它应该在RouteServiceProvider中,而不是AppServiceProvider中 Thanks to all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM