2

I have the following problem:

I have a route defined only for development or testing purposes, which I 'protect' by doing the following in my controller:

public function testAction()
{
    $env = $this->container->get('kernel')->getEnvironment();

    if ($env !== 'test' || $env !== 'dev')
    {
        throw $this->createNotFoundException('Oops, page not available in production environment...');
    }

    ...
}

Now this works well in production, but when I try to run my tests it keeps firing an 404.

When I echo the $env variable during tests it says: test

FtestFtestFtest.......... (output from phpunit)

I've tryed using != and " but no luck.

In the tests I do something alike:

$client = static::createClient();
$client->request('GET', '/basecontroller/test');
$this->assertEquals(200, $client->getResponse()->getStatusCode());

When I comment out the $env check all tests pass. What could be the cause of this?

2 Answers 2

4
if ($env !== 'test' || $env !== 'dev')

should be

if ($env !== 'test' && $env !== 'dev')

Moreover you don't need identity match but only value match as you're sure here that you're comparing strings

Moreover, you could bypass all this mess by defining this route only in routing_dev.yml and routing_test.yml (or equivalents that you need to "include" somehow)

Sign up to request clarification or add additional context in comments.

1 Comment

After taking a 5 minute walk I came to the same conclusion, I will indeed put it in routing_dev.yml. Thanks for the clarification
1

You could also match against the debug parameter in order to prevent that every new environment you make should be added to the comparison:

if (!$this->get('kernel')->isDebug()) {
    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.