KodeFor.Me
KodeFor.Me

Reputation: 13521

Symfony: Can I change container parameters on run time?

I am totally new to Symfony, and I try to run some Acceptance tests. All good until now, but when I run the test I get a response like the following:

enter image description here

When I run my API controllers using the PostMan, I don't get any related information. I have this output only while I run the tests in my command line.

Based on the output message:

The "UsersBundle\Controller\UsersController" service is private, getting it from the contains is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

Apart from that I don't use the UsersController directly in my test method:

public function test_cannot_send_multiple_possword_reset_requests() {
    $client = static::createClient( $this->defaults );

    $client->request(
        'POST',
        '/api/v1/user-account/request/reset',
        [
            'username' => 'merianos',
        ],
        [],
        [
            'Content-Type' => 'application/json',
        ]
    );

    $this->assertEquals( 400, $client->getResponse()->getStatusCode() );
    $this->assertContains(
        'An email containing your password reset token has been send.',
        $client->getResponse()->getContent()
    );
}

I was wondering if it is possible to override the following setting at runtime only for my unit/acceptance tests:

# /src/UsersBundle/Resources/config/services.yml
services:
    _defaults:
        public: false

Or of course, if there's any other way to achieve that same result.

Note, that I am using Symfony 3.4.

Upvotes: 1

Views: 2490

Answers (2)

KodeFor.Me
KodeFor.Me

Reputation: 13521

Finally I hack it in a totally different way :)

Here is my solution:

<!-- ./phpunit.xml.dist -->
<phpunit other="settings-here">
    <php>
        <!-- some other settings here -->
        <env name="environment" value="test" />
    </php>
    <!-- Rest of the settings -->
</phpunit>

Then I did this:

<?php
// ./src/UsersBundle/Resources/config/environment-setup.php

if (
    isset( $_ENV['environment'] ) &&
    in_array( $_ENV['environment'], [ 'test', 'acceptance' ] )
) {
    $container->setParameter( 'public_services', true );
} else {
    $container->setParameter( 'public_services', false );
}

And finally I did this:

# ./src/UsersBundle/Resources/config/serviecs.yml

imports:
    - { resource: environment-setup.php }

services:
    _defaults:
        public: '%public_services%'
    # Rest of the setup.

Upvotes: 2

mtricht
mtricht

Reputation: 452

You should be able to override it in config_test.yml. You can read more about environments here: https://symfony.com/doc/current/configuration/environments.html

Upvotes: 2

Related Questions