Файл: cobisja/BootHelp/tests/src/DropdownTest.php
Строк: 475
<?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 cobisjaBootHelpDropdown;
use cobisjaBootHelpHelpersLinkTo;
class DropdownTest extends PHPUnit_Framework_TestCase {
public function testWithNoDropdownOptions() {
/**
* It should generates:
*
* <div class="dropdown">
* <button data-toggle="dropdown" id="label-dropdown-{id}" type="button" class="dropdown-toggle btn btn-default">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{id}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* <li><a href="#" role="menuitem">Profile</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', function(){
return [
new LinkTo('Home'),
new LinkTo('Users'),
new LinkTo('Profile')
];
});
$html=$dropdown->getHtml();
$this->assertTrue($html->isA('div', ['class'=>'dropdown']));
$this->assertTrue(2 === $html->numberOfChildren());
$child_1 = $html->getChild(0);
$child_2 = $html->getChild(1);
$this->assertTrue($child_1->isA('button', ['data-toggle'=>'dropdown', 'class'=>'dropdown-toggle btn btn-default']));
$this->assertTrue($child_2->isA('ul', ['class'=>'dropdown-menu']));
$this->assertTrue($child_1->getChild(1)->isA('span', ['class'=>'caret']));
$this->assertTrue(3 === $child_2->numberOfChildren());
foreach($child_2->getChildren() as $child) {
$this->assertTrue($child->isA('li'));
$this->assertTrue($child->getChild(0)->isA('a', ['role'=>'menuitem']));
}
}
public function testIdToConnectButtonAndUlAreTheSame() {
/**
* It should generates:
*
* <div class="dropdown">
* <button data-toggle="dropdown" id="my-id" type="button" class="dropdown-toggle btn btn-default">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="my-id" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* <li><a href="#" role="menuitem">Profile</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', ['id'=>'my-id'], function(){
return [
new LinkTo('Home'),
new LinkTo('Users'),
new LinkTo('Profile')
];
});
$html = $dropdown->getHtml();
$button_id = $html->getChild(0)->getAttribute('id');
$ul_id = $html->getChild(1)->getAttribute('aria-labelledby');
$this->assertEquals($button_id, $ul_id);
}
/**
* @dataProvider get_contexts
*/
public function testSetButtonContextClassWhenSetContextOption($context) {
/**
* For $context = ['success', 'info', 'warning', 'danger'], it should generates:
*
* <div class="dropdown">
* <button data-toggle="dropdown" id="label-dropdown-{id}" type="button" class="dropdown-toggle btn btn-{$context}">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{id}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', ['context'=>$context], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$html = $dropdown->getHtml();
$button = $html->getChild(0);
$this->assertTrue($button->hasAttribute('class', 'btn-' . $context));
}
/**
* @dataProvider get_sizes
*/
public function testAddButtonSizeClassWhenSetSizeOption($size) {
/**
* For $size = ['xs', 'sm', 'lg'], it should generates:
*
* <div class="dropdown">
* <button data-toggle="dropdown" id="label-dropdown-{id}" type="button" class="dropdown-toggle btn btn-default btn-{size}">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{id}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', ['size'=>$size], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$html = $dropdown->getHtml();
$button = $html->getChild(0);
$this->assertTrue($button->hasAttribute('class', 'btn-' . $size));
}
public function testSetUlClassWhenSpecifyRightAlignement() {
/**
* It should generates:
*
* <div class="dropdown">
* <button data-toggle="dropdown" id="label-dropdown-894474825" type="button" class="dropdown-toggle btn btn-default" aria-expanded="false">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-894474825" role="menu" class="dropdown-menu dropdown-menu-right">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', ['align'=>'right'], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$html = $dropdown->getHtml();
$ul = $html->getChild(1);
$this->assertTrue($ul->hasAttribute('class', 'dropdown-menu-right'));
}
public function testSetDivClassWhenSpecifyGroupableOption() {
/**
* It should generates:
*
* <div class="btn-group">
* <button data-toggle="dropdown" id="label-dropdown-{$id1}" type="button" class="dropdown-toggle btn btn-default" aria-expanded="false">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{$id1}" role="menu" class="dropdown-menu dropdown-menu-right">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
* </div>
* <div class="btn-group">
* <button data-toggle="dropdown" id="label-dropdown-{$id2}" type="button" class="dropdown-toggle btn btn-default">
* Profile
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{$id2}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Edit profile</a></li>
* </ul>
* </div>
*/
$dropdown1 = new Dropdown('Menu', ['groupable'=>true], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$dropdown2 = new Dropdown('Profile', ['groupable'=>true], function(){
return new LinkTo('Edit profile');
});
$html1 = $dropdown1->getHtml();
$html2 = $dropdown2->getHtml();
$this->assertTrue($html1->isA('div', ['class'=>'dropdown btn-group']) &&
$html2->isA('div', ['class'=> 'dropdown btn-group']));
}
public function testSetDivClassWhenSetDirectionToUp() {
/**
* It should be generates:
*
* <div class="dropup">
* <button data-toggle="dropdown" id="label-dropdown-{id}" type="button" class="dropdown-toggle btn btn-default">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{id}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', ['direction'=>'up'], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$html = $dropdown->getHtml();
$this->assertTrue($html->isA('div', ['class'=>'dropup']));
}
public function testCanGenerateASplitButton() {
/**
* It should generates:
*
* <div class="btn-group dropdown">
* <button class="btn btn-default" type="button">
* Menu
* </button>
* <button data-toggle="dropdown" id="label-dropdown-{$id}" type="button" class="dropdown-toggle btn btn-default">
* <span class="caret"></span>
* <span class="sr-only">Toggle Dropdown</span>
* </button>
* <ul aria-labelledby="label-dropdown-{$id}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
*</div>
*/
$dropdown = new Dropdown('Menu', ['split'=>true], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$html = $dropdown->getHtml();
$this->assertTrue(3 === $html->numberOfChildren());
$child_1 = $html->getChild(0);
$child_2 = $html->getChild(1);
$this->assertTrue($child_1->isA('button') && $child_2->isA('button'));
$this->assertTrue(2 === $child_2->numberOfChildren());
$this->assertTrue($child_2->getChild(0)->isA('span', ['class'=>'caret']));
$this->assertTrue($child_2->getChild(1)->isA('span', ['class'=>'sr-only']));
}
public function testSetCustomClassToButtonDropdown() {
/**
* It should be generates:
*
* <div class="dropup">
* <button data-toggle="dropdown" id="label-dropdown-{id}" type="button" class="en dropdown-toggle btn btn-default">
* Menu
* <span class="caret"></span>
* </button>
* <ul aria-labelledby="label-dropdown-{id}" role="menu" class="dropdown-menu">
* <li><a href="#" role="menuitem">Home</a></li>
* <li><a href="#" role="menuitem">Users</a></li>
* </ul>
* </div>
*/
$dropdown = new Dropdown('Menu', ['button'=>['class'=>'en']], function(){
return [
new LinkTo('Home'),
new LinkTo('Users')
];
});
$html = $dropdown->getHtml();
$button = $html->getChild(0);
$this->assertTrue($button->hasAttribute('class', 'en'));
}
public function get_contexts() {
return [ ['success'], ['info'], ['warning'], ['danger'] ];
}
public function get_sizes() {
return [ ['xs'], ['sm'], ['lg'] ];
}
}