16

I am testing a class I made that uses a helper function. The function I'm testing looks like:

public function upload(UploadedFile $file)
{
    $file = $file->move(base_path() . '/temp');
    $file = new FileSystemPicture($file);

    return $file;
}

When I run the tests and it hits the helper function it says:

PHP Fatal error:  Call to a member function make() on a non-object

I have tracked the helper function down to be the following code:

function base_path($path = '')
{
    return app()->make('path.base').($path ? '/'.$path : $path);
}

I know that I'm getting the error because the "app" hasn't been created because I'm only testing the my class, not the whole application. Are there other effective ways to get the root directory of my project or is there a way to force the "app" to load so it can be used in this function?

3 Answers 3

19

I had this problem, solved making sure that:

  1. My test class extended TestCase.
  2. If my test class had a setUp() method, it called parent::setUp()

app_path() and similar functions worked after that.

3
  • 1
    That's alright for integration tests, but for the unit tests you need a way to mock the helper function.
    – Rob_vH
    Jul 21, 2015 at 21:14
  • 1
    parent::setUp() should be called before any helper methods are called
    – haakym
    May 2, 2017 at 13:37
  • 3
    To be clear, the test class should extend Tests\TestCase, not PHPUnit\Framework\TestCase
    – murribu
    Jan 16, 2018 at 16:37
1

There is a way to mock helper functions that use app() function in a complete isolation from laravel environment. Considering that your package has illuminate/support dependency you could do something like this:

<?php

    class Test extends PHPUnit_Framework_TestCase
    {
        protected function mockApplication(array $methods = [])
        {
            return $this->getMock('Illuminate\Foundation\Application', $methods);
        }

        public function testHelperFunction()
        {
            $appMock = $this->mockApplication(['make']);

            \Illuminate\Support\Facades\Facade::setFacadeApplication($appMock);

            $testPath = "my/path";
            $myPath = "otherPath";

            $expectedValue = "{$testPath}/{$myPath}";

            $appMock->expects($this->once())
                    ->method('make')->with('path.base')
                    ->will($this->returnCallback(function () use ($testPath) {
                        return $testPath;
                    }));

            $this->assertSame($expectedValue, base_path($myPath));
        }

    }
0

I just ran into this issue, and my solution is to wrap the whole helpers.php contents with

if (!file_exists('newFunction')) {

This should prevent the attempt at re-declaring the functions after the first test case has executed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.