Laravel Testing - the name is already in use

195 views Asked by At

Im having problem with phpunit, I don't know why Im having this kind of error when the file as okay, My theory is, when the test is running it will reinclude the name file instead of checking if the file was already been imported then it should not be reissued.

i tried doing require_once on CreateApplications But it causes error,

I cannot Test all test cases on one class because of the error

Cannot declare class <class omitted>, because the name is already in use in <file omitted>

<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;

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

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

My question is, How to fix this error or how not to reimported the class? how to prevent it?

as of now Im using --filter option on PHPUnit in order to call specific method on the Test Class

1

There are 1 answers

0
Al Lestaire On BEST ANSWER

Update

There is a custom package, which uses include and this causes the error, In my case, it is place on the custom package service provider, at boot method

<?php

namespace CustomPackage;

use Illuminate\Support\ServiceProvider;

class CustomPackageProvider extends ServiceProvider{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(){
        ...
        include(__DIR__.'/Nem/NemPhp.php');
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register(){

    }
}

so changing the include to require_once solve the issue The error is not too clear on the report thats why Im having hard time questioning the theorising how it because an error :)