mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 08:47:52 +00:00
[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:
@@ -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/**
|
|
||||||
@@ -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
|
|
||||||
|
|
||||||
-25
@@ -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)
|
|
||||||
@@ -92,22 +92,29 @@ class InputHandler
|
|||||||
/* Parse get requests */
|
/* Parse get requests */
|
||||||
if (\count($_FILES) !== 0) {
|
if (\count($_FILES) !== 0) {
|
||||||
$this->originalFile = $_FILES;
|
$this->originalFile = $_FILES;
|
||||||
$this->file = $this->parseFiles();
|
$this->file = $this->parseFiles($this->originalFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function parseFiles(): array
|
public function parseFiles(array $files, $parentKey = null): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$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
|
// Handle array input
|
||||||
if (\is_array($value['name']) === false) {
|
if (\is_array($value['name']) === false) {
|
||||||
$values['index'] = $key;
|
$values['index'] = $parentKey ?? $key;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$list[$key] = InputFile::createFromArray($values + $value);
|
$list[$key] = InputFile::createFromArray($values + $value);
|
||||||
} catch (InvalidArgumentException $e) {
|
} catch (InvalidArgumentException $e) {
|
||||||
|
|||||||
@@ -122,50 +122,87 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
|||||||
$_GET = [];
|
$_GET = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function testFile()
|
public function testFile()
|
||||||
{
|
{
|
||||||
global $_FILES;
|
global $_FILES;
|
||||||
$temp_dir = sys_get_temp_dir();
|
|
||||||
|
|
||||||
$test_file_input_name = 'test_input';
|
$testFile = $this->generateFile();
|
||||||
$test_file = array(
|
|
||||||
'name' => 'test.txt',
|
|
||||||
'type' => 'text/plain',
|
|
||||||
'tmp_name' => $temp_dir . '/phpYfWUiw',
|
|
||||||
'error' => 0,
|
|
||||||
'size' => 4
|
|
||||||
);
|
|
||||||
$test_file_content = 'test_content';
|
|
||||||
|
|
||||||
$_FILES = array(
|
$_FILES = [
|
||||||
$test_file_input_name => $test_file
|
'test_input' => $testFile,
|
||||||
);
|
];
|
||||||
|
|
||||||
$router = TestRouter::router();
|
$router = TestRouter::router();
|
||||||
$router->reset();
|
$router->reset();
|
||||||
$router->getRequest()->setMethod('post');
|
$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->assertInstanceOf(InputFile::class, $file);
|
||||||
$this->assertEquals($test_file['name'], $file->getFilename());
|
$this->assertEquals($testFile['name'], $file->getFilename());
|
||||||
$this->assertEquals($test_file['type'], $file->getType());
|
$this->assertEquals($testFile['type'], $file->getType());
|
||||||
$this->assertEquals($test_file['tmp_name'], $file->getTmpName());
|
$this->assertEquals($testFile['tmp_name'], $file->getTmpName());
|
||||||
$this->assertEquals($test_file['error'], $file->getError());
|
$this->assertEquals($testFile['error'], $file->getError());
|
||||||
$this->assertEquals($test_file['size'], $file->getSize());
|
$this->assertEquals($testFile['size'], $file->getSize());
|
||||||
$this->assertEquals(pathinfo($test_file['name'], PATHINFO_EXTENSION), $file->getExtension());
|
$this->assertEquals(pathinfo($testFile['name'], PATHINFO_EXTENSION), $file->getExtension());
|
||||||
|
|
||||||
file_put_contents($test_file['tmp_name'], $test_file_content);
|
file_put_contents($testFile['tmp_name'], $testFileContent);
|
||||||
$this->assertEquals($test_file_content, $file->getContents());
|
$this->assertEquals($testFileContent, $file->getContents());
|
||||||
|
|
||||||
//cleanup
|
// Cleanup
|
||||||
unlink($test_file['tmp_name']);
|
unlink($testFile['tmp_name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFiles()
|
public function testFilesArray()
|
||||||
{
|
{
|
||||||
// TODO: implement test-files
|
global $_FILES;
|
||||||
$this->assertEquals(true, true);
|
|
||||||
|
$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()
|
public function testAll()
|
||||||
@@ -218,4 +255,20 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase
|
|||||||
$_POST = [];
|
$_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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user