I'm trying to get a Symfony controller in a test harness using Codeception. Every method starts as follows:
public function saveAction(Request $request, $id)
{
// Entity management
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
/* Actual code here
...
*/
}
public function submitAction(Request $request, $id)
{
// Entity management
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
/* 200+ lines of procedural code here
...
*/
}
I've tried:
$request = \Symfony\Component\HttpFoundation\Request::create(
$uri, $method, $parameters, $cookies, $files, $server, $content);
$my_controller = new MyController();
$my_controller->submitAction($request, $id);
from my unit tests, but it seems there's a lot of other setup I don't know about that Symfony does in the background. Every time I find a missing object and initialise it, there's another one that fails at some point.
I've also tried stepping through the test from PhpStorm, but PhpUnit has some output that causes Symfony to die before it gets anywhere near the code I'm trying to test because it can't start the $_SESSION
after any output has occurred. I don't think this happens from the command line, but I'm not really close enough to tell yet.
How can I simply and extensibly run this code in a Unit Test?
A bit of background:
I inherited this code. I know it's dirty and smells because it's doing model logic in the controller. I know that what I'm asking for is not a "pure" unit test because it touches virtually the whole application.
But I need to be able to run just this "small" (200+ lines) bit of code automatically. The code should run in no more than a couple of seconds. I don't know how long because I've never been able to run it independently.
Currently, the setup time to run this code through the website is huge, as well as being complex. The code does not generate a web page, it's basically an API call that generates a file. I need to be able to generate as many of these test files as I like in a short amount of time as I'm making coding changes.
The code is what it is. It's my job to be able to make changes to it and at the moment I can't even run it without a huge overhead each time. It would be irresponsible to make changes to it without knowing what it's doing.