8000 [HttpKernel] added a unit test to demonstrate that passing objects wo… · symfony/symfony@43ce368 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43ce368

Browse files
committed
[HttpKernel] added a unit test to demonstrate that passing objects works for inline controllers
1 parent 70f3399 commit 43ce368

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/Symfony/Component/HttpKernel/Fragment/RoutableFragmentRenderer.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,11 @@ protected function generateFragmentUri(ControllerReference $reference, Request $
6464

6565
private function checkNonScalar($values)
6666
{
67-
foreach ($values as $value) {
67+
foreach ($values as $key => $value) {
6868
if (is_array($value)) {
6969
$this->checkNonScalar($value);
70-
}
71-
72-
if (!is_scalar($value)) {
73-
throw new \LogicException('Controller attributes cannot contain non-scalar values.');
70+
} elseif (!is_scalar($value)) {
71+
throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar values (value for key "%s" is not a scalar).', $key));
7472
}
7573
}
7674
}

src/Symfony/Component/HttpKernel/Tests/Fragment/InlineFragmentRendererTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ public function testRenderWithObjectsAsAttributes()
7171
$strategy->render(new ControllerReference('main_controller', array('object' => $object), array()), Request::create('/'));
7272
}
7373

74+
public function testRenderWithObjectsAsAttributesPassedAsObjectsInTheController()
75+
{
76+
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver', array('getController'));
77+
$resolver
78+
->expects($this->once())
79+
->method('getController')
80+
->will($this->returnValue(function (\stdClass $object, Bar $object1) {
81+
return new Response($object1->getBar());
82+
}))
83+
;
84+
85+
$kernel = new HttpKernel(new EventDispatcher(), $resolver);
86+
$renderer = new InlineFragmentRenderer($kernel);
87+
88+
$response = $renderer->render(new ControllerReference('main_controller', array('object' => new \stdClass(), 'object1' => new Bar()), array()), Request::create('/'));
89+
$this->assertEquals('bar', $response->getContent());
90+
}
91+
7492
8000 /**
7593
* @expectedException \RuntimeException
7694
*/
@@ -168,3 +186,12 @@ public function testESIHeaderIsKeptInSubrequest()
168186
$strategy->render('/', $request);
169187
}
170188
}
189+
190+
class Bar {
191+
public $bar = 'bar';
192+
193+
public function getBar()
194+
{
195+
return $this->bar;
196+
}
197+
}

src/Symfony/Component/HttpKernel/Tests/Fragment/RoutableFragmentRendererTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ public function testGenerateFragmentUriWithARequest()
4747

4848
/**
4949
* @expectedException LogicException
50+
* @dataProvider getGenerateFragmentUriDataWithNonScalar
5051
*/
51-
public function testGenerateFragmentUriWithObject()
52+
public function testGenerateFragmentUriWithNonScalar($controller)
5253
{
53-
$controller = new ControllerReference('controller', array('foo' => new Foo(), 'bar' => 'bar'), array());
54-
5554
$this->callGenerateFragmentUriMethod($controller, Request::create('/'));
5655
}
5756

57+
public function getGenerateFragmentUriDataWithNonScalar()
58+
{
59+
return array(
60+
array(new ControllerReference('controller', array('foo' => new Foo(), 'bar' => 'bar'), array())),
61+
array(new ControllerReference('controller', array('foo' => array('foo' => 'foo'), 'bar' => array('bar' => new Foo())), array())),
62+
);
63+
}
64+
5865
private function callGenerateFragmentUriMethod(ControllerReference $reference, Request $request)
5966
{
6067
$renderer = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer');
35EA

0 commit comments

Comments
 (0)
0