Home » Php » php – Unit testing with the config app file in Laravel

php – Unit testing with the config app file in Laravel

Posted by: admin July 12, 2020 Leave a comment


My model method relies on the config() global, here;

public function getGroup()
    if(config('app.pages.'.$this->group.'.0')) {
        return $this->group;
    return "city";

I am trying to test this method in my unit test class, here;

public function testGetGroupReturnsCityAsDefault()
    $response = new Response();
    $response->group = "town";
    $test = $response->getGroup();

The error I get is;

Error: Call to a member function make() on null

I know this is related to the config() global. But not sure how to set it in my test. I tried

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

But got the same error. How can I set this up?

How to&Answers:

I’m not sure if you have solved this yet but I just had a similar issue.

There were two things I had to do to get it to work:

1) Make sure you are calling the setUp parent method like so:

public function setUp()

    // other stuff

2) Make sure your test class is extending the Laravel TestCase rather than the PHPUnit_Framework_TestCase

Brief explanation: Basically the error you are getting is because the ContainerInstance of Laravel is null since you are going through PHPUnit and as such it was never created. If you do the above steps you’ll ensure that Laravel will first instantiate a container instance.

P.S. If you are going to end up ultimately referencing env variables, you should look into the phpunit.xmlsection for enviornment variables.


PHP Unit Test Implement in Laravel

1) install php unit in your system

composer global require phpunit/phpunit *

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

        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"

2) check in CMD – phpunit
– Create .env.testing and create new test database
– Create Faker for dummy data
– write test case logic

3)phpunit file_path –filter FunctionName

4)phpunit –coverage-html reports/


<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
      <testsuite name="APIs">
            <directory suffix="Test.php">./tests/APIs</directory>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        <log type="coverage-html" target="./report"
            lowUpperBound="50" highLowerBound="80" />
        <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"/>



namespace Tests\Unit;

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

class ExampleTest extends TestCase
     * A basic test example.
     * @return void
    public function testBasicTest()