diff options
Diffstat (limited to 'tests/controller/controller_test.php')
-rw-r--r-- | tests/controller/controller_test.php | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/tests/controller/controller_test.php b/tests/controller/controller_test.php index 62feee3fed..d921d0eade 100644 --- a/tests/controller/controller_test.php +++ b/tests/controller/controller_test.php @@ -11,7 +11,8 @@ * */ -require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +include_once(__DIR__ . '/ext/vendor2/foo/controller.php'); +include_once(__DIR__.'/phpbb/controller/foo.php'); use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Config\FileLocator; @@ -30,14 +31,25 @@ class phpbb_controller_controller_test extends phpbb_test_case 'ext_active' => '1', 'ext_path' => 'ext/vendor2/foo/', ), + 'vendor2/bar' => array( + 'ext_name' => 'vendor2/bar', + 'ext_active' => '1', + 'ext_path' => 'ext/vendor2/bar/', + ), )); } - public function test_provider() + public function test_router_default_loader() { - $provider = new \phpbb\controller\provider(); - $provider->find_routing_files($this->extension_manager->get_finder()); - $routes = $provider->find(__DIR__)->get_routes(); + $container = new phpbb_mock_container_builder(); + $container->setParameter('core.environment', PHPBB_ENVIRONMENT); + + $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader( + new \phpbb\routing\file_locator(new \phpbb\filesystem\filesystem(), dirname(__FILE__) . '/') + ); + $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(dirname(__FILE__) . '/', PHPBB_ENVIRONMENT, $this->extension_manager); + $router = new phpbb_mock_router($container, $resources_locator, $loader, dirname(__FILE__) . '/', 'php'); + $routes = $router->get_routes(); // This will need to be updated if any new routes are defined $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('core_controller')); @@ -49,10 +61,13 @@ class phpbb_controller_controller_test extends phpbb_test_case $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller2')); $this->assertEquals('/foo/bar', $routes->get('controller2')->getPath()); + $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller3')); + $this->assertEquals('/bar', $routes->get('controller3')->getPath()); + $this->assertNull($routes->get('controller_noroute')); } - public function test_controller_resolver() + protected function get_foo_container() { $container = new ContainerBuilder(); // YamlFileLoader only uses one path at a time, so we need to loop @@ -63,26 +78,59 @@ class phpbb_controller_controller_test extends phpbb_test_case $loader->load('services.yml'); } - // Autoloading classes within the tests folder does not work - // so I'll include them manually. - if (!class_exists('vendor2\\foo\\controller')) - { - include(__DIR__ . '/ext/vendor2/foo/controller.php'); - } - if (!class_exists('phpbb\\controller\\foo')) - { - include(__DIR__.'/phpbb/controller/foo.php'); - } + return $container; + } - $resolver = new \phpbb\controller\resolver(new \phpbb\user('\phpbb\datetime'), $container, dirname(__FILE__) . '/'); + public function test_controller_resolver() + { + $container = $this->get_foo_container(); + + $resolver = new \phpbb\controller\resolver($container, dirname(__FILE__) . '/'); $symfony_request = new Request(); $symfony_request->attributes->set('_controller', '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->attributes->set('_controller', 'core_foo.controller: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)); } } |