Файл: cobisja/BootHelp/tests/src/CarouselTest.php
Строк: 582
<?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 cobisjaBootHelpTests;
use cobisjaBootHelpCarousel;
use cobisjaBootHelpImage;
class CarouselTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider imagesFixtures
*/
public function testCarouselWithNoControls($images)
{
/**
* It should generates:
*
* <div data-ride="carousel" id="my-carousel" class="carousel slide">
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* </div>
*/
$carousel = new Carousel($images, ['id'=>'my-carousel', 'show_controls'=>false, 'show_indicators'=>false]);
$html = $carousel->getHtml();
$this->validateBasicCarouselStructure($html);
$items_container = $html->getChild(0);
$this->assertTrue(4 == $items_container->numberOfChildren());
$this->validateItemContainer($items_container);
}
/**
* @dataProvider imagesFixtures
*/
public function testCarouselWithOnlyIndicators($images)
{
/**
* It should generates:
*
* <div data-ride="carousel" id="my-carousel" class="carousel slide">
* <ol class="carousel-indicators">
* <li data-slide-to="0" data-target="#my-carousel" class="active"></li>
* <li data-slide-to="1" data-target="#my-carousel"></li>
* <li data-slide-to="2" data-target="#my-carousel"></li>
* <li data-slide-to="3" data-target="#my-carousel"></li>
* </ol>
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* </div>
*/
$carousel = new Carousel($images, ['id'=>'my-carousel', 'show_controls'=>false]);
$html = $carousel->getHtml();
$this->validateBasicCarouselStructure($html);
$this->assertNotTrue($html->hasAChildOfType('a', ['class'=>'carousel-control']));
$indicators_container = $html->getChild(0);
$items_container = $html->getChild(1);
$this->assertTrue($indicators_container->numberOfChildren() == $items_container->numberOfChildren());
$this->validateIndicatorsContainer($indicators_container, $html->getAttribute('id'));
}
/**
* @dataProvider imagesFixtures
*/
public function testCarouselWithOnlyControls($images)
{
/**
* It should generates:
*
* <div data-ride="carousel" id="my-carousel" class="carousel slide">
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* <a data-slide="prev" role="button" href="#my-carousel" class="left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="#my-carousel" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel($images, ['id'=>'my-carousel', 'show_indicators'=>false]);
$html = $carousel->getHtml();
$this->validateBasicCarouselStructure($html);
$this->assertNotTrue($html->hasAChildOfType('ol', ['class'=>'carousel-indicators']));
$control_left = $html->getChild(1);
$control_right = $html->getChild(2);
$this->assertTrue($control_left->isA('a', ['class'=>'left carousel-control']));
$this->assertTrue($control_right->isA('a', ['class'=>'right carousel-control']));
}
/**
* @dataProvider imagesFixtures
*/
public function testStandardCarousel($images)
{
/**
* It should generates:
*
* <div data-ride="carousel" id="my-carousel" class="carousel slide">
* <ol class="carousel-indicators">
* <li data-slide-to="0" data-target="#my-carousel" class="active"></li>
* <li data-slide-to="1" data-target="#my-carousel"></li>
* <li data-slide-to="2" data-target="#my-carousel"></li>
* <li data-slide-to="3" data-target="#my-carousel"></li>
* </ol>*
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* <a data-slide="prev" role="button" href="#my-carousel" class="left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="#my-carousel" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel($images, ['id'=>'my-carousel']);
$html = $carousel->getHtml();
$this->validateBasicCarouselStructure($html);
$this->assertTrue($html->hasAChildOfType('ol', ['class'=>'carousel-indicators']));
$this->assertTrue($html->hasAChildOfType('div', ['class'=>'carousel-inner']));
$this->assertTrue($html->getChild(2)->isA('a', ['class'=>'left carousel-control']));
$this->assertTrue($html->getChild(3)->isA('a', ['class'=>'right carousel-control']));
}
/**
* @dataProvider imagesFixtures
*/
public function testStandardCarouselWithNoOptions($images)
{
/**
* It should generates:
*
* <div data-ride="carousel" id="carousel-{random-id}" class="carousel slide">
* <ol class="carousel-indicators">
* <li data-slide-to="0" data-target="carousel-{random-id}" class="active"></li>
* <li data-slide-to="1" data-target="carousel-{random-id}"></li>
* <li data-slide-to="2" data-target="carousel-{random-id}"></li>
* <li data-slide-to="3" data-target="carousel-{random-id}"></li>
* </ol>*
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* <a data-slide="prev" role="button" href="carousel-{random-id}" class="left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="carousel-{random-id}" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel($images);
$html = $carousel->getHtml();
$carousel_id = $html->getAttribute('id');
$this->validateBasicCarouselStructure($html);
$this->validateIndicatorsContainer($html->getChild(0), $carousel_id);
$this->validateItemContainer($html->getChild(1));
$this->validateControls(['left'=>$html->getChild(2), 'right'=>$html->getChild(3)], $carousel_id);
}
public function testStandardCarouselWithClosureAndNoOptions()
{
/**
* It should generates:
*
* <div data-ride="carousel" id="carousel-{random-id}" class="carousel slide">
* <ol class="carousel-indicators">
* <li data-slide-to="0" data-target="carousel-{random-id}" class="active"></li>
* <li data-slide-to="1" data-target="carousel-{random-id}"></li>
* <li data-slide-to="2" data-target="carousel-{random-id}"></li>
* <li data-slide-to="3" data-target="carousel-{random-id}"></li>
* </ol>*
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* <a data-slide="prev" role="button" href="carousel-{random-id}" class="left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="carousel-{random-id}" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel(
function () {
return [
new Image(['src'=>'img1.jpg']),
new Image(['src'=>'img2.jpg']),
new Image(['src'=>'img3.jpg']),
new Image(['src'=>'img4.jpg'])
];
}
);
$html = $carousel->getHtml();
$carousel_id = $html->getAttribute('id');
$this->validateBasicCarouselStructure($html);
$this->validateIndicatorsContainer($html->getChild(0), $carousel_id);
$this->validateItemContainer($html->getChild(1));
$this->validateControls(['left'=>$html->getChild(2), 'right'=>$html->getChild(3)], $carousel_id);
}
public function testStandardCarouselWithClosureAndOptions()
{
/**
* It should generates:
*
* <div data-ride="carousel" id="carousel-{random-id}" class="carousel slide">
* <ol class="carousel-indicators">
* <li data-slide-to="0" data-target="carousel-{random-id}" class="active"></li>
* <li data-slide-to="1" data-target="carousel-{random-id}"></li>
* <li data-slide-to="2" data-target="carousel-{random-id}"></li>
* <li data-slide-to="3" data-target="carousel-{random-id}"></li>
* </ol>*
* <div role="listbox" class="carousel-inner">
* <div class="item active"><img alt="img1" src="img1.jpg"></div>
* <div class="item"><img alt="img2" src="img2.jpg"></div>
* <div class="item"><img alt="img3" src="img3.jpg"></div>
* <div class="item"><img alt="img4" src="img4.jpg"></div>
* </div>
* <a data-slide="prev" role="button" href="carousel-{random-id}" class="left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="carousel-{random-id}" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel(
['id'=>'my-carousel'],
function () {
return [
new Image(['src'=>'img1.jpg']),
new Image(['src'=>'img2.jpg']),
new Image(['src'=>'img3.jpg']),
new Image(['src'=>'img4.jpg'])
];
}
);
$html = $carousel->getHtml();
$carousel_id = $html->getAttribute('id');
$this->validateBasicCarouselStructure($html);
$this->validateIndicatorsContainer($html->getChild(0), $carousel_id);
$this->validateItemContainer($html->getChild(1));
$this->validateControls(['left'=>$html->getChild(2), 'right'=>$html->getChild(3)], $carousel_id);
}
public function testStandardCarouselWithCustomizedItems()
{
/**
* It should generates:
*
* <div data-ride="carousel" id="carousel-{random-id}" class="carousel slide">
* <ol class="carousel-indicators">
* <li data-slide-to="0" data-target="carousel-{random-id}" class="active"></li>
* <li data-slide-to="1" data-target="carousel-{random-id}"></li>
* <li data-slide-to="2" data-target="carousel-{random-id}"></li>
* <li data-slide-to="3" data-target="carousel-{random-id}"></li>
* </ol>*
* <div role="listbox" class="carousel-inner">
* <div class="item active">
* <img alt="img1" src="img1.jpg">
* <div class="carousel-caption>
* <h3>First slide label</h3>
* <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
* </div>
* </div>
* <div class="item">
* <img alt="img2" src="img2.jpg">
* <div class="carousel-caption">
* <h2>A simple customized caption</h2>
* </div>
* </div>
* <div class="item">
* <img alt="img3" src="img3.jpg">
* </div>
* <div class="item">
* <img alt="img4" src="img4.jpg">
* <div class="carousel-caption">
* <p>Lorem ipsum dolor sit ame</p>
* </div>
* </div>
* </div>
* <a data-slide="prev" role="button" href="carousel-{random-id}" class="left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="carousel-{random-id}" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel(
['id'=>'my-carousel'],
function () {
return [
[new Image(['src'=>'img1.jpg']), 'caption'=>['title'=>'First slide label', 'content'=>'Nulla vitae elit libero, a pharetra augue mollis interdum.']],
[new Image(['src'=>'img2.jpg']), 'caption'=>function (){return '<h2>A simple customized caption</h2>';}],
[new Image(['src'=>'img3.jpg'])],
[new Image(['src'=>'img4.jpg']), 'caption'=>'Lorem ipsum dolor sit ame']
];
}
);
$html = $carousel->getHtml();
$item_container = $html->getChild(1);
$this->validateBasicCarouselStructure($html);
$this->validateItemContainer($item_container);
$first_item = $item_container->getChild(0);
$second_item = $item_container->getChild(1);
$third_item = $item_container->getChild(2);
$fourth_item = $item_container->getChild(3);
// Validates first item.
$this->assertTrue(2 == $first_item->numberOfChildren());
$item_caption = $first_item->getChild(1);
$this->assertTrue($item_caption->isA('div', ['class'=>'carousel-caption']));
$this->assertTrue(2 == $item_caption->numberOfChildren());
$this->assertTrue($item_caption->getChild(0)->isA('h3'));
$this->assertTrue($item_caption->getChild(1)->isA('p'));
// Validates second item.
$this->assertTrue(2 == $second_item->numberOfChildren());
$item_caption = $second_item->getChild(1);
$this->assertTrue($item_caption->isA('div', ['class'=>'carousel-caption']));
$this->assertTrue(1 == $item_caption->numberOfChildren());
$this->assertEquals('<h2>A simple customized caption</h2>', $item_caption->getChild(0));
// Validates third item.
$this->assertTrue(1 == $third_item->numberOfChildren());
$this->assertNotTrue($third_item->hasAChildOfType('div', ['class'=>'carousel-caption']));
// Validates fourh item.
$this->assertTrue(2 == $fourth_item->numberOfChildren());
$item_caption = $fourth_item->getChild(1);
$this->assertTrue(1 == $item_caption->numberOfChildren());
$this->assertNotTrue($item_caption->hasAChildOfType('h3'));
$this->assertTrue($item_caption->hasAChildOfType('p'));
}
public function testStandardCarouselWithCustomizedOptions()
{
/**
* It should generates:
*
* <div data-ride="carousel" id="my-carousel" class="en carousel slide">
* <ol data-js="1" class="en-indicator carousel-indicators">
* <li class="active" data-slide-to="0" data-target="#my-carousel"></li>
* <li data-slide-to="1" data-target="#my-carousel" class=""></li>
* <li data-slide-to="2" data-target="#my-carousel" class=""></li>
* <li data-slide-to="3" data-target="#my-carousel" class=""></li>
* </ol>
* <div role="listbox" id="my-items" class="en-content carousel-inner">
* <div class="item active">
* <img alt="pet1" src="pet1.jpg">
* </div>
* <div class="item">
* <img alt="pet2" src="pet2.jpg">
* </div>
* <div class="item">
* <img alt="pet3" src="pet3.jpg">
* </div>
* <div class="item">
* <img alt="pet4" src="pet4.jpg">
* </div>
* </div>
* <a data-slide="prev" role="button" href="#my-carousel" class="en-control left carousel-control">
* <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
* <span class="sr-only">Previous</span>
* </a>
* <a data-slide="next" role="button" href="#my-carousel" class="right carousel-control">
* <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
* <span class="sr-only">Next</span>
* </a>
* </div>
*/
$carousel = new Carousel(
[
new Image(['src'=>'Guide/img/pet1.jpg']),
new Image(['src'=>'Guide/img/pet2.jpg']),
new Image(['src'=>'Guide/img/pet3.jpg']),
new Image(['src'=>'Guide/img/pet4.jpg'])
],
['id'=>'my-carousel', 'class'=>'en',
'indicators_options'=>['class'=>'en-indicator', 'data-js'=>1],
'content_options'=>['class'=>'en-content', 'id'=>'my-items'],
'controls_options'=>['left'=>['class'=>'en-control']]
]
);
$html = $carousel->getHtml();
$carousel_id = $html->getAttribute('id');
$indicator_container = $html->getChild(0);
$item_container = $html->getChild(1);
$left_control = $html->getChild(2);
$right_control = $html->getChild(3);
$this->validateIndicatorsContainer($indicator_container, $carousel_id);
$this->validateItemContainer($item_container);
$this->validateControls(['left'=>$left_control, 'right'=>$right_control], $carousel_id);
$this->assertTrue($html->hasAttribute('class', 'en'));
$this->assertTrue($html->hasAttribute('id', $carousel_id));
$this->assertTrue($indicator_container->hasAttribute('class', 'en-indicator'));
$this->assertTrue($indicator_container->hasAttribute('data-js', 1));
$this->assertTrue($item_container->hasAttribute('class', 'en-content'));
$this->assertTrue($item_container->hasAttribute('id', "my-items"));
$this->assertTrue($left_control->hasAttribute('class', 'en-control'));
}
private function validateBasicCarouselStructure($html)
{
$this->assertTrue($html->isA('div', ['data-ride'=>'carousel']));
$this->assertTrue($html->hasAttribute('class', 'carousel slide'));
$this->assertTrue($html->hasAChildOfType('div', ['class'=>'carousel-inner']));
}
private function validateIndicatorsContainer($indicators_container, $carousel_id)
{
$this->assertTrue($indicators_container->isA('ol'));
$this->assertTrue($indicators_container->hasAttribute('class', 'carousel-indicators'));
$this->assertTrue($indicators_container->hasAChildOfType('li', ['class'=>'active']));
$indicators = $indicators_container->getChildren();
$indicator_id = 0;
foreach ($indicators as $indicator) {
$this->assertTrue($indicator->isA('li'));
$this->assertTrue($indicator->hasAttribute('data-target', "#$carousel_id"));
$this->assertTrue($indicator->hasAttribute('data-slide-to', (string)$indicator_id));
$indicator_id++;
}
}
private function validateItemContainer($items_container)
{
$this->assertTrue($items_container->hasAChildOfType('div', ['class'=>'item active']));
$items = $items_container->getChildren();
foreach ($items as $item) {
$this->assertTrue($item->isA('div'));
$this->assertTrue($item->hasAttribute('class', 'item'));
}
}
private function validateControls($controls, $carousel_id)
{
foreach ($controls as $type => $control) {
$this->assertTrue($control->isA('a'));
$this->assertTrue($control->hasAttribute('class', [$type, 'carousel-control']));
$this->assertTrue($control->hasAttribute('href', '#' . $carousel_id));
}
}
public function imagesFixtures()
{
return [[[
new Image(['src'=>'img1.jpg']),
new Image(['src'=>'img2.jpg']),
new Image(['src'=>'img3.jpg']),
new Image(['src'=>'img4.jpg'])
]]];
}
}