Skip to content

Commit 5d8c978

Browse files
author
Igor Chepurnoy
committed
update widget, add tests
1 parent b420b4c commit 5d8c978

File tree

11 files changed

+265
-48
lines changed

11 files changed

+265
-48
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore all test and documentation for archive
2+
/.gitattributes export-ignore
3+
/.gitignore export-ignore
4+
/.scrutinizer.yml export-ignore
5+
/.travis.yml export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/tests export-ignore
8+
/docs export-ignore

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
/composer.lock
19+
20+
# composer itself is not needed
21+
composer.phar
22+
23+
# Mac DS_Store Files
24+
.DS_Store
25+
26+
# phpunit itself is not needed
27+
phpunit.phar
28+
# local phpunit config
29+
/phpunit.xml
30+
31+
# local tests configuration
32+
/tests/data/config.local.php
33+
34+
# runtime cache
35+
/tests/runtime

.travis.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- hhvm
9+
10+
# run build against hhvm but allow them to fail
11+
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
12+
matrix:
13+
fast_finish: true
14+
allow_failures:
15+
- php: hhvm
16+
17+
# faster builds on new travis setup not using sudo
18+
sudo: false
19+
20+
# cache vendor dirs
21+
cache:
22+
directories:
23+
- $HOME/.composer/cache
24+
25+
install:
26+
- travis_retry composer self-update && composer --version
27+
- travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1"
28+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
29+
- travis_retry composer install --prefer-dist --no-interaction
30+
31+
script:
32+
- phpunit --verbose $PHPUNIT_FLAGS

IonSlider.php

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace yii2mod\slider;
44

5-
use yii\base\InvalidConfigException;
5+
use yii\helpers\ArrayHelper;
66
use yii\helpers\Html;
77
use yii\helpers\Json;
88
use yii\widgets\InputWidget;
@@ -14,34 +14,28 @@
1414
class IonSlider extends InputWidget
1515
{
1616
/**
17-
* @var array the HTML attributes for the input tag.
18-
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
17+
* Single type of the slider
1918
*/
20-
public $options = [];
19+
const TYPE_SINGLE = 'single';
2120

2221
/**
23-
* @var array plugin options
22+
* Double type of the slider
2423
*/
25-
public $pluginOptions = [];
24+
const TYPE_DOUBLE = 'double';
2625

2726
/**
28-
* Slider type - single, double
29-
* @var string
27+
* @var string the type of the slider. Defaults to `TYPE_SINGLE`
3028
*/
31-
public $type = 'single';
29+
public $type = self::TYPE_SINGLE;
3230

3331
/**
34-
* Initializes the object.
35-
* This method is invoked at the end of the constructor after the object is initialized with the
36-
* given configuration.
32+
* @var array plugin options
3733
*/
38-
public function init()
39-
{
40-
parent::init();
41-
}
34+
public $pluginOptions = [];
4235

4336
/**
4437
* Render range slider
38+
*
4539
* @return string|void
4640
*/
4741
public function run()
@@ -51,6 +45,7 @@ public function run()
5145
} else {
5246
echo Html::textInput($this->name, $this->value, $this->options);
5347
}
48+
5449
$this->registerAssets();
5550
}
5651

@@ -61,29 +56,19 @@ protected function registerAssets()
6156
{
6257
$view = $this->getView();
6358
IonSliderAsset::register($view);
64-
$js = '$("#' . $this->getInputId() . '").ionRangeSlider(' . $this->getPluginOptions() . ');';
59+
$js = '$("#' . $this->options['id'] . '").ionRangeSlider(' . $this->getPluginOptions() . ');';
6560
$view->registerJs($js, $view::POS_END);
6661
}
6762

6863
/**
6964
* Return plugin options in json format
65+
*
7066
* @return string
7167
*/
7268
public function getPluginOptions()
7369
{
74-
if (!isset($this->pluginOptions['type'])) {
75-
$this->pluginOptions['type'] = $this->type;
76-
}
77-
return Json::encode($this->pluginOptions);
78-
}
70+
$this->pluginOptions['type'] = ArrayHelper::getValue($this->pluginOptions, 'type', $this->type);
7971

80-
/**
81-
* Return select id
82-
* @return mixed
83-
* @throws InvalidConfigException
84-
*/
85-
public function getInputId()
86-
{
87-
return $this->options['id'];
72+
return Json::encode($this->pluginOptions);
8873
}
89-
}
74+
}

IonSliderAsset.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
class IonSliderAsset extends AssetBundle
1212
{
13-
1413
/**
1514
* @var string
1615
*/

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Once the extension is installed, simply add widget to your page as follows:
3131

3232
1) Usage with ActiveForm and model
3333
```php
34-
echo $form->field($model, "attribute")->widget(IonSlider::className(), [
34+
echo $form->field($model, "attribute")->widget(\yii2mod\slider\IonSlider::className(), [
3535
'pluginOptions' => [
3636
'min' => 0,
3737
'max' => 1,
@@ -46,22 +46,22 @@ echo $form->field($model, "attribute")->widget(IonSlider::className(), [
4646
```
4747
2) Usage without ActiveForm and model
4848
```php
49-
echo IonSlider::widget([
50-
'name' => "slider",
51-
'type' => "double",
52-
'pluginOptions' => [
53-
'min' => 0,
54-
'max' => 20,
55-
'from' => 2,
56-
'to' => 18,
57-
'step' => 1,
58-
'hide_min_max' => true,
59-
'hide_from_to' => true
60-
]
49+
echo \yii2mod\slider\IonSlider::widget([
50+
'name' => 'slider',
51+
'type' => \yii2mod\slider\IonSlider::TYPE_DOUBLE,
52+
'pluginOptions' => [
53+
'min' => 0,
54+
'max' => 20,
55+
'from' => 2,
56+
'to' => 18,
57+
'step' => 1,
58+
'hide_min_max' => true,
59+
'hide_from_to' => true
60+
]
6161
]);
6262

6363
```
64-
- To change the slider skin, you can configure the assetManager array in your application configuration:
64+
**To change the slider skin, you can configure the assetManager array in your application configuration:**
6565
```php
6666
'assetManager' => [
6767
'bundles' => [

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "yii2mod/yii2-ion-slider",
3-
"description": "Ion Slider widget based on Ion.RangeSlider extension {@link http://ionden.com/a/plugins/ion.rangeSlider/en.html)",
3+
"description": "Ion.RangeSlider Widget for Yii 2",
44
"type": "yii2-extension",
5-
"keywords": ["yii2", "module"],
5+
"keywords": ["yii2", "yii2 ion slider", "yii2 slider", "yii2 range slider"],
66
"license": "MIT",
77
"authors": [
88
{
@@ -18,5 +18,10 @@
1818
"psr-4": {
1919
"yii2mod\\slider\\": ""
2020
}
21+
},
22+
"extra": {
23+
"asset-installer-paths": {
24+
"bower-asset-library": "vendor/bower"
25+
}
2126
}
2227
}

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Yii2mod Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

tests/IonSliderTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace yii2mod\slider\tests;
4+
5+
use yii\base\DynamicModel;
6+
use yii\helpers\Html;
7+
use yii2mod\slider\IonSlider;
8+
9+
/**
10+
* Class IonSliderTest
11+
* @package yii2mod\slider\tests
12+
*/
13+
class IonSliderTest extends TestCase
14+
{
15+
public function testRenderSliderWithModel()
16+
{
17+
$model = new DynamicModel(['price' => 0]);
18+
19+
$widget = IonSlider::widget([
20+
'model' => $model,
21+
'attribute' => 'price',
22+
'pluginOptions' => [
23+
'min' => 0,
24+
'max' => 20,
25+
'from' => 2,
26+
'to' => 18,
27+
'step' => 1,
28+
'hide_min_max' => true,
29+
'hide_from_to' => true
30+
]
31+
]);
32+
33+
$this->assertEquals(Html::activeTextInput($model, 'price'), $widget);
34+
}
35+
36+
public function testRenderSliderWithoutModel()
37+
{
38+
$widget = IonSlider::widget([
39+
'name' => 'price',
40+
'type' => IonSlider::TYPE_DOUBLE,
41+
'pluginOptions' => [
42+
'min' => 0,
43+
'max' => 20,
44+
'from' => 2,
45+
'to' => 18,
46+
'step' => 1,
47+
]
48+
]);
49+
50+
$this->assertEquals(Html::textInput('price', null, ['id' => 'w0']), $widget);
51+
}
52+
}

tests/TestCase.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace yii2mod\slider\tests;
4+
5+
use yii\helpers\ArrayHelper;
6+
use Yii;
7+
use yii\helpers\FileHelper;
8+
9+
/**
10+
* This is the base class for all yii framework unit tests.
11+
*/
12+
class TestCase extends \PHPUnit_Framework_TestCase
13+
{
14+
protected function setUp()
15+
{
16+
parent::setUp();
17+
$this->mockApplication();
18+
$testFilePath = $this->getTestFilePath();
19+
FileHelper::createDirectory($testFilePath);
20+
}
21+
22+
protected function tearDown()
23+
{
24+
$this->destroyApplication();
25+
}
26+
27+
/**
28+
* Populates Yii::$app with a new application
29+
* The application will be destroyed on tearDown() automatically.
30+
* @param array $config The application configuration, if needed
31+
* @param string $appClass name of the application class to create
32+
*/
33+
protected function mockApplication($config = [], $appClass = '\yii\web\Application')
34+
{
35+
new $appClass(ArrayHelper::merge([
36+
'id' => 'testapp',
37+
'basePath' => __DIR__,
38+
'vendorPath' => $this->getVendorPath(),
39+
'components' => [
40+
'request' => [
41+
'hostInfo' => 'http://domain.com',
42+
'scriptUrl' => 'index.php'
43+
],
44+
'assetManager' => [
45+
'basePath' => $this->getTestFilePath(),
46+
],
47+
],
48+
], $config));
49+
}
50+
51+
/**
52+
* @return string vendor path
53+
*/
54+
protected function getVendorPath()
55+
{
56+
return dirname(__DIR__) . '/vendor';
57+
}
58+
59+
/**
60+
* Destroys application in Yii::$app by setting it to null.
61+
*/
62+
protected function destroyApplication()
63+
{
64+
Yii::$app = null;
65+
}
66+
67+
/**
68+
* Returns the test file path.
69+
* @return string file path.
70+
*/
71+
protected function getTestFilePath()
72+
{
73+
return dirname(__DIR__) . '/tests/runtime' . DIRECTORY_SEPARATOR . getmypid();
74+
}
75+
}

0 commit comments

Comments
 (0)