FileHandlerTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 18
loc 96
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A createKernel() 0 5 1
A createFileHandler() 0 4 1
A createFile() 0 7 1
A testConstruction() 0 5 1
A testFactoryReturnsAValidFileHandlerObject() 0 7 1
A testUpdate() 0 5 1
A testDelete() 0 9 1
A testUnDelete() 0 7 1
A testSave() 9 9 1
A testAccessKeyIsValid() 9 9 1
A testCreateTemporaryFile() 0 5 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
require_once 'Intraface/functions.php';
3
require_once 'Intraface/modules/filemanager/FileHandler.php';
4
require_once 'file_functions.php';
5
6
class FileHandlerTest extends PHPUnit_Framework_TestCase
7
{
8
    private $file_name = 'tester.jpg';
9
10
    function setUp()
11
    {
12
        $db = MDB2::singleton(DB_DSN);
13
        $db->query('TRUNCATE file_handler');
14
        fht_deltree(PATH_UPLOAD . '1');
15
    }
16
17
    function createKernel()
18
    {
19
        $kernel = new Stub_Kernel;
20
        return $kernel;
21
    }
22
23
    function createFileHandler()
24
    {
25
        return new FileHandler($this->createKernel());
26
    }
27
28
    function createFile()
29
    {
30
        $data = array('file_name' => $this->file_name);
31
        $filehandler = $this->createFileHandler();
32
        $this->assertTrue($filehandler->update($data) > 0);
33
        return $filehandler;
34
    }
35
36
    ////////////////////////////////////////////////////////////////
37
38
    function testConstruction()
39
    {
40
        $filehandler = $this->createFileHandler();
41
        $this->assertTrue(is_object($filehandler));
42
    }
43
44
    function testFactoryReturnsAValidFileHandlerObject()
45
    {
46
        $fh = $this->createFile();
47
        $accesskey = $fh->getAccessKey();
48
        $filehandler = FileHandler::factory($this->createKernel(), $accesskey);
49
        $this->assertTrue(is_object($filehandler));
50
    }
51
52
    function testUpdate()
53
    {
54
        $fh = $this->createFile();
55
        $this->assertEquals($this->file_name, $fh->get('file_name'));
56
    }
57
58
    function testDelete()
59
    {
60
        // @todo how do we test precisely that it is deleted
61
        $fh = $this->createFile();
62
        $id = $fh->getId();
63
64
        $fh = new FileHandler($this->createKernel(), $id);
65
        $this->assertTrue($fh->delete());
66
    }
67
68
    function testUnDelete()
69
    {
70
        // @todo how do we test precisely that it is undeleted
71
        $fh = $this->createFile();
72
        $fh->delete();
73
        $this->assertTrue($fh->undelete());
74
    }
75
76 View Code Duplication
    function testSave()
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...
77
    {
78
        $fh = new FileHandler($this->createKernel());
79
        // first we make a copy of the file as it is moved by upload.
80
        copy(dirname(__FILE__) . '/wideonball.jpg', PATH_UPLOAD.'wideonball.jpg');
81
        $id = $fh->save(PATH_UPLOAD.'wideonball.jpg', 'Filename');
82
        $fh->error->view();
83
        $this->assertTrue($id > 0);
84
    }
85
86 View Code Duplication
    function testAccessKeyIsValid()
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...
87
    {
88
        $fh = new FileHandler($this->createKernel());
89
        // first we make a copy of the file as it is moved by upload.
90
        copy(dirname(__FILE__) . '/wideonball.jpg', PATH_UPLOAD.'wideonball.jpg');
91
        $id = $fh->save(PATH_UPLOAD.'wideonball.jpg', 'Filename');
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        $fh->load();
93
        $this->assertEquals(50, strlen($fh->get('access_key')));
94
    }
95
96
    function testCreateTemporaryFile()
97
    {
98
        $fh = new FileHandler($this->createKernel());
99
        $this->assertEquals('TemporaryFile', get_class($fh->createTemporaryFile()));
100
    }
101
}
102