mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
Merge branch '3.2.x'
* 3.2.x: [ticket/13502] Also cover passing object to resolver in tests [ticket/13502] Test getArguments() method of controller resolver [ticket/13502] Fix coding style [ticket/13502] Controller resolver should handle callable functions and objects
This commit is contained in:
commit
71c4ffceec
3 changed files with 78 additions and 15 deletions
|
@ -126,9 +126,21 @@ class resolver implements ControllerResolverInterface
|
||||||
*/
|
*/
|
||||||
public function getArguments(Request $request, $controller)
|
public function getArguments(Request $request, $controller)
|
||||||
{
|
{
|
||||||
// At this point, $controller contains the object and method name
|
// At this point, $controller should be a callable
|
||||||
list($object, $method) = $controller;
|
if (is_array($controller))
|
||||||
$mirror = new \ReflectionMethod($object, $method);
|
{
|
||||||
|
list($object, $method) = $controller;
|
||||||
|
$mirror = new \ReflectionMethod($object, $method);
|
||||||
|
}
|
||||||
|
else if (is_object($controller) && !$controller instanceof \Closure)
|
||||||
|
{
|
||||||
|
$mirror = new \ReflectionObject($controller);
|
||||||
|
$mirror = $mirror->getMethod('__invoke');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$mirror = new \ReflectionFunction($controller);
|
||||||
|
}
|
||||||
|
|
||||||
$arguments = array();
|
$arguments = array();
|
||||||
$parameters = $mirror->getParameters();
|
$parameters = $mirror->getParameters();
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
include_once(__DIR__ . '/ext/vendor2/foo/controller.php');
|
||||||
|
include_once(__DIR__.'/phpbb/controller/foo.php');
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
@ -64,7 +67,7 @@ class phpbb_controller_controller_test extends phpbb_test_case
|
||||||
$this->assertNull($routes->get('controller_noroute'));
|
$this->assertNull($routes->get('controller_noroute'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_controller_resolver()
|
protected function get_foo_container()
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
// YamlFileLoader only uses one path at a time, so we need to loop
|
// YamlFileLoader only uses one path at a time, so we need to loop
|
||||||
|
@ -75,26 +78,59 @@ class phpbb_controller_controller_test extends phpbb_test_case
|
||||||
$loader->load('services.yml');
|
$loader->load('services.yml');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Autoloading classes within the tests folder does not work
|
return $container;
|
||||||
// so I'll include them manually.
|
}
|
||||||
if (!class_exists('vendor2\\foo\\controller'))
|
|
||||||
{
|
public function test_controller_resolver()
|
||||||
include(__DIR__ . '/ext/vendor2/foo/controller.php');
|
{
|
||||||
}
|
$container = $this->get_foo_container();
|
||||||
if (!class_exists('phpbb\\controller\\foo'))
|
|
||||||
{
|
|
||||||
include(__DIR__.'/phpbb/controller/foo.php');
|
|
||||||
}
|
|
||||||
|
|
||||||
$resolver = new \phpbb\controller\resolver($container, dirname(__FILE__) . '/');
|
$resolver = new \phpbb\controller\resolver($container, dirname(__FILE__) . '/');
|
||||||
$symfony_request = new Request();
|
$symfony_request = new Request();
|
||||||
$symfony_request->attributes->set('_controller', 'foo.controller:handle');
|
$symfony_request->attributes->set('_controller', 'foo.controller:handle');
|
||||||
|
|
||||||
$this->assertEquals($resolver->getController($symfony_request), array(new foo\controller, 'handle'));
|
$this->assertEquals($resolver->getController($symfony_request), array(new foo\controller, 'handle'));
|
||||||
|
$this->assertEquals(array('foo'), $resolver->getArguments($symfony_request, $resolver->getController($symfony_request)));
|
||||||
|
|
||||||
$symfony_request = new Request();
|
$symfony_request = new Request();
|
||||||
$symfony_request->attributes->set('_controller', 'core_foo.controller:bar');
|
$symfony_request->attributes->set('_controller', 'core_foo.controller:bar');
|
||||||
|
|
||||||
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb\controller\foo, 'bar'));
|
$this->assertEquals($resolver->getController($symfony_request), array(new phpbb\controller\foo, 'bar'));
|
||||||
|
$this->assertEquals(array(), $resolver->getArguments($symfony_request, $resolver->getController($symfony_request)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function data_get_arguments()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(array(new foo\controller(), 'handle2'), array('foo', 0)),
|
||||||
|
array(array(new foo\controller(), 'handle_fail'), array('default'), array('no_default' => 'default')),
|
||||||
|
array(new foo\controller(), array(), array()),
|
||||||
|
array(array(new foo\controller(), 'handle_fail'), array(), array(), '\phpbb\controller\exception', 'CONTROLLER_ARGUMENT_VALUE_MISSING'),
|
||||||
|
array('', array(), array(), '\ReflectionException', 'Function () does not exist'),
|
||||||
|
array(new phpbb\controller\foo, array(), array(), '\ReflectionException', 'Method __invoke does not exist'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider data_get_arguments
|
||||||
|
*/
|
||||||
|
public function test_get_arguments($input, $expected, $set_attributes = array(), $exception = '', $exception_message = '')
|
||||||
|
{
|
||||||
|
$container = $this->get_foo_container();
|
||||||
|
|
||||||
|
$resolver = new \phpbb\controller\resolver($container, dirname(__FILE__) . '/');
|
||||||
|
$symfony_request = new Request();
|
||||||
|
|
||||||
|
foreach ($set_attributes as $name => $value)
|
||||||
|
{
|
||||||
|
$symfony_request->attributes->set($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($exception))
|
||||||
|
{
|
||||||
|
$this->setExpectedException($exception, $exception_message);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals($expected, $resolver->getArguments($symfony_request, $input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,23 @@ class controller
|
||||||
*
|
*
|
||||||
* @return null
|
* @return null
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle($optional = 'foo')
|
||||||
{
|
{
|
||||||
return new Response('Test', 200);
|
return new Response('Test', 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handle2($foo = 'foo', $very_optional = 0)
|
||||||
|
{
|
||||||
|
return new Response('Test2', 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle_fail($no_default)
|
||||||
|
{
|
||||||
|
return new Response('Test_fail', 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke()
|
||||||
|
{
|
||||||
|
$this->handle();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue