Passed
Push — master ( 360d9f...31271b )
by Alexander
04:18 queued 01:59
created

FileLoaderBehaviorTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Horat1us\Yii\Tests\Behaviors;
4
5
use Faker\Factory;
6
use Horat1us\Yii\Tests\AbstractTestCase;
7
use Horat1us\Yii\Tests\Mocks\FileLoaderBehaviorMock;
8
use yii\web\UploadedFile;
9
10
class FileLoaderBehaviorTest extends AbstractTestCase
11
{
12
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
13
    {
14
        $faker = Factory::create();
15
        $file = $faker->image();
16
        $_FILES = [
17
            'FileLoaderBehaviorMock' => [
18
                'name' => [
19
                    'file' => $faker->name,
20
                ],
21
                'type' => [
22
                    'file' => 'application/octet-stream',
23
                ],
24
                'tmp_name' => [
25
                    'file' => $file,
26
                ],
27
                'error' => [
28
                    'file' => 0,
29
                ],
30
                'size' => [
31
                    'file' => filesize($file),
32
                ],
33
            ],
34
        ];
35
        return parent::setUp();
36
    }
37
38
    public function testLoadingSingleFile()
39
    {
40
        $model = new FileLoaderBehaviorMock();
41
42
        $model->validate();
43
        $this->assertInstanceOf(UploadedFile::class, $model->file);
44
    }
45
46
    public function testLoadingMultipleFiles()
47
    {
48
        $model = new FileLoaderBehaviorMock(['multiple' => true,]);
49
        $model->validate();
50
        $this->assertNotEmpty($model->file);
51
        $this->assertTrue(is_array($model->file));
52
        $this->assertGreaterThanOrEqual(1, count($model->file));
53
    }
54
}
55