Completed
Push — 2.0 ( 60b181...751b46 )
by Nicolas
03:03
created

FileTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 10.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 3
dl 11
loc 107
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A it_creates_a_file() 0 6 1
A it_should_return_media_path_value_object_on_path_attribtue() 0 6 1
A it_should_cast_the_path_value_object_to_string() 0 6 1
A it_should_guess_the_media_type_of_object() 0 6 1
A it_can_check_if_file_is_an_image() 0 9 1
A it_can_get_the_thumbnail() 0 6 1
A it_wont_get_thumbnail_of_non_image_file() 0 6 1
A createFile() 11 11 1
A resetDatabase() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Modules\Media\Tests;
4
5
use Modules\Media\Entities\File;
6
use Modules\Media\Repositories\FileRepository;
7
use Modules\Media\ValueObjects\MediaPath;
8
9
class FileTest extends MediaTestCase
10
{
11
    /**
12
     * @var FileRepository
13
     */
14
    private $file;
15
16
    public function setUp()
17
    {
18
        parent::setUp();
19
20
        $this->resetDatabase();
21
22
        $this->file = app(FileRepository::class);
23
    }
24
25
    /** @test */
26
    public function it_creates_a_file()
27
    {
28
        $this->createFile('my/file/name.jpg');
29
30
        $this->assertCount(1, $this->file->all());
31
    }
32
33
    /** @test */
34
    public function it_should_return_media_path_value_object_on_path_attribtue()
35
    {
36
        $file = $this->createFile('my/file/name.jpg');
37
38
        $this->assertInstanceOf(MediaPath::class, $file->path);
39
    }
40
41
    /** @test */
42
    public function it_should_cast_the_path_value_object_to_string()
43
    {
44
        $file = $this->createFile('my/file/name.jpg');
45
46
        $this->assertEquals('http://localhost/my/file/name.jpg', $file->path_string);
0 ignored issues
show
Documentation introduced by
The property path_string does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
    }
48
49
    /** @test */
50
    public function it_should_guess_the_media_type_of_object()
51
    {
52
        $file = $this->createFile('my/file/name.jpg');
53
54
        $this->assertEquals('image', $file->media_type);
0 ignored issues
show
Documentation introduced by
The property media_type does not exist on object<Modules\Media\Entities\File>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
    }
56
57
    /** @test */
58
    public function it_can_check_if_file_is_an_image()
59
    {
60
        $this->assertTrue($this->createFile('my/file/name.jpg')->isImage());
61
        $this->assertTrue($this->createFile('my/file/name.png')->isImage());
62
        $this->assertTrue($this->createFile('my/file/name.jpeg')->isImage());
63
        $this->assertTrue($this->createFile('my/file/name.gif')->isImage());
64
        $this->assertFalse($this->createFile('my/file/name.pdf')->isImage());
65
        $this->assertFalse($this->createFile('my/file/name.doc')->isImage());
66
    }
67
68
    /** @test */
69
    public function it_can_get_the_thumbnail()
70
    {
71
        $file = $this->createFile('my/file/name.jpg');
72
73
        $this->assertEquals('http://localhost/name_smallThumb.jpg', $file->getThumbnail('smallThumb'));
74
    }
75
76
    /** @test */
77
    public function it_wont_get_thumbnail_of_non_image_file()
78
    {
79
        $file = $this->createFile('my/file/name.pdf');
80
81
        $this->assertFalse($file->getThumbnail('smallThumb'));
82
    }
83
84 View Code Duplication
    private function createFile($fileName = 'random/name.jpg')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        return File::create([
87
            'filename' => $fileName,
88
            'path' => config('asgard.media.config.files-path') . $fileName,
89
            'extension' => substr(strrchr($fileName, "."), 1),
90
            'mimetype' => 'image/jpg',
91
            'filesize' => '1024',
92
            'folder_id' => 0,
93
        ]);
94
    }
95
96
    private function resetDatabase()
97
    {
98
        // Relative to the testbench app folder: vendors/orchestra/testbench/src/fixture
99
        $migrationsPath = 'Database/Migrations';
100
        // Makes sure the migrations table is created
101
        $this->artisan('migrate', [
102
            '--database' => 'sqlite',
103
            '--path'     => $migrationsPath,
104
        ]);
105
        // We empty all tables
106
        $this->artisan('migrate:reset', [
107
            '--database' => 'sqlite',
108
        ]);
109
        // Migrate
110
        $this->artisan('migrate', [
111
            '--database' => 'sqlite',
112
            '--path'     => $migrationsPath,
113
        ]);
114
    }
115
}
116