Skip to content

Commit

Permalink
Fixes #14089: Added tests for yii\base\Theme
Browse files Browse the repository at this point in the history
  • Loading branch information
vladis84 authored and samdark committed Jun 11, 2017
1 parent b00c82c commit f6b54e1
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 5 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #14264: Fixed a bug where `yii\log\Logger::calculateTimings()` was not accepting messages with array tokens (bizley)
- Chg #14201: `yii\console\controllers\MessageController::extractMessagesFromTokens()` is now protected (faenir)
- Enh #13787: Added `yii\db\Migration::$maxSqlOutputLength` that allows limiting number of characters for outputting SQL (thiagotalma)
- Enh #14089: Added tests for `yii\base\Theme` (vladis84)

2.0.12 June 05, 2017
--------------------
Expand Down
7 changes: 2 additions & 5 deletions framework/base/Theme.php
Expand Up @@ -99,7 +99,7 @@ public function getBaseUrl()
*/
public function setBaseUrl($url)
{
$this->_baseUrl = rtrim(Yii::getAlias($url), '/');
$this->_baseUrl = $url === null ? null : rtrim(Yii::getAlias($url), '/');
}

private $_basePath;
Expand Down Expand Up @@ -139,9 +139,7 @@ public function applyTo($path)
}
$pathMap = [Yii::$app->getBasePath() => [$basePath]];
}

$path = FileHelper::normalizePath($path);

foreach ($pathMap as $from => $tos) {
$from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
if (strpos($path, $from) === 0) {
Expand All @@ -155,7 +153,6 @@ public function applyTo($path)
}
}
}

return $path;
}

Expand All @@ -178,7 +175,7 @@ public function getUrl($url)
* Converts a relative file path into an absolute one using [[basePath]].
* @param string $path the relative file path to be converted.
* @return string the absolute file path
* @throws InvalidConfigException if [[baseUrl]] is not set
* @throws InvalidConfigException if [[basePath]] is not set
*/
public function getPath($path)
{
Expand Down
165 changes: 165 additions & 0 deletions tests/framework/base/ThemeTest.php
@@ -0,0 +1,165 @@
<?php

namespace yiiunit\framework\base;

use Yii;
use yiiunit\TestCase;
use yii\base\Theme;

/**
* @group base
*/
class ThemeTest extends TestCase
{
protected function setUp()
{
$config = ['aliases' => ['@web' => '']];
$this->mockWebApplication($config);
}

private function assertPathEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
{
$expected = str_replace('\\', '/', $expected);
$actual = str_replace('\\', '/', $actual);
$this->assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
}

public function testSetBaseUrl()
{
$theme = new Theme(['baseUrl' => '@web/themes/basic']);
$expected = Yii::getAlias('@web/themes/basic');

$this->assertEquals($expected, $theme->baseUrl);
}

public function testGetUrlFilledBaseUrl()
{
$theme = new Theme(['baseUrl' => '@web/themes/basic']);
$expected = Yii::getAlias('@web/themes/basic/js/test.js');

$actual = $theme->getUrl('/js/test.js');

$this->assertEquals($expected, $actual);
}

public function testGetUrlNotFilledBaseUrl()
{
$theme = new Theme(['baseUrl' => null]);

$this->expectException('yii\base\InvalidConfigException');

$theme->getUrl('/js/test.js');
}

public function testSetBasePath()
{
$theme = new Theme(['basePath' => '@app/framework/base/fixtures/themes/basic']);
$expected = Yii::getAlias('@app/framework/base/fixtures/themes/basic');

$this->assertEquals($expected, $theme->basePath);
}

public function testGetPathFilledBasePath()
{
$theme = new Theme(['basePath' => '@app/framework/base/fixtures/themes/basic']);
$expected = Yii::getAlias('@app/framework/base/fixtures/themes/basic/img/logo.gif');

$actual = $theme->getPath('/img/logo.gif');

$this->assertPathEquals($expected, $actual);
}

public function testGetPathNotFilledBasePath()
{
$theme = new Theme(['baseUrl' => null]);

$this->expectException('yii\base\InvalidConfigException');

$theme->getPath('/img/logo.gif');
}

public function testApplyToEmptyBasePath()
{
$theme = new Theme(['basePath' => null]);

$this->expectException('yii\base\InvalidConfigException');

$theme->applyTo(null);
}

public function testApplyToEmptyPathMap()
{
$theme = new Theme(['basePath' => '@app/framework/base/fixtures/themes/basic']);
$expected = Yii::getAlias('@app/framework/base/fixtures/themes/basic/views/site/index.php');

$actual = $theme->applyTo(Yii::$app->basePath . '/views/site/index.php');

$this->assertPathEquals($expected, $actual);
}

public function testApplyToFilledPathMap()
{
$config = [
'pathMap' => [
'@app/views' => '@app/framework/base/fixtures/themes/basic/views',
],
];
$theme = new Theme($config);
$expected = Yii::getAlias('@app/framework/base/fixtures/themes/basic/views/site/index.php');

$actual = $theme->applyTo(Yii::$app->basePath . '/views/site/index.php');

$this->assertPathEquals($expected, $actual);
}

public function testApplyToFilledPathMapNotExistsViewInFirstTheme()
{
$config = [
'pathMap' => [
'@app/views' => [
'@app/framework/base/fixtures/themes/basic/views',
'@app/framework/base/fixtures/themes/christmas/views',
],
],
];
$theme = new Theme($config);
$expected = Yii::getAlias('@app/framework/base/fixtures/themes/christmas/views/site/main.php');

$actual = $theme->applyTo(Yii::$app->basePath . '/views/site/main.php');

$this->assertPathEquals($expected, $actual);
}

public function testApplyToFilledPathMapAndInheritThemes()
{
$config = [
'pathMap' => [
'@app/views' => [
'@app/framework/base/fixtures/themes/christmas/views',
'@app/framework/base/fixtures/themes/basic/views',
],
],
];
$theme = new Theme($config);
$expected = Yii::getAlias('@app/framework/base/fixtures/themes/christmas/views/site/index.php');

$actual = $theme->applyTo(Yii::$app->basePath . '/views/site/index.php');

$this->assertPathEquals($expected, $actual);
}

public function testApplyToFilledPathMapAndFileNotExists()
{
$config = [
'pathMap' => [
'@app/views' => '@app/framework/base/fixtures/themes/christmas/views',
],
];
$theme = new Theme($config);
$expected = Yii::getAlias(Yii::$app->basePath . '/views/main/index.php');

$actual = $theme->applyTo($expected);

$this->assertPathEquals($expected, $actual);
}
}
@@ -0,0 +1 @@
<?php
@@ -0,0 +1 @@
<?php
@@ -0,0 +1 @@
<?php

0 comments on commit f6b54e1

Please sign in to comment.