How to provide namespace of Kernel to Codeception for a Symfony4 project

455 views Asked by At

I'm testing Symfony4 new directory structure with Coception.

Codeception is installed and configured. Because of the end of bundle strategy, I known that an error will occured and here is the error:

PHP Fatal error: Uncaught Error: Class 'Kernel' not found in ...\my_project\vendor\codeception\codeception\src\Codeception\Module\Symfony.php:146

Codeception don't find Kernel class, because of its new namespace which is App and not an empty namespace.

Kernel is now in the src directory and declared in the App namespace.

The new Kernel declaration

src/Kernel.php

namespace App; // <-- the namespace

[...]

class Kernel extends BaseKernel[...]

Previously, Kernel was named AppKernel and didn't have namespace:

The old Kernel declaration

app/AppKernel.php

<?php

//no namespace

[...]

class AppKernel extends Kernel

I edited the 265th line of the Symfony.php file to change this line which is returning Kernel:

$class = $file->getBasename('.php');

by this line to return App\Kernel:

$class = 'App\\'.$file->getBasename('.php');

This modification forces the new namespace of the new structure.

My functional tests run perfectly, but I want to avoid modification in vendor file.

How can I force the App namespace without this hack? It seems there is no parameter to do it, but can I encapsulate codeception in the app namespace or find a hack to put it in my files, not in the vendor file?

2

There are 2 answers

0
Federkun On BEST ANSWER

You could create an alias for the Kernel class:

   class_alias('App\Kernel', 'Kernel');
0
zstate On

There is a known issue in Codeception https://github.com/Codeception/Codeception/issues/4695. Basically Codeception only allows certain Symfony Kernel names:

# codeception/src/Codeception/Module/Symfony.php:287

$possibleKernelClasses = [
    'AppKernel', // Symfony Standard
    'App\Kernel', // Symfony Flex
];

The workaround, for now, would be to use class_alias('MyNamespace\Kernel', 'App\Kernel'); as was noted by @Federkun.