[FEATURE] Added better support for nested file/arrays in InputHandler.

- Added unit tests for file arrays
- Removed legacy .yml configs
This commit is contained in:
Simon Sessingø
2021-03-25 05:58:49 +01:00
parent fdf11bbc9c
commit 86bb88a41f
5 changed files with 92 additions and 92 deletions

View File

@@ -1,22 +0,0 @@
engines:
phpmd:
enabled: true
checks:
Design/TooManyPublicMethods:
enabled: true
Naming/ShortVariable:
enabled: true
CleanCode/StaticAccess:
enabled: true
Controversial/CamelCaseMethodName:
enabled: true
fixme:
enabled: true
duplication:
enabled: true
config:
languages:
- php:
ratings:
paths:
- src/**

View File

@@ -1,13 +0,0 @@
build:
tests:
override:
-
command: './vendor/bin/phpunit --coverage-clover=coverage.clover'
coverage:
file: 'coverage.clover'
format: 'clover'
checks:
php:
code_rating: true
duplication: true

View File

@@ -1,25 +0,0 @@
language: php
php:
- 7.4.2
before_script:
- mkdir -p _clover
- ls -al
script:
- ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml ./tests
install:
# Install composer packages
- travis_retry composer install --no-interaction --no-suggest
# Install coveralls.phar
- wget -c -nc --retry-connrefused --tries=0 https://github.com/php-coveralls/php-coveralls/releases/download/v2.0.0/php-coveralls.phar -O coveralls.phar
- chmod +x coveralls.phar
- php coveralls.phar --version
after_success:
# Submit coverage report to Coveralls servers, see .coveralls.yml
- travis_retry php coveralls.phar -v
# Submit coverage report to codecov.io
- bash <(curl -s https://codecov.io/bash)

View File

@@ -92,22 +92,29 @@ class InputHandler
/* Parse get requests */
if (\count($_FILES) !== 0) {
$this->originalFile = $_FILES;
$this->file = $this->parseFiles();
$this->file = $this->parseFiles($this->originalFile);
}
}
/**
* @return array
*/
public function parseFiles(): array
public function parseFiles(array $files, $parentKey = null): array
{
$list = [];
foreach ($_FILES as $key => $value) {
foreach ($files as $key => $value) {
// Parse multi dept file array
if(isset($value['name']) === false && \is_array($value) === true) {
$list[$key] = $this->parseFiles($value, $key);
continue;
}
// Handle array input
if (\is_array($value['name']) === false) {
$values['index'] = $key;
$values['index'] = $parentKey ?? $key;
try {
$list[$key] = InputFile::createFromArray($values + $value);
} catch (InvalidArgumentException $e) {

View File

@@ -122,50 +122,87 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$_GET = [];
}
public function testFile()
{
global $_FILES;
$temp_dir = sys_get_temp_dir();
$test_file_input_name = 'test_input';
$test_file = array(
'name' => 'test.txt',
'type' => 'text/plain',
'tmp_name' => $temp_dir . '/phpYfWUiw',
'error' => 0,
'size' => 4
);
$test_file_content = 'test_content';
$testFile = $this->generateFile();
$_FILES = array(
$test_file_input_name => $test_file
);
$_FILES = [
'test_input' => $testFile,
];
$router = TestRouter::router();
$router->reset();
$router->getRequest()->setMethod('post');
$inputHandler = TestRouter::request()->getInputHandler();
$testFileContent = md5(uniqid('test', false));
$file = $inputHandler->file('test_input');
$handler = TestRouter::request()->getInputHandler();
$file = $handler->file($test_file_input_name);
$this->assertInstanceOf(InputFile::class, $file);
$this->assertEquals($test_file['name'], $file->getFilename());
$this->assertEquals($test_file['type'], $file->getType());
$this->assertEquals($test_file['tmp_name'], $file->getTmpName());
$this->assertEquals($test_file['error'], $file->getError());
$this->assertEquals($test_file['size'], $file->getSize());
$this->assertEquals(pathinfo($test_file['name'], PATHINFO_EXTENSION), $file->getExtension());
$this->assertEquals($testFile['name'], $file->getFilename());
$this->assertEquals($testFile['type'], $file->getType());
$this->assertEquals($testFile['tmp_name'], $file->getTmpName());
$this->assertEquals($testFile['error'], $file->getError());
$this->assertEquals($testFile['size'], $file->getSize());
$this->assertEquals(pathinfo($testFile['name'], PATHINFO_EXTENSION), $file->getExtension());
file_put_contents($test_file['tmp_name'], $test_file_content);
$this->assertEquals($test_file_content, $file->getContents());
file_put_contents($testFile['tmp_name'], $testFileContent);
$this->assertEquals($testFileContent, $file->getContents());
//cleanup
unlink($test_file['tmp_name']);
// Cleanup
unlink($testFile['tmp_name']);
}
public function testFiles()
public function testFilesArray()
{
// TODO: implement test-files
$this->assertEquals(true, true);
global $_FILES;
$testFiles = [
$file = $this->generateFile(),
$file = $this->generateFile(),
$file = $this->generateFile(),
$file = $this->generateFile(),
$file = $this->generateFile(),
];
$_FILES = [
'my_files' => $testFiles,
];
$router = TestRouter::router();
$router->reset();
$router->getRequest()->setMethod('post');
$inputHandler = TestRouter::request()->getInputHandler();
$files = $inputHandler->file('my_files');
$this->assertCount(5, $files);
/* @var $file InputFile */
foreach ($files as $key => $file) {
$testFileContent = md5(uniqid('test', false));
$this->assertInstanceOf(InputFile::class, $file);
$this->assertEquals($testFiles[$key]['name'], $file->getFilename());
$this->assertEquals($testFiles[$key]['type'], $file->getType());
$this->assertEquals($testFiles[$key]['tmp_name'], $file->getTmpName());
$this->assertEquals($testFiles[$key]['error'], $file->getError());
$this->assertEquals($testFiles[$key]['size'], $file->getSize());
$this->assertEquals(pathinfo($testFiles[$key]['name'], PATHINFO_EXTENSION), $file->getExtension());
file_put_contents($testFiles[$key]['tmp_name'], $testFileContent);
$this->assertEquals($testFileContent, $file->getContents());
// Cleanup
unlink($testFiles[$key]['tmp_name']);
}
}
public function testAll()
@@ -218,4 +255,20 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
$_POST = [];
}
protected function generateFile()
{
return [
'name' => uniqid('', false) . '.txt',
'type' => 'text/plain',
'tmp_name' => sys_get_temp_dir() . '/phpYfWUiw',
'error' => 0,
'size' => rand(3, 40),
];
}
protected function generateFileContent()
{
return md5(uniqid('', false));
}
}