theUnforgiven's avatar

PHPUnit getting started

Based on my contributions/questions earlier today, I've decided I'm going to do some Unit Testing, but come across my first stumbling block!

When I run phpunit from the terminal I get the following error:

lee$ phpunit
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Configuration read from /Users/lee/code/l5blog/phpunit.xml

PHP Fatal error:  Call to a member function make() on a non-object in /Users/lee/.composer/vendor/illuminate/support/Illuminate/Support/helpers.php on line 33

Fatal error: Call to a member function make() on a non-object in /Users/lee/.composer/vendor/illuminate/support/Illuminate/Support/helpers.php on line 33

Anyone know what's wrong & how I can fix?

I have one test that comes out of the box with L5

class ExampleTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $response = $this->call('GET', '/');

        $this->assertEquals(200, $response->getStatusCode());
    }

}

@blackbird

0 likes
21 replies
bobbybouwmann's avatar

How does your TestCase class looks like? As far as I know you need to do some preparation before you can run any unit tests! You can find this class in tests/TestCase.php

Aren't you using codeception? You can run the unit tests from as well by codeception like this:

// Run all tests
./vendor/bin/codecept run

// Just the unit tests
./vendor/bin/codecept run unit
theUnforgiven's avatar

TestCase

<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase {

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }

}

theUnforgiven's avatar

I'm not using Codeception in this case but it is installed on my machine globally.

bobbybouwmann's avatar

The only thing that I have different from you is that I used the setUp method in the TestCase class

<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase {

    /**
     * Default preparation for each test.
     */
    public function setUp()
    {
        parent::setUp();
    }

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__ . '/../bootstrap/app.php';

        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }

}
bobbybouwmann's avatar

I have no clue on what's happening here! If you do something like this, does it return an Application object?

public function createApplication()
{
    $app = require __DIR__ . '/../bootstrap/app.php';
        
    dd($app);

    $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

    return $app;
}
bobbybouwmann's avatar

Mmmh, sorry man! I seriously have no clue on this!

You did run composer install and the installation is working in the browser right?

theUnforgiven's avatar

Yes I have a site setup just fine, and also done a composer update to get the latest version of L5

theUnforgiven's avatar

Even after pulling down a fresh install and doing nothing apart from phpunit from the terminal, I still get the same errors

theUnforgiven's avatar

I've logged into my Homestead VM and installed PHPUnit then run the test and this worked, but surely I shouldn't have to login to the VM to run tests?

@JeffreyWay your a legend on this type of thing can you help?

bobbybouwmann's avatar

Of course you have to login in your vm to run the tests! You need a server to run the tests on!

theUnforgiven's avatar

Just realised what I said there, my bad (smacks head in anger) how stupid am I

theUnforgiven's avatar

So my next question would be what do you name your methods? What ever it is you want to test?

it_should_show_X_amount

bobbybouwmann's avatar

call it whatever you want!

There are two ways to define a test

// start the function name with test
public function test_initialization()
{
    $this->assertInstanceOf('expected', 'actual');
}

Or

// Use annotation
/** @test */
public function initialization()
{
    $this->assertInstanceOf('expected', 'actual');
}
HRcc's avatar

Either test_that_doing_this_thing_produces_that_output_under_those_circumstances or it_provides_an_unicorn_if_you_request_it :)

To be honest, just be descriptive and avoid method names like testSecurity or testUpload.

theUnforgiven's avatar

Thanks both for advice and now know I need to be within the VM to load phpunit from the terminal. Will continue this tomorrow.

mabasic's avatar

You don't need to be inside vm to run tests ...

You just have to have php configured on your host to execute php files.

I am facing this exact problem but it works on my other PC.

Fresh install of L5 getting this error on phpunit:

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

If I run phpunit on different PC the tests pass normal. Both PC's have same composer dependencies phpunit ~4.0.

Don't know what is going on. I have tried deleting vendor folder, deleting composer cache, but it still does not work on one PC, but it works on another.

If I run phpunit on L4 apps; it works normal.

Please or to participate in this conversation.