Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

FilesTest::testGetFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Http;
13
14
use PHPUnit\Framework\TestCase;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Message\UploadedFileInterface;
17
use Spiral\Core\Container;
18
use Spiral\Http\Request\InputManager;
19
use Laminas\Diactoros\ServerRequest;
20
use Laminas\Diactoros\UploadedFile;
21
22
class FilesTest extends TestCase
23
{
24
    /**
25
     * @var Container
26
     */
27
    private $container;
28
29
    /**
30
     * @var InputManager
31
     */
32
    private $input;
33
34
    public function setUp(): void
35
    {
36
        $this->container = new Container();
37
        $this->input = new InputManager($this->container);
38
    }
39
40
    public function testShortcut(): void
41
    {
42
        $request = new ServerRequest();
43
        $request = $request->withUploadedFiles([
44
            'file' => new UploadedFile(
45
                fopen(__FILE__, 'r'),
0 ignored issues
show
Bug introduced by
It seems like fopen(__FILE__, 'r') can also be of type false; however, parameter $streamOrFile of Laminas\Diactoros\UploadedFile::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                /** @scrutinizer ignore-type */ fopen(__FILE__, 'r'),
Loading history...
46
                filesize(__FILE__),
47
                0,
48
                __FILE__
49
            )
50
        ]);
51
52
        $this->container->bind(ServerRequestInterface::class, $request);
0 ignored issues
show
Bug introduced by
$request of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ $request);
Loading history...
53
54
        $this->assertInstanceOf(UploadedFileInterface::class, $this->input->file('file'));
55
        $this->assertSame(null, $this->input->file('other'));
56
    }
57
58
    public function testGetFilename(): void
59
    {
60
        $request = new ServerRequest();
61
        $request = $request->withUploadedFiles([
62
            'file' => new UploadedFile(
63
                fopen(__FILE__, 'r'),
0 ignored issues
show
Bug introduced by
It seems like fopen(__FILE__, 'r') can also be of type false; however, parameter $streamOrFile of Laminas\Diactoros\UploadedFile::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
                /** @scrutinizer ignore-type */ fopen(__FILE__, 'r'),
Loading history...
64
                filesize(__FILE__),
65
                0,
66
                __FILE__
67
            )
68
        ]);
69
70
        $this->container->bind(ServerRequestInterface::class, $request);
0 ignored issues
show
Bug introduced by
$request of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ $request);
Loading history...
71
72
73
        $filename = $this->input->files->getFilename('file');
74
        $this->assertTrue(file_exists($filename));
75
76
        $this->assertSame(file_get_contents(__FILE__), file_get_contents($filename));
77
    }
78
79
80
    public function testGetFilenameMissing(): void
81
    {
82
        $request = new ServerRequest();
83
        $request = $request->withUploadedFiles([
84
            'file' => new UploadedFile(
85
                fopen(__FILE__, 'r'),
0 ignored issues
show
Bug introduced by
It seems like fopen(__FILE__, 'r') can also be of type false; however, parameter $streamOrFile of Laminas\Diactoros\UploadedFile::__construct() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                /** @scrutinizer ignore-type */ fopen(__FILE__, 'r'),
Loading history...
86
                filesize(__FILE__),
87
                0,
88
                __FILE__
89
            )
90
        ]);
91
92
        $this->container->bind(ServerRequestInterface::class, $request);
0 ignored issues
show
Bug introduced by
$request of type Laminas\Diactoros\ServerRequest is incompatible with the type array|callable|string expected by parameter $resolver of Spiral\Core\Container::bind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
        $this->container->bind(ServerRequestInterface::class, /** @scrutinizer ignore-type */ $request);
Loading history...
93
94
        $filename = $this->input->files->getFilename('file2');
95
        $this->assertNull($filename);
96
    }
97
}
98