Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.94% |
55 / 64 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_controller_controller_test | |
85.94% |
55 / 64 |
|
83.33% |
5 / 6 |
10.28 | |
0.00% |
0 / 1 |
| setUp | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| test_router_default_loader | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| get_foo_container | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| test_controller_resolver | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| data_get_arguments | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| test_get_arguments | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | include_once(__DIR__ . '/ext/vendor2/foo/controller.php'); |
| 15 | include_once(__DIR__.'/phpbb/controller/foo.php'); |
| 16 | |
| 17 | use Symfony\Component\HttpFoundation\Request; |
| 18 | use Symfony\Component\Config\FileLocator; |
| 19 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
| 21 | |
| 22 | class phpbb_controller_controller_test extends phpbb_test_case |
| 23 | { |
| 24 | /** @var phpbb_mock_extension_manager */ |
| 25 | protected $extension_manager; |
| 26 | |
| 27 | protected function setUp(): void |
| 28 | { |
| 29 | $this->extension_manager = new phpbb_mock_extension_manager( |
| 30 | __DIR__ . '/', |
| 31 | array( |
| 32 | 'vendor2/foo' => array( |
| 33 | 'ext_name' => 'vendor2/foo', |
| 34 | 'ext_active' => '1', |
| 35 | 'ext_path' => 'ext/vendor2/foo/', |
| 36 | ), |
| 37 | 'vendor2/bar' => array( |
| 38 | 'ext_name' => 'vendor2/bar', |
| 39 | 'ext_active' => '1', |
| 40 | 'ext_path' => 'ext/vendor2/bar/', |
| 41 | ), |
| 42 | )); |
| 43 | } |
| 44 | |
| 45 | public function test_router_default_loader() |
| 46 | { |
| 47 | $container = new phpbb_mock_container_builder(); |
| 48 | $container->setParameter('core.environment', PHPBB_ENVIRONMENT); |
| 49 | |
| 50 | $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader( |
| 51 | new \phpbb\routing\file_locator(__DIR__ . '/') |
| 52 | ); |
| 53 | $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(__DIR__ . '/', PHPBB_ENVIRONMENT, $this->extension_manager); |
| 54 | $router = new phpbb_mock_router($container, $resources_locator, $loader, 'php', __DIR__ . '/', true, true); |
| 55 | $routes = $router->get_routes(); |
| 56 | |
| 57 | // This will need to be updated if any new routes are defined |
| 58 | $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('core_controller')); |
| 59 | $this->assertEquals('/core_foo', $routes->get('core_controller')->getPath()); |
| 60 | |
| 61 | $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller1')); |
| 62 | $this->assertEquals('/foo', $routes->get('controller1')->getPath()); |
| 63 | |
| 64 | $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller2')); |
| 65 | $this->assertEquals('/foo/bar', $routes->get('controller2')->getPath()); |
| 66 | |
| 67 | $this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('controller3')); |
| 68 | $this->assertEquals('/bar', $routes->get('controller3')->getPath()); |
| 69 | |
| 70 | $this->assertNull($routes->get('controller_noroute')); |
| 71 | } |
| 72 | |
| 73 | protected function get_foo_container() |
| 74 | { |
| 75 | $container = new ContainerBuilder(); |
| 76 | // YamlFileLoader only uses one path at a time, so we need to loop |
| 77 | // through all of the ones we are using. |
| 78 | foreach (array(__DIR__.'/config', __DIR__ . '/ext/vendor2/foo/config') as $path) |
| 79 | { |
| 80 | $loader = new YamlFileLoader($container, new FileLocator($path)); |
| 81 | $loader->load('services.yml'); |
| 82 | } |
| 83 | |
| 84 | return $container; |
| 85 | } |
| 86 | |
| 87 | public function test_controller_resolver() |
| 88 | { |
| 89 | $container = $this->get_foo_container(); |
| 90 | |
| 91 | $resolver = new \phpbb\controller\resolver($container, __DIR__ . '/'); |
| 92 | $symfony_request = new Request(); |
| 93 | $symfony_request->attributes->set('_controller', 'foo.controller:handle'); |
| 94 | |
| 95 | $this->assertEquals($resolver->getController($symfony_request), array(new foo\controller, 'handle')); |
| 96 | $this->assertEquals(array('foo'), $resolver->getArguments($symfony_request, $resolver->getController($symfony_request))); |
| 97 | |
| 98 | $symfony_request = new Request(); |
| 99 | $symfony_request->attributes->set('_controller', 'core_foo.controller:bar'); |
| 100 | |
| 101 | $this->assertEquals($resolver->getController($symfony_request), array(new phpbb\controller\foo, 'bar')); |
| 102 | $this->assertEquals(array(), $resolver->getArguments($symfony_request, $resolver->getController($symfony_request))); |
| 103 | } |
| 104 | |
| 105 | public static function data_get_arguments() |
| 106 | { |
| 107 | return array( |
| 108 | array(array(new foo\controller(), 'handle2'), array('foo', 0)), |
| 109 | array(array(new foo\controller(), 'handle_fail'), array('default'), array('no_default' => 'default')), |
| 110 | array(new foo\controller(), array(), array()), |
| 111 | array(array(new foo\controller(), 'handle_fail'), array(), array(), '\phpbb\controller\exception', 'CONTROLLER_ARGUMENT_VALUE_MISSING'), |
| 112 | array('', array(), array(), '\ReflectionException', 'Function () does not exist'), |
| 113 | // Before PHP 8: 'Method __invoke does not exist' |
| 114 | // As of PHP 8: 'Method phpbb\controller\foo::__invoke() does not exist' |
| 115 | array(new phpbb\controller\foo, array(), array(), '\ReflectionException', |
| 116 | 'Method ' . (version_compare(PHP_VERSION, '8', '>=') ? 'phpbb\controller\foo::__invoke()' : '__invoke') . ' does not exist'), |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @dataProvider data_get_arguments |
| 122 | */ |
| 123 | public function test_get_arguments($input, $expected, $set_attributes = array(), $exception = '', $exception_message = '') |
| 124 | { |
| 125 | $container = $this->get_foo_container(); |
| 126 | |
| 127 | $resolver = new \phpbb\controller\resolver($container, __DIR__ . '/'); |
| 128 | $symfony_request = new Request(); |
| 129 | |
| 130 | foreach ($set_attributes as $name => $value) |
| 131 | { |
| 132 | $symfony_request->attributes->set($name, $value); |
| 133 | } |
| 134 | |
| 135 | if (!empty($exception)) |
| 136 | { |
| 137 | $this->expectException($exception); |
| 138 | $this->expectExceptionMessage($exception_message); |
| 139 | } |
| 140 | |
| 141 | $this->assertEquals($expected, $resolver->getArguments($symfony_request, $input)); |
| 142 | } |
| 143 | } |