Files
yii2/tests/framework/web/UrlNormalizerTest.php
Carsten Brandt 8faedcbd7c 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](4187718c14/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)
2017-01-31 02:38:35 +01:00

157 lines
6.0 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;
use yii\web\NotFoundHttpException;
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);
}
}
/**
* Test usage of UrlNormalizer in UrlManager
*
* trailing slash is insignificant if normalizer is enabled
*/
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);
}
}