CallTest::testBootboxLibraryShow()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 27
rs 9.6333
c 1
b 0
f 0
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\Exception\RequestException;
9
use Jaxon\Exception\SetupException;
10
use Nyholm\Psr7Server\ServerRequestCreator;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Dialog;
14
use TestDialogLibrary;
15
16
use function Jaxon\jaxon;
17
use function Jaxon\rq;
18
use function Jaxon\pm;
19
use function Jaxon\Dialogs\dialog;
20
use function Jaxon\Dialogs\_register as register_dialogs;
21
22
class CallTest extends TestCase
23
{
24
    /**
25
     * @throws SetupException
26
     */
27
    public function setUp(): void
28
    {
29
        register_dialogs();
30
31
        jaxon()->setOption('core.prefix.class', '');
32
        jaxon()->setOption('core.request.uri', 'http://example.test/path');
33
        jaxon()->register(Jaxon::CALLABLE_CLASS, Dialog::class);
34
        dialog()->registerLibrary(TestDialogLibrary::class, TestDialogLibrary::NAME);
35
    }
36
37
    /**
38
     * @throws SetupException
39
     */
40
    public function tearDown(): void
41
    {
42
        jaxon()->reset();
43
        parent::tearDown();
44
    }
45
46
    /**
47
     * @throws RequestException
48
     */
49
    public function testDefaultDialogSuccess()
50
    {
51
        // The server request
52
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
53
            return $c->g(ServerRequestCreator::class)
54
                ->fromGlobals()
55
                ->withQueryParams([
56
                    'jxncall' => json_encode([
57
                        'type' => 'class',
58
                        'name' => 'Dialog',
59
                        'method' => 'success',
60
                        'args' => [],
61
                    ]),
62
                ]);
63
        });
64
65
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
66
        jaxon()->di()->getRequestHandler()->processRequest();
67
68
        $aCommands = jaxon()->getResponse()->getCommands();
69
        $this->assertCount(1, $aCommands);
70
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
71
    }
72
73
    /**
74
     * @throws RequestException
75
     */
76
    public function testDialogLibrarySuccess()
77
    {
78
        jaxon()->setAppOptions([
79
            'modal' => 'alertify',
80
            'alert' => 'alertify',
81
            'confirm' => 'alertify',
82
        ], 'dialogs.default');
83
        // The server request
84
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
85
            return $c->g(ServerRequestCreator::class)
86
                ->fromGlobals()
87
                ->withQueryParams([
88
                    'jxncall' => json_encode([
89
                        'type' => 'class',
90
                        'name' => 'Dialog',
91
                        'method' => 'success',
92
                        'args' => [],
93
                    ]),
94
                ]);
95
        });
96
97
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
98
        jaxon()->di()->getRequestHandler()->processRequest();
99
100
        $aCommands = jaxon()->getResponse()->getCommands();
101
        $this->assertCount(1, $aCommands);
102
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
103
        $this->assertEquals('dialog', $aCommands[0]['options']['plugin']);
104
    }
105
106
    /**
107
     * @throws RequestException
108
     */
109
    public function testDefaultDialogWarning()
110
    {
111
        // The server request
112
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
113
            return $c->g(ServerRequestCreator::class)
114
                ->fromGlobals()
115
                ->withQueryParams([
116
                    'jxncall' => json_encode([
117
                        'type' => 'class',
118
                        'name' => 'Dialog',
119
                        'method' => 'warning',
120
                        'args' => [],
121
                    ]),
122
                ]);
123
        });
124
125
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
126
        jaxon()->di()->getRequestHandler()->processRequest();
127
128
        $aCommands = jaxon()->getResponse()->getCommands();
129
        $this->assertCount(1, $aCommands);
130
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
131
    }
132
133
    /**
134
     * @throws RequestException
135
     */
136
    public function testDialogLibraryWarning()
137
    {
138
        jaxon()->setAppOptions([
139
            'modal' => 'alertify',
140
            'alert' => 'alertify',
141
            'confirm' => 'alertify',
142
        ], 'dialogs.default');
143
        // The server request
144
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
145
            return $c->g(ServerRequestCreator::class)
146
                ->fromGlobals()
147
                ->withQueryParams([
148
                    'jxncall' => json_encode([
149
                        'type' => 'class',
150
                        'name' => 'Dialog',
151
                        'method' => 'warning',
152
                        'args' => [],
153
                    ]),
154
                ]);
155
        });
156
157
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
158
        jaxon()->di()->getRequestHandler()->processRequest();
159
160
        $aCommands = jaxon()->getResponse()->getCommands();
161
        $this->assertCount(1, $aCommands);
162
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
163
        $this->assertEquals('dialog', $aCommands[0]['options']['plugin']);
164
    }
165
166
    /**
167
     * @throws RequestException
168
     */
169
    public function testDefaultDialogInfo()
170
    {
171
        // The server request
172
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
173
            return $c->g(ServerRequestCreator::class)
174
                ->fromGlobals()
175
                ->withQueryParams([
176
                    'jxncall' => json_encode([
177
                        'type' => 'class',
178
                        'name' => 'Dialog',
179
                        'method' => 'info',
180
                        'args' => [],
181
                    ]),
182
                ]);
183
        });
184
185
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
186
        jaxon()->di()->getRequestHandler()->processRequest();
187
188
        $aCommands = jaxon()->getResponse()->getCommands();
189
        $this->assertCount(1, $aCommands);
190
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
191
    }
192
193
    /**
194
     * @throws RequestException
195
     */
196
    public function testDialogLibraryInfo()
197
    {
198
        jaxon()->setAppOptions([
199
            'modal' => 'alertify',
200
            'alert' => 'alertify',
201
            'confirm' => 'alertify',
202
        ], 'dialogs.default');
203
        // The server request
204
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
205
            return $c->g(ServerRequestCreator::class)
206
                ->fromGlobals()
207
                ->withQueryParams([
208
                    'jxncall' => json_encode([
209
                        'type' => 'class',
210
                        'name' => 'Dialog',
211
                        'method' => 'info',
212
                        'args' => [],
213
                    ]),
214
                ]);
215
        });
216
217
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
218
        jaxon()->di()->getRequestHandler()->processRequest();
219
220
        $aCommands = jaxon()->getResponse()->getCommands();
221
        $this->assertCount(1, $aCommands);
222
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
223
        $this->assertEquals('dialog', $aCommands[0]['options']['plugin']);
224
    }
225
226
    /**
227
     * @throws RequestException
228
     */
229
    public function testDefaultDialogError()
230
    {
231
        // The server request
232
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
233
            return $c->g(ServerRequestCreator::class)
234
                ->fromGlobals()
235
                ->withQueryParams([
236
                    'jxncall' => json_encode([
237
                        'type' => 'class',
238
                        'name' => 'Dialog',
239
                        'method' => 'error',
240
                        'args' => [],
241
                    ]),
242
                ]);
243
        });
244
245
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
246
        jaxon()->di()->getRequestHandler()->processRequest();
247
248
        $aCommands = jaxon()->getResponse()->getCommands();
249
        $this->assertCount(1, $aCommands);
250
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
251
    }
252
253
    /**
254
     * @throws RequestException
255
     */
256
    public function testDialogLibraryError()
257
    {
258
        jaxon()->setAppOptions([
259
            'modal' => 'alertify',
260
            'alert' => 'alertify',
261
            'confirm' => 'alertify',
262
        ], 'dialogs.default');
263
        // The server request
264
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
265
            return $c->g(ServerRequestCreator::class)
266
            ->fromGlobals()
267
            ->withQueryParams([
268
                'jxncall' => json_encode([
269
                    'type' => 'class',
270
                    'name' => 'Dialog',
271
                    'method' => 'error',
272
                    'args' => [],
273
                ]),
274
            ]);
275
        });
276
277
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
278
        jaxon()->di()->getRequestHandler()->processRequest();
279
280
        $aCommands = jaxon()->getResponse()->getCommands();
281
        $this->assertCount(1, $aCommands);
282
        $this->assertEquals('dialog.alert.show', $aCommands[0]['name']);
283
        $this->assertEquals('dialog', $aCommands[0]['options']['plugin']);
284
    }
285
286
    /**
287
     * @throws RequestException
288
     */
289
    public function testDialogLibraryShow()
290
    {
291
        jaxon()->setAppOptions([
292
            'modal' => 'alertify',
293
            'alert' => 'alertify',
294
            'confirm' => 'alertify',
295
        ], 'dialogs.default');
296
        // The server request
297
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
298
            return $c->g(ServerRequestCreator::class)
299
                ->fromGlobals()
300
                ->withQueryParams([
301
                    'jxncall' => json_encode([
302
                        'type' => 'class',
303
                        'name' => 'Dialog',
304
                        'method' => 'show',
305
                        'args' => [],
306
                    ]),
307
                ]);
308
        });
309
310
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
311
        jaxon()->di()->getRequestHandler()->processRequest();
312
313
        $aCommands = jaxon()->getResponse()->getCommands();
314
        $this->assertCount(1, $aCommands);
315
        $this->assertEquals('dialog.modal.show', $aCommands[0]['name']);
316
        $this->assertEquals('dialog', $aCommands[0]['options']['plugin']);
317
    }
318
319
    /**
320
     * @throws RequestException
321
     */
322
    public function testBootboxLibraryShow()
323
    {
324
        jaxon()->setAppOptions([
325
            'modal' => 'alertify',
326
            'alert' => 'alertify',
327
            'confirm' => 'alertify',
328
        ], 'dialogs.default');
329
        // The server request
330
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
331
            return $c->g(ServerRequestCreator::class)
332
                ->fromGlobals()
333
                ->withQueryParams([
334
                    'jxncall' => json_encode([
335
                        'type' => 'class',
336
                        'name' => 'Dialog',
337
                        'method' => 'show',
338
                        'args' => [],
339
                    ]),
340
                ]);
341
        });
342
343
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
344
        jaxon()->di()->getRequestHandler()->processRequest();
345
346
        $aCommands = jaxon()->getResponse()->getCommands();
347
        $this->assertCount(1, $aCommands);
348
        $this->assertEquals('dialog.modal.show', $aCommands[0]['name']);
349
    }
350
351
    /**
352
     * @throws RequestException
353
     */
354
    public function testDialogLibraryShowWith()
355
    {
356
        jaxon()->setAppOptions([
357
            'modal' => 'alertify',
358
            'alert' => 'alertify',
359
            'confirm' => 'alertify',
360
        ], 'dialogs.default');
361
        // The server request
362
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
363
            return $c->g(ServerRequestCreator::class)
364
                ->fromGlobals()
365
                ->withQueryParams([
366
                    'jxncall' => json_encode([
367
                        'type' => 'class',
368
                        'name' => 'Dialog',
369
                        'method' => 'showWith',
370
                        'args' => [],
371
                    ]),
372
                ]);
373
        });
374
375
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
376
        jaxon()->di()->getRequestHandler()->processRequest();
377
378
        $aCommands = jaxon()->getResponse()->getCommands();
379
        $this->assertCount(1, $aCommands);
380
        $this->assertEquals('dialog.modal.show', $aCommands[0]['name']);
381
    }
382
383
    /**
384
     * @throws RequestException
385
     */
386
    public function testDialogLibraryHide()
387
    {
388
        jaxon()->setAppOptions([
389
            'modal' => 'alertify',
390
            'alert' => 'alertify',
391
            'confirm' => 'alertify',
392
        ], 'dialogs.default');
393
        // The server request
394
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
395
            return $c->g(ServerRequestCreator::class)
396
                ->fromGlobals()
397
                ->withQueryParams([
398
                    'jxncall' => json_encode([
399
                        'type' => 'class',
400
                        'name' => 'Dialog',
401
                        'method' => 'hide',
402
                        'args' => [],
403
                    ]),
404
                ]);
405
        });
406
407
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
408
        jaxon()->di()->getRequestHandler()->processRequest();
409
410
        $aCommands = jaxon()->getResponse()->getCommands();
411
        $this->assertCount(1, $aCommands);
412
        $this->assertEquals('dialog.modal.hide', $aCommands[0]['name']);
413
        $this->assertEquals('dialog', $aCommands[0]['options']['plugin']);
414
    }
415
416
    /**
417
     * @throws SetupException
418
     */
419
    public function testConfirmMessageSuccess()
420
    {
421
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleDialog', __DIR__ . '/../src/sample.php');
422
        jaxon()->setAppOption('dialogs.default.alert', 'cute');
423
        jaxon()->setAppOption('dialogs.default.confirm', 'noty');
424
        $this->assertEquals(
425
            'jaxon.exec({"_type":"expr","calls":[{"_type":"func","_name":"SampleDialog.method",' .
426
                '"args":[{"_type":"html","_name":"elt_id"}]}],' .
427
                '"confirm":{"lib":"noty","question":{"title":"","phrase":{"str":"Really?","args":[]}}},' .
428
                '"alert":{"lib":"cute","message":{"type":"success","title":"","phrase":{"str":"No confirm","args":[]}}}})',
429
            rq('SampleDialog')->method(pm()->html('elt_id'))->confirm("Really?")
0 ignored issues
show
Deprecated Code introduced by
The function Jaxon\pm() has been deprecated: Use the call factory functions instead. ( Ignorable by Annotation )

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

429
            rq('SampleDialog')->method(/** @scrutinizer ignore-deprecated */ pm()->html('elt_id'))->confirm("Really?")

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
The method method() does not exist on Jaxon\Script\Call\JxnCall. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

429
            rq('SampleDialog')->/** @scrutinizer ignore-call */ method(pm()->html('elt_id'))->confirm("Really?")
Loading history...
430
                ->elseSuccess("No confirm")->__toString()
431
        );
432
    }
433
434
    /**
435
     * @throws SetupException
436
     */
437
    public function testConfirmMessageInfo()
438
    {
439
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleDialog', __DIR__ . '/../src/sample.php');
440
        jaxon()->setAppOptions([
441
            'alert' => 'cute',
442
            'confirm' => 'noty',
443
        ], 'dialogs.default');
444
        $this->assertEquals(
445
            'jaxon.exec({"_type":"expr","calls":[{"_type":"func","_name":"SampleDialog.method",' .
446
                '"args":[{"_type":"html","_name":"elt_id"}]}],' .
447
                '"confirm":{"lib":"noty","question":{"title":"","phrase":{"str":"Really?","args":[]}}},' .
448
                '"alert":{"lib":"cute","message":{"type":"info","title":"","phrase":{"str":"No confirm","args":[]}}}})',
449
            rq('SampleDialog')->method(pm()->html('elt_id'))->confirm("Really?")
0 ignored issues
show
Deprecated Code introduced by
The function Jaxon\pm() has been deprecated: Use the call factory functions instead. ( Ignorable by Annotation )

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

449
            rq('SampleDialog')->method(/** @scrutinizer ignore-deprecated */ pm()->html('elt_id'))->confirm("Really?")

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
450
                ->elseInfo("No confirm")->__toString()
451
        );
452
    }
453
454
    /**
455
     * @throws SetupException
456
     */
457
    public function testConfirmMessageWarning()
458
    {
459
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleDialog', __DIR__ . '/../src/sample.php');
460
        jaxon()->setAppOptions([
461
            'alert' => 'cute',
462
            'confirm' => 'noty',
463
        ], 'dialogs.default');
464
        $this->assertEquals(
465
            'jaxon.exec({"_type":"expr","calls":[{"_type":"func","_name":"SampleDialog.method",' .
466
                '"args":[{"_type":"html","_name":"elt_id"}]}],' .
467
                '"confirm":{"lib":"noty","question":{"title":"","phrase":{"str":"Really?","args":[]}}},' .
468
                '"alert":{"lib":"cute","message":{"type":"warning","title":"","phrase":{"str":"No confirm","args":[]}}}})',
469
            rq('SampleDialog')->method(pm()->html('elt_id'))->confirm("Really?")
0 ignored issues
show
Deprecated Code introduced by
The function Jaxon\pm() has been deprecated: Use the call factory functions instead. ( Ignorable by Annotation )

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

469
            rq('SampleDialog')->method(/** @scrutinizer ignore-deprecated */ pm()->html('elt_id'))->confirm("Really?")

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
470
                ->elseWarning("No confirm")->__toString()
471
        );
472
    }
473
474
    /**
475
     * @throws SetupException
476
     */
477
    public function testConfirmMessageError()
478
    {
479
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleDialog', __DIR__ . '/../src/sample.php');
480
        jaxon()->setAppOptions([
481
            'alert' => 'cute',
482
            'confirm' => 'noty',
483
        ], 'dialogs.default');
484
        $this->assertEquals(
485
            'jaxon.exec({"_type":"expr","calls":[{"_type":"func","_name":"SampleDialog.method",' .
486
                '"args":[{"_type":"html","_name":"elt_id"}]}],' .
487
                '"confirm":{"lib":"noty","question":{"title":"","phrase":{"str":"Really?","args":[]}}},' .
488
                '"alert":{"lib":"cute","message":{"type":"error","title":"","phrase":{"str":"No confirm","args":[]}}}})',
489
            rq('SampleDialog')->method(pm()->html('elt_id'))->confirm("Really?")
0 ignored issues
show
Deprecated Code introduced by
The function Jaxon\pm() has been deprecated: Use the call factory functions instead. ( Ignorable by Annotation )

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

489
            rq('SampleDialog')->method(/** @scrutinizer ignore-deprecated */ pm()->html('elt_id'))->confirm("Really?")

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
490
                ->elseError("No confirm")->__toString()
491
        );
492
    }
493
}
494