Completed
Push — master ( d6cfab...7ae117 )
by Lorenzo
10:24
created

UploadedFileTestable::getUploadedFileForTest()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 1
nop 3
1
<?php
2
3
namespace Padosoft\Laravel\Request;
4
5
use Illuminate\Http\UploadedFile;
6
7
/**
8
 * Class UploadedFileTestable
9
 * Trait to easy get an istance of Illuminate\Http\UploadedFile for testing.
10
 * @package Padosoft\Laravel\Request
11
 */
12
trait UploadedFileTestable
13
{
14
    /**
15
     * Create an instance of Illuminate\Http\UploadedFile for testing (param test=true).
16
     * Before creating UploadedFile class check if file exists with assertFileExists($fullPath).
17
     * @param string $fullPath
18
     * @param string $mimeType if empty try to resolve mimeType automatically.
19
     * @param int $errorCode default 0 (no error).
20
     * For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage()
21
     * @return UploadedFile
22
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
23
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
24
     */
25
    public function getUploadedFileForTest(string $fullPath, string $mimeType = '', int $errorCode = 0) : UploadedFile
26
    {
27
        $this->assertFileExists($fullPath);
0 ignored issues
show
Bug introduced by
It seems like assertFileExists() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
29
        $uploadedFile = new UploadedFile(
30
            $fullPath,
31
            pathinfo($fullPath, PATHINFO_BASENAME),
32
            ($mimeType === null || $mimeType == '') ? mime_content_type($fullPath) : $mimeType,
33
            filesize($fullPath),
34
            $errorCode,
35
            true // true for test
36
        );
37
        return $uploadedFile;
38
    }
39
40
    /**
41
     * Take $fullPath existing file, copy it to system temp dir with random name and
42
     * Create an instance of Illuminate\Http\UploadedFile for testing (param test=true).
43
     * Before creating UploadedFile class check if file exists with assertFileExists($fullPath).
44
     * @param string $fullPath
45
     * @param string $mimeType if empty try to resolve mimeType automatically.
46
     * @param int $errorCode default 0 (no error).
47
     * For all possible values see Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage()
48
     * @return UploadedFile
49
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
50
     * @throws \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
51
     */
52
    public function getUploadedFileForTestAndCopyInSysTempDir(
53
        string $fullPath,
54
        string $mimeType = '',
55
        int $errorCode = 0
56
    ) : UploadedFile
57
    {
58
        $this->assertFileExists($fullPath);
0 ignored issues
show
Bug introduced by
It seems like assertFileExists() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
60
        $origname = pathinfo($fullPath, PATHINFO_BASENAME);
61
        $path = sys_get_temp_dir() . '/' . $origname;
62
        if (file_exists($path)) {
63
            unlink($path);
64
        }
65
        copy($fullPath, $path);
66
67
        $uploadedFile = $this->getUploadedFileForTest($path, $mimeType, $errorCode);
68
        return $uploadedFile;
69
    }
70
}
71