symfonycodeception

Use Symfony TestContainer in an environment not called "test"


I write a unit test in CodeCception, my config Unit.suite.yml

actor: UnitTester
modules:
    enabled:
      - Asserts
      - Symfony:
          environment: 'autotest'
step_decorators: ~

I have a separate environment for running Unit test in pipeline - autotest, and I would like to use my services via Codeception\Module\Symfony->grabService, but I get the error that the service should be public, however, if I use the test environment, Symfony loads Symfony\Bundle\FrameworkBundle\Test\TestContainer and there are no errors, is there any way to get Symfony to load this container for the autotest environment?

I tried to make all services public in config/packages/services.yml via

when@autotest:
  services:
    _defaults:
      public: true
      autowire: true
      autoconfigure: true

I also tried adding each separate service and putting public: true on it, but it didn't help


Solution

  • Set framework.test to true in your custom environment.

    E.g.

    #framework.yaml
    
    when@test:
      framework:
        test: true
        csrf_protection: false
        session:
          storage_factory_id: session.storage.factory.mock_file
    
    
    when@autotest:
      framework:
        test: true
        csrf_protection: false
        session:
          storage_factory_id: session.storage.factory.mock_file
    

    I think this might do the trick. Hopefully there aren't other sites where the "test" environment is "assumed" to be literally "test".