Php 使用Laravel中的配置应用程序文件进行单元测试

Php 使用Laravel中的配置应用程序文件进行单元测试,php,unit-testing,laravel-5,Php,Unit Testing,Laravel 5,我的模型方法依赖于config()global,在这里 public function getGroup() { if(config('app.pages.'.$this->group.'.0')) { return $this->group; } return "city"; } 我试图在我的单元测试类中测试这个方法,在这里 public function testGetGroupReturnsCityAsDefault() { $

我的模型方法依赖于
config()
global,在这里

public function getGroup()
{
    if(config('app.pages.'.$this->group.'.0')) {
        return $this->group;
    }
    return "city";
}
我试图在我的单元测试类中测试这个方法,在这里

public function testGetGroupReturnsCityAsDefault()
{
    $response = new Response();
    $response->group = "town";
    $test = $response->getGroup();
    dd($test);
}
我得到的错误是

Error: Call to a member function make() on null
/home/vagrant/sites/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:62
/home/vagrant/sites/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:163
我知道这与config()全局变量有关。但我不知道如何在我的测试中设置它。我试过了

public function setUp()
{
config(['app.pages' => [
    'city' => [........

但也犯了同样的错误。我如何设置它?

我不确定您是否已经解决了这个问题,但我刚刚遇到了一个类似的问题

为了让它发挥作用,我必须做两件事:

1) 确保您正在调用设置父方法,如下所示:

public function setUp()
{
    parent::setUp();

    // other stuff
}
2) 确保您的测试类扩展的是Laravel
TestCase
,而不是
PHPUnit\u框架\u TestCase

简要说明:基本上,您得到的错误是因为Laravel的
容器状态
为空,因为您要通过PHPUnit,因此它从未创建过。如果执行上述步骤,您将确保Laravel首先实例化一个容器实例


p.S.如果您最终要引用
env
变量,您应该查看
phpunit.xml
部分中的环境变量。

在Laravel中实现PHP单元测试

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
      <testsuite name="APIs">
            <directory suffix="Test.php">./tests/APIs</directory>
        </testsuite>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./report"
            lowUpperBound="50" highLowerBound="80" />
    </logging>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>
1) 在系统中安装php单元

composer global require phpunit/phpunit *

- Add this configuration in below path in  D:\wamp64\bin\php\php7.3.5\php.ini

[xdebug]
        zend_extension="d:/wamp64/bin/php/php7.3.5/zend_ext/php_xdebug-2.7.2-7.3-vc15-x86_64.dll"
        xdebug.remote_enable = on
        xdebug.profiler_enable = off
        xdebug.profiler_enable_trigger = Off
        xdebug.profiler_output_name = cachegrind.out.%t.%p
        xdebug.profiler_output_dir ="d:/wamp64/tmp"
        xdebug.show_local_vars=0
        xdebug.remote_autostart=On
2) 签入CMD-phpunit -创建.env.testing并创建新的测试数据库 -为虚拟数据创建伪造者 -编写测试用例逻辑

3) phpunit文件\ u路径--过滤器函数名

4) phpunit——覆盖率html报告/

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
      <testsuite name="APIs">
            <directory suffix="Test.php">./tests/APIs</directory>
        </testsuite>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./report"
            lowUpperBound="50" highLowerBound="80" />
    </logging>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

/测试/原料药
/测试/单元
/测试/特性
/应用
ExampleTest.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

谢谢你的解答,忘了使用parent::setUp()方法。值得注意的是,在以后的laravel版本中,这个示例将不起作用,因为它需要与parent方法兼容,并使用
void
返回类型,如下所示:
公共函数setUp():void
对我来说,它是#2!非常感谢!