Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit fb010dd

Browse files
kalyabindmirogin
authored andcommitted
fix #6 (#7)
* fixes #6 * fixes #6 * tests * revert protected methods
1 parent da907cb commit fb010dd

File tree

2 files changed

+197
-28
lines changed

2 files changed

+197
-28
lines changed

src/JsUrlManager.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use yii\helpers\Json;
99
use yii\base\Application;
1010
use yii\web\JsExpression;
11+
use yii\web\Request;
1112
use yii\web\UrlManager;
1213
use yii\web\UrlRule;
1314
use yii\web\View;
@@ -32,6 +33,12 @@ class JsUrlManager extends Object implements BootstrapInterface
3233
*/
3334
public $configureThroughVariable = false;
3435

36+
/**
37+
* Initialize configuration on AJAX requests
38+
* @var bool
39+
*/
40+
public $configureOnAjaxRequests = true;
41+
3542
/**
3643
* @var UrlManager
3744
*/
@@ -44,9 +51,10 @@ public function bootstrap($app)
4451
{
4552
$this->setUrlManager($app->urlManager);
4653
$configuration = $this->defineConfiguration();
47-
if ($this->configureThroughVariable) {
54+
$enableConfiguration = !$app->request instanceof \yii\web\Request || !$app->request->getIsAjax() || $this->configureOnAjaxRequests;
55+
if ($enableConfiguration && $this->configureThroughVariable) {
4856
$this->configureFrontendUrlManagerThroughVariable($configuration);
49-
} else {
57+
} elseif ($enableConfiguration) {
5058
$this->configureFrontendUrlManager($configuration);
5159
}
5260

@@ -158,4 +166,4 @@ protected function configureFrontendUrlManagerThroughVariable(array $configurati
158166
private function prepareForFrontend($value) {
159167
return (string) new JsExpression(Json::encode($value));
160168
}
161-
}
169+
}

tests/JsUrlManagerTest.php

Lines changed: 186 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,86 @@
11
<?php
22

33

4+
use dmirogin\js\urlmanager\JsUrlManager;
5+
use yii\helpers\Json;
6+
use yii\web\Application;
7+
use yii\web\JsExpression;
8+
use yii\web\Request;
9+
use yii\web\UrlManager;
10+
use yii\web\View;
11+
412
class JsUrlManagerTest extends \PHPUnit\Framework\TestCase
513
{
14+
protected $urlManagerConfiguration = [
15+
'class' => \yii\web\UrlManager::class,
16+
'enablePrettyUrl' => true,
17+
'showScriptName' => false,
18+
'suffix' => '.html',
19+
'rules' => [
20+
'/' => '/site/index',
21+
'/add/<id:\d+>' => '/site/add',
22+
[
23+
'pattern' => '/foo/<id:(\\d+)>/bar/<type:(first|second)>/',
24+
'route' => '/foo/bar',
25+
'suffix' => '/'
26+
],
27+
[
28+
'class' => \yii\web\GroupUrlRule::class,
29+
'prefix' => 'admin',
30+
'rules' => [
31+
'login' => 'user/login',
32+
'logout' => 'user/logout',
33+
'dashboard' => 'default/dashboard',
34+
],
35+
]
36+
],
37+
];
38+
39+
protected function mockWebApplication()
40+
{
41+
return new Application([
42+
'id' => 'testing',
43+
'basePath' => dirname(__DIR__),
44+
'components' => [
45+
'urlManager' => $this->urlManagerConfiguration,
46+
]
47+
]);
48+
}
49+
650
/**
751
* @return \yii\web\UrlManager
852
*/
953
protected function mockUrlManager()
1054
{
1155
/** @var \yii\web\UrlManager $urlManager */
12-
$urlManager = Yii::createObject([
13-
'class' => \yii\web\UrlManager::class,
14-
'enablePrettyUrl' => true,
15-
'showScriptName' => false,
16-
'suffix' => '.html',
17-
'rules' => [
18-
'/' => '/site/index',
19-
'/add/<id:\d+>' => '/site/add',
20-
[
21-
'pattern' => '/foo/<id:(\\d+)>/bar/<type:(first|second)>/',
22-
'route' => '/foo/bar',
23-
'suffix' => '/'
24-
],
25-
[
26-
'class' => \yii\web\GroupUrlRule::class,
27-
'prefix' => 'admin',
28-
'rules' => [
29-
'login' => 'user/login',
30-
'logout' => 'user/logout',
31-
'dashboard' => 'default/dashboard',
32-
],
33-
]
34-
],
35-
]);
56+
$urlManager = Yii::createObject($this->urlManagerConfiguration);
3657

3758
return $urlManager;
3859
}
3960

61+
/**
62+
* @covers JsUrlManager::getUrlManager()
63+
* @covers JsUrlManager::setUrlManager()
64+
*/
65+
public function testSetUrlManager()
66+
{
67+
$urlManager = $this->mockUrlManager();
68+
69+
/** @var JsUrlManager $jsUrlManager */
70+
$jsUrlManager = Yii::createObject(JsUrlManager::class);
71+
72+
$this->assertInstanceOf(JsUrlManager::class, $jsUrlManager);
73+
$this->assertNull($jsUrlManager->getUrlManager());
74+
75+
$jsUrlManager->setUrlManager($urlManager);
76+
77+
$this->assertInstanceOf(UrlManager::class, $jsUrlManager->getUrlManager());
78+
}
79+
80+
/**
81+
* @covers JsUrlManager::getRules()
82+
* @depends testSetUrlManager
83+
*/
4084
public function testGetRules()
4185
{
4286
$urlManager = $this->mockUrlManager();
@@ -64,5 +108,122 @@ public function testGetRules()
64108
], $jsUrlManager->getRules());
65109
}
66110

111+
/**
112+
* @covers JsUrlManager::getPrefix()
113+
* @depends testSetUrlManager
114+
*/
115+
public function testGetPrefix()
116+
{
117+
$urlManager = $this->mockUrlManager();
118+
119+
/** @var JsUrlManager $jsUrlManager */
120+
$jsUrlManager = Yii::createObject(JsUrlManager::class);
121+
$jsUrlManager->setUrlManager($urlManager);
122+
123+
$urlManager->showScriptName = true;
124+
$urlManager->enablePrettyUrl = true;
125+
126+
$this->assertEquals($urlManager->getScriptUrl(), $jsUrlManager->getPrefix());
127+
128+
$urlManager->showScriptName = false;
129+
$urlManager->enablePrettyUrl = false;
130+
131+
$this->assertEquals($urlManager->getScriptUrl(), $jsUrlManager->getPrefix());
132+
133+
$urlManager->showScriptName = false;
134+
$urlManager->enablePrettyUrl = true;
135+
136+
$this->assertEquals($urlManager->getBaseUrl(), $jsUrlManager->getPrefix());
137+
}
138+
139+
/**
140+
* @covers JsUrlManager::defineConfiguration()
141+
* @depends testSetUrlManager
142+
* @depends testGetRules
143+
* @depends testGetPrefix
144+
*/
145+
public function testDefineConfiguration()
146+
{
147+
$urlManager = $this->mockUrlManager();
148+
149+
/** @var JsUrlManager $jsUrlManager */
150+
$jsUrlManager = Yii::createObject(JsUrlManager::class);
151+
$jsUrlManager->setUrlManager($urlManager);
152+
153+
$this->assertEquals([
154+
'enablePrettyUrl' => true,
155+
'showScriptName' => false,
156+
'suffix' => '.html',
157+
'rules' => $jsUrlManager->getRules(),
158+
'prefix' => $jsUrlManager->getPrefix(),
159+
], $jsUrlManager->defineConfiguration());
160+
}
161+
162+
/**
163+
* @covers JsUrlManager::bootstrap()
164+
* @depends testGetRules
165+
* @depends testGetPrefix
166+
* @depends testSetUrlManager
167+
* @depends testDefineConfiguration
168+
*/
169+
public function testBootstrap()
170+
{
171+
$app = $this->mockWebApplication();
172+
173+
$configureRequest = function($isAjax) use ($app) {
174+
$request = $this->createMock(Request::class);
175+
176+
$request->expects($this->any())
177+
->method('getIsAjax')
178+
->willReturn($isAjax);
179+
180+
$app->set('request', $request);
181+
};
182+
183+
$configureViewWithRegisterJs = function($js, $pos = View::POS_BEGIN) use ($app) {
184+
$view = $this->createMock(View::class);
185+
186+
$view->expects($this->once())
187+
->method('registerJs')
188+
->with($js, $pos);
67189

68-
}
190+
$app->set('view', $view);
191+
};
192+
193+
/** @var JsUrlManager $jsUrlManager */
194+
$jsUrlManager = Yii::createObject(JsUrlManager::class);
195+
$jsUrlManager->setUrlManager($app->urlManager);
196+
$jsUrlManager->configurationStringPosition = View::POS_BEGIN;
197+
198+
// Disable AJAX mode
199+
$jsUrlManager->configureOnAjaxRequests = false;
200+
$configureRequest(false);
201+
202+
$configureViewWithRegisterJs('document.urlManagerConfiguration = ' . new JsExpression(Json::encode($jsUrlManager->defineConfiguration())) . ';');
203+
$jsUrlManager->configureThroughVariable = true;
204+
$jsUrlManager->bootstrap($app);
205+
206+
$configureViewWithRegisterJs('UrlManager.configure(' . new JsExpression(Json::encode($jsUrlManager->defineConfiguration())) . ');');
207+
$jsUrlManager->configureThroughVariable = false;
208+
$jsUrlManager->bootstrap($app);
209+
210+
// enable AJAX mode
211+
$jsUrlManager->configureOnAjaxRequests = true;
212+
$configureRequest(true);
213+
214+
$configureViewWithRegisterJs('document.urlManagerConfiguration = ' . new JsExpression(Json::encode($jsUrlManager->defineConfiguration())) . ';');
215+
$jsUrlManager->configureThroughVariable = true;
216+
$jsUrlManager->bootstrap($app);
217+
218+
$configureViewWithRegisterJs('UrlManager.configure(' . new JsExpression(Json::encode($jsUrlManager->defineConfiguration())) . ');');
219+
$jsUrlManager->configureThroughVariable = false;
220+
$jsUrlManager->bootstrap($app);
221+
222+
// disable bootstrapping using AJAX requests
223+
$jsUrlManager->configureOnAjaxRequests = false;
224+
$jsUrlManager->configureThroughVariable = true;
225+
$jsUrlManager->bootstrap($app);
226+
$jsUrlManager->configureThroughVariable = false;
227+
$jsUrlManager->bootstrap($app);
228+
}
229+
}

0 commit comments

Comments
 (0)