jaxon-php /
jaxon-mono
| 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 League\Flysystem\InMemory\InMemoryFilesystemAdapter; |
||||
| 9 | use Nyholm\Psr7\UploadedFile; |
||||
| 10 | use Nyholm\Psr7Server\ServerRequestCreator; |
||||
| 11 | use Psr\Http\Message\ServerRequestInterface; |
||||
| 12 | use PHPUnit\Framework\TestCase; |
||||
| 13 | |||||
| 14 | use function Jaxon\jaxon; |
||||
| 15 | use function Jaxon\Upload\_register as _registerUpload; |
||||
| 16 | use function copy; |
||||
| 17 | use function filesize; |
||||
| 18 | use function mkdir; |
||||
| 19 | |||||
| 20 | class UploadFsLocalTest extends TestCase |
||||
| 21 | { |
||||
| 22 | /** |
||||
| 23 | * @var string |
||||
| 24 | */ |
||||
| 25 | protected $sNameWhite; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @var string |
||||
| 29 | */ |
||||
| 30 | protected $sPathWhite; |
||||
| 31 | |||||
| 32 | /** |
||||
| 33 | * @var int |
||||
| 34 | */ |
||||
| 35 | protected $sSizeWhite; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * @var string |
||||
| 39 | */ |
||||
| 40 | protected $sNameBlue; |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * @var string |
||||
| 44 | */ |
||||
| 45 | protected $sPathBlue; |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * @var int |
||||
| 49 | */ |
||||
| 50 | protected $sSizeBlue; |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * @throws SetupException |
||||
| 54 | */ |
||||
| 55 | public function setUp(): void |
||||
| 56 | { |
||||
| 57 | jaxon()->di()->getPluginManager()->registerPlugins(); |
||||
| 58 | jaxon()->setOption('core.response.send', false); |
||||
| 59 | jaxon()->setAppOption('upload.default.storage', 'uploads'); |
||||
| 60 | jaxon()->setAppOptions([ |
||||
| 61 | 'adapter' => 'local', |
||||
| 62 | 'dir' => __DIR__ . '/../upload/dst', |
||||
| 63 | // 'options' => [], |
||||
| 64 | ], 'storage.stores.uploads'); |
||||
| 65 | |||||
| 66 | _registerUpload(); |
||||
| 67 | |||||
| 68 | $tmpDir = __DIR__ . '/../upload/tmp'; |
||||
| 69 | @mkdir($tmpDir); |
||||
|
0 ignored issues
–
show
|
|||||
| 70 | |||||
| 71 | $sSrcWhite = __DIR__ . '/../upload/src/white.png'; |
||||
| 72 | $this->sNameWhite = 'white.png'; |
||||
| 73 | $this->sPathWhite = "$tmpDir/{$this->sNameWhite}"; |
||||
| 74 | $this->sSizeWhite = filesize($sSrcWhite); |
||||
| 75 | // Copy the file to the temp dir. |
||||
| 76 | @copy($sSrcWhite, $this->sPathWhite); |
||||
|
0 ignored issues
–
show
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
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...
|
|||||
| 77 | |||||
| 78 | $sSrcBlue = __DIR__ . '/../upload/src/blue.png'; |
||||
| 79 | $this->sNameBlue = 'blue.png'; |
||||
| 80 | $this->sPathBlue = "$tmpDir/{$this->sNameBlue}"; |
||||
| 81 | $this->sSizeBlue = filesize($sSrcBlue); |
||||
| 82 | // Copy the file to the temp dir. |
||||
| 83 | @copy($sSrcBlue, $this->sPathBlue); |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * @throws SetupException |
||||
| 88 | */ |
||||
| 89 | public function tearDown(): void |
||||
| 90 | { |
||||
| 91 | jaxon()->reset(); |
||||
| 92 | parent::tearDown(); |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * @throws RequestException |
||||
| 97 | */ |
||||
| 98 | public function testHttpUploadDisabled() |
||||
| 99 | { |
||||
| 100 | jaxon()->setAppOption('upload.enabled', false); |
||||
| 101 | jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
||||
| 102 | jaxon()->di()->getBootstrap()->onBoot(); |
||||
| 103 | |||||
| 104 | // Send a request to the registered class |
||||
| 105 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 106 | return $c->g(ServerRequestCreator::class) |
||||
| 107 | ->fromGlobals() |
||||
| 108 | ->withUploadedFiles([ |
||||
| 109 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 110 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 111 | ]) |
||||
| 112 | ->withMethod('POST'); |
||||
| 113 | }); |
||||
| 114 | |||||
| 115 | $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | /** |
||||
| 119 | * @throws RequestException |
||||
| 120 | */ |
||||
| 121 | public function testRequestWithNoPluginNoUpload() |
||||
| 122 | { |
||||
| 123 | jaxon()->setAppOption('upload.enabled', false); |
||||
| 124 | jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
||||
| 125 | jaxon()->di()->getBootstrap()->onBoot(); |
||||
| 126 | |||||
| 127 | // Send a request to the registered class |
||||
| 128 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 129 | return $c->g(ServerRequestCreator::class) |
||||
| 130 | ->fromGlobals() |
||||
| 131 | ->withParsedBody([ |
||||
| 132 | 'jxncall' => json_encode([ |
||||
| 133 | 'who' => 'Nobody', |
||||
| 134 | 'args' => [], |
||||
| 135 | ]), |
||||
| 136 | ]) |
||||
| 137 | ->withMethod('POST'); |
||||
| 138 | }); |
||||
| 139 | |||||
| 140 | $this->assertFalse(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * @throws RequestException |
||||
| 145 | */ |
||||
| 146 | public function testUploadInMemoryStorage() |
||||
| 147 | { |
||||
| 148 | jaxon()->setAppOption('upload.enabled', true); |
||||
| 149 | jaxon()->setAppOption('upload.default.storage', 'memory'); |
||||
| 150 | jaxon()->setAppOptions([ |
||||
| 151 | 'adapter' => 'memory', |
||||
| 152 | 'dir' => __DIR__ . '/../upload/dst', |
||||
| 153 | // 'options' => [], |
||||
| 154 | ], 'storage.stores.memory'); |
||||
| 155 | |||||
| 156 | jaxon()->register(Jaxon::CALLABLE_CLASS, 'SampleUpload', __DIR__ . '/../src/sample.php'); |
||||
| 157 | jaxon()->di()->getBootstrap()->onBoot(); |
||||
| 158 | |||||
| 159 | // In memory file system adapter |
||||
| 160 | jaxon()->upload()->registerStorageAdapter('memory', |
||||
| 161 | fn() => new InMemoryFilesystemAdapter()); |
||||
| 162 | |||||
| 163 | // Send a request to the registered class |
||||
| 164 | jaxon()->di()->set(ServerRequestInterface::class, function($c) { |
||||
| 165 | return $c->g(ServerRequestCreator::class) |
||||
| 166 | ->fromGlobals() |
||||
| 167 | ->withParsedBody([ |
||||
| 168 | 'jxncall' => json_encode([ |
||||
| 169 | 'type' => 'class', |
||||
| 170 | 'name' => 'SampleUpload', |
||||
| 171 | 'method' => 'myMethod', |
||||
| 172 | 'args' => [], |
||||
| 173 | ]), |
||||
| 174 | ]) |
||||
| 175 | ->withUploadedFiles([ |
||||
| 176 | 'image' => new UploadedFile($this->sPathWhite, $this->sSizeWhite, |
||||
| 177 | UPLOAD_ERR_OK, $this->sNameWhite, 'png'), |
||||
| 178 | ]) |
||||
| 179 | ->withMethod('POST'); |
||||
| 180 | }); |
||||
| 181 | |||||
| 182 | $this->assertTrue(jaxon()->di()->getRequestHandler()->canProcessRequest()); |
||||
| 183 | $this->assertTrue(jaxon()->di()->getUploadHandler()->canProcessRequest(jaxon()->di()->getRequest())); |
||||
| 184 | jaxon()->processRequest(); |
||||
| 185 | |||||
| 186 | // Uploaded files |
||||
| 187 | $aFiles = jaxon()->upload()->files(); |
||||
| 188 | $this->assertCount(1, $aFiles); |
||||
| 189 | $this->assertCount(1, $aFiles['image']); |
||||
|
0 ignored issues
–
show
$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
Loading history...
|
|||||
| 190 | $xFile = $aFiles['image'][0]; |
||||
| 191 | $this->assertEquals('white', $xFile->name()); |
||||
| 192 | $this->assertEquals($this->sNameWhite, $xFile->filename()); |
||||
| 193 | $this->assertEquals('png', $xFile->type()); |
||||
| 194 | $this->assertEquals('png', $xFile->extension()); |
||||
| 195 | } |
||||
| 196 | } |
||||
| 197 |
If you suppress an error, we recommend checking for the error condition explicitly: