AssetFactoryTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 22.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 19
loc 86
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestFilePath() 0 4 1
A setUp() 0 4 1
A tearDown() 0 4 1
A testAssetPropertiesFromPath() 9 9 1
A testAssetPropertiesFromSplFileInfo() 10 10 1
A testAssetPropertiesFromUploadedFile() 0 16 1
A testAssetInvalidFile() 0 4 1
A testCustomPropertyStorage() 0 10 1
A testAssetInheritance() 0 6 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
/*
4
 * This file is part of the limit0/assets package.
5
 *
6
 * (c) Limit Zero, LLC <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Limit0\Assets\Tests;
13
14
use Limit0\Assets\Asset;
15
use Limit0\Assets\AssetFactory;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
18
/**
19
 * Tests basic AssetFactory functions
20
 *
21
 * @author  Josh Worden <[email protected]>
22
 */
23
class AssetFactoryTest extends \PHPUnit_Framework_TestCase
24
{
25
    private function getTestFilePath()
26
    {
27
        return sprintf('%s/asset-management.test', sys_get_temp_dir());
28
    }
29
30
    public function setUp()
31
    {
32
        file_put_contents($this->getTestFilePath(), 'Lorem ipsum dolor sit amet.');
33
    }
34
35
    public function tearDown()
36
    {
37
        unlink($this->getTestFilePath());
38
    }
39
40 View Code Duplication
    public function testAssetPropertiesFromPath()
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...
41
    {
42
        $asset = AssetFactory::createFromPath($this->getTestFilePath());
43
44
        $this->assertEquals('asset-management.test', $asset->getFilename());
45
        $this->assertEquals('test', $asset->getExtension());
46
        $this->assertEquals($this->getTestFilePath(), $asset->getPathname());
47
        $this->assertEquals('text/plain', $asset->getMimeType());
48
    }
49
50 View Code Duplication
    public function testAssetPropertiesFromSplFileInfo()
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...
51
    {
52
        $file = new \SplFileInfo($this->getTestFilePath());
53
        $asset = AssetFactory::createFromSplFileInfo($file);
54
55
        $this->assertEquals('asset-management.test', $asset->getFilename());
56
        $this->assertEquals('test', $asset->getExtension());
57
        $this->assertEquals($this->getTestFilePath(), $asset->getPathname());
58
        $this->assertEquals('text/plain', $asset->getMimeType());
59
    }
60
61
    public function testAssetPropertiesFromUploadedFile()
62
    {
63
        $clientName = 'client_original_name.doc';
64
        $clientType = 'application/banana';
65
        $file = new UploadedFile($this->getTestFilePath(), $clientName, $clientType);
66
        $asset = AssetFactory::createFromUploadedFile($file);
67
68
        $this->assertEquals('asset-management.test', $asset->getFilename());
69
        $this->assertEquals('test', $asset->getExtension());
70
        $this->assertEquals($this->getTestFilePath(), $asset->getPathname());
71
        $this->assertEquals('text/plain', $asset->getMimeType());
72
73
        $this->assertEquals($clientName, $asset->getClientOriginalName());
74
        $this->assertEquals('doc', $asset->getClientOriginalExtension());
75
        $this->assertEquals($clientType, $asset->getClientMimeType());
76
    }
77
78
    /**
79
     * @expectedException        Limit0\Assets\Exception\FactoryException
80
     * @expectedExceptionCode    0
81
     * @expectedExceptionMessage Unable to open file /tmp/file/does/not/exist
82
     */
83
    public function testAssetInvalidFile()
84
    {
85
        AssetFactory::createFromPath('/tmp/file/does/not/exist');
86
    }
87
88
    public function testCustomPropertyStorage()
89
    {
90
        $asset = AssetFactory::createFromPath($this->getTestFilePath());
91
92
        $filePath = 'some/test/value';
93
94
        $asset->setFilePath($filePath);
95
96
        $this->assertEquals($filePath, $asset->getFilePath());
97
    }
98
99
    /**
100
     *
101
     */
102
    public function testAssetInheritance()
103
    {
104
        $asset = AssetFactory::createFromPath($this->getTestFilePath());
105
        $this->assertInstanceOf('Symfony\Component\HttpFoundation\File\UploadedFile', $asset);
106
        $this->assertInstanceOf('SplFileInfo', $asset);
107
    }
108
}
109