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();
dd($test);
}
The error I get is;
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
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?
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()
{
parent::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.xml
section 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]
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) 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/
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);
}
}