jeudi 23 janvier 2020

symfony + codeception - New services cannot be grabbed

I have the following setup:

Symfony 5 project, with codeception 4.

I am trying to create a test for a newly created service. The problem is that I receive the error below when I try to test it.

Test  tests/integration/Shop/NewServiceCest.php:testSomething
Step  Grab service "App\Service\NewService"
Fail  Service App\Service\NewService is not available in container

The service code is below (is basically empty):

<?php

namespace App\Service;

class NewService
{

    public function __construct()
    {

    }

}

The test code is below (again, basically empty):

<?php

namespace App\Tests\Integration\New;

use App\Exception\NewException;
use App\Service\NewService;
use App\Tests\IntegrationTester;

class NewServiceCest {

    private NewService $newService;

    public function _before(IntegrationTester $i) {
        $this->newService = $i->grabService(NewService::class);
    }

    public function testSomething(IntegrationTester $i) {
        // $i->expectThrowable(NewException::class, $this->newService->doStuff('invalidArgument'));
    }
}

Integration tests yml:

actor: IntegrationTester
modules:
    enabled:
        - Symfony:
              app_path: 'src'
              environment: 'test'
        - Doctrine2:
              depends: Symfony
              cleanup: true
              dump: 'tests/_data/integration.sql'
              populate: true
              reconnect: true
        - \App\Tests\Helper\Integration
        - Asserts

I have tried to clear cache, recreate it etc. everything I could think of from cli, including (current path is project dir):

$./bin/console c:c
$./bin/console c:c --env=test
$rm -rf var (this should do nothing but still tried it)
$rm -rf tests/_support/_generated
$./bin/console cache:warmup --env=test
$./vendor/bin/codecept build

When I ran the test, same issue.

What did make it work, was to inject the service somewhere (for example in a controller), execute the controller, then use ./vendor/bin/codecept build. After I did those steps the test worked as expected. The obvious problem with this is that I am trying to create a pipeline for deploy where I raise a test environment (clean, on the fly before deploying to servers), run tests, and ensure everything is fine. Injecting all services somewhere just to be able to build is not an option (but I am opened to any cli options, including running most commands).

Even more strange is that after I did this, now the test works as expected even after I clean caches (var and _generated). Is like it would save something in a different place that I can't seem to find (can't find any relevant configs regarding this and google and bing fail me).

So... why is this happening, and how can I reliably make it work?

Aucun commentaire:

Enregistrer un commentaire