mockWebApplication([ 'components' => [ 'request' => [ 'class' => 'yii\web\Request', 'scriptUrl' => '/base/index.php', 'hostInfo' => 'http://example.com/', 'url' => '/base/index.php&r=site%2Fcurrent&id=42' ], 'urlManager' => [ 'class' => 'yii\web\UrlManager', 'baseUrl' => '/base', 'scriptUrl' => '/base/index.php', 'hostInfo' => 'http://example.com/', ] ], ]); } public function testIds() { Nav::$counter = 0; $out = Nav::widget( [ 'items' => [ [ 'label' => 'Page1', 'content' => 'Page1', ], [ 'label' => 'Dropdown1', 'items' => [ ['label' => 'Page2', 'content' => 'Page2'], ['label' => 'Page3', 'content' => 'Page3'], ] ], [ 'label' => 'Dropdown2', 'visible' => false, 'items' => [ ['label' => 'Page4', 'content' => 'Page4'], ['label' => 'Page5', 'content' => 'Page5'], ] ] ] ] ); $expected = <<
  • Page1
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); } public function testRenderDropDownWithDropDownOptions() { Nav::$counter = 0; $out = Nav::widget( [ 'items' => [ [ 'label' => 'Page1', 'content' => 'Page1', ], [ 'label' => 'Dropdown1', 'dropDownOptions' => ['class' => 'test', 'data-id' => 't1', 'id' => 'test1'], 'items' => [ ['label' => 'Page2', 'content' => 'Page2'], ['label' => 'Page3', 'content' => 'Page3'], ] ], [ 'label' => 'Dropdown2', 'visible' => false, 'items' => [ ['label' => 'Page4', 'content' => 'Page4'], ['label' => 'Page5', 'content' => 'Page5'], ] ] ] ] ); $expected = <<
  • Page1
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); } public function testEmptyItems() { Nav::$counter = 0; $out = Nav::widget([ 'items' => [ [ 'label' => 'Page1', 'items' => null, ], [ 'label' => 'Dropdown1', 'items' => [ ['label' => 'Page2', 'content' => 'Page2'], ['label' => 'Page3', 'content' => 'Page3'], ], ], [ 'label' => 'Page4', 'items' => [], ], ], ]); $expected = <<
  • Page1
  • Page4
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); } /** * @see https://github.com/yiisoft/yii2-bootstrap/issues/162 */ public function testExplicitActive() { $this->mockAction('site', 'index'); Nav::$counter = 0; $out = Nav::widget([ 'activateItems' => false, 'items' => [ [ 'label' => 'Item1', 'active' => true, ], [ 'label' => 'Item2', 'url' => ['site/index'], ], ], ]); $expected = <<
  • Item1
  • Item2
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); $this->removeMockedAction(); } /** * @see https://github.com/yiisoft/yii2-bootstrap/issues/162 */ public function testImplicitActive() { $this->mockAction('site', 'index'); Nav::$counter = 0; $out = Nav::widget([ 'items' => [ [ 'label' => 'Item1', 'active' => true, ], [ 'label' => 'Item2', 'url' => ['site/index'], ], ], ]); $expected = <<
  • Item1
  • Item2
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); $this->removeMockedAction(); } /** * @see https://github.com/yiisoft/yii2-bootstrap/issues/162 */ public function testExplicitActiveSubitems() { $this->mockAction('site', 'index'); Nav::$counter = 0; $out = Nav::widget([ 'activateItems' => false, 'items' => [ [ 'label' => 'Item1', ], [ 'label' => 'Item2', 'items' => [ ['label' => 'Page2', 'content' => 'Page2', 'url' => ['site/index']], ['label' => 'Page3', 'content' => 'Page3', 'active' => true], ], ], ], ]); $expected = <<
  • Item1
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); $this->removeMockedAction(); } /** * @see https://github.com/yiisoft/yii2-bootstrap/issues/162 */ public function testImplicitActiveSubitems() { $this->mockAction('site', 'index'); Nav::$counter = 0; $out = Nav::widget([ 'items' => [ [ 'label' => 'Item1', ], [ 'label' => 'Item2', 'items' => [ ['label' => 'Page2', 'content' => 'Page2', 'url' => ['site/index']], ['label' => 'Page3', 'content' => 'Page3', 'active' => true], ], ], ], ]); $expected = <<
  • Item1
  • EXPECTED; $this->assertEqualsWithoutLE($expected, $out); $this->removeMockedAction(); } /** * Mocks controller action with parameters * * @param string $controllerId * @param string $actionID * @param string $moduleID * @param array $params */ protected function mockAction($controllerId, $actionID, $moduleID = null, $params = []) { \Yii::$app->controller = $controller = new Controller($controllerId, \Yii::$app); $controller->actionParams = $params; $controller->action = new Action($actionID, $controller); if ($moduleID !== null) { $controller->module = new Module($moduleID); } } protected function removeMockedAction() { \Yii::$app->controller = null; } }