From 5624c4b2bbd52ec6634f98c98e9d8b2432c2b5e0 Mon Sep 17 00:00:00 2001 From: DeveloperMarius Date: Wed, 24 Mar 2021 13:11:07 +0100 Subject: [PATCH 1/3] testFiles and test file content on server --- tests/Pecee/SimpleRouter/InputHandlerTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Pecee/SimpleRouter/InputHandlerTest.php b/tests/Pecee/SimpleRouter/InputHandlerTest.php index 7fcb87b..99bd83e 100644 --- a/tests/Pecee/SimpleRouter/InputHandlerTest.php +++ b/tests/Pecee/SimpleRouter/InputHandlerTest.php @@ -113,6 +113,38 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase public function testFile() { + global $_FILES; + + $_FILES = array( + 'test' => array( + 'name' => 'test.txt', + 'type' => 'text/plain', + 'tmp_name' => '/tmp/phpYfWUiw', + 'error' => 0, + 'size' => 4 + ) + ); + + $router = TestRouter::router(); + $router->reset(); + $router->getRequest()->setMethod('post'); + + $handler = TestRouter::request()->getInputHandler(); + $file = $handler->file('test'); + $this->assertInstanceOf(\Pecee\Http\Input\InputFile::class, $file); + $this->assertEquals('test.txt', $file->getFilename()); + $this->assertEquals('text/plain', $file->getType()); + $this->assertEquals('/tmp/phpYfWUiw', $file->getTmpName()); + $this->assertEquals(0, $file->getError()); + $this->assertEquals(4, $file->getSize()); + $this->assertEquals('txt', $file->getExtension()); + + + if(file_exists('/tmp')){ + file_put_contents('/tmp/phpYfWUiw', 'test'); + $this->assertEquals('test', $file->getContents()); + } + // TODO: implement test-file $this->assertEquals(true, true); } From fef65313e5d4744a0d5910bc5da379347b1df92c Mon Sep 17 00:00:00 2001 From: DeveloperMarius Date: Wed, 24 Mar 2021 13:20:41 +0100 Subject: [PATCH 2/3] Added file content test for all platforms --- tests/Pecee/SimpleRouter/InputHandlerTest.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/Pecee/SimpleRouter/InputHandlerTest.php b/tests/Pecee/SimpleRouter/InputHandlerTest.php index 99bd83e..163ab67 100644 --- a/tests/Pecee/SimpleRouter/InputHandlerTest.php +++ b/tests/Pecee/SimpleRouter/InputHandlerTest.php @@ -114,12 +114,13 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase public function testFile() { global $_FILES; + $temp_dir = sys_get_temp_dir(); $_FILES = array( 'test' => array( 'name' => 'test.txt', 'type' => 'text/plain', - 'tmp_name' => '/tmp/phpYfWUiw', + 'tmp_name' => $temp_dir . '/phpYfWUiw', 'error' => 0, 'size' => 4 ) @@ -134,19 +135,16 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase $this->assertInstanceOf(\Pecee\Http\Input\InputFile::class, $file); $this->assertEquals('test.txt', $file->getFilename()); $this->assertEquals('text/plain', $file->getType()); - $this->assertEquals('/tmp/phpYfWUiw', $file->getTmpName()); + $this->assertEquals($temp_dir . '/phpYfWUiw', $file->getTmpName()); $this->assertEquals(0, $file->getError()); $this->assertEquals(4, $file->getSize()); $this->assertEquals('txt', $file->getExtension()); + file_put_contents($temp_dir . '/phpYfWUiw', 'test'); + $this->assertEquals('test', $file->getContents()); - if(file_exists('/tmp')){ - file_put_contents('/tmp/phpYfWUiw', 'test'); - $this->assertEquals('test', $file->getContents()); - } - - // TODO: implement test-file - $this->assertEquals(true, true); + //cleanup + unlink($temp_dir . '/phpYfWUiw'); } public function testFiles() From 3e1333ccd4447fab1df81c813b0e55ec8c0a85c8 Mon Sep 17 00:00:00 2001 From: DeveloperMarius Date: Wed, 24 Mar 2021 13:32:15 +0100 Subject: [PATCH 3/3] Defined variables for the file data --- tests/Pecee/SimpleRouter/InputHandlerTest.php | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/tests/Pecee/SimpleRouter/InputHandlerTest.php b/tests/Pecee/SimpleRouter/InputHandlerTest.php index 163ab67..fc3baf1 100644 --- a/tests/Pecee/SimpleRouter/InputHandlerTest.php +++ b/tests/Pecee/SimpleRouter/InputHandlerTest.php @@ -1,5 +1,7 @@ 'test.txt', + 'type' => 'text/plain', + 'tmp_name' => $temp_dir . '/phpYfWUiw', + 'error' => 0, + 'size' => 4 + ); + $test_file_content = 'test_content'; + $_FILES = array( - 'test' => array( - 'name' => 'test.txt', - 'type' => 'text/plain', - 'tmp_name' => $temp_dir . '/phpYfWUiw', - 'error' => 0, - 'size' => 4 - ) + $test_file_input_name => $test_file ); $router = TestRouter::router(); @@ -131,20 +137,20 @@ class InputHandlerTest extends \PHPUnit\Framework\TestCase $router->getRequest()->setMethod('post'); $handler = TestRouter::request()->getInputHandler(); - $file = $handler->file('test'); - $this->assertInstanceOf(\Pecee\Http\Input\InputFile::class, $file); - $this->assertEquals('test.txt', $file->getFilename()); - $this->assertEquals('text/plain', $file->getType()); - $this->assertEquals($temp_dir . '/phpYfWUiw', $file->getTmpName()); - $this->assertEquals(0, $file->getError()); - $this->assertEquals(4, $file->getSize()); - $this->assertEquals('txt', $file->getExtension()); + $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()); - file_put_contents($temp_dir . '/phpYfWUiw', 'test'); - $this->assertEquals('test', $file->getContents()); + file_put_contents($test_file['tmp_name'], $test_file_content); + $this->assertEquals($test_file_content, $file->getContents()); //cleanup - unlink($temp_dir . '/phpYfWUiw'); + unlink($test_file['tmp_name']); } public function testFiles()