Skip to content

Instantly share code, notes, and snippets.

@avalanche123
Created September 30, 2010 11:29
Show Gist options
  • Save avalanche123/604430 to your computer and use it in GitHub Desktop.
Save avalanche123/604430 to your computer and use it in GitHub Desktop.
<?php
namespace Company\ApplicationBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class ApplicationBundle extends Bundle {
public function registerExtensions(ContainerBuilder $container) {
parent::registerExtensions($container);
// register controllers
$loader = new XmlFileLoader($container);
$loader->load(__DIR__.'/Resources/config/controllers.xml');
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://www.symfony-project.org/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/routing http://www.symfony-project.org/schema/routing/routing-1.0.xsd">
<route id="index" pattern="/index">
<default key="_controller">index_controller:indexAction</default>
</route>
</routes>
<?php
$client = $this->createClient();
$client->request('GET', '/index');
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertRegExp('/<h1>My Cool Website<\/h1>/', $response->getContent());
<?xml version="1.0" ?>
<container xmlns="http://www.symfony-project.org/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
<services>
<service id="index_controller" class="Company\ApplicationBundle\Controller\IndexController">
<call method="setTemplating" />
<argument type="service" id="templating" />
</call>
</service>
</services>
</container>
Company
| - ApplicationBundle
| | - Controller
| | | - IndexController.php
| | - Resources
| | | - config
| | | | - controller_routing.xml
| | | | - controllers.xml
| | | - views
| | | | - Index
| | | | | - index.php
| | - ApplicationBundle.php
<?php
namespace Company\ApplicationBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\Engine;
class IndexController
{
/**
* @var Symfony\Bundle\FrameworkBundle\Templating\Engine
*/
private $templating;
/**
* @param Symfony\Bundle\FrameworkBundle\Templating\Engine $templating
*/
public function setTemplating(Engine $templating)
{
$this->templating = $templating;
}
/**
* @return Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
return $this->templating->render('ApplicationBundle:Index:index');
}
}
<?php
namespace Company\ApplicationBundle\Tests\Controller;
use Company\ApplicationBundle\Controller\IndexController;
class IndexControllerTest extends \PHPUnit_Framework_TestCase
{
//...
public function testIndexAction()
{
$templating = $this->getMockTemplating();
$templating->expects($this->once())
->method('render')
->with('ApplicationBundle:Index:index')
->will($this->returnValue('success'))
;
$controller = new IndexController();
$controller->setTemplating($templating);
$this->assertEquals('success', $controller->indexAction());
}
private function getMockTemplating()
{
return $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\Engine', array(), array(), '', false, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment