Skip to content

Commit 3d5de34

Browse files
committed
Removed container dependency from controller
1 parent e44c1fa commit 3d5de34

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

spec/Peterjmit/BlogBundle/Controller/BlogControllerSpec.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
77

8-
use Symfony\Component\DependencyInjection\ContainerInterface;
9-
108
use Doctrine\Common\Persistence\ManagerRegistry;
119
use Doctrine\Common\Persistence\ObjectManager;
1210
use Doctrine\Common\Persistence\ObjectRepository;
@@ -17,20 +15,15 @@
1715
class BlogControllerSpec extends ObjectBehavior
1816
{
1917
function let(
20-
ContainerInterface $container,
2118
ManagerRegistry $registry,
2219
ObjectManager $manager,
2320
ObjectRepository $repository,
2421
EngineInterface $templating
2522
) {
26-
$container->has('doctrine')->willReturn(true);
27-
$container->get('doctrine')->willReturn($registry);
28-
$container->get('templating')->willReturn($templating);
29-
3023
$registry->getManager()->willReturn($manager);
3124
$manager->getRepository('PeterjmitBlogBundle:Blog')->willReturn($repository);
3225

33-
$this->setContainer($container);
26+
$this->beConstructedWith($registry, $templating);
3427
}
3528

3629
function it_is_initializable()
@@ -48,8 +41,7 @@ function it_should_respond_to_index_action(
4841
$templating
4942
->renderResponse(
5043
'PeterjmitBlogBundle:Blog:index.html.twig',
51-
array('posts' => array()),
52-
null
44+
array('posts' => array())
5345
)
5446
->willReturn($mockResponse)
5547
;

src/Peterjmit/BlogBundle/Controller/BlogController.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,40 @@
22

33
namespace Peterjmit\BlogBundle\Controller;
44

5-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
5+
use Doctrine\Common\Persistence\ManagerRegistry;
6+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
67

7-
class BlogController extends Controller
8+
class BlogController
89
{
10+
private $doctrine;
11+
private $templating;
12+
13+
public function __construct(ManagerRegistry $doctrine, EngineInterface $templating)
14+
{
15+
$this->doctrine = $doctrine;
16+
$this->templating = $templating;
17+
}
18+
919
public function indexAction()
1020
{
11-
$entityManager = $this->getDoctrine()->getManager();
21+
$entityManager = $this->doctrine->getManager();
1222
$posts = $entityManager->getRepository('PeterjmitBlogBundle:Blog')->findAll();
1323

14-
return $this->render('PeterjmitBlogBundle:Blog:index.html.twig', array(
24+
return $this->templating->renderResponse('PeterjmitBlogBundle:Blog:index.html.twig', array(
1525
'posts' => $posts
1626
));
1727
}
1828

1929
public function showAction($id)
2030
{
21-
$entityManager = $this->getDoctrine()->getManager();
31+
$entityManager = $this->doctrine->getManager();
2232
$post = $entityManager->getRepository('PeterjmitBlogBundle:Blog')->find($id);
2333

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

28-
return $this->render('PeterjmitBlogBundle:Blog:show.html.twig', array(
38+
return $this->templating->renderResponse('PeterjmitBlogBundle:Blog:show.html.twig', array(
2939
'posts' => $posts
3040
));
3141
}

0 commit comments

Comments
 (0)