Passed
Push — main ( 938a8d...3607b0 )
by Thierry
03:58
created

UploadFsLocalTest::testUploadInMemoryStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 27
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 38
rs 9.488
1
<?php
2
3
namespace Jaxon\Upload\Tests\TestUpload;
4
5
use Jaxon\Jaxon;
6
use Jaxon\Exception\RequestException;
7
use Jaxon\Exception\SetupException;
8
use Nyholm\Psr7\UploadedFile;
9
use Nyholm\Psr7Server\ServerRequestCreator;
10
use Psr\Http\Message\ServerRequestInterface;
11
use PHPUnit\Framework\TestCase;
12
13
use function Jaxon\jaxon;
14
use function Jaxon\Upload\_register;
15
use function copy;
16
use function filesize;
17
use function mkdir;
18
19
class UploadFsLocalTest extends TestCase
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $sNameWhite;
25
26
    /**
27
     * @var string
28
     */
29
    protected $sPathWhite;
30
31
    /**
32
     * @var int
33
     */
34
    protected $sSizeWhite;
35
36
    /**
37
     * @var string
38
     */
39
    protected $sNameBlue;
40
41
    /**
42
     * @var string
43
     */
44
    protected $sPathBlue;
45
46
    /**
47
     * @var int
48
     */
49
    protected $sSizeBlue;
50
51
    /**
52
     * @throws SetupException
53
     */
54
    public function setUp(): void
55
    {
56
        jaxon()->di()->getPluginManager()->registerPlugins();
57
        jaxon()->setOption('core.response.send', false);
58
        jaxon()->setOption('upload.default.dir', __DIR__ . '/../upload/dst');
59
        _register();
60
61
        $tmpDir = __DIR__ . '/../upload/tmp';
62
        @mkdir($tmpDir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

62
        /** @scrutinizer ignore-unhandled */ @mkdir($tmpDir);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
64
        $sSrcWhite = __DIR__ . '/../upload/src/white.png';
65
        $this->sNameWhite = 'white.png';
66
        $this->sPathWhite = "$tmpDir/{$this->sNameWhite}";
67
        $this->sSizeWhite = filesize($sSrcWhite);
68
        // Copy the file to the temp dir.
69
        @copy($sSrcWhite, $this->sPathWhite);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for copy(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-unhandled */ @copy($sSrcWhite, $this->sPathWhite);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
70
71
        $sSrcBlue = __DIR__ . '/../upload/src/blue.png';
72
        $this->sNameBlue = 'blue.png';
73
        $this->sPathBlue = "$tmpDir/{$this->sNameBlue}";
74
        $this->sSizeBlue = filesize($sSrcBlue);
75
        // Copy the file to the temp dir.
76
        @copy($sSrcBlue, $this->sPathBlue);
77
    }
78
79
    /**
80
     * @throws SetupException
81
     */
82
    public function tearDown(): void
83
    {
84
        jaxon()->reset();
85
        parent::tearDown();
86
    }
87
88
    /**
89
     * @throws RequestException
90
     */
91
    public function testHttpUploadDisabled()
92
    {
93
        jaxon()->setOption('core.upload.enabled', false);
94
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php');
95
96
        // Send a request to the registered class
97
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
98
            return $c->g(ServerRequestCreator::class)
99
                ->fromGlobals()
100
                ->withUploadedFiles([
101
                    'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite,
102
                        UPLOAD_ERR_OK, $this->sNameWhite, 'png'),
103
                ])
104
                ->withMethod('POST');
105
        });
106
107
        $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest());
108
    }
109
110
    /**
111
     * @throws RequestException
112
     */
113
    public function testRequestWithNoPluginNoUpload()
114
    {
115
        jaxon()->setOption('core.upload.enabled', false);
116
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php');
117
118
        // Send a request to the registered class
119
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
120
            return $c->g(ServerRequestCreator::class)
121
                ->fromGlobals()
122
                ->withParsedBody([
123
                    'jxncall' => json_encode([
124
                        'who' => 'Nobody',
125
                        'args' => [],
126
                    ]),
127
                ])
128
                ->withMethod('POST');
129
        });
130
131
        $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest());
132
    }
133
134
    /**
135
     * @throws RequestException
136
     */
137
    public function testUploadInMemoryStorage()
138
    {
139
        jaxon()->setOption('core.upload.enabled', true);
140
        jaxon()->setOption('upload.default.storage', 'memory');
141
        jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php');
142
143
        // Send a request to the registered class
144
        jaxon()->di()->set(ServerRequestInterface::class, function($c) {
145
            return $c->g(ServerRequestCreator::class)
146
                ->fromGlobals()
147
                ->withParsedBody([
148
                    'jxncall' => json_encode([
149
                        'type' => 'class',
150
                        'name' => 'SampleUpload',
151
                        'method' => 'myMethod',
152
                        'args' => [],
153
                    ]),
154
                ])
155
                ->withUploadedFiles([
156
                    'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite,
157
                        UPLOAD_ERR_OK, $this->sNameWhite, 'png'),
158
                ])
159
                ->withMethod('POST');
160
        });
161
162
        $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest());
163
        $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest()));
164
        jaxon()->processRequest();
165
166
        // Uploaded files
167
        $aFiles = jaxon()->upload()->files();
168
        $this->assertCount(1, $aFiles);
169
        $this->assertCount(1, $aFiles['image']);
0 ignored issues
show
Bug introduced by
$aFiles['image'] of type Jaxon\Request\Upload\FileInterface is incompatible with the type Countable|iterable expected by parameter $haystack of PHPUnit\Framework\Assert::assertCount(). ( Ignorable by Annotation )

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

169
        $this->assertCount(1, /** @scrutinizer ignore-type */ $aFiles['image']);
Loading history...
170
        $xFile = $aFiles['image'][0];
171
        $this->assertEquals('white', $xFile->name());
172
        $this->assertEquals($this->sNameWhite, $xFile->filename());
173
        $this->assertEquals('png', $xFile->type());
174
        $this->assertEquals('png', $xFile->extension());
175
    }
176
}
177