Completed
Push — master ( 4a31b6...a9f978 )
by Dmitry
15:45
created

JsonEditorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 2
cbo 8
dl 0
loc 114
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A assetProvider() 0 6 1
A testAsset() 0 19 1
A assetProductionProvider() 0 6 1
A testAssetProduction() 0 12 2
A testEditorWidget() 0 22 1
A testEditorActiveWidgetAndDefaults() 0 13 1
1
<?php
2
3
namespace kdn\yii2;
4
5
use kdn\yii2\assets\JsonEditorFullAsset;
6
use kdn\yii2\assets\JsonEditorMinimalistAsset;
7
use kdn\yii2\mocks\ModelMock;
8
use Yii;
9
use yii\widgets\ActiveForm;
10
11
/**
12
 * Class JsonEditorTest.
13
 * @package kdn\yii2
14
 */
15
class JsonEditorTest extends TestCase
16
{
17
    public static function assetProvider()
18
    {
19
        return [
20
            'development' => ['jsoneditor.min.css', 'jsoneditor-minimalist.min.js', 'jsoneditor.min.js'],
21
        ];
22
    }
23
24
    /**
25
     * @param string $css
26
     * @param string $minimalistJs
27
     * @param string $fullJs
28
     * @covers       \kdn\yii2\assets\JsonEditorAsset
29
     * @covers       \kdn\yii2\JsonEditor
30
     * @dataProvider assetProvider
31
     * @small
32
     */
33
    public function testAsset($css, $minimalistJs, $fullJs)
34
    {
35
        $testWidgetAsset = function ($config, $assetName, $css, $js) {
36
            JsonEditor::widget($config);
37
            $bundles = Yii::$app->assetManager->bundles;
38
            $this->assertArrayHasKey($assetName, $bundles);
39
            $this->assertEquals([$css], $bundles[$assetName]->css);
40
            $this->assertEquals([$js], $bundles[$assetName]->js);
41
        };
42
43
        $fullAssetName = JsonEditorFullAsset::className();
44
        $minimalistAssetName = JsonEditorMinimalistAsset::className();
45
46
        $testWidgetAsset(['name' => 'data'], $minimalistAssetName, $css, $minimalistJs);
47
        static::mockWebApplication();
48
        $testWidgetAsset(['name' => 'data', 'clientOptions' => ['mode' => 'code']], $fullAssetName, $css, $fullJs);
49
        static::mockWebApplication();
50
        $testWidgetAsset(['name' => 'data', 'clientOptions' => ['modes' => ['code']]], $fullAssetName, $css, $fullJs);
51
    }
52
53
    public static function assetProductionProvider()
54
    {
55
        return [
56
            'production' => ['jsoneditor.css', 'jsoneditor-minimalist.js', 'jsoneditor.js'],
57
        ];
58
    }
59
60
    /**
61
     * @param string $css
62
     * @param string $minimalistJs
63
     * @param string $fullJs
64
     * @covers       \kdn\yii2\assets\JsonEditorAsset
65
     * @covers       \kdn\yii2\JsonEditor
66
     * @dataProvider assetProductionProvider
67
     * @small
68
     */
69
    public function testAssetProduction($css, $minimalistJs, $fullJs)
70
    {
71
        if (!function_exists('runkit_constant_redefine')) {
72
            $this->markTestSkipped('runkit extension required.');
73
            return;
74
        }
75
76
        $yiiEnvDev = YII_ENV_DEV;
77
        runkit_constant_redefine('YII_ENV_DEV', true);
78
        $this->testAsset($css, $minimalistJs, $fullJs);
79
        runkit_constant_redefine('YII_ENV_DEV', $yiiEnvDev);
80
    }
81
82
    /**
83
     * @covers \kdn\yii2\JsonEditor
84
     * @uses   \kdn\yii2\assets\JsonEditorAsset
85
     * @small
86
     */
87
    public function testEditorWidget()
88
    {
89
        $html = JsonEditor::widget(
90
            [
91
                'clientOptions' => [
92
                    'modes' => ['code', 'form', 'text', 'tree', 'view'],
93
                    'mode' => 'view',
94
                    'onChange' => 'function () {console.log(this);}',
95
                    'onError' => 'function (error) {console.log(error);}',
96
                    'onModeChange' => 'function (nMode, oMode) {console.log(this, nMode, oMode);}',
97
                ],
98
                'collapseAll' => ['view'],
99
                'containerOptions' => ['class' => 'container'],
100
                'expandAll' => ['tree', 'form'],
101
                'name' => 'data',
102
                'options' => ['id' => 'data'],
103
            ]
104
        );
105
        $this->assertStringEqualsHtmlFile(__FUNCTION__, $html);
106
        $jsCodeBlock = reset(Yii::$app->view->js);
107
        $this->assertStringEqualsJsFile(__FUNCTION__, reset($jsCodeBlock));
108
    }
109
110
    /**
111
     * @covers \kdn\yii2\JsonEditor
112
     * @uses   \kdn\yii2\assets\JsonEditorAsset
113
     * @small
114
     */
115
    public function testEditorActiveWidgetAndDefaults()
116
    {
117
        $html = static::catchOutput(
118
            function () {
119
                $form = ActiveForm::begin(['id' => 'data-form', 'action' => 'test', 'options' => ['csrf' => false]]);
120
                echo $form->field(new ModelMock, 'data')->widget(JsonEditor::className(), ['expandAll' => ['tree']]);
121
                ActiveForm::end();
122
            }
123
        )['output'];
124
        $this->assertStringEqualsHtmlFile(__FUNCTION__, $html);
125
        $jsCodeBlock = reset(Yii::$app->view->js);
126
        $this->assertStringEqualsJsFile(__FUNCTION__, reset($jsCodeBlock));
127
    }
128
}
129