Skip to content

Commit

Permalink
# 1.1.0 - 2016-09-10
Browse files Browse the repository at this point in the history
## CHANGE
- Change namespace of UploadedFileTestable trait to easy use in other package unit test.
  • Loading branch information
lopadova committed Sep 10, 2016
1 parent d6cfab5 commit 7ae1177
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All Notable changes to `laravel-request` will be documented in this file

## 1.1.0 - 2016-09-10
### CHANGE
- Change namespace of UploadedFileTestable trait to easy use in other package unit test.

## 1.0.0 - 2016-08-28

- Initial release
70 changes: 70 additions & 0 deletions src/UploadedFileTestable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Padosoft\Laravel\Request;

use Illuminate\Http\UploadedFile;

/**
* Class UploadedFileTestable
* Trait to easy get an istance of Illuminate\Http\UploadedFile for testing.
* @package Padosoft\Laravel\Request
*/
trait UploadedFileTestable
{
/**
* Create an instance of Illuminate\Http\UploadedFile for testing (param test=true).
* Before creating UploadedFile class check if file exists with assertFileExists($fullPath).
* @param string $fullPath
* @param string $mimeType if empty try to resolve mimeType automatically.
* @param int $errorCode default 0 (no error).
* For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage()
* @return UploadedFile
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
*/
public function getUploadedFileForTest(string $fullPath, string $mimeType = '', int $errorCode = 0) : UploadedFile
{
$this->assertFileExists($fullPath);

$uploadedFile = new UploadedFile(
$fullPath,
pathinfo($fullPath, PATHINFO_BASENAME),
($mimeType === null || $mimeType == '') ? mime_content_type($fullPath) : $mimeType,
filesize($fullPath),
$errorCode,
true // true for test
);
return $uploadedFile;
}

/**
* Take $fullPath existing file, copy it to system temp dir with random name and
* Create an instance of Illuminate\Http\UploadedFile for testing (param test=true).
* Before creating UploadedFile class check if file exists with assertFileExists($fullPath).
* @param string $fullPath
* @param string $mimeType if empty try to resolve mimeType automatically.
* @param int $errorCode default 0 (no error).
* For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage()
* @return UploadedFile
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
*/
public function getUploadedFileForTestAndCopyInSysTempDir(
string $fullPath,
string $mimeType = '',
int $errorCode = 0
) : UploadedFile
{
$this->assertFileExists($fullPath);

$origname = pathinfo($fullPath, PATHINFO_BASENAME);
$path = sys_get_temp_dir() . '/' . $origname;
if (file_exists($path)) {
unlink($path);
}
copy($fullPath, $path);

$uploadedFile = $this->getUploadedFileForTest($path, $mimeType, $errorCode);
return $uploadedFile;
}
}
1 change: 1 addition & 0 deletions tests/RequestHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Padosoft\Laravel\Request\RequestHelper;
use Padosoft\Test\traits\ExceptionTestable;
use Padosoft\Test\traits\FileSystemTestable;
use Padosoft\Laravel\Request\UploadedFileTestable;

class RequestHelperTest extends Orchestra
{
Expand Down
1 change: 1 addition & 0 deletions tests/UploadedFileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Padosoft\Laravel\Request\UploadedFileHelper;
use Padosoft\Test\traits\ExceptionTestable;
use Padosoft\Test\traits\FileSystemTestable;
use Padosoft\Laravel\Request\UploadedFileTestable;

class UploadedFileHelperTest extends \PHPUnit_Framework_TestCase
{
Expand Down
32 changes: 0 additions & 32 deletions tests/UploadedFileTestable.php

This file was deleted.

0 comments on commit 7ae1177

Please sign in to comment.