Search code examples
phptestingnamespacesautoloadcodeception

Codeception's Autoload class doesn't autoload


I'm trying to autoload a bunch of helpers into a test suite. The helpers are located in a folder outside of the test suite in the hopes that I can reuse them across multiple projects if needed.

This is pretty much what I have:

- helpers
    - TestHelper.php
- tests
    - _data
    - _output
    - _support
        - _generated
        - Helper
            - Integration.php
        - IntegrationTester.php
    - integration
    - bootstrap.php
    - integration.suite.yml
- vendor
- codeception.yml

This is the bootstrap file

// bootstrap.php

<?php

\Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "../helpers" );

This is the global config file:

// codeception.yml

bootstrap: bootstrap.php
namespace: main
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed

This is the integration suite's config file:

// integration.suite.yml

actor: IntegrationTester
modules:
    enabled:
        - \awesome\helpers\TestHelper
        - \main\Helper\Integration

This is the TestHelper:

<?php

namespace awesome\helpers;

class TestHelper extends \Codeception\Module{

   public function sayHello() {
      return "Hello"; 
   }

}

As soon as I do codecept run, I get the following error:

Module \awesome\helpers\TestHelper could not be found and loaded

I'm not posting any tests because it's irrelevant, the error is raised before any test is performed as it's a config issue.

From what I understand, the global config file should run the bootstrap file before the tests are run, and the Autoload class in the bootstrap file should load the helpers in the awesome\helpers namespace, but that's clearly not happening. What am I doing wrong?


Solution

  • I think that you made a classical mistake and missed / before .. so you set the path to tests../helpers.

    Change

    \Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "../helpers" );
    

    to

    \Codeception\Util\Autoload::addNamespace( "awesome\helpers", __DIR__ . "/../helpers" );