mabasic's avatar

Call to a member function make() on null

This is strange.

Same global composer dependencies on both PCs but running phpunit on one PC throws fatal error while running phpunit on another PC returns all tests green.

This is a fresh installation of L5. When working with L4 applications everything works on both PCs.

I will start from start. First the test:

<?php
class ExampleTest extends TestCase {
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $response = $this->call('GET', '/');
        $this->assertEquals(200, $response->getStatusCode());
    }
}

Now my global composer dependencies:

{
    "require": {
        "laravel/installer": "~1.1",
        "laravel/envoy": "~1.0",
        "laravel/homestead": "~2.0",
        "phpunit/phpunit": "~4.0",
        "mabasic/kalista": "0.7.*",
        "codeception/codeception": "~2.0"
    }
}

The dependencies are the same on both PCs.

Now the error I get on one PC:

PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Configuration read from F:\Repositories\acme\phpunit.xml

PHP Fatal error:  Call to a member function make() on null in F:\Repositories\acme\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php on line 164

Now the things that I have tried:

I have tried

  • deleting local composer vendor folder and using composer update
  • deleting global composer vendor folder and using composer update
  • deleting composer cache and both local and global vendor folders and using composer update

The weird stuff:

  • When inside vm (homestead) if i run vendor/bin/phpunit everything works on both PCs. (so my reasoning is that there is nothing wrong with the L5 app, but something is most likely wrong in my global dependencies.)
  • Running phpunit on any L4 app works. It does not throw any kind of error. All tests run and pass.
  • I have tried running phpunit on two L5 apps, both fail with fatal error.

What can I do now?

0 likes
12 replies
mabasic's avatar
mabasic
OP
Best Answer
Level 3

OMG :OOOO

Ok, I have managed to get it working.

Steps:

  • Update composer composer self-update
  • Delete composer cache located in AppData\Local\Composer
  • Delete composer global vendor folder: AppData\Roaming\Composer\vendor
  • Close all of your terminals, shells, command line programs
  • Start shell as administrator and run composer global update

Maybe the update composer and run shell as administrator parts were crucial. I really don't know why this worked now and before it did not.

3 likes
uni_t's avatar

This still doesn't work for me, even after following the five steps above :/

The app() function in helpers.php seems to return NULL, what could be causing this?

1 like
Maxime.Lespilette's avatar

Happen to me today,

In fact I just forgot to add parent::setUp(); in my test setup method.

34 likes
karolis's avatar

In the test class add the constructor with parent::setUp();

function __construct()
    {
        parent::setUp();
    }
20 likes
aakarim's avatar

I had the same problem. Turns out I had declared my own constructor which was overriding the parent constructor, I called the same function from TestCase.php and it worked:

    public function __construct(){
        // Overriding the constructor so need to reinstantiate
        parent::createApplication();
    
        //Constructor code here
     }
1 like
yigitozkavci's avatar

Actually, the problem is that you should call parent::setUp() as @karolis mentioned. What solved for me was this:

public function setUp() {
    parent::setUp();
    // Your code here
}
7 likes
dmccull2000's avatar

I had the same problem. Laracasts should allow the users to vote for the correct answer. The one the user poster chose is incorrect. Really glad I continued reading through all the comments. Forgetting to add parent::setUp() to my setUp method also caused me issues.

1 like
jjtriff's avatar

@MAXIME.LESPILETTE - This made it for me. $this->app was returning null on my testClass but worked fine in other testClasses.

The difference was the setUp() method, and of course, once you use setUp() you must include parent::setUp()

That's the gained experience :)

1 like
cristimocean's avatar

I get that error if I add Test at the end of the method. Why ? (Laravel 5.2)

MosesNdeda's avatar

This is exactly the same thing I was missing. So my IOC container was not initiating so when I called resolve(SomeInterface::class) I would get a BindingResolutionException and if I called $this->app->make(SomeInterface::class) I would receive error call to app make() on null. Always call setUp and tearDown methods in test classes and in each always remember to put first line as parent::setUp() and parent::tearDown(). This small glitch can cost you hours of development time; but it's experience nonetheless, just like @jjtriff said.

Please or to participate in this conversation.