Files
yii2/tests/framework/web/GroupUrlRuleTest.php
Robert Korulczyk ba0ab403b5 Added php-cs-fixer coding standards validation to Travis CI (#14100)
* php-cs-fixer: PSR2 rule.

* php-cs-fixer: PSR2 rule - fix views.

* Travis setup refactoring.

* Add php-cs-fixer to travis cs tests.

* Fix tests on hhvm-3.12

* improve travis config

* composer update

* revert composer update

* improve travis config

* Fix CS.

* Extract config to separate classes.

* Extract config to separate classes.

* Add file header.

* Force short array syntax.

* binary_operator_spaces fixer

* Fix broken tests

* cast_spaces fixer

* concat_space fixer

* dir_constant fixer

* ereg_to_preg fixer

* function_typehint_space fixer

* hash_to_slash_comment fixer

* is_null fixer

* linebreak_after_opening_tag fixer

* lowercase_cast fixer

* magic_constant_casing fixer

* modernize_types_casting fixer

* native_function_casing fixer

* new_with_braces fixer

* no_alias_functions fixer

* no_blank_lines_after_class_opening fixer

* no_blank_lines_after_phpdoc fixer

* no_empty_comment fixer

* no_empty_phpdoc fixer

* no_empty_statement fixer

* no_extra_consecutive_blank_lines fixer

* no_leading_import_slash fixer

* no_leading_namespace_whitespace fixer

* no_mixed_echo_print fixer

* no_multiline_whitespace_around_double_arrow fixer

* no_multiline_whitespace_before_semicolons fixer

* no_php4_constructor fixer

* no_short_bool_cast fixer

* no_singleline_whitespace_before_semicolons fixer

* no_spaces_around_offset fixer

* no_trailing_comma_in_list_call fixer

* no_trailing_comma_in_singleline_array fixer

* no_unneeded_control_parentheses fixer

* no_unused_imports fixer

* no_useless_return fixer

* no_whitespace_before_comma_in_array fixer

* no_whitespace_in_blank_line fixer

* not_operator_with_successor_space fixer

* object_operator_without_whitespace fixer

* ordered_imports fixer

* php_unit_construct fixer

* php_unit_dedicate_assert fixer

* php_unit_fqcn_annotation fixer

* phpdoc_indent fixer

* phpdoc_no_access fixer

* phpdoc_no_empty_return fixer

* phpdoc_no_package fixer

* phpdoc_no_useless_inheritdoc fixer

* Fix broken tests

* phpdoc_return_self_reference fixer

* phpdoc_single_line_var_spacing fixer

* phpdoc_single_line_var_spacing fixer

* phpdoc_to_comment fixer

* phpdoc_trim fixer

* phpdoc_var_without_name fixer

* psr4 fixer

* self_accessor fixer

* short_scalar_cast fixer

* single_blank_line_before_namespace fixer

* single_quote fixer

* standardize_not_equals fixer

* ternary_operator_spaces fixer

* trailing_comma_in_multiline_array fixer

* trim_array_spaces fixer

* protected_to_private fixer

* unary_operator_spaces fixer

* whitespace_after_comma_in_array fixer

* `parent::setRules()` -> `$this->setRules()`

* blank_line_after_opening_tag fixer

* Update finder config.

* Revert changes for YiiRequirementChecker.

* Fix array formatting.

* Add missing import.

* Fix CS for new code merged from master.

* Fix some indentation issues.
2017-06-12 12:25:45 +03:00

254 lines
8.6 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\web;
use yii\web\GroupUrlRule;
use yii\web\Request;
use yii\web\UrlManager;
use yii\web\UrlRule;
use yiiunit\TestCase;
/**
* @group web
*/
class GroupUrlRuleTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function testCreateUrl()
{
$manager = new UrlManager(['cache' => null]);
$suites = $this->getTestsForCreateUrl();
foreach ($suites as $i => $suite) {
list($name, $config, $tests) = $suite;
$rule = new GroupUrlRule($config);
foreach ($tests as $j => $test) {
list($route, $params, $expected, $status) = $test;
$url = $rule->createUrl($manager, $route, $params);
$this->assertEquals($expected, $url, "Test#$i-$j: $name");
$this->assertSame($status, $rule->getCreateUrlStatus(), "Test#$i-$j: $name");
}
}
}
public function testParseRequest()
{
$manager = new UrlManager(['cache' => null]);
$request = new Request(['hostInfo' => 'http://en.example.com']);
$suites = $this->getTestsForParseRequest();
foreach ($suites as $i => $suite) {
list($name, $config, $tests) = $suite;
$rule = new GroupUrlRule($config);
foreach ($tests as $j => $test) {
$request->pathInfo = $test[0];
$route = $test[1];
$params = isset($test[2]) ? $test[2] : [];
$result = $rule->parseRequest($manager, $request);
if ($route === false) {
$this->assertFalse($result, "Test#$i-$j: $name");
} else {
$this->assertEquals([$route, $params], $result, "Test#$i-$j: $name");
}
}
}
}
protected function getTestsForCreateUrl()
{
// structure of each test
// message for the test
// config for the URL rule
// list of inputs and outputs
// route
// params
// expected output
// expected getCreateUrlStatus() result
return [
[
'no prefix',
[
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['user/login', [], 'login', UrlRule::CREATE_STATUS_SUCCESS],
['user/logout', [], 'logout', UrlRule::CREATE_STATUS_SUCCESS],
['user/create', [], false, UrlRule::CREATE_STATUS_ROUTE_MISMATCH],
],
],
[
'prefix only',
[
'prefix' => 'admin',
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['admin/user/login', [], 'admin/login', UrlRule::CREATE_STATUS_SUCCESS],
['admin/user/logout', [], 'admin/logout', UrlRule::CREATE_STATUS_SUCCESS],
['user/create', [], false, UrlRule::CREATE_STATUS_ROUTE_MISMATCH],
],
],
[
'prefix and routePrefix different',
[
'prefix' => '_',
'routePrefix' => 'admin',
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['admin/user/login', [], '_/login', UrlRule::CREATE_STATUS_SUCCESS],
['admin/user/logout', [], '_/logout', UrlRule::CREATE_STATUS_SUCCESS],
['user/create', [], false, UrlRule::CREATE_STATUS_ROUTE_MISMATCH],
],
],
[
'ruleConfig with suffix',
[
'prefix' => '_',
'routePrefix' => 'admin',
'ruleConfig' => [
'suffix' => '.html',
'class' => 'yii\\web\\UrlRule',
],
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['admin/user/login', [], '_/login.html', UrlRule::CREATE_STATUS_SUCCESS],
['admin/user/logout', [], '_/logout.html', UrlRule::CREATE_STATUS_SUCCESS],
['user/create', [], false, UrlRule::CREATE_STATUS_ROUTE_MISMATCH],
],
],
[
'createStatus for failed statuses',
[
'prefix' => '_',
'routePrefix' => 'admin',
'ruleConfig' => [
'suffix' => '.html',
'class' => 'yii\web\UrlRule',
],
'rules' => [
'login' => 'user/login',
[
'pattern' => 'logout',
'route' => 'user/logout',
'mode' => UrlRule::PARSING_ONLY,
],
[
'pattern' => 'logout/<token:\w+>',
'route' => 'user/logout',
],
],
],
[
[
'admin/user/logout', [], false,
UrlRule::CREATE_STATUS_PARSING_ONLY | UrlRule::CREATE_STATUS_ROUTE_MISMATCH | UrlRule::CREATE_STATUS_PARAMS_MISMATCH,
],
],
],
];
}
protected function getTestsForParseRequest()
{
// structure of each test
// message for the test
// config for the URL rule
// list of inputs and outputs
// pathInfo
// expected route, or false if the rule doesn't apply
// expected params, or not set if empty
return [
[
'no prefix',
[
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['login', 'user/login'],
['logout', 'user/logout'],
['create', false],
],
],
[
'prefix only',
[
'prefix' => 'admin',
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['admin/login', 'admin/user/login'],
['admin/logout', 'admin/user/logout'],
['admin/create', false],
['create', false],
],
],
[
'prefix and routePrefix different',
[
'prefix' => '_',
'routePrefix' => 'admin',
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['_/login', 'admin/user/login'],
['_/logout', 'admin/user/logout'],
['_/create', false],
['create', false],
],
],
[
'ruleConfig with suffix',
[
'prefix' => '_',
'routePrefix' => 'admin',
'ruleConfig' => [
'suffix' => '.html',
'class' => 'yii\\web\\UrlRule',
],
'rules' => [
'login' => 'user/login',
'logout' => 'user/logout',
],
],
[
['_/login.html', 'admin/user/login'],
['_/logout.html', 'admin/user/logout'],
['_/logout', false],
['_/create.html', false],
],
],
];
}
}