Skip to content

Commit

Permalink
Removed container dependency from controller
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjmit committed Sep 4, 2013
1 parent e44c1fa commit 3d5de34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
12 changes: 2 additions & 10 deletions spec/Peterjmit/BlogBundle/Controller/BlogControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use Symfony\Component\DependencyInjection\ContainerInterface;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
Expand All @@ -17,20 +15,15 @@
class BlogControllerSpec extends ObjectBehavior
{
function let(
ContainerInterface $container,
ManagerRegistry $registry,
ObjectManager $manager,
ObjectRepository $repository,
EngineInterface $templating
) {
$container->has('doctrine')->willReturn(true);
$container->get('doctrine')->willReturn($registry);
$container->get('templating')->willReturn($templating);

$registry->getManager()->willReturn($manager);
$manager->getRepository('PeterjmitBlogBundle:Blog')->willReturn($repository);

$this->setContainer($container);
$this->beConstructedWith($registry, $templating);
}

function it_is_initializable()
Expand All @@ -48,8 +41,7 @@ function it_should_respond_to_index_action(
$templating
->renderResponse(
'PeterjmitBlogBundle:Blog:index.html.twig',
array('posts' => array()),
null
array('posts' => array())
)
->willReturn($mockResponse)
;
Expand Down
22 changes: 16 additions & 6 deletions src/Peterjmit/BlogBundle/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@

namespace Peterjmit\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

class BlogController extends Controller
class BlogController
{
private $doctrine;
private $templating;

public function __construct(ManagerRegistry $doctrine, EngineInterface $templating)
{
$this->doctrine = $doctrine;
$this->templating = $templating;
}

public function indexAction()
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager = $this->doctrine->getManager();
$posts = $entityManager->getRepository('PeterjmitBlogBundle:Blog')->findAll();

return $this->render('PeterjmitBlogBundle:Blog:index.html.twig', array(
return $this->templating->renderResponse('PeterjmitBlogBundle:Blog:index.html.twig', array(
'posts' => $posts
));
}

public function showAction($id)
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager = $this->doctrine->getManager();
$post = $entityManager->getRepository('PeterjmitBlogBundle:Blog')->find($id);

if (!$post) {
throw $this->createNotFoundException(sprintf('Blog post %s was not found', $id));
}

return $this->render('PeterjmitBlogBundle:Blog:show.html.twig', array(
return $this->templating->renderResponse('PeterjmitBlogBundle:Blog:show.html.twig', array(
'posts' => $posts
));
}
Expand Down

0 comments on commit 3d5de34

Please sign in to comment.