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


Unit testing with the config app file in Laravel

我的模型方法依赖于全局config(),这里;

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. 确保像这样调用 setUp 父方法:
public function setUp()
{
    parent::setUp();
    // other stuff
}
  1. 确保您的测试类正在扩展 Laravel TestCase而不是PHPUnit_Framework_TestCase

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

附言如果您最终要引用env变量,则应查看环境变量的phpunit.xml部分。

PHP 单元测试在 Laravel 中实现

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.test 并创建新的测试数据库 - 为虚拟数据创建伪造者 - 编写测试用例逻辑

3)phpunit file_path --filter 函数名称

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>

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