Passed
Push — main ( 10601e...ca0571 )
by Thierry
04:19
created

testCommandUpdateValueWithMethodGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 34
rs 9.552
c 0
b 0
f 0
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 PluginDatabagTest 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
    public function testCommandGetValue()
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' => 'TestDatabag',
47
                        'method' => 'getValue',
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(1, $xResponse->getCommandCount());
58
        $aCommand = $xResponse->getCommands()[0];
59
        $this->assertEquals('node.assign', $aCommand['name']);
60
        $this->assertEquals('div-id', $aCommand['args']['id']);
61
        $this->assertEquals('innerHTML', $aCommand['args']['attr']);
62
        $this->assertEquals('Default value', $aCommand['args']['value']);
63
    }
64
65
    /**
66
     * @throws SetupException
67
     * @throws RequestException
68
     */
69
    public function testCommandSetValue()
70
    {
71
        // Send a request to the registered class
72
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
73
            return $c->g(ServerRequestCreator::class)
74
                ->fromGlobals()
75
                ->withParsedBody([
76
                    'jxncall' => json_encode([
77
                        'type' => 'class',
78
                        'name' => 'TestDatabag',
79
                        'method' => 'setValue',
80
                        'args' => [],
81
                    ]),
82
                ])
83
                ->withMethod('POST');
84
        });
85
        // Process the request and get the response
86
        $this->assertTrue(jaxon()->canProcessRequest());
87
        jaxon()->di()->getRequestHandler()->processRequest();
88
        $xResponse = jaxon()->getResponse();
89
        $this->assertEquals(1, $xResponse->getCommandCount());
90
        $aCommand = $xResponse->getCommands()[0];
91
        // $this->assertEquals('', json_encode($aCommand));
92
        $this->assertEquals('bags', $aCommand['options']['plugin']);
93
        $this->assertEquals('databag.set', $aCommand['name']);
94
        // $this->assertEquals('value', $aCommand['args']['values']['data']['dataset']['key']);
95
    }
96
97
    /**
98
     * @throws SetupException
99
     * @throws RequestException
100
     */
101
    public function testCommandUpdateValueWithMethodPost()
102
    {
103
        // Send a request to the registered class
104
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
105
            return $c->g(ServerRequestCreator::class)
106
                ->fromGlobals()
107
                ->withParsedBody([
108
                    'jxncall' => json_encode([
109
                        'type' => 'class',
110
                        'name' => 'TestDatabag',
111
                        'method' => 'updateValue',
112
                        'args' => [],
113
                    ]),
114
                    'jxnbags' => json_encode(['dataset' => ['key1' => 'value1']]),
115
                ])
116
                ->withMethod('POST');
117
        });
118
        // Process the request and get the response
119
        $this->assertTrue(jaxon()->canProcessRequest());
120
        jaxon()->di()->getRequestHandler()->processRequest();
121
        $xResponse = jaxon()->getResponse();
122
        $this->assertEquals(2, $xResponse->getCommandCount());
123
124
        // Test the assign command
125
        $aAssignCommand = $xResponse->getCommands()[0];
126
        $this->assertEquals('node.assign', $aAssignCommand['name']);
127
        $this->assertEquals('div-id', $aAssignCommand['args']['id']);
128
        $this->assertEquals('innerHTML', $aAssignCommand['args']['attr']);
129
        $this->assertEquals('value1', $aAssignCommand['args']['value']);
130
131
        // Test the databag update command
132
        $aBagCommand = $xResponse->getCommands()[1];
133
        $this->assertEquals('bags', $aBagCommand['options']['plugin']);
134
        $this->assertEquals('databag.set', $aBagCommand['name']);
135
        // $this->assertEquals('value1', $aBagCommand['data']['dataset']['key1']);
136
        // $this->assertEquals('value2', $aBagCommand['data']['dataset']['key2']);
137
    }
138
139
    /**
140
     * @throws SetupException
141
     * @throws RequestException
142
     */
143
    public function testCommandUpdateValueWithMethodGet()
144
    {
145
        // Send a request to the registered class
146
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
147
            return $c->g(ServerRequestCreator::class)
148
                ->fromGlobals()
149
                ->withQueryParams([
150
                    'jxncall' => json_encode([
151
                        'type' => 'class',
152
                        'name' => 'TestDatabag',
153
                        'method' => 'updateValue',
154
                        'args' => [],
155
                    ]),
156
                    'jxnbags' => json_encode(['dataset' => ['key1' => 'value1']]),
157
                ])
158
                ->withMethod('GET');
159
        });
160
        // Process the request and get the response
161
        $this->assertTrue(jaxon()->canProcessRequest());
162
        jaxon()->di()->getRequestHandler()->processRequest();
163
        $xResponse = jaxon()->getResponse();
164
        $this->assertEquals(2, $xResponse->getCommandCount());
165
166
        // Test the assign command
167
        $aAssignCommand = $xResponse->getCommands()[0];
168
        $this->assertEquals('node.assign', $aAssignCommand['name']);
169
        $this->assertEquals('div-id', $aAssignCommand['args']['id']);
170
        $this->assertEquals('innerHTML', $aAssignCommand['args']['attr']);
171
        $this->assertEquals('value1', $aAssignCommand['args']['value']);
172
173
        // Test the databag update command
174
        $aBagCommand = $xResponse->getCommands()[1];
175
        $this->assertEquals('bags', $aBagCommand['options']['plugin']);
176
        $this->assertEquals('databag.set', $aBagCommand['name']);
177
        // $this->assertEquals('value1', $aBagCommand['data']['dataset']['key1']);
178
        // $this->assertEquals('value2', $aBagCommand['data']['dataset']['key2']);
179
    }
180
}
181