Issues (686)

jaxon-dialogs/tests/TestDialog/SetupTest.php (4 issues)

1
<?php
2
3
namespace Jaxon\Dialogs\Tests\TestDialog;
4
5
require_once __DIR__ . '/../src/dialog.php';
6
7
use Jaxon\Jaxon;
8
use Jaxon\Dialogs\DialogPlugin;
9
use Jaxon\Dialogs\Dialog\Library\Alert;
10
use Jaxon\Dialogs\Dialog\Library\Alertify;
11
use Jaxon\Dialogs\Dialog\Library\Bootbox;
12
use Jaxon\Exception\SetupException;
13
use Jaxon\Utils\Http\UriException;
14
use PHPUnit\Framework\TestCase;
15
16
use ClassWithInterface;
17
use Dialog;
18
use TestDialogLibrary;
19
20
use function get_class;
21
use function Jaxon\jaxon;
22
use function Jaxon\Dialogs\dialog;
23
use function Jaxon\Dialogs\_register as register_dialogs;
24
25
class SetupTest extends TestCase
26
{
27
    /**
28
     * @throws SetupException
29
     */
30
    public function setUp(): void
31
    {
32
        register_dialogs();
33
34
        jaxon()->setOption('core.prefix.class', '');
35
        jaxon()->setOption('core.request.uri', 'http://example.test/path');
36
        jaxon()->register(Jaxon::CALLABLE_CLASS, Dialog::class);
37
    }
38
39
    /**
40
     * @throws SetupException
41
     */
42
    public function tearDown(): void
43
    {
44
        jaxon()->reset();
45
        parent::tearDown();
46
    }
47
48
    public function testDialogSettings()
49
    {
50
        $this->assertEquals('', dialog()->getConfirmLibrary()->getName());
51
        $this->assertEquals(Alert::class, get_class(dialog()->getConfirmLibrary()));
52
        $this->assertEquals(Alert::class, get_class(dialog()->getAlertLibrary()));
53
        $this->assertEquals(null, dialog()->getModalLibrary());
54
55
        jaxon()->setAppOptions([
56
            'modal' => 'alertify',
57
            'alert' => 'alertify',
58
            'confirm' => 'alertify',
59
        ], 'dialogs.default');
60
        $this->assertEquals(Alertify::class, get_class(dialog()->getConfirmLibrary()));
61
        $this->assertEquals(Alertify::class, get_class(dialog()->getAlertLibrary()));
62
        $this->assertEquals(Alertify::class, get_class(dialog()->getModalLibrary()));
0 ignored issues
show
It seems like dialog()->getModalLibrary() can also be of type null; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $this->assertEquals(Alertify::class, get_class(/** @scrutinizer ignore-type */ dialog()->getModalLibrary()));
Loading history...
63
64
        jaxon()->setAppOptions([
65
            'modal' => 'bootbox',
66
            'alert' => 'bootbox',
67
            'confirm' => 'bootbox',
68
        ], 'dialogs.default');
69
        $this->assertEquals(Bootbox::class, get_class(dialog()->getConfirmLibrary()));
70
        $this->assertEquals(Bootbox::class, get_class(dialog()->getAlertLibrary()));
71
        $this->assertEquals(Bootbox::class, get_class(dialog()->getModalLibrary()));
72
    }
73
74
    public function testDialogOptions()
75
    {
76
        jaxon()->app()->setup(__DIR__ . '/../config/dialog.php');
77
        $this->assertStringContainsString('toast-top-center', jaxon()->script());
78
79
        /** @var DialogPlugin */
80
        $xDialogPlugin = jaxon()->di()->g(DialogPlugin::class);
81
        $this->assertStringContainsString('5.0.0', $xDialogPlugin->getHash());
82
    }
83
84
    public function testDialogDefaultMethods()
85
    {
86
        dialog()->registerLibrary(TestDialogLibrary::class, TestDialogLibrary::NAME);
87
        // Registering a library twice should not cause any issue.
88
        dialog()->registerLibrary(TestDialogLibrary::class, TestDialogLibrary::NAME);
89
        jaxon()->setAppOption('dialogs.default.confirm', TestDialogLibrary::NAME);
90
91
        $xConfirmLibrary = dialog()->getConfirmLibrary();
92
        $this->assertEmpty($xConfirmLibrary->getJsUrls());
93
        $this->assertEmpty($xConfirmLibrary->getCssUrls());
94
        $this->assertEquals('', $xConfirmLibrary->getJsCode());
95
        $this->assertEquals('', $xConfirmLibrary->getCssCode());
96
    }
97
98
    public function testExtDialogLibrary()
99
    {
100
        dialog()->registerLibrary(TestDialogLibrary::class, TestDialogLibrary::NAME);
101
        jaxon()->setAppOption('dialogs.default.confirm', TestDialogLibrary::NAME);
102
        $this->assertEquals(TestDialogLibrary::class, get_class(dialog()->getConfirmLibrary()));
103
    }
104
105
    public function testExtDialogLibraryConfigSet()
106
    {
107
        jaxon()->setAppOption('dialogs.lib.ext', [
108
            TestDialogLibrary::NAME => TestDialogLibrary::class,
109
        ]);
110
        jaxon()->setAppOption('dialogs.default.confirm', TestDialogLibrary::NAME);
111
        $this->assertEquals(TestDialogLibrary::class, get_class(dialog()->getConfirmLibrary()));
112
    }
113
114
    public function testExtDialogLibraryConfigFile()
115
    {
116
        jaxon()->app()->setup(__DIR__ . '/../config/ext.php');
117
        $this->assertEquals(TestDialogLibrary::class, get_class(dialog()->getConfirmLibrary()));
118
    }
119
120
    public function testDialogJsCode()
121
    {
122
        jaxon()->setAppOption('dialogs.lib.use', ['bootbox', 'alertify', 'cute']);
123
        $sJsCode = jaxon()->js();
124
        $this->assertStringContainsString('bootbox.min.js', $sJsCode);
125
        $this->assertStringContainsString('alertify.min.js', $sJsCode);
126
        $this->assertStringContainsString('cute-alert.js', $sJsCode);
127
    }
128
129
    public function testDialogCssCode()
130
    {
131
        jaxon()->setAppOption('dialogs.lib.use', ['alertify', 'cute']);
132
        $sCssCode = jaxon()->css();
133
        $this->assertStringContainsString('alertify.min.css', $sCssCode);
134
        $this->assertStringContainsString('cute-alert/style.css', $sCssCode);
135
    }
136
137
    /**
138
     * @throws UriException
139
     */
140
    public function testDialogScriptCode()
141
    {
142
        jaxon()->setAppOptions([
143
            'default' => [
144
                'modal' => 'alertify',
145
                'alert' => 'alertify',
146
                'confirm' => 'alertify',
147
            ],
148
            'lib' => [
149
                'use' => ['bootbox', 'cute', 'jalert'],
150
            ],
151
        ], 'dialogs');
152
153
        $sScriptCode = jaxon()->getScript();
154
        $this->assertStringContainsString("jaxon.dialog.register('alertify'", $sScriptCode);
155
        $this->assertStringContainsString("jaxon.dialog.register('bootbox'", $sScriptCode);
156
        $this->assertStringContainsString("jaxon.dialog.register('cute'", $sScriptCode);
157
        $this->assertStringContainsString("jaxon.dialog.register('jalert'", $sScriptCode);
158
    }
159
160
    /**
161
     * @throws UriException
162
     */
163
    public function testDialogScriptFile()
164
    {
165
        jaxon()->setAppOptions([
166
            'export' => true,
167
            'minify' => false,
168
            'file' => 'app.dialog',
169
            'dir' => __DIR__ . '/../js',
170
            'uri' => 'http://localhost',
171
        ], 'assets.js');
172
        jaxon()->setAppOptions([
173
            'default' => [
174
                'modal' => 'alertify',
175
                'alert' => 'alertify',
176
                'confirm' => 'alertify',
177
            ],
178
            'lib' => [
179
                'use' => ['bootbox', 'cute', 'jalert'],
180
            ],
181
        ], 'dialogs');
182
183
        $sScriptCode = jaxon()->getScript();
184
        $this->assertStringNotContainsString("jaxon.dialog.register('alertify'", $sScriptCode);
185
        $this->assertStringNotContainsString("jaxon.dialog.register('bootbox'", $sScriptCode);
186
        $this->assertStringNotContainsString("jaxon.dialog.register('cute'", $sScriptCode);
187
        $this->assertStringNotContainsString("jaxon.dialog.register('jalert'", $sScriptCode);
188
189
        $this->assertStringContainsString('app.dialog.js', $sScriptCode);
190
    }
191
192
    /**
193
     * @throws UriException
194
     */
195
    public function testDialogScriptMinFile()
196
    {
197
        jaxon()->setAppOptions([
198
            'export' => true,
199
            'minify' => true,
200
            'file' => 'app.dialog',
201
            'dir' => __DIR__ . '/../js',
202
            'uri' => 'http://localhost',
203
        ], 'assets.js');
204
        jaxon()->setAppOptions([
205
            'default' => [
206
                'modal' => 'alertify',
207
                'alert' => 'alertify',
208
                'confirm' => 'alertify',
209
            ],
210
            'lib' => [
211
                'use' => ['bootbox', 'cute', 'jalert'],
212
            ],
213
        ], 'dialogs');
214
215
        $sScriptCode = jaxon()->getScript();
216
        $this->assertStringNotContainsString("jaxon.dialog.register('alertify'", $sScriptCode);
217
        $this->assertStringNotContainsString("jaxon.dialog.register('bootbox'", $sScriptCode);
218
        $this->assertStringNotContainsString("jaxon.dialog.register('cute'", $sScriptCode);
219
        $this->assertStringNotContainsString("jaxon.dialog.register('jalert'", $sScriptCode);
220
221
        $this->assertStringContainsString('app.dialog.min.js', $sScriptCode);
222
    }
223
224
    public function testAlertifyLibrary()
225
    {
226
        jaxon()->setAppOption('dialogs.lib.use', ['alertify']);
227
228
        $this->assertStringContainsString('alertify.min.js', jaxon()->js());
229
        $this->assertStringContainsString('css/alertify.min.css', jaxon()->css());
230
        $this->assertStringContainsString('css/themes/default.min.css', jaxon()->css());
231
        $this->assertStringContainsString("jaxon.dialog.register('alertify'", jaxon()->script());
232
    }
233
234
    public function testBootboxLibrary()
235
    {
236
        jaxon()->setAppOption('dialogs.lib.use', ['bootbox']);
237
238
        $this->assertStringContainsString('bootbox.min.js', jaxon()->js());
239
        $this->assertStringContainsString("jaxon.dialog.register('bootbox'", jaxon()->script());
240
    }
241
242
    public function testBootstrap3Library()
243
    {
244
        jaxon()->setAppOption('dialogs.lib.use', ['bootstrap3']);
245
246
        $this->assertStringContainsString("jaxon.dialog.register('bootstrap3'", jaxon()->script());
247
    }
248
249
    public function testBootstrap4Library()
250
    {
251
        jaxon()->setAppOption('dialogs.lib.use', ['bootstrap4']);
252
253
        $this->assertStringContainsString("jaxon.dialog.register('bootstrap4'", jaxon()->script());
254
    }
255
256
    public function testBootstrap5Library()
257
    {
258
        jaxon()->setAppOption('dialogs.lib.use', ['bootstrap5']);
259
260
        $this->assertStringContainsString("jaxon.dialog.register('bootstrap5'", jaxon()->script());
261
    }
262
263
    public function testButterupLibrary()
264
    {
265
        jaxon()->setAppOption('dialogs.lib.use', ['butterup']);
266
267
        $this->assertStringContainsString('butterup.min.js', jaxon()->js());
268
        $this->assertStringContainsString('butterup.min.css', jaxon()->css());
269
        $this->assertStringContainsString("jaxon.dialog.register('butterup'", jaxon()->script());
270
    }
271
272
    public function testCuteAlertLibrary()
273
    {
274
        jaxon()->setAppOption('dialogs.lib.use', ['cute']);
275
276
        $this->assertStringContainsString('cute-alert/cute-alert.js', jaxon()->js());
277
        $this->assertStringContainsString('cute-alert/style.css', jaxon()->css());
278
        $this->assertStringContainsString("jaxon.dialog.register('cute'", jaxon()->script());
279
    }
280
281
    public function testIziToastLibrary()
282
    {
283
        jaxon()->setAppOption('dialogs.lib.use', ['izitoast']);
284
285
        $this->assertStringContainsString('js/iziToast.min.js', jaxon()->js());
286
        $this->assertStringContainsString('css/iziToast.min.css', jaxon()->css());
287
        $this->assertStringContainsString("jaxon.dialog.register('izitoast'", jaxon()->script());
288
    }
289
290
    public function testJAlertLibrary()
291
    {
292
        jaxon()->setAppOption('dialogs.lib.use', ['jalert']);
293
294
        $this->assertStringContainsString('jAlert.min.js', jaxon()->js());
295
        $this->assertStringContainsString('jAlert.min.css', jaxon()->css());
296
        $this->assertStringContainsString("jaxon.dialog.register('jalert'", jaxon()->script());
297
    }
298
299
    public function testJQueryConfirmLibrary()
300
    {
301
        jaxon()->setAppOption('dialogs.lib.use', ['jconfirm']);
302
303
        $this->assertStringContainsString('jquery-confirm.min.js', jaxon()->js());
304
        $this->assertStringContainsString('jquery-confirm.min.css', jaxon()->css());
305
        $this->assertStringContainsString("jaxon.dialog.register('jconfirm'", jaxon()->script());
306
    }
307
308
    public function testNotifyLibrary()
309
    {
310
        jaxon()->setAppOption('dialogs.lib.use', ['notify']);
311
312
        $this->assertStringContainsString('notify.min.js', jaxon()->js());
313
        $this->assertStringContainsString("jaxon.dialog.register('notify'", jaxon()->script());
314
    }
315
316
    public function testNotyLibrary()
317
    {
318
        jaxon()->setAppOption('dialogs.lib.use', ['noty']);
319
320
        $this->assertStringContainsString('noty.min.js', jaxon()->js());
321
        $this->assertStringContainsString('noty.min.css', jaxon()->css());
322
        $this->assertStringContainsString("jaxon.dialog.register('noty'", jaxon()->script());
323
    }
324
325
    public function testNotyfLibrary()
326
    {
327
        jaxon()->setAppOption('dialogs.lib.use', ['notyf']);
328
329
        $this->assertStringContainsString('notyf.min.js', jaxon()->js());
330
        $this->assertStringContainsString('notyf.min.css', jaxon()->css());
331
        $this->assertStringContainsString("jaxon.dialog.register('notyf'", jaxon()->script());
332
    }
333
334
    public function testQuantumLibrary()
335
    {
336
        jaxon()->setAppOption('dialogs.lib.use', ['quantum']);
337
338
        $this->assertStringContainsString('minfile/quantumalert.js', jaxon()->js());
339
        $this->assertStringContainsString("jaxon.dialog.register('quantum'", jaxon()->script());
340
    }
341
342
    public function testSweetAlertLibrary()
343
    {
344
        jaxon()->setAppOption('dialogs.lib.use', ['sweetalert']);
345
346
        $this->assertStringContainsString('sweetalert.min.js', jaxon()->js());
347
        $this->assertStringContainsString("jaxon.dialog.register('sweetalert'", jaxon()->script());
348
    }
349
350
    public function testTingleLibrary()
351
    {
352
        jaxon()->setAppOption('dialogs.lib.use', ['tingle']);
353
354
        $this->assertStringContainsString('tingle.min.js', jaxon()->js());
355
        $this->assertStringContainsString('tingle.min.css', jaxon()->css());
356
        $this->assertStringContainsString("jaxon.dialog.register('tingle'", jaxon()->script());
357
    }
358
359
    public function testToastrLibrary()
360
    {
361
        jaxon()->setAppOption('dialogs.lib.use', ['toastr']);
362
363
        $this->assertStringContainsString('toastr.min.js', jaxon()->js());
364
        $this->assertStringContainsString('build/toastr.min.css', jaxon()->css());
365
        $this->assertStringContainsString("jaxon.dialog.register('toastr'", jaxon()->script());
366
    }
367
368
    /**
369
     * @throws SetupException
370
     */
371
    public function testErrorRegisterIncorrectDialogClass()
372
    {
373
        $this->expectException(SetupException::class);
374
        dialog()->registerLibrary(Dialog::class, 'incorrect');
375
    }
376
377
    public function testErrorRegisterIncorrectDialogClassWithInterface()
378
    {
379
        $this->expectException(SetupException::class);
380
        dialog()->registerLibrary(ClassWithInterface::class, 'incorrect');
381
    }
382
383
    public function testErrorSetWrongAlertLibrary()
384
    {
385
        $this->expectException(SetupException::class);
386
        jaxon()->setAppOption('dialogs.default.alert', 'incorrect');
387
        $xLibrary = dialog()->getAlertLibrary();
0 ignored issues
show
The assignment to $xLibrary is dead and can be removed.
Loading history...
388
    }
389
390
    public function testErrorSetWrongModalLibrary()
391
    {
392
        $this->expectException(SetupException::class);
393
        jaxon()->setAppOption('dialogs.default.modal', 'incorrect');
394
        $xLibrary = dialog()->getModalLibrary();
0 ignored issues
show
The assignment to $xLibrary is dead and can be removed.
Loading history...
395
    }
396
397
    public function testErrorSetWrongConfirmLibrary()
398
    {
399
        $this->expectException(SetupException::class);
400
        jaxon()->setAppOption('dialogs.default.confirm', 'incorrect');
401
        $xLibrary = dialog()->getConfirmLibrary();
0 ignored issues
show
The assignment to $xLibrary is dead and can be removed.
Loading history...
402
    }
403
}
404