nivv's avatar
Level 5

Using namespace with Laravels TestCase (integrated testing)

Hey!

The reason I'm asking this is because php-cs-fixer spits out an error for all my test classes. The reason is that the test classes doesn't have a namespace, which they don't.

I've tried adding a namespace myself, but that doesn't seem to work. The error I get is class must be in a namespace of at least one level

It's explained here (rule 3): http://www.php-fig.org/psr/psr-1/#3

I've tried updating my composer file to have a namespace as the regular App has, but then the TestCase class is not found.

Any ideas?

Screenshot:

Alt text

0 likes
5 replies
bobbybouwmann's avatar
Level 88

Well you can easy add the namespace like this

<?php

namespace Tests;

class TestCase {

And for any other classes, let's say you have integration directory

<?php

namespace Tests\Integration;

use Tests/TestCase;

class MyIntegrationTestClass extends TestCase {

Also make sure you autoload the tests directory

// composer.json

"autoload-dev": {
    "psr-4" : [
        "Tests\\": "tests",
    ],
    "classmap": [
        "tests/TestCase.php"
    ]
},
3 likes
nivv's avatar
Level 5

@bobbybouwmann I tried this but I get below error

ReflectionException: Class Tests\Illuminate\Contracts\Console\Kernel does not exist

Edit: Nevermind! I forgot to add a \ here: $app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

zanematthew's avatar

Although this is old, the updated(?), all that was needed for me was


namespace Tests;

use TestCase;
kyslik's avatar

Why does my composer.json look like this? "psr-4" : { <-- curly

"autoload-dev": {
        "psr-4" : {
            "Tests\\": "tests/"
        },
        "classmap": [
            "tests/TestCase.php"
        ]
    },

Please or to participate in this conversation.