Файл: cobisja/BootHelp/tests/src/ListGroupTest.php
Строк: 113
<?php
/**
* The MIT License
*
* Copyright 2015 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 cobisjaBootHelpListGroup;
class ListGroupTest extends PHPUnit_Framework_TestCase
{
public function testWithSimpleItemsAndNoOptions()
{
/**
* It should generates:
*
* <ul class="list-group">
* <li class="list-group-item">Cras justo odio</li>
* <li class="list-group-item">Dapibus ac facilisis in</li>
* <li class="list-group-item">Morbi leo risus</li>
* <li class="list-group-item">Porta ac consectetur ac</li>
* <li class="list-group-item">Vestibulum at eros</li>
* </ul>
*/
$items = [
'Cras justo odio',
'Dapibus ac facilisis in',
'Morbi leo risus',
'Porta ac consectetur ac',
'Vestibulum at eros'
];
$list_group = new ListGroup($items);
$html = $list_group->getHtml();
$this->validateBasicListGroupStructure($html, count($items));
}
public function testWithCustomizedItemsAndNoOptions()
{
/**
* It should generates:
*
* <ul class="list-group">
* <li class="list-group-item active">
* Cras justo odio
* <span class="badge">42</span>
* </li>
* <li href="http://google.com" id="my-disabled-item" class="list-group-item disabled">
* Dapibus ac facilisis in
* </li>
* <li class="list-group-item">
* Morbi leo risus
* </li>
* <li class="list-group-item list-group-item-danger">
* Porta ac consectetur ac
* </li>
* <li id="my-customized-item" class="list-group-item cusit">
* <h4>List group item heading</h4>
* </li>
* </ul>
*/
$items = [
['Cras justo odio', 'active'=>true, 'badge'=>42],
['Dapibus ac facilisis in', 'disabled'=>true, 'id'=>'my-disabled-item', 'href'=>'http://google.com'],
'Morbi leo risus',
['Porta ac consectetur ac', 'context'=>'danger'],
['title'=>'List group item heading', 'content'=>'Donec id elit.', 'id'=>'my-customized-item', 'class'=>'cusit']
];
$list_group = new ListGroup($items);
$html = $list_group->getHtml();
$this->validateBasicListGroupStructure($html, count($items));
// Validates first list group item.
$first_item = $html->getChild(0);
$this->assertTrue($first_item->hasAttribute('class', 'active'));
$this->assertTrue($first_item->hasAChildOfType('span', ['class'=>'badge']));
// Validates second list group item.
$second_item = $html->getChild(1);
$this->assertTrue($second_item->hasAttribute('href', 'http://google.com'));
$this->assertTrue($second_item->hasAttribute('id', 'my-disabled-item'));
$this->assertTrue($second_item->hasAttribute('class', ['list-group-item', 'disabled']));
// Validates fourth list group item.
$fourth_item = $html->getChild(3);
$this->assertTrue($fourth_item->hasAttribute('class', 'list-group-item-danger'));
// Validates fifth list group item.
$fifth_item = $html->getChild(4);
$this->assertTrue($fifth_item->hasAttribute('id', 'my-customized-item'));
$this->assertTrue($fifth_item->hasAttribute('class', 'cusit'));
$this->assertTrue($fifth_item->getChild(0)->isA('h4'));
$this->assertTrue($fifth_item->getChild(1)->isA('p'));
}
public function testWithSimpleItemsAndListGroupOptions()
{
/**
* It should generates:
*
* <div id="my-list-group" class="list-group">
* <a href="#" class="list-group-item">Cras justo odio</a>
* <a href="#" class="list-group-item">Dapibus ac facilisis in</a>
* <a href="#" class="list-group-item">Morbi leo risus</a>
* <a href="#" class="list-group-item">Porta ac consectetur ac</a>
* <a href="#" class="list-group-item">Vestibulum at eros</a>
* </div>
*/
$items = [
'Cras justo odio',
'Dapibus ac facilisis in',
'Morbi leo risus',
'Porta ac consectetur ac',
'Vestibulum at eros'
];
$list_group = new ListGroup($items, ['id'=>'my-list-group', 'linked'=>true]);
$html = $list_group->getHtml();
$this->validateBasicListGroupStructure($html, count($items), true);
$this->assertTrue($html->hasAttribute('id', 'my-list-group'));
}
public function testWithItemsViaClosureAndListGroupOptions()
{
/**
* It should generates:
*
* <div id="my-list-group" class="list-group">
* <a href="#" class="list-group-item">Cras justo odio</a>
* <a href="#" class="list-group-item">Dapibus ac facilisis in</a>
* <a href="#" class="list-group-item">Morbi leo risus</a>
* <a href="#" class="list-group-item">Porta ac consectetur ac</a>
* <a href="#" class="list-group-item">Vestibulum at eros</a>
* </div>
*/
$items = [
'Cras justo odio',
'Dapibus ac facilisis in',
'Morbi leo risus',
'Porta ac consectetur ac',
'Vestibulum at eros'
];
$list_group = new ListGroup(
['id'=>'my-list-group', 'linked'=>true],
function () use ($items) {
return $items;
}
);
$html = $list_group->getHtml();
$this->validateBasicListGroupStructure($html, count($items), true);
$this->assertTrue($html->hasAttribute('id', 'my-list-group'));
}
private function validateBasicListGroupStructure($html, $nItems, $linked=false)
{
if ($linked) {
$main_tag = 'div';
$item_tag = 'a';
} else {
$main_tag = 'ul';
$item_tag = 'li';
}
$this->assertTrue($html->isA($main_tag));
$this->assertTrue($html->hasAttribute('class', 'list-group'));
$this->assertEquals($nItems, $html->numberOfChildren());
foreach($html->getChildren() as $item) {
$this->assertTrue($item->isA($item_tag));
$this->assertTrue($item->hasAttribute('class', 'list-group-item'));
}
}
}