0

I'm trying to test a service which takes the Symfony\Component\HttpKernel\Kernel as a dependency.

This is what i do in order to mock the kernel.

$this->kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$this->kernel->expects($this->any())
     ->method('getEnvironment')
     ->will($this->returnValue('dev'));

When i run the test, i get something as the following error.

Fatal error: Cannot redeclare static foo\foo\WebTestCase::$kernel as non static foo\foo\MyTest::$kernel in ...

Any ideas why?

1 Answer 1

2

Read the error message carefully. WebTestCase::$kernel is a static variable, which means you cannot access it with $this. An attempt to override it as an instance variable causes this error.

Besides, WebTestCase class is not meant to be a base class for unit tests where you mock the kernel. It's a class that facilitates writing functional tests, which is why the real kernel is injected into this static variable. If you want to write a unit test and mock the kernel, use the usual TestCase class from PHPUnit.

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

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.