Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

795 lines
34 KiB

Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
<?php
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.
7 years ago
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
namespace yiiunit\framework\web;
use Yii;
use yii\caching\ArrayCache;
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
use yii\web\UrlManager;
use yii\web\UrlRule;
use yiiunit\framework\web\stubs\CachedUrlRule;
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
use yiiunit\TestCase;
/**
* This class implements the tests for URL creation with "pretty" url format.
*
* See [[UrlManagerTest]] for tests with "default" URL format.
* See [[UrlManagerParseUrlTest]] for url parsing with "pretty" URL format.
*
* Behavior of UrlManager::createUrl() for the "pretty" URL format varies among the following options:
* - show script name = true / false
* - rules format
* - key => value
* - array config
*
* The following features are tested:
* - route only Url::to(['post/index']);
* - with params Url::to(['post/view', 'id' => 100]);
* - with anchor Url::to(['post/view', 'id' => 100, '#' => 'content']);
* - named parameters
* - as query params
* - as controller/actions '<controller:(post|comment)>/<id:\d+>/<action:(update|delete)>' => '<controller>/<action>',
* - Rules with Server Names
* - with protocol (TODO)
* - without protocol i.e protocol relative, see https://github.com/yiisoft/yii2/pull/12697 (TODO)
* - with parameters
* - with suffix
* - with default values
* - with HTTP methods (TODO)
* - absolute/relative
*
* - Adding rules dynamically (TODO)
* - Test custom rules that only implement the interface (TODO)
*
* NOTE: if a test is added here, you probably also need to add one in UrlManagerParseUrlTest.
*
* @group web
*/
class UrlManagerCreateUrlTest extends TestCase
{
protected function getUrlManager($config = [], $showScriptName = true)
{
// in this test class, all tests have enablePrettyUrl enabled.
$config['enablePrettyUrl'] = true;
// set default values if they are not set
$config = array_merge([
'baseUrl' => '',
'scriptUrl' => '/index.php',
'hostInfo' => 'http://www.example.com',
'cache' => null,
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
'showScriptName' => $showScriptName,
], $config);
return new UrlManager($config);
}
public function variationsProvider()
{
$baseUrlConfig = [
'baseUrl' => '/test',
'scriptUrl' => '/test/index.php',
];
return [
// method name, $showScriptName, expected URL prefix
['createUrl', true, '/index.php', []],
['createUrl', false, '', []],
['createAbsoluteUrl', true, 'http://www.example.com/index.php', []],
['createAbsoluteUrl', false, 'http://www.example.com', []],
// with different baseUrl
['createUrl', true, '/test/index.php', $baseUrlConfig],
['createUrl', false, '/test', $baseUrlConfig],
['createAbsoluteUrl', true, 'http://www.example.com/test/index.php', $baseUrlConfig],
['createAbsoluteUrl', false, 'http://www.example.com/test', $baseUrlConfig],
];
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* without rules.
*
* @dataProvider variationsProvider
*/
public function testWithoutRules($method, $showScriptName, $prefix, $config)
{
$manager = $this->getUrlManager($config, $showScriptName);
$url = $manager->$method('post/view');
$this->assertEquals("$prefix/post/view", $url);
$url = $manager->$method(['post/view']);
$this->assertEquals("$prefix/post/view", $url);
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/post/view?id=1&title=sample+post", $url);
$url = $manager->$method(['post/view', '#' => 'testhash']);
$this->assertEquals("$prefix/post/view#testhash", $url);
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post', '#' => 'testhash']);
$this->assertEquals("$prefix/post/view?id=1&title=sample+post#testhash", $url);
// with defaultAction
$url = $manager->$method(['/post', 'page' => 1]);
$this->assertEquals("$prefix/post?page=1", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* without rules.
* With UrlManager::$suffix
*
* @dataProvider variationsProvider
*/
public function testWithoutRulesWithSuffix($method, $showScriptName, $prefix, $config)
{
$config['suffix'] = '.html';
$manager = $this->getUrlManager($config, $showScriptName);
$url = $manager->$method('post/view');
$this->assertEquals("$prefix/post/view.html", $url);
$url = $manager->$method(['post/view']);
$this->assertEquals("$prefix/post/view.html", $url);
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/post/view.html?id=1&title=sample+post", $url);
$url = $manager->$method(['post/view', '#' => 'testhash']);
$this->assertEquals("$prefix/post/view.html#testhash", $url);
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post', '#' => 'testhash']);
$this->assertEquals("$prefix/post/view.html?id=1&title=sample+post#testhash", $url);
// with defaultAction
$url = $manager->$method(['/post', 'page' => 1]);
$this->assertEquals("$prefix/post.html?page=1", $url);
// test suffix '/' as it may be trimmed
$config['suffix'] = '/';
$manager = $this->getUrlManager($config, $showScriptName);
$url = $manager->$method('post/view');
$this->assertEquals("$prefix/post/view/", $url);
$url = $manager->$method(['post/view']);
$this->assertEquals("$prefix/post/view/", $url);
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/post/view/?id=1&title=sample+post", $url);
$url = $manager->$method(['post/view', '#' => 'testhash']);
$this->assertEquals("$prefix/post/view/#testhash", $url);
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post', '#' => 'testhash']);
$this->assertEquals("$prefix/post/view/?id=1&title=sample+post#testhash", $url);
// with defaultAction
$url = $manager->$method(['/post', 'page' => 1]);
$this->assertEquals("$prefix/post/?page=1", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* with simple rules.
*
* @dataProvider variationsProvider
*/
public function testSimpleRules($method, $showScriptName, $prefix, $config)
{
$config['rules'] = [
'post/<id:\d+>' => 'post/view',
'posts' => 'post/index',
'book/<id:\d+>/<title>' => 'book/view',
];
$manager = $this->getUrlManager($config, $showScriptName);
// does not match any rule
$url = $manager->$method(['post/view']);
$this->assertEquals("$prefix/post/view", $url);
// with defaultAction also does not match any rule
$url = $manager->$method(['/post', 'page' => 1]);
$this->assertEquals("$prefix/post?page=1", $url);
// match first rule
$url = $manager->$method(['post/view', 'id' => 1]);
$this->assertEquals("$prefix/post/1", $url);
// match first rule with additional param
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/post/1?title=sample+post", $url);
// match first rule with hash
$url = $manager->$method(['post/view', 'id' => 1, '#' => 'testhash']);
$this->assertEquals("$prefix/post/1#testhash", $url);
// match second rule
$url = $manager->$method(['post/index']);
$this->assertEquals("$prefix/posts", $url);
// match second rule with additional param
$url = $manager->$method(['post/index', 'category' => 'test']);
$this->assertEquals("$prefix/posts?category=test", $url);
// match third rule, ensure encoding of params
$url = $manager->$method(['book/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/book/1/sample+post", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* with simple rules.
* With UrlManager::$suffix
*
* @dataProvider variationsProvider
*/
public function testSimpleRulesWithSuffix($method, $showScriptName, $prefix, $config)
{
$config['rules'] = [
'post/<id:\d+>' => 'post/view',
'posts' => 'post/index',
'book/<id:\d+>/<title>' => 'book/view',
];
$config['suffix'] = '/';
$manager = $this->getUrlManager($config, $showScriptName);
// does not match any rule
$url = $manager->$method(['post/view']);
$this->assertEquals("$prefix/post/view/", $url);
// with defaultAction also does not match any rule
$url = $manager->$method(['/post', 'page' => 1]);
$this->assertEquals("$prefix/post/?page=1", $url);
// match first rule
$url = $manager->$method(['post/view', 'id' => 1]);
$this->assertEquals("$prefix/post/1/", $url);
// match first rule with additional param
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/post/1/?title=sample+post", $url);
// match first rule with hash
$url = $manager->$method(['post/view', 'id' => 1, '#' => 'testhash']);
$this->assertEquals("$prefix/post/1/#testhash", $url);
// match second rule
$url = $manager->$method(['post/index']);
$this->assertEquals("$prefix/posts/", $url);
// match second rule with additional param
$url = $manager->$method(['post/index', 'category' => 'test']);
$this->assertEquals("$prefix/posts/?category=test", $url);
// match third rule, ensure encoding of params
$url = $manager->$method(['book/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/book/1/sample+post/", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* with rules that have varadic controller/actions.
*
* @dataProvider variationsProvider
*/
public function testControllerActionParams($method, $showScriptName, $prefix, $config)
{
$config['rules'] = [
'<controller>/<id:\d+>' => '<controller>/view',
'<controller>s' => '<controller>/index',
'<controller>/default' => '<controller>', // rule to match default action
'<controller>/test/<action:\w+>' => '<controller>/<action>',
];
$manager = $this->getUrlManager($config, $showScriptName);
// match last rule
$url = $manager->$method(['post/view']);
$this->assertEquals("$prefix/post/test/view", $url);
// defaultAction should match third rule
$url = $manager->$method(['/post/']);
$this->assertEquals("$prefix/post/default", $url);
$url = $manager->$method(['/post']);
$this->assertEquals("$prefix/post/default", $url);
// match first rule
$url = $manager->$method(['post/view', 'id' => 1]);
$this->assertEquals("$prefix/post/1", $url);
// match first rule with additional param
$url = $manager->$method(['post/view', 'id' => 1, 'title' => 'sample post']);
$this->assertEquals("$prefix/post/1?title=sample+post", $url);
// match first rule with hash
$url = $manager->$method(['post/view', 'id' => 1, '#' => 'testhash']);
$this->assertEquals("$prefix/post/1#testhash", $url);
// match second rule
$url = $manager->$method(['post/index']);
$this->assertEquals("$prefix/posts", $url);
// match second rule with additional param
$url = $manager->$method(['post/index', 'category' => 'test']);
$this->assertEquals("$prefix/posts?category=test", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* with rules that have default values for parameters.
*
* @dataProvider variationsProvider
*/
public function testRulesWithDefaultParams($method, $showScriptName, $prefix, $config)
{
$config['rules'] = [
[
'pattern' => '',
'route' => 'frontend/page/view',
'defaults' => ['slug' => 'index'],
],
'page/<slug>' => 'frontend/page/view',
[
'pattern' => '<language>',
'route' => 'site/index',
'defaults' => [
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.
7 years ago
'language' => 'en',
],
],
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
];
$manager = $this->getUrlManager($config, $showScriptName);
// match first rule
$url = $manager->$method(['frontend/page/view', 'slug' => 'index']);
$this->assertEquals("$prefix/", $url);
// match first rule with additional param
$url = $manager->$method(['frontend/page/view', 'slug' => 'index', 'sort' => 'name']);
$this->assertEquals("$prefix/?sort=name", $url);
// match first rule with hash
$url = $manager->$method(['frontend/page/view', 'slug' => 'index', '#' => 'testhash']);
$this->assertEquals("$prefix/#testhash", $url);
// match second rule
$url = $manager->$method(['frontend/page/view', 'slug' => 'services']);
$this->assertEquals("$prefix/page/services", $url);
// match second rule with additional param
$url = $manager->$method(['frontend/page/view', 'slug' => 'services', 'sort' => 'name']);
$this->assertEquals("$prefix/page/services?sort=name", $url);
// match second rule with hash
$url = $manager->$method(['frontend/page/view', 'slug' => 'services', '#' => 'testhash']);
$this->assertEquals("$prefix/page/services#testhash", $url);
// match third rule
$url = $manager->$method(['site/index', 'language' => 'en']);
$this->assertEquals("$prefix/", $url);
$url = $manager->$method(['site/index', 'language' => 'de']);
$this->assertEquals("$prefix/de", $url);
// match third rule with additional param
$url = $manager->$method(['site/index', 'language' => 'en', 'sort' => 'name']);
$this->assertEquals("$prefix/?sort=name", $url);
$url = $manager->$method(['site/index', 'language' => 'de', 'sort' => 'name']);
$this->assertEquals("$prefix/de?sort=name", $url);
// match first rule with hash
$url = $manager->$method(['site/index', 'language' => 'en', '#' => 'testhash']);
$this->assertEquals("$prefix/#testhash", $url);
$url = $manager->$method(['site/index', 'language' => 'de', '#' => 'testhash']);
$this->assertEquals("$prefix/de#testhash", $url);
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
// matches none of the rules
$url = $manager->$method(['frontend/page/view']);
$this->assertEquals("$prefix/frontend/page/view", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* with empty or null parameters.
*
* @dataProvider variationsProvider
* @see https://github.com/yiisoft/yii2/issues/10935
*/
public function testWithNullParams($method, $showScriptName, $prefix, $config)
{
$config['rules'] = [
'<param1>/<param2>' => 'site/index',
'<param1>' => 'site/index',
];
$manager = $this->getUrlManager($config, $showScriptName);
// match first rule
$url = $manager->$method(['site/index', 'param1' => 111, 'param2' => 222]);
$this->assertEquals("$prefix/111/222", $url);
$url = $manager->$method(['site/index', 'param1' => 112, 'param2' => 222]);
$this->assertEquals("$prefix/112/222", $url);
// match second rule
$url = $manager->$method(['site/index', 'param1' => 111, 'param2' => null]);
$this->assertEquals("$prefix/111", $url);
$url = $manager->$method(['site/index', 'param1' => 123, 'param2' => null]);
$this->assertEquals("$prefix/123", $url);
// match none of the rules
$url = $manager->$method(['site/index', 'param1' => null, 'param2' => 111]);
$this->assertEquals("$prefix/site/index?param2=111", $url);
$url = $manager->$method(['site/index', 'param1' => null, 'param2' => 123]);
$this->assertEquals("$prefix/site/index?param2=123", $url);
}
/**
* Test createUrl() and createAbsoluteUrl()
* with varying $showScriptName
* with empty pattern.
*
* @dataProvider variationsProvider
* @see https://github.com/yiisoft/yii2/issues/6717
*/
public function testWithEmptyPattern($method, $showScriptName, $prefix, $config)
{
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.
7 years ago
$assertations = function ($manager) use ($method, $prefix) {
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
// match first rule
$url = $manager->$method(['front/site/index']);
$this->assertEquals("$prefix/", $url);
$url = $manager->$method(['/front/site/index']);
$this->assertEquals("$prefix/", $url);
// match first rule with additional parameter
$url = $manager->$method(['front/site/index', 'page' => 1]);
$this->assertEquals("$prefix/?page=1", $url);
$url = $manager->$method(['/front/site/index', 'page' => 1]);
$this->assertEquals("$prefix/?page=1", $url);
// no match
$url = $manager->$method(['']);
$this->assertEquals("$prefix/", $url);
$url = $manager->$method(['/']);
$this->assertEquals("$prefix/", $url);
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
};
// normal rule
$config['rules'] = [
'' => 'front/site/index',
];
$manager = $this->getUrlManager($config, $showScriptName);
$assertations($manager);
// rule prefixed with /
$config['rules'] = [
'' => '/front/site/index',
];
$manager = $this->getUrlManager($config, $showScriptName);
$assertations($manager);
// with suffix
$config['rules'] = [
'' => 'front/site/index',
];
$config['suffix'] = '/';
$manager = $this->getUrlManager($config, $showScriptName);
$assertations($manager);
}
public function absolutePatternsVariations()
{
$baseUrlConfig = [
'baseUrl' => '/test',
'scriptUrl' => '/test/index.php',
];
return [
// $showScriptName, expected URL prefix
[true, '/index.php', []],
[false, '', []],
// with different baseUrl
[true, '/test/index.php', $baseUrlConfig],
[false, '/test', $baseUrlConfig],
];
}
/**
* Test rules that have host info in the patterns.
* @dataProvider absolutePatternsVariations
*/
public function testAbsolutePatterns($showScriptName, $prefix, $config)
{
$config['rules'] = [
[
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
'host' => 'http://<lang:en|fr>.example.com',
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
],
// note: baseUrl is not included in the pattern
'http://www.example.com/login' => 'site/login',
];
$manager = $this->getUrlManager($config, $showScriptName);
// first rule matches
$urlParams = ['post/view', 'id' => 1, 'title' => 'sample post', 'lang' => 'en'];
$expected = "http://en.example.com$prefix/post/1/sample+post";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals('https' . substr($expected, 4), $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals(substr($expected, 5), $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
$urlParams = ['post/view', 'id' => 1, 'title' => 'sample post', 'lang' => 'en', '#' => 'testhash'];
$expected = "http://en.example.com$prefix/post/1/sample+post#testhash";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams));
// second rule matches
$urlParams = ['site/login'];
$expected = "http://www.example.com$prefix/login";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals('https' . substr($expected, 4), $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals(substr($expected, 5), $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
// none of the rules matches
$urlParams = ['post/index', 'page' => 1];
$this->assertEquals("$prefix/post/index?page=1", $manager->createUrl($urlParams));
$expected = "http://www.example.com$prefix/post/index?page=1";
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals('https' . substr($expected, 4), $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals(substr($expected, 5), $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
$urlParams = ['post/index', 'page' => 1, '#' => 'testhash'];
$this->assertEquals("$prefix/post/index?page=1#testhash", $manager->createUrl($urlParams));
$expected = "http://www.example.com$prefix/post/index?page=1#testhash";
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams));
}
/**
* Test rules that have host info in the patterns, that are protocol relative.
* @dataProvider absolutePatternsVariations
* @see https://github.com/yiisoft/yii2/issues/12691
*/
public function testProtocolRelativeAbsolutePattern($showScriptName, $prefix, $config)
{
$config['rules'] = [
[
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
'host' => '//<lang:en|fr>.example.com',
],
// note: baseUrl is not included in the pattern
'//www.example.com/login' => 'site/login',
'//app.example.com' => 'app/index',
'//app2.example.com/' => 'app2/index',
];
$manager = $this->getUrlManager($config, $showScriptName);
// first rule matches
$urlParams = ['post/view', 'id' => 1, 'title' => 'sample post', 'lang' => 'en'];
$expected = "//en.example.com$prefix/post/1/sample+post";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals("https:$expected", $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
$urlParams = ['post/view', 'id' => 1, 'title' => 'sample post', 'lang' => 'en', '#' => 'testhash'];
$expected = "//en.example.com$prefix/post/1/sample+post#testhash";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams));
// second rule matches
$urlParams = ['site/login'];
$expected = "//www.example.com$prefix/login";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals("https:$expected", $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
// third rule matches
$urlParams = ['app/index'];
$expected = "//app.example.com$prefix";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals("https:$expected", $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
// fourth rule matches
$urlParams = ['app2/index'];
$expected = "//app2.example.com$prefix";
$this->assertEquals($expected, $manager->createUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals("https:$expected", $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
// none of the rules matches
$urlParams = ['post/index', 'page' => 1];
$this->assertEquals("$prefix/post/index?page=1", $manager->createUrl($urlParams));
$expected = "//www.example.com$prefix/post/index?page=1";
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, true));
$this->assertEquals("http:$expected", $manager->createAbsoluteUrl($urlParams, 'http'));
$this->assertEquals("https:$expected", $manager->createAbsoluteUrl($urlParams, 'https'));
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams, '')); // protocol relative Url
$urlParams = ['post/index', 'page' => 1, '#' => 'testhash'];
$this->assertEquals("$prefix/post/index?page=1#testhash", $manager->createUrl($urlParams));
$expected = "http://www.example.com$prefix/post/index?page=1#testhash";
$this->assertEquals($expected, $manager->createAbsoluteUrl($urlParams));
}
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
public function multipleHostsRulesDataProvider()
{
return [
['http://example.com'],
['https://example.com'],
['http://example.fr'],
['https://example.fr'],
];
}
/**
* Test matching of Url rules dependent on the current host info
*
* @dataProvider multipleHostsRulesDataProvider
* @see https://github.com/yiisoft/yii2/issues/7948
*/
public function testMultipleHostsRules($host)
{
$manager = new UrlManager([
'enablePrettyUrl' => true,
'cache' => null,
'rules' => [
['host' => 'http://example.com', 'pattern' => '<slug:(search)>', 'route' => 'products/search', 'defaults' => ['lang' => 'en']],
['host' => 'http://example.fr', 'pattern' => '<slug:(search)>', 'route' => 'products/search', 'defaults' => ['lang' => 'fr']],
],
'hostInfo' => $host,
'baseUrl' => '/',
'scriptUrl' => '',
]);
$url = $manager->createAbsoluteUrl(['products/search', 'lang' => 'en', 'slug' => 'search'], 'https');
$this->assertEquals('https://example.com/search', $url);
$url = $manager->createUrl(['products/search', 'lang' => 'en', 'slug' => 'search']);
$this->assertEquals('http://example.com/search', $url);
$url = $manager->createUrl(['products/search', 'lang' => 'en', 'slug' => 'search', 'param1' => 'value1']);
$this->assertEquals('http://example.com/search?param1=value1', $url);
$url = $manager->createAbsoluteUrl(['products/search', 'lang' => 'fr', 'slug' => 'search'], 'https');
$this->assertEquals('https://example.fr/search', $url);
$url = $manager->createUrl(['products/search', 'lang' => 'fr', 'slug' => 'search']);
$this->assertEquals('http://example.fr/search', $url);
$url = $manager->createUrl(['products/search', 'lang' => 'fr', 'slug' => 'search', 'param1' => 'value1']);
$this->assertEquals('http://example.fr/search?param1=value1', $url);
}
public function testCreateUrlCache()
{
/* @var $rules CachedUrlRule[] */
$rules = [
Yii::createObject([
'class' => CachedUrlRule::className(),
'route' => 'user/show',
'pattern' => 'user/<name:[\w-]+>',
]),
Yii::createObject([
'class' => CachedUrlRule::className(),
'route' => '<controller>/<action>',
'pattern' => '<controller:\w+>/<action:\w+>',
]),
];
$manager = $this->getUrlManager([
'rules' => $rules,
], false);
$this->assertEquals('/user/rob006', $manager->createUrl(['user/show', 'name' => 'rob006']));
$this->assertEquals(UrlRule::CREATE_STATUS_SUCCESS, $rules[0]->getCreateUrlStatus());
$this->assertEquals(1, $rules[0]->createCounter);
$this->assertEquals(0, $rules[1]->createCounter);
$this->assertEquals('/user/show?name=John+Doe', $manager->createUrl(['user/show', 'name' => 'John Doe']));
$this->assertEquals(UrlRule::CREATE_STATUS_PARAMS_MISMATCH, $rules[0]->getCreateUrlStatus());
$this->assertEquals(UrlRule::CREATE_STATUS_SUCCESS, $rules[1]->getCreateUrlStatus());
$this->assertEquals(2, $rules[0]->createCounter);
$this->assertEquals(1, $rules[1]->createCounter);
$this->assertEquals('/user/profile?name=rob006', $manager->createUrl(['user/profile', 'name' => 'rob006']));
$this->assertEquals(UrlRule::CREATE_STATUS_ROUTE_MISMATCH, $rules[0]->getCreateUrlStatus());
$this->assertEquals(UrlRule::CREATE_STATUS_SUCCESS, $rules[1]->getCreateUrlStatus());
$this->assertEquals(3, $rules[0]->createCounter);
$this->assertEquals(2, $rules[1]->createCounter);
$this->assertEquals('/user/profile?name=John+Doe', $manager->createUrl(['user/profile', 'name' => 'John Doe']));
$this->assertEquals(UrlRule::CREATE_STATUS_ROUTE_MISMATCH, $rules[0]->getCreateUrlStatus());
$this->assertEquals(UrlRule::CREATE_STATUS_SUCCESS, $rules[1]->getCreateUrlStatus());
// fist rule is skipped - cached rule has precedence
$this->assertEquals(3, $rules[0]->createCounter);
$this->assertEquals(3, $rules[1]->createCounter);
}
/**
* @see https://github.com/yiisoft/yii2/pull/1335
*/
public function testUrlCreateCacheWithParameterMismatch()
{
/* @var $rules CachedUrlRule[] */
$rules = [
Yii::createObject([
'class' => CachedUrlRule::className(),
'route' => 'user/show',
'pattern' => 'user/<name:[\w-]+>',
]),
Yii::createObject([
'class' => CachedUrlRule::className(),
'route' => '<controller>/<action>',
'pattern' => '<controller:\w+>/<action:\w+>',
]),
];
$manager = $this->getUrlManager([
'rules' => $rules,
], false);
$this->assertEquals('/user/show?name=John+Doe', $manager->createUrl(['user/show', 'name' => 'John Doe']));
$this->assertEquals(UrlRule::CREATE_STATUS_PARAMS_MISMATCH, $rules[0]->getCreateUrlStatus());
$this->assertEquals(UrlRule::CREATE_STATUS_SUCCESS, $rules[1]->getCreateUrlStatus());
$this->assertEquals(1, $rules[0]->createCounter);
$this->assertEquals(1, $rules[1]->createCounter);
$this->assertEquals('/user/rob006', $manager->createUrl(['user/show', 'name' => 'rob006']));
$this->assertEquals(UrlRule::CREATE_STATUS_SUCCESS, $rules[0]->getCreateUrlStatus());
$this->assertEquals(2, $rules[0]->createCounter);
$this->assertEquals(1, $rules[1]->createCounter);
}
/**
* @see https://github.com/yiisoft/yii2/issues/14406
*/
public function testCreatingRulesWithDifferentRuleConfigAndEnabledCache()
{
$this->mockWebApplication([
'components' => [
'cache' => ArrayCache::className(),
],
]);
$urlManager = $this->getUrlManager([
'cache' => 'cache',
'rules' => [
'/' => 'site/index',
],
]);
$cachedUrlManager = $this->getUrlManager([
'cache' => 'cache',
'ruleConfig' => [
'class' => CachedUrlRule::className(),
],
'rules' => [
'/' => 'site/index',
],
]);
$this->assertNotEquals($urlManager->rules, $cachedUrlManager->rules);
$this->assertInstanceOf(UrlRule::className(), $urlManager->rules[0]);
$this->assertInstanceOf(CachedUrlRule::className(), $cachedUrlManager->rules[0]);
}
Refactored UrlManagerTest to cover more use cases UrlManager is a complex class with a large bunch of options that needs to be tested and we had a few regressions in the past even though there are already a lot of tests covering it. Test coverage measured by lines does not help us determine how good it is tested, we need test coverage for every major path through the code. I have refactored the UrlManager tests to reflect the different options and cases to have a better overview of which cases are covered and which are not. UrlManager has two main operation modes: - "default" url format, which is the simple case. These are covered by methods in `UrlManagerTest`. - "pretty" url format. This is the complex case, which involves UrlRules and url parsing. I have created two separate classes for this case: Url creation for "pretty" url format is covered by `UrlManagerCreateUrlTest`. Url parsing for "pretty" url format is covered by `UrlManagerParseUrlTest`. Each of the test classes have a `getUrlManager` method that creates a UrlManager instance with a specific configuration and certain variations in options. It is also tested that options that are not relevant in a certain operation mode have no effect on the result. To make sure to not remove tests that have existed before, here is a map of where code has been moved. The following test methods existed in the [old test class](https://github.com/yiisoft/yii2/blob/4187718c148610b02fc973a19be53600e6230604/tests/framework/web/UrlManagerTest.php): - `testCreateUrl()` split between UrlManagerTest and UrlManagerCreateUrlTest variations should all be covered by `variationsProvider()`. - `testCreateUrlWithNullParams()` covered by UrlManagerCreateUrlTest by `testWithNullParams()` - `testCreateUrlWithEmptyPattern()` - `testCreateAbsoluteUrl()` covered in UrlManagerCreateUrlTest by new tests via `variationsProvider()`. - `testCreateAbsoluteUrlWithSuffix()` covered in UrlManagerCreateUrlTest by `testAbsolutePatterns`. - `testParseRequest()` covered by UrlManagerParseUrlTest, UrlNormalizer related parts moved to UrlNormalizerTest. - `testParseRESTRequest()` moved to UrlManagerParseUrlTest - `testHash()` covered in different tests in UrlManagerCreateUrlTest. - `testMultipleHostsRules($host)` kept as is. Before: $ vendor/bin/phpunit tests/framework/web/UrlManagerTest.php ... OK (12 tests, 89 assertions) After: $ vendor/bin/phpunit tests/framework/web/UrlManager*.php ... OK (72 tests, 648 assertions)
8 years ago
}