Skip to content

Commit ead4893

Browse files
committed
Update tests
1 parent 0e38819 commit ead4893

File tree

5 files changed

+212
-24
lines changed

5 files changed

+212
-24
lines changed

src/Pell.php

+52-21
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Pell extends PellInputWidget
7373
*/
7474
public $clientOptions = [];
7575

76-
private $clientPluginInstance = 'pell';
76+
protected $clientPluginInstanceName = 'pell';
7777

7878
/**
7979
* Initializes the Pell widget.
@@ -86,13 +86,13 @@ public function init()
8686

8787
//Attribute id in textarea must be set
8888
if(!isset($this->inputOptions['id']) && true === $this->asFormPart){
89-
$pell = $this->clientPluginInstance;
89+
$pell = $this->clientPluginInstanceName;
9090
$this->inputOptions['id'] = "$pell-textarea-" . $this->getId();
9191
}
9292

9393
//In Html::textarea attribute name must be set
9494
if(!$this->name && true === $this->asFormPart && false === $this->hasModel()){
95-
throw new InvalidParamException("Param 'name' must be specified.");
95+
throw new InvalidParamException("Param 'name' must be specified for textarea attribute name.");
9696
}
9797

9898
if(!isset($this->wrapperOptions['tag'])){
@@ -126,8 +126,8 @@ public function init()
126126
public function run()
127127
{
128128
echo $this->renderWidget() . "\n";
129-
130-
$this->registerClientScript();
129+
$js = $this->buildClientScript();
130+
$this->registerClientScript($js);
131131
}
132132

133133
/**
@@ -143,23 +143,21 @@ protected function renderWidget()
143143
$content = $this->getTextArea();
144144
}
145145

146-
$tag = $this->wrapperOptions['tag'];
146+
$tag = $this->getWrapperTag();
147147
ArrayHelper::remove($this->wrapperOptions, 'tag');
148148

149149
return Html::tag($tag, $content, $this->wrapperOptions);
150150
}
151151

152152
/**
153-
* Registers Pell js plugin
154-
* @return void
153+
* Build js script
154+
*
155+
* @return string
155156
*/
156-
protected function registerClientScript()
157+
protected function buildClientScript()
157158
{
158159
$js = [];
159-
$view = $this->getView();
160-
PellAsset::register($view);
161160
$wrapperId = $this->getWrapperId();
162-
163161
$element = new JsExpression("document.getElementById('$wrapperId')");
164162

165163
$this->clientOptions['element'] = $element;
@@ -169,19 +167,20 @@ protected function registerClientScript()
169167
$textAreaId = $this->getTextAreaId();
170168
//Write content from editor to hidden texarea when that was changed
171169
$this->clientOptions['onChange'] = new JsExpression(
172-
"html => {
173-
document.getElementById('$textAreaId').innerHTML = html;
174-
},"
170+
"html => {
171+
document.getElementById('$textAreaId').innerHTML = html;
172+
},"
175173
);
176174
}
177175

178176
$clientOptions = Json::encode($this->clientOptions);
179-
$pell = $this->clientPluginInstance;
177+
$pell = $this->clientPluginInstanceName;
180178

181179
//Editor js instance constant name
182180
$editorJsVar = "{$pell}Editor_" . $this->getId();
183181

184182
//Init plugin javascript
183+
$js[] = "";//set \n
185184
$js[] = "const $editorJsVar = $pell.init($clientOptions);";
186185

187186
//If isset default value like value from db, or if set `$this->value`
@@ -191,8 +190,25 @@ protected function registerClientScript()
191190
//Pass value to editor
192191
$js[] = new JsExpression("$editorJsVar.content.innerHTML = `$defVal`");
193192
}
194-
195-
$view->registerJs(implode("\n", $js), View::POS_END);
193+
$js[] = "";//set \n
194+
195+
return implode("\n", $js);
196+
}
197+
198+
/**
199+
* Registers Pell js plugin
200+
* @param string $js the JS code block to be registered
201+
* @param string $key the key that identifies the JS code block. If null, it will use
202+
* $js as the key. If two JS code blocks are registered with the same key, the latter
203+
* will overwrite the former.
204+
*
205+
* @return void
206+
*/
207+
protected function registerClientScript($js, $key = null)
208+
{
209+
$view = $this->getView();
210+
PellAsset::register($view);
211+
$view->registerJs($js, View::POS_END, $key);
196212
}
197213

198214
/**
@@ -219,22 +235,37 @@ protected function getTextArea()
219235
*
220236
* @return string
221237
*/
222-
protected function getWrapperId()
238+
public function getWrapperId()
223239
{
224-
$pell = $this->clientPluginInstance;
240+
$pell = $this->clientPluginInstanceName;
225241
return "$pell-wrap-". $this->getId();
226242
}
227243

244+
public function getWrapperClass()
245+
{
246+
return $this->wrapperOptions['class'];
247+
}
248+
228249
/**
229250
* Return a textarea attribute id
230251
*
231252
* @return string
232253
*/
233-
protected function getTextAreaId()
254+
public function getTextAreaId()
234255
{
235256
return $this->inputOptions['id'];
236257
}
237258

259+
/**
260+
* Return wrapper tag name
261+
*
262+
* @return string
263+
*/
264+
public function getWrapperTag()
265+
{
266+
return $this->wrapperOptions['tag'];
267+
}
268+
238269
/**
239270
* Return default value returned of model attribute if exists or by passed to `inputOptions['value']`
240271
*

tests/PellTest.php

+132
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use coderius\pell\Pell;
1212
use tests\models\Post;
1313
use yii\web\View;
14+
use tests\overrides\TestPell;
15+
use yii\base\InvalidParamException;
16+
use yii\web\JsExpression;
1417
use Yii;
1518

1619
class PellTest extends TestCase
@@ -46,4 +49,133 @@ public function testRenderWithActiveForm()
4649
$expected = file_get_contents(__DIR__ . '/data/test-form-wich-widget.bin');
4750
$this->assertEqualsWithoutLE($expected, $actual);
4851
}
52+
53+
public function testBuildClientScriptMethod()
54+
{
55+
$key = 'test-pell-js';
56+
$model = new Post();
57+
$widget = TestPell::begin(
58+
[
59+
'model' => $model,
60+
'attribute' => 'message',
61+
]
62+
);
63+
$view = $this->getView();
64+
$widget->setView($view);
65+
$widget->setId('w10');
66+
67+
$class = new \ReflectionClass('tests\\overrides\\TestPell');
68+
$js = $class->getMethod('buildClientScript');
69+
$js->setAccessible(true);
70+
$js = $js->invoke($widget);
71+
72+
$test = <<<JS
73+
74+
const pellEditor_w10 = pell.init({"element":document.getElementById('pell-wrap-w10'),"onChange":html => {
75+
document.getElementById('post-message').innerHTML = html;
76+
},});
77+
78+
JS;
79+
$this->assertEquals($test, $js);
80+
}
81+
82+
public function testRegisterClientScriptMethod()
83+
{
84+
$key = 'test-pell-js';
85+
$model = new Post();
86+
$widget = TestPell::begin(
87+
[
88+
'model' => $model,
89+
'attribute' => 'message',
90+
]
91+
);
92+
$view = $this->getView();
93+
$widget->setView($view);
94+
$widget->setId('w10');
95+
96+
$class = new \ReflectionClass('tests\\overrides\\TestPell');
97+
$js = $class->getMethod('buildClientScript');
98+
$js->setAccessible(true);
99+
$js = $js->invoke($widget);
100+
101+
$method = $class->getMethod('registerClientScript');
102+
$method->setAccessible(true);
103+
$method = $method->invokeArgs($widget, [$js, $key]);
104+
105+
$test = <<<JS
106+
107+
const pellEditor_w10 = pell.init({"element":document.getElementById('pell-wrap-w10'),"onChange":html => {
108+
document.getElementById('post-message').innerHTML = html;
109+
},});
110+
111+
JS;
112+
$this->assertEquals($test, $view->js[View::POS_END][$key]);
113+
}
114+
115+
public function testInitTextareaWithoutName()
116+
{
117+
$this->expectException(InvalidParamException::class);
118+
119+
$widget = Pell::widget(
120+
[
121+
'value' => 'message',
122+
]
123+
);
124+
}
125+
126+
public function testInitTextareaWithOnChange()
127+
{
128+
$this->expectException(InvalidParamException::class);
129+
130+
$widget = Pell::widget(
131+
[
132+
'name' => 'attr_name',
133+
'value' => 'message',
134+
'clientOptions' =>[
135+
'onChange' => new JsExpression(
136+
"html => {
137+
console.log(html);
138+
},"
139+
)
140+
]
141+
]
142+
);
143+
}
144+
145+
public function testInitDefaultParams()
146+
{
147+
$widget = TestPell::begin(
148+
[
149+
'id' => 'w11',
150+
'name' => 'attr_name',
151+
'value' => 'message',
152+
]
153+
);
154+
$view = $this->getView();
155+
$widget->setView($view);
156+
$this->assertEquals('w11', $widget->getId());
157+
$this->assertEquals('pell-textarea-w11', $widget->getTextAreaId());
158+
$this->assertEquals('div', $widget->getWrapperTag());
159+
$this->assertEquals('pell-wrap-w11', $widget->getWrapperId());
160+
$this->assertEquals('form-control', $widget->getWrapperClass());
161+
}
162+
163+
public function testCantSetClientOptionsElement()
164+
{
165+
$this->expectException(InvalidParamException::class);
166+
167+
$widget = TestPell::begin(
168+
[
169+
'name' => 'attr_name',
170+
'value' => 'message',
171+
'clientOptions' =>[
172+
'element' => new JsExpression(
173+
"document.getElementById('someId')"
174+
)
175+
]
176+
]
177+
);
178+
179+
}
180+
49181
}

tests/TestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use yii\di\Container;
99
use yii\helpers\ArrayHelper;
1010
use yii\web\Controller;
11+
use yii\web\View;
12+
use yii\web\AssetManager;
1113
/**
1214
* This is the base class for all yii framework unit tests.
1315
*/

tests/data/test-form-wich-widget.bin

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
<script src="/54622894/jquery.js?v=1573333595"></script>
2121
<script src="/ffe44444/yii.js?v=1573333595"></script>
2222
<script src="/ffe44444/yii.activeForm.js?v=1573333595"></script>
23-
<script>const pellEditor_w3 = pell.init({"element":document.getElementById('pell-wrap-w3'),"onChange":html => {
24-
document.getElementById('post-message').innerHTML = html;
25-
},});</script>
23+
<script>
24+
const pellEditor_w3 = pell.init({"element":document.getElementById('pell-wrap-w3'),"onChange":html => {
25+
document.getElementById('post-message').innerHTML = html;
26+
},});
27+
</script>
2628
<script>jQuery(function ($) {
2729
jQuery('#w2').yiiActiveForm([], []);
2830
});</script></body>

tests/overrides/TestPell.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace tests\overrides;
4+
5+
use yii\helpers\Json;
6+
use yii\web\View;
7+
use yii\web\JsExpression;
8+
use coderius\pell\Pell;
9+
use coderius\pell\PellAsset;
10+
11+
class TestPell extends Pell{
12+
13+
/**
14+
* Override registerClientScript()
15+
* @return void
16+
*/
17+
protected function registerClientScript($js, $key = null)
18+
{
19+
parent::registerClientScript($js, $key);
20+
}
21+
}

0 commit comments

Comments
 (0)