1

I'm trying to introduce functional testing into an old codebase using various tools, one of which is Codeception (2.2.0). Acceptance tests pass a basic log in scenario:

public function login($name, $password)
{
    $I = $this;
    $I->amOnPage('/login');
    $I->submitForm('.loginform', array(
        '_username' => $name,
        '_password' => $password
    ));
    $I->see('User', '.navbar');
}

however when I try to run a functional test of the same scenario, it fails

[Exception] environment instance is already set

Looking at the stack trace, it originates from the listener that is registering the instance:

public function onKernelRequest() {
    $this->getContainer()->get('environment');
}

which leads to the service in config.yml:

environment:
    class: MyApp\Environment
    calls:
        - [ setContainer, [ @service_container ] ]

pointing to the class:

class Environment {


public function __construct() {
    self::setInstance($this);
   }


public static function setInstance(Environment $environment) {
    if (null !== self::$instance) {
        throw new \Exception('environment instance is already set');
    }
    self::$instance = $environment;
}

How best can I understand how to remove the error with the functional testing? Is the code an anti-pattern and in need of refactoring? Or is it a problem with my Codeception configuration?

class_name: FunctionalTester
modules:
    enabled:
        - Symfony2:
            app_path: 'app'
            var_path: 'app'
        - Doctrine2:
            depends: Symfony2
        - \Helper\Functional
        - Asserts
        - Sequence

If you need me to add more information here, let me know.

Update

I found this answer and by switching from Symfony module to PhpBrowser, the tests passed. However, the original question - of how to run the functional test using the Symfony module - persists.

Michael Millar
  • 1,364
  • 2
  • 24
  • 46
  • @DharmaSaputra Because this is the basic step before I continue with the rest of the testing. If I can't pass a test of the logging in of a user, then the rest of the functional testing won't work – Michael Millar Nov 15 '17 at 08:14
  • What's the point of the docs then? https://codeception.com/docs/04-FunctionalTests – Michael Millar Nov 15 '17 at 08:44
  • That's why I tagged it in Symfony. The Environment does not come from the Symfony core. I am following the Codeception documentation, which includes a log in scenario for a functional test. Ignore the acceptance test, because that is not what my question concerns. – Michael Millar Nov 15 '17 at 09:27
  • Sorry, my bad. Will remove all my recent comments then. So this question still clean. Sorry for messing up. – Dharma Saputra Nov 15 '17 at 09:39

0 Answers0