5
votes

Right now when I setup a new test for my Laravel application, it extends from the base TestCase class

class SomeTest extends TestCase
{
}

I'd like to create a new base test class named AnotherTestCase, so I can create test cases that share setup/teardown/helper methods/etc...

class SomeTest extends AnotherTestCase
{
}

However, when I run

phpunit app/tests/SomeTest.php

I get the following error

PHP Fatal error:  Class 'AnotherTestCase' not found in /[...]/app/tests/SomeTest.php on line 3

This is despite the fact I have a class defined at

#File: app/tests/AnotherTestCase.php
<?php
class AnotherTestCase extends TestCase
{
}

This is confusing, since phpunit seems to automatically load the TestCase class.

Do I need to manually require in custom base test classes, or is there a way to tell phpunit about my new base test class? Put another way, why does phpunit automatically load TestCase, but doesn't automatically load AnotherTestCase

1

1 Answers

7
votes

You can get around this error by adding this to your composer.json:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/filters",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/tests/AnotherTestCase.php"  // <-- Add Me
    ],
// ...

Afterwards be sure you do a composer dump-autoload. I just tested this by adding the following class:

class AnotherTestCase extends TestCase {}

And changed one of my existing tests to use this as its parent, instead. I believe that entry in composer.json is how you are able to load TestCase.