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.

156 lines
6.0 KiB

<?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;
use yii\web\NotFoundHttpException;
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\Request;
use yii\web\UrlManager;
use yii\web\UrlNormalizer;
use yii\web\UrlNormalizerRedirectException;
use yiiunit\TestCase;
/**
* @group web
*/
class UrlNormalizerTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function testNormalizePathInfo()
{
$normalizer = new UrlNormalizer();
$this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/a/'));
$this->assertEquals('post/123', $normalizer->normalizePathInfo('post//123//', '/a'));
$this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/'));
$this->assertEquals('post/123', $normalizer->normalizePathInfo('post//123//', ''));
$normalizer->collapseSlashes = false;
$this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/a/'));
$this->assertEquals('post//123', $normalizer->normalizePathInfo('post//123//', '/a'));
$this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/'));
$this->assertEquals('post//123', $normalizer->normalizePathInfo('post//123//', ''));
$normalizer->normalizeTrailingSlash = false;
$this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/a/'));
$this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/a'));
$this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', '/'));
$this->assertEquals('post//123//', $normalizer->normalizePathInfo('post//123//', ''));
$normalizer->collapseSlashes = true;
$this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/a/'));
$this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/a'));
$this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', '/'));
$this->assertEquals('post/123/', $normalizer->normalizePathInfo('post//123//', ''));
}
public function testNormalizeRoute()
{
$normalizer = new UrlNormalizer();
$route = ['site/index', ['id' => 1, 'name' => 'test']];
// 404 error as default action
$normalizer->action = UrlNormalizer::ACTION_NOT_FOUND;
$expected = new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
try {
$result = $normalizer->normalizeRoute($route);
$this->fail('Expected throwing NotFoundHttpException');
} catch (NotFoundHttpException $exc) {
$this->assertEquals($expected, $exc);
}
// 301 redirect as default action
$normalizer->action = UrlNormalizer::ACTION_REDIRECT_PERMANENT;
$expected = new UrlNormalizerRedirectException([$route[0]] + $route[1], 301);
try {
$result = $normalizer->normalizeRoute($route);
$this->fail('Expected throwing UrlNormalizerRedirectException');
} catch (UrlNormalizerRedirectException $exc) {
$this->assertEquals($expected, $exc);
}
// 302 redirect as default action
$normalizer->action = UrlNormalizer::ACTION_REDIRECT_TEMPORARY;
$expected = new UrlNormalizerRedirectException([$route[0]] + $route[1], 302);
try {
$result = $normalizer->normalizeRoute($route);
$this->fail('Expected throwing UrlNormalizerRedirectException');
} catch (UrlNormalizerRedirectException $exc) {
$this->assertEquals($expected, $exc);
}
// no action
$normalizer->action = null;
$this->assertEquals($route, $normalizer->normalizeRoute($route));
// custom callback which modifies the route
$normalizer->action = function ($route, $normalizer) {
$route[0] = 'site/redirect';
$route['normalizeTrailingSlash'] = $normalizer->normalizeTrailingSlash;
return $route;
};
$expected = $route;
$expected[0] = 'site/redirect';
$expected['normalizeTrailingSlash'] = $normalizer->normalizeTrailingSlash;
$this->assertEquals($expected, $normalizer->normalizeRoute($route));
// custom callback which throw custom 404 error
$normalizer->action = function ($route, $normalizer) {
throw new NotFoundHttpException('Custom error message.');
};
$expected = new NotFoundHttpException('Custom error message.');
try {
$result = $normalizer->normalizeRoute($route);
$this->fail('Expected throwing NotFoundHttpException');
} catch (NotFoundHttpException $exc) {
$this->assertEquals($expected, $exc);
}
}
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
/**
* Test usage of UrlNormalizer in UrlManager.
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
*
* Trailing slash is insignificant if normalizer is enabled.
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 testUrlManager()
{
$config = [
'enablePrettyUrl' => true,
'cache' => null,
'normalizer' => [
'class' => 'yii\web\UrlNormalizer',
'action' => null,
],
];
$request = new Request();
// pretty URL without rules
$config['rules'] = [];
$manager = new UrlManager($config);
$request->pathInfo = '/module/site/index/';
$result = $manager->parseRequest($request);
$this->assertEquals(['module/site/index', []], $result);
// pretty URL with rules
$config['rules'] = [
[
'pattern' => 'post/<id>/<title>',
'route' => 'post/view',
],
];
$manager = new UrlManager($config);
$request->pathInfo = 'post/123/this+is+sample/';
$result = $manager->parseRequest($request);
$this->assertEquals(['post/view', ['id' => '123', 'title' => 'this+is+sample']], $result);
}
}