Skip to content

Commit

Permalink
Handle case of static controller method and controllers using magic _…
Browse files Browse the repository at this point in the history
…_call() method
  • Loading branch information
DerManoMann authored and fabpot committed Nov 24, 2012
1 parent 5aa6788 commit e477a2e
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 7 deletions.
Expand Up @@ -98,13 +98,25 @@ public function collect(Request $request, Response $response, \Exception $except
if (isset($this->controllers[$request])) {
$controller = $this->controllers[$request];
if (is_array($controller)) {
$r = new \ReflectionMethod($controller[0], $controller[1]);
$this->data['controller'] = array(
'class' => get_class($controller[0]),
'method' => $controller[1],
'file' => $r->getFilename(),
'line' => $r->getStartLine(),
);
try {
$r = new \ReflectionMethod($controller[0], $controller[1]);
$this->data['controller'] = array(
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
'method' => $controller[1],
'file' => $r->getFilename(),
'line' => $r->getStartLine(),
);
} catch (\ReflectionException $re) {
if (is_callable($controller)) {
// using __call or __callStatic
$this->data['controller'] = array(
'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0],
'method' => $controller[1],
'file' => 'n/a',
'line' => 'n/a',
);
}
}
} elseif ($controller instanceof \Closure) {
$this->data['controller'] = 'Closure';
} else {
Expand Down
Expand Up @@ -11,10 +11,14 @@

namespace Symfony\Component\HttpKernel\Tests\DataCollector;

use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\EventDispatcher\EventDispatcher;

class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -50,6 +54,95 @@ public function testCollect(Request $request, Response $response)
$this->assertEquals('application/json',$c->getContentType());
}

/**
* Test various types of controller callables.
*
* @dataProvider provider
*/
public function testControllerInspection(Request $request, Response $response)
{
// make sure we always match the line number
$r1 = new \ReflectionMethod($this, 'testControllerInspection');
$r2 = new \ReflectionMethod($this, 'staticControllerMethod');
// test name, callable, expected
$controllerTests = array(
array(
'"Regular" callable',
array($this, 'testControllerInspection'),
array(
'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
'method' => 'testControllerInspection',
'file' => __FILE__,
'line' => $r1->getStartLine()
),
),

array(
'Closure',
function() { return 'foo'; },
'Closure',
),

array(
'Static callback as string',
'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod',
),

array(
'Static callable with instance',
array($this, 'staticControllerMethod'),
array(
'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
'method' => 'staticControllerMethod',
'file' => __FILE__,
'line' => $r2->getStartLine()
),
),

array(
'Static callable with class name',
array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
array(
'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
'method' => 'staticControllerMethod',
'file' => __FILE__,
'line' => $r2->getStartLine()
),
),

array(
'Callable with instance depending on __call()',
array($this, 'magicMethod'),
array(
'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
'method' => 'magicMethod',
'file' => 'n/a',
'line' => 'n/a'
),
),

array(
'Callable with class name depending on __callStatic()',
array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
array(
'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
'method' => 'magicMethod',
'file' => 'n/a',
'line' => 'n/a'
),
),
);

$c = new RequestDataCollector();

foreach ($controllerTests as $controllerTest) {
$this->injectController($c, $controllerTest[1], $request);
$c->collect($request, $response);
$this->assertEquals($controllerTest[2], $c->getController(), sprintf('Testing: %s', $controllerTest[0]));
}
}

public function provider()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
Expand All @@ -71,4 +164,39 @@ public function provider()
);
}

/**
* Inject the given controller callable into the data collector.
*/
protected function injectController($collector, $controller, $request)
{
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
$httpKernel = new HttpKernel(new EventDispatcher(), $resolver);
$event = new FilterControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
$collector->onKernelController($event);
}

/**
* Dummy method used as controller callable
*/
public static function staticControllerMethod()
{
throw new \LogicException('Unexpected method call');
}

/**
* Magic method to allow non existing methods to be called and delegated.
*/
public function __call($method, $args)
{
throw new \LogicException('Unexpected method call');
}

/**
* Magic method to allow non existing methods to be called and delegated.
*/
public static function __callStatic($method, $args)
{
throw new \LogicException('Unexpected method call');
}

}

0 comments on commit e477a2e

Please sign in to comment.