Sorry about my previous post. It was a old project that i just resumed and I forgot that sendForm was an auxiliary method that I created for my tests.
Anyway as an example is the best documentation, I'll post here the bare minimum to reproduce the problem.
First. I have a Service that just returns a string.
- Code: Select all
namespace TestVendor\WebTestBundle\Service;
class MyService {
public function testMethod() { return "Real service"; }
}
Then, I have a controller that uses this Service, through dependency injection, after a form is submitted. Then prints on screen the string that the service returned.
- Code: Select all
namespace TestVendor\WebTestBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* @Route("/")
* @Template()
*/
public function indexAction(Request $request) {
if ($request->getMethod() == 'POST') {
$service = $this->get("myservice");
$testingValue = $service->testMethod();
return new Response( $testingValue );
}
return array();
}
}
I have configured the service at bundle level using services.yml...
- Code: Select all
services:
myservice:
class: TestVendor\WebTestBundle\Service\MyService
I have a very simple view, just a form with a submit button
- Code: Select all
<form method="post" action="">
<input type="submit" id="submit" value="submit" />
</form>
And now, the important bit.. a test case that fails (when I expected it to work)
The test try to replace the service with a stub service that return another text and then asserts for that text.
- Code: Select all
<?php
namespace TestVendor\WebTestBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();
// mock the service
$stub = $this->getMock('TestVendor\WebTestBundle\Service\MyService');
$stub->expects($this->any())
->method('testMethod')
->will( $this->returnValue("Mock Service") );
$client->getContainer()->set('myservice', $stub);
$crawler = $client->request('GET', '/test/');
// I also tried changing the container here, with the same result.
$form = $crawler->selectButton("submit")->form();
$crawler = $client->submit($form, array());
$this->assertTrue($crawler->filter('html:contains("Mock service")')->count() > 0);
}
}
It is just a simplified version of my actual problem, but I believe that is ok as an example.
I do not want to change services.yml to inject another service, when running on test environment, because I have many tests like this, some uses the real service, other need to use mocks with slightly different behavior.
The problem is with Submit method, if I tried something similar, but using request, it works ok.
Thank in advance.
Sorry about my english
