用于自定义域无法正常工作的Laravel5.3 phpunit

时间:2017-01-21 14:14:32

标签: laravel phpunit laravel-5.3

当我尝试使用像这样的自定义域

<?php

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

protected $prefixDomain = '';

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

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

    return $app;
}

protected function setBaseUrl()
{
    $this->baseUrl = env('APP_URL', $this->baseUrl);
    if ($this->prefixDomain) {
        $this->baseUrl = substr_replace(
            $this->prefixDomain,
            $this->baseUrl,
            strlen('https://'),
            0
        );
    }
}

我设置了&#39; APP_URL&#39;在.env文件

当我在Illuminate \ Routing \ RouteCollection.php中转储并死掉时,似乎请求uri不正确

/**
 * Find the first route matching a given request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Routing\Route
 *
 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
 */
public function match(Request $request)
{
    dd($request);
    $routes = $this->get($request->getMethod());

我期望的输出是&#34; REQUEST_URI&#34; =&GT; &#34; /测试&#34;但实际上

enter image description here

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好像我解决了这个问题。 我应该添加&#34; http://&#34;到APP_URL

// .env
APP_URL=http://domain.com

我应该看到的代码就在这里

// Illuminate\Foundation\Testing\Concerns\MakesHttpRequests@654
protected function prepareUrlForRequest($uri)
{
    if (Str::startsWith($uri, '/')) {
        $uri = substr($uri, 1);
    }

    if (! Str::startsWith($uri, 'http')) {
        $uri = $this->baseUrl.'/'.$uri;
    }

    return trim($uri, '/');
}

希望这能帮助像我这样的人!