Файл: cobisja/BootHelp/tests/src/NavbarTest.php
Строк: 733
<?php
/*
* BootHelp - Bootstrap Helpers written in PHP
*
* (The MIT License)
*
* Copyright (c) 2015 Jorge Cobis <jcobis@gmail.com / http://twitter.com/cobisja>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace BootHelpTests;
use cobisjaBootHelpNavbar;
use cobisjaBootHelpNav;
use cobisjaBootHelpButton;
use cobisjaBootHelpDropdown;
use cobisjaBootHelpHelpersLinkTo;
use cobisjaBootHelpHelpersVertical;
use cobisjaBootHelpHelpersHorizontal;
use cobisjaBootHelpHelpersDivider;
class NavbarTest extends PHPUnit_Framework_TestCase {
public function testWithNoOptions() {
/**
* It should generates:
*
* <nav class="navbar navbar-default" role="navigation">
* <div class="container">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-4880904" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-4880904" class="collapse navbar-collapse">
* <ul class="navbar-right nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(function(){
return [
new Vertical(function(){ return new LinkTo('Home'); }),
new Horizontal(function(){
return new Nav(['class'=>'navbar-right'], function(){
return [new LinkTo('Profile'), new LinkTo('Settings')];
});
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html);
}
public function testInvertedColorWhenSetInvertedOption() {
/**
* It should generates:
*
* <nav class="navbar navbar-inverse" role="navigation">
* <div class="container">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-4880904" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-4880904" class="collapse navbar-collapse">
* <ul class="navbar-right nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(['inverted'=>true], function(){
return [
new Vertical(function(){ return new LinkTo('Home'); }),
new Horizontal(function(){
return new Nav(['class'=>'navbar-right'], function(){
return [new LinkTo('Profile'), new LinkTo('Settings')];
});
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html);
$this->assertTrue($html->hasAttribute('class', 'navbar-inverse'));
}
public function testGeneratesFuildContainerWhenSetFluidOption() {
/**
* It should generates:
*
* <nav class="navbar navbar-default" role="navigation">
* <div class="container-fluid">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-4880904" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-4880904" class="collapse navbar-collapse">
* <ul class="navbar-right nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(['fluid'=>true], function(){
return [
new Vertical(function(){ return new LinkTo('Home'); }),
new Horizontal(function(){
return new Nav(['class'=>'navbar-right'], function(){
return [new LinkTo('Profile'), new LinkTo('Settings')];
});
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html);
$this->assertTrue($html->getChild(0)->isA('div', ['class'=>'container-fluid']));
}
/**
* @dataProvider get_positions
*/
public function testStaticNavBarWhenSetPositioOptionToUpOrDown($position) {
/**
* It should generates:
*
* <style>body {padding-{$position}: 70px}</style>
* <nav class="navbar navbar-default navbar-fixed-{$position}" role="navigation">
* <div class="container">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-4880904" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-4880904" class="collapse navbar-collapse">
* <ul class="navbar-right nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(['position'=>$position], function(){
return [
new Vertical(function(){ return new LinkTo('Home'); }),
new Horizontal(function(){
return new Nav(['class'=>'navbar-right'], function(){
return [new LinkTo('Profile'), new LinkTo('Settings')];
});
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html, true);
$this->assertTrue($html->hasAttribute('class', 'navbar-fixed-' . $position));
$this->assertEquals("<style>body {padding-$position: 70px}</style>", $html->getChild(1)->toString());
}
public function testStaticNavBarWhenSetPositioOptionToStatic() {
/**
* It should generates:
*
* <style>body {padding-{$position}: 70px}</style>
* <nav class="navbar navbar-default navbar-static-top" role="navigation">
* <div class="container">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-4880904" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-4880904" class="collapse navbar-collapse">
* <ul class="navbar-right nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(['position'=>'static'], function(){
return [
new Vertical(function(){ return new LinkTo('Home'); }),
new Horizontal(function(){
return new Nav(['class'=>'navbar-right'], function(){
return [new LinkTo('Profile'), new LinkTo('Settings')];
});
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html);
$this->assertTrue($html->hasAttribute('class', ['navbar-default', 'navbar-static-top']));
}
public function testWithExtraOptions() {
/**
* It should generates:
*
* <nav class="navbar navbar-default navbar-fixed-{$position}" role="navigation">
* <div class="container">
* <div data-js="1" class="en navbar-header">
* <button data-target="#my-navbar" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="my-navbar" class="en2 collapse navbar-collapse">
* <ul class="navbar-right nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$id = 'my-navbar';
$navbar = new Navbar(['id'=>$id], function(){
return [
new Vertical(['class'=>'en', 'data-js'=>1], function(){ return new LinkTo('Home'); }),
new Horizontal(['class'=>'en2'], function(){
return new Nav(['class'=>'navbar-right'], function(){
return [new LinkTo('Profile'), new LinkTo('Settings')];
});
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html);
$container = $html->getChild(0);
$this->assertTrue($container->getChild(0)->hasAttribute('data-js', 1));
$this->assertTrue($container->getChild(0)->hasAttribute('class', 'en'));
$this->assertTrue($container->getChild(0)->getChild(0)->hasAttribute('data-target', '#' . $id));
$this->assertTrue($container->getChild(1)->hasAttribute('id', 'my-navbar'));
$this->assertTrue($container->getChild(1)->hasAttribute('class', 'en2'));
}
public function testNavbarWithButtonEmbedded() {
/**
* It should generates:
*
* <nav role="navigation" class="navbar navbar-default">
* <div class="container">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-1134424" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-1134424" class="collapse navbar-collapse">ç
* <ul class="nav navbar-nav">
* <li><a href="#">Profile</a></li>
* <li><a href="#">Settings</a></li>
* </ul>
* <button class="btn btn-default navbar-btn">Click me!</button>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(function(){
return [
new Vertical(function(){ return new LinkTo('Home'); }),
new Horizontal(function(){
return [
new Nav(function(){
return [
new LinkTo('Profile'),
new LinkTo('Settings')
];
}),
new Button('Click me!')
];
})
];
});
$html = $navbar->getHtml();
$this->validate_navbar($html);
$horizontal = $html->getChild(0)->getChild(1);
$this->assertTrue($horizontal->hasAChildOfType('button', ['class'=>'btn btn-default navbar-btn']));
}
public function testDropdownIntoNavbar() {
/**
* It sould generates:
*
* <nav role="navigation" class="navbar navbar-default">
* <div class="container">
* <div class="navbar-header">
* <button data-target="#navbar-collapse-7159829" data-toggle="collapse" class="navbar-toggle" type="button">
* <span class="sr-only">Toggle navigation</span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* <span class="icon-bar"></span>
* </button>
* <a href="#" class="navbar-brand">Home</a>
* </div>
* <div id="navbar-collapse-7159829" class="collapse navbar-collapse">
* <div class="dropdown">
* <ul class="nav navbar-nav">
* <li class="dropdown">
* <a data-toggle="dropdown" class="dropdown-toggle" href="#">
* TV series
* <span class="caret"></span>
* </a>
* <ul aria-labelledby="label-dropdown-343446555" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">The walking dead</a></li>
* <li><a href="#" role="menuitem">Scorpio</a></li>
* <li class="divider"></li><li><a href="#" role="menuitem">Old series</a></li>
* </ul>
* </li>
* </ul>
* </div>
* <ul class="nav navbar-nav">
* <li><a href="#">About us</a></li>
* </ul>
* </div>
* </div>
* </nav>
*/
$navbar = new Navbar(function() {
return [
new Vertical(function(){
return new LinkTo('Home');
}),
new Horizontal(function(){
return [
new Dropdown('TV series', function(){
return [
new LinkTo('The walking dead'),
new LinkTo('Scorpio'),
new Divider(),
new LinkTo('Old series')
];
}),
new Nav(function(){return new LinkTo('About us');})
];
})
];
});
$html = $navbar->getHtml();
$horizontal = $html->getChild(0)->getChild(1);
$dropdown = $horizontal->getChild(0);
$nav_items = $horizontal->getChild(1);
$this->assertTrue($dropdown->isA('div', ['class'=>'dropdown']));
$this->assertTrue($dropdown->getChild(0)->isA('ul', ['class'=>'nav navbar-nav']));
$this->assertTrue($nav_items->isA('ul', ['class'=>'nav navbar-nav']));
}
public function get_positions() {
return [ ['top'], ['bottom'] ];
}
private function validate_navbar($html, $position_top_or_bottom=false) {
$this->assertTrue($html->isA('nav', ['role'=>'navigation']));
$this->assertTrue($html->hasAttribute('class', 'navbar'));
if (!$position_top_or_bottom) {
$this->assertTrue(1 === $html->numberOfChildren());
} else {
$this->assertTrue(2 === $html->numberOfChildren());
}
$navbar_header = $html->getChild(0)->getChild(0);
$navbar_collapse = $html->getChild(0)->getChild(1);
$this->assertTrue($navbar_header->isA('div'));
$this->assertTrue($navbar_header->hasAttribute('class','navbar-header'));
$this->assertTrue($navbar_header->getChild(0)->isA('button', ['class'=>'navbar-toggle']));
$this->assertTrue($navbar_collapse->isA('div'));
$this->assertTrue($navbar_collapse->hasAttribute('class', 'collapse navbar-collapse'));
$this->assertTrue($navbar_collapse->getChild(0)->isA('ul'));
$this->assertTrue($navbar_collapse->getChild(0)->hasAttribute('class', 'nav'));
}
}