Browse Source

added unit tests

tags/2.0.4
Carsten Brandt 10 years ago
parent
commit
90e3df6da4
  1. 2
      .gitignore
  2. 48
      .travis.yml
  3. 7
      README.md
  4. 4
      composer.json
  5. 13
      phpunit.xml.dist
  6. 31
      tests/ButtonDropdownTest.php
  7. 74
      tests/CollapseTest.php
  8. 50
      tests/DropdownTest.php
  9. 50
      tests/NavTest.php
  10. 74
      tests/TabsTest.php
  11. 83
      tests/TestCase.php
  12. 2
      tests/assets/.gitignore
  13. 15
      tests/bootstrap.php

2
.gitignore vendored

@ -0,0 +1,2 @@
/vendor
/composer.lock

48
.travis.yml

@ -0,0 +1,48 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- hhvm-nightly
# run build against hhvm but allow them to fail
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
matrix:
fast_finish: true
allow_failures:
- php: hhvm-nightly
- php: 7.0
# faster builds on new travis setup not using sudo
sudo: false
# cache vendor dirs
cache:
directories:
- vendor
- $HOME/.composer/cache
install:
- travis_retry composer self-update && composer --version
- travis_retry composer global require "fxp/composer-asset-plugin:1.0.0"
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction
before_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
fi
script:
- phpunit --verbose $PHPUNIT_FLAGS
after_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
travis_retry wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi

7
README.md

@ -1,12 +1,17 @@
Twitter Bootstrap Extension for Yii 2
=====================================
This is the Twitter Bootstrap extension for Yii 2. It encapsulates [Bootstrap](http://getbootstrap.com/) components
This is the Twitter Bootstrap extension for [Yii framework 2.0](http://www.yiiframework.com). It encapsulates [Bootstrap](http://getbootstrap.com/) components
and plugins in terms of Yii widgets, and thus makes using Bootstrap components/plugins
in Yii applications extremely easy.
For license information check the [LICENSE](LICENSE.md)-file.
[![Latest Stable Version](https://poser.pugx.org/yiisoft/yii2-bootstrap/v/stable.png)](https://packagist.org/packages/yiisoft/yii2-bootstrap)
[![Total Downloads](https://poser.pugx.org/yiisoft/yii2-bootstrap/downloads.png)](https://packagist.org/packages/yiisoft/yii2-bootstrap)
[![Build Status](https://travis-ci.org/yiisoft/yii2-bootstrap.svg?branch=master)](https://travis-ci.org/yiisoft/yii2-bootstrap)
Installation
------------

4
composer.json

@ -29,6 +29,10 @@
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
},
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
}

13
phpunit.xml.dist

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>

31
tests/ButtonDropdownTest.php

@ -0,0 +1,31 @@
<?php
namespace yiiunit\extensions\bootstrap;
use yii\bootstrap\ButtonDropdown;
/**
* @group bootstrap
*/
class ButtonDropdownTest extends TestCase
{
public function testContainerOptions()
{
$containerClass = "dropup";
ButtonDropdown::$counter = 0;
$out = ButtonDropdown::widget([
'containerOptions' => [
'class' => $containerClass,
],
'label' => 'Action',
'dropdown' => [
'items' => [
['label' => 'DropdownA', 'url' => '/'],
['label' => 'DropdownB', 'url' => '#'],
],
],
]);
$this->assertContains("$containerClass btn-group", $out);
}
}

74
tests/CollapseTest.php

@ -0,0 +1,74 @@
<?php
namespace yiiunit\extensions\bootstrap;
use yii\bootstrap\Collapse;
/**
* @group bootstrap
*/
class CollapseTest extends TestCase
{
public function testRender()
{
Collapse::$counter = 0;
$output = Collapse::widget([
'items' => [
[
'label' => 'Collapsible Group Item #1',
'content' => 'test content1',
],
[
'label' => '<h1>Collapsible Group Item #2</h1>',
'content' => '<h2>test content2</h2>',
'contentOptions' => [
'class' => 'testContentOptions2'
],
'options' => [
'class' => 'testClass2',
'id' => 'testId2'
],
'encode' => true
],
[
'label' => '<h1>Collapsible Group Item #3</h1>',
'content' => '<h2>test content3</h2>',
'contentOptions' => [
'class' => 'testContentOptions3'
],
'options' => [
'class' => 'testClass3',
'id' => 'testId3'
],
'encode' => false
],
[
'label' => '<h1>Collapsible Group Item #4</h1>',
'content' => '<h1>test content4</h1>',
],
]
]);
$this->assertEqualsWithoutLE(<<<HTML
<div id="w0" class="panel-group">
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse1" data-toggle="collapse" data-parent="#w0">Collapsible Group Item #1</a>
</h4></div>
<div id="w0-collapse1" class="panel-collapse collapse"><div class="panel-body">test content1</div>
</div></div>
<div id="testId2" class="testClass2 panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse2" data-toggle="collapse" data-parent="#w0">&lt;h1&gt;Collapsible Group Item #2&lt;/h1&gt;</a>
</h4></div>
<div id="w0-collapse2" class="testContentOptions2 panel-collapse collapse"><div class="panel-body"><h2>test content2</h2></div>
</div></div>
<div id="testId3" class="testClass3 panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse3" data-toggle="collapse" data-parent="#w0"><h1>Collapsible Group Item #3</h1></a>
</h4></div>
<div id="w0-collapse3" class="testContentOptions3 panel-collapse collapse"><div class="panel-body"><h2>test content3</h2></div>
</div></div>
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse4" data-toggle="collapse" data-parent="#w0">&lt;h1&gt;Collapsible Group Item #4&lt;/h1&gt;</a>
</h4></div>
<div id="w0-collapse4" class="panel-collapse collapse"><div class="panel-body"><h1>test content4</h1></div>
</div></div>
</div>
HTML
, $output);
}
}

50
tests/DropdownTest.php

@ -0,0 +1,50 @@
<?php
namespace yiiunit\extensions\bootstrap;
use yii\bootstrap\Dropdown;
/**
* Tests for Dropdown widget
*
* @group bootstrap
*/
class DropdownTest extends TestCase
{
public function testIds()
{
Dropdown::$counter = 0;
$out = Dropdown::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 = <<<EXPECTED
<ul id="w0" class="dropdown-menu"><li class="dropdown-header">Page1</li>
<li class="dropdown-submenu"><a href="#" tabindex="-1">Dropdown1</a><ul class="dropdown-menu"><li class="dropdown-header">Page2</li>
<li class="dropdown-header">Page3</li></ul></li></ul>
EXPECTED;
$this->assertEqualsWithoutLE($expected, $out);
}
}

50
tests/NavTest.php

@ -0,0 +1,50 @@
<?php
namespace yiiunit\extensions\bootstrap;
use yii\bootstrap\Nav;
/**
* Tests for Nav widget
*
* @group bootstrap
*/
class NavTest extends TestCase
{
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 = <<<EXPECTED
<ul id="w0" class="nav"><li><a href="#">Page1</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Dropdown1 <b class="caret"></b></a><ul id="w1" class="dropdown-menu"><li class="dropdown-header">Page2</li>
<li class="dropdown-header">Page3</li></ul></li></ul>
EXPECTED;
$this->assertEqualsWithoutLE($expected, $out);
}
}

74
tests/TabsTest.php

@ -0,0 +1,74 @@
<?php
namespace yiiunit\extensions\bootstrap;
use yii\bootstrap\Tabs;
/**
* Tests for Tabs widget
*
* @group bootstrap
*/
class TabsTest extends TestCase
{
/**
* Each tab should have a corresponding unique ID
*
* @see https://github.com/yiisoft/yii2/issues/6150
*/
public function testIds()
{
Tabs::$counter = 0;
$out = Tabs::widget([
'items' => [
[
'label' => 'Page1', 'content' => 'Page1',
],
[
'label' => 'Dropdown1',
'items' => [
['label' => 'Page2', 'content' => 'Page2'],
['label' => 'Page3', 'content' => 'Page3'],
]
],
[
'label' => 'Dropdown2',
'items' => [
['label' => 'Page4', 'content' => 'Page4'],
['label' => 'Page5', 'content' => 'Page5'],
]
]
]
]);
$page1 = 'w0-tab0';
$page2 = 'w0-dd1-tab0';
$page3 = 'w0-dd1-tab1';
$page4 = 'w0-dd2-tab0';
$page5 = 'w0-dd2-tab1';
$shouldContain = [
'w0', // nav widget container
"#$page1", // Page1
'w1', // Dropdown1
"$page2", // Page2
"$page3", // Page3
'w2', // Dropdown2
"#$page4", // Page4
"#$page5", // Page5
// containers
"id=\"$page1\"",
"id=\"$page2\"",
"id=\"$page3\"",
"id=\"$page4\"",
"id=\"$page5\"",
];
foreach ($shouldContain as $string) {
$this->assertContains($string, $out);
}
}
}

83
tests/TestCase.php

@ -0,0 +1,83 @@
<?php
namespace yiiunit\extensions\bootstrap;
use yii\di\Container;
use yii\helpers\ArrayHelper;
use Yii;
/**
* This is the base class for all yii framework unit tests.
*/
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockWebApplication();
}
/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown()
{
parent::tearDown();
$this->destroyApplication();
}
/**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed
* @param string $appClass name of the application class to create
*/
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => dirname(__DIR__) . '/vendor',
], $config));
}
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => dirname(__DIR__) . '/vendor',
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ .'/index.php',
'scriptUrl' => '/index.php',
],
]
], $config));
}
/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
Yii::$app = null;
Yii::$container = new Container();
}
/**
* Asserting two strings equality ignoring line endings
*
* @param string $expected
* @param string $actual
*/
public function assertEqualsWithoutLE($expected, $actual)
{
$expected = str_replace("\r\n", "\n", $expected);
$actual = str_replace("\r\n", "\n", $actual);
$this->assertEquals($expected, $actual);
}
}

2
tests/assets/.gitignore vendored

@ -0,0 +1,2 @@
*
!.gitignore

15
tests/bootstrap.php

@ -0,0 +1,15 @@
<?php
// ensure we get report on all possible php errors
error_reporting(-1);
define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
Yii::setAlias('@yiiunit/extensions/bootstrap', __DIR__);
Yii::setAlias('@yii/bootstrap', dirname(__DIR__));
Loading…
Cancel
Save