PageJsTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 288
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 145
dl 0
loc 288
rs 10
c 1
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testCommandRemoveHandler() 0 21 1
A testCommandRedirect() 0 21 1
A setUp() 0 4 1
A testCommandAddHandler() 0 21 1
A testCommandSetEvent() 0 21 1
A testCommandOnClick() 0 21 1
A testCommandConfirm() 0 21 1
A tearDown() 0 4 1
A testCommandSleep() 0 21 1
A testCommandCall() 0 21 1
A testCommandAlert() 0 21 1
A testCommandDebug() 0 21 1
1
<?php
2
3
namespace Jaxon\Tests\TestResponse;
4
5
use Jaxon\Jaxon;
6
use Jaxon\Exception\RequestException;
7
use Jaxon\Exception\SetupException;
8
use Nyholm\Psr7Server\ServerRequestCreator;
9
use Psr\Http\Message\ServerRequestInterface;
10
use PHPUnit\Framework\TestCase;
11
12
13
class PageJsTest extends TestCase
14
{
15
    /**
16
     * @throws SetupException
17
     */
18
    public function setUp(): void
19
    {
20
        jaxon()->setOption('core.prefix.class', '');
21
        jaxon()->register(Jaxon::CALLABLE_DIR, __DIR__ . '/../src/response');
22
    }
23
24
    /**
25
     * @throws SetupException
26
     */
27
    public function tearDown(): void
28
    {
29
        jaxon()->reset();
30
        parent::tearDown();
31
    }
32
33
    /**
34
     * @throws SetupException
35
     * @throws RequestException
36
     */
37
    function testCommandRedirect()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    {
39
        // Send a request to the registered class
40
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
41
            return $c->g(ServerRequestCreator::class)
42
                ->fromGlobals()
43
                ->withParsedBody([
44
                    'jxncall' => json_encode([
45
                        'type' => 'class',
46
                        'name' => 'TestJs',
47
                        'method' => 'redirect',
48
                        'args' => [],
49
                    ]),
50
                ])
51
                ->withMethod('POST');
52
        });
53
        // Process the request and get the response
54
        $this->assertTrue(jaxon()->canProcessRequest());
55
        jaxon()->di()->getRequestHandler()->processRequest();
56
        $xResponse = jaxon()->getResponse();
57
        $this->assertEquals(2, $xResponse->getCommandCount());
58
    }
59
60
    /**
61
     * @throws SetupException
62
     * @throws RequestException
63
     */
64
    function testCommandConfirm()
65
    {
66
        // Send a request to the registered class
67
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
68
            return $c->g(ServerRequestCreator::class)
69
                ->fromGlobals()
70
                ->withParsedBody([
71
                    'jxncall' => json_encode([
72
                        'type' => 'class',
73
                        'name' => 'TestJs',
74
                        'method' => 'confirm',
75
                        'args' => [],
76
                    ]),
77
                ])
78
                ->withMethod('POST');
79
        });
80
        // Process the request and get the response
81
        $this->assertTrue(jaxon()->canProcessRequest());
82
        jaxon()->di()->getRequestHandler()->processRequest();
83
        $xResponse = jaxon()->getResponse();
84
        $this->assertEquals(3, $xResponse->getCommandCount());
85
    }
86
87
    /**
88
     * @throws SetupException
89
     * @throws RequestException
90
     */
91
    function testCommandAlert()
92
    {
93
        // Send a request to the registered class
94
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
95
            return $c->g(ServerRequestCreator::class)
96
                ->fromGlobals()
97
                ->withParsedBody([
98
                    'jxncall' => json_encode([
99
                        'type' => 'class',
100
                        'name' => 'TestJs',
101
                        'method' => 'alert',
102
                        'args' => [],
103
                    ]),
104
                ])
105
                ->withMethod('POST');
106
        });
107
        // Process the request and get the response
108
        $this->assertTrue(jaxon()->canProcessRequest());
109
        jaxon()->di()->getRequestHandler()->processRequest();
110
        $xResponse = jaxon()->getResponse();
111
        $this->assertEquals(1, $xResponse->getCommandCount());
112
    }
113
114
    /**
115
     * @throws SetupException
116
     * @throws RequestException
117
     */
118
    function testCommandDebug()
119
    {
120
        // Send a request to the registered class
121
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
122
            return $c->g(ServerRequestCreator::class)
123
                ->fromGlobals()
124
                ->withParsedBody([
125
                    'jxncall' => json_encode([
126
                        'type' => 'class',
127
                        'name' => 'TestJs',
128
                        'method' => 'debug',
129
                        'args' => [],
130
                    ]),
131
                ])
132
                ->withMethod('POST');
133
        });
134
        // Process the request and get the response
135
        $this->assertTrue(jaxon()->canProcessRequest());
136
        jaxon()->di()->getRequestHandler()->processRequest();
137
        $xResponse = jaxon()->getResponse();
138
        $this->assertEquals(1, $xResponse->getCommandCount());
139
    }
140
141
    /**
142
     * @throws SetupException
143
     * @throws RequestException
144
     */
145
    function testCommandCall()
146
    {
147
        // Send a request to the registered class
148
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
149
            return $c->g(ServerRequestCreator::class)
150
                ->fromGlobals()
151
                ->withParsedBody([
152
                    'jxncall' => json_encode([
153
                        'type' => 'class',
154
                        'name' => 'TestJs',
155
                        'method' => 'call',
156
                        'args' => [],
157
                    ]),
158
                ])
159
                ->withMethod('POST');
160
        });
161
        // Process the request and get the response
162
        $this->assertTrue(jaxon()->canProcessRequest());
163
        jaxon()->di()->getRequestHandler()->processRequest();
164
        $xResponse = jaxon()->getResponse();
165
        $this->assertEquals(1, $xResponse->getCommandCount());
166
    }
167
168
    /**
169
     * @throws SetupException
170
     * @throws RequestException
171
     */
172
    function testCommandSetEvent()
173
    {
174
        // Send a request to the registered class
175
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
176
            return $c->g(ServerRequestCreator::class)
177
                ->fromGlobals()
178
                ->withParsedBody([
179
                    'jxncall' => json_encode([
180
                        'type' => 'class',
181
                        'name' => 'TestJs',
182
                        'method' => 'setEvent',
183
                        'args' => [],
184
                    ]),
185
                ])
186
                ->withMethod('POST');
187
        });
188
        // Process the request and get the response
189
        $this->assertTrue(jaxon()->canProcessRequest());
190
        jaxon()->di()->getRequestHandler()->processRequest();
191
        $xResponse = jaxon()->getResponse();
192
        $this->assertEquals(1, $xResponse->getCommandCount());
193
    }
194
195
    /**
196
     * @throws SetupException
197
     * @throws RequestException
198
     */
199
    function testCommandOnClick()
200
    {
201
        // Send a request to the registered class
202
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
203
            return $c->g(ServerRequestCreator::class)
204
                ->fromGlobals()
205
                ->withParsedBody([
206
                    'jxncall' => json_encode([
207
                        'type' => 'class',
208
                        'name' => 'TestJs',
209
                        'method' => 'onClick',
210
                        'args' => [],
211
                    ]),
212
                ])
213
                ->withMethod('POST');
214
        });
215
        // Process the request and get the response
216
        $this->assertTrue(jaxon()->canProcessRequest());
217
        jaxon()->di()->getRequestHandler()->processRequest();
218
        $xResponse = jaxon()->getResponse();
219
        $this->assertEquals(1, $xResponse->getCommandCount());
220
    }
221
222
    /**
223
     * @throws SetupException
224
     * @throws RequestException
225
     */
226
    function testCommandAddHandler()
227
    {
228
        // Send a request to the registered class
229
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
230
            return $c->g(ServerRequestCreator::class)
231
                ->fromGlobals()
232
                ->withParsedBody([
233
                    'jxncall' => json_encode([
234
                        'type' => 'class',
235
                        'name' => 'TestJs',
236
                        'method' => 'addHandler',
237
                        'args' => [],
238
                    ]),
239
                ])
240
                ->withMethod('POST');
241
        });
242
        // Process the request and get the response
243
        $this->assertTrue(jaxon()->canProcessRequest());
244
        jaxon()->di()->getRequestHandler()->processRequest();
245
        $xResponse = jaxon()->getResponse();
246
        $this->assertEquals(1, $xResponse->getCommandCount());
247
    }
248
249
    /**
250
     * @throws SetupException
251
     * @throws RequestException
252
     */
253
    function testCommandRemoveHandler()
254
    {
255
        // Send a request to the registered class
256
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
257
            return $c->g(ServerRequestCreator::class)
258
                ->fromGlobals()
259
                ->withParsedBody([
260
                    'jxncall' => json_encode([
261
                        'type' => 'class',
262
                        'name' => 'TestJs',
263
                        'method' => 'removeHandler',
264
                        'args' => [],
265
                    ]),
266
                ])
267
                ->withMethod('POST');
268
        });
269
        // Process the request and get the response
270
        $this->assertTrue(jaxon()->canProcessRequest());
271
        jaxon()->di()->getRequestHandler()->processRequest();
272
        $xResponse = jaxon()->getResponse();
273
        $this->assertEquals(1, $xResponse->getCommandCount());
274
    }
275
276
    /**
277
     * @throws SetupException
278
     * @throws RequestException
279
     */
280
    function testCommandSleep()
281
    {
282
        // Send a request to the registered class
283
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
284
            return $c->g(ServerRequestCreator::class)
285
                ->fromGlobals()
286
                ->withParsedBody([
287
                    'jxncall' => json_encode([
288
                        'type' => 'class',
289
                        'name' => 'TestJs',
290
                        'method' => 'sleep',
291
                        'args' => [],
292
                    ]),
293
                ])
294
                ->withMethod('POST');
295
        });
296
        // Process the request and get the response
297
        $this->assertTrue(jaxon()->canProcessRequest());
298
        jaxon()->di()->getRequestHandler()->processRequest();
299
        $xResponse = jaxon()->getResponse();
300
        $this->assertEquals(1, $xResponse->getCommandCount());
301
    }
302
}
303