Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4.2: Test Case Autoloading

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

like image 875
Alan Storm Avatar asked Sep 02 '14 17:09

Alan Storm


1 Answers

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.

like image 111
Jeff Lambert Avatar answered Sep 23 '22 02:09

Jeff Lambert