Completed
Push — master ( c9c188...858b2f )
by Deven
03:01
created

ReportsControllerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 150
Duplicated Lines 18 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 27
loc 150
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testIndex() 0 8 1
B testView() 0 36 2
A testDataTables() 0 51 1
A testMarkRelatedTo() 16 16 1
A _testUnmarkRelatedTo() 11 11 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 App\Test\TestCase\Controller;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\IntegrationTestCase;
7
8
class ReportsControllerTest extends IntegrationTestCase
9
{
10
    public $fixtures = array(
11
        'app.notifications',
12
        'app.developers',
13
        'app.reports',
14
        'app.incidents',
15
    );
16
17
    public function setUp()
18
    {
19
        $this->Reports = TableRegistry::get('Reports');
0 ignored issues
show
Bug introduced by
The property Reports does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        $this->session(array('Developer.id' => 1));
21
    }
22
23
    public function testIndex()
24
    {
25
        $this->get('/reports');
26
        $this->assertEquals(array('3.8', '4.0'), $this->viewVariable('distinct_versions'));
27
        $this->assertEquals(array('new'), $this->viewVariable('distinct_statuses'));
28
        $this->assertEquals(array('error1', 'error2'),
29
                $this->viewVariable('distinct_error_names'));
30
    }
31
32
    public function testView()
33
    {
34
        $this->get('/reports/view/1');
35
36
        $this->assertEquals(1, $this->viewVariable('report')[0]['id']);
37
        $this->assertEquals('error2', $this->viewVariable('report')[0]['error_name']);
38
39
        $this->assertNotEmpty($this->viewVariable('project_name'));
40
        $this->assertNotEmpty($this->viewVariable('columns'));
41
42
        $this->assertNotEmpty($this->viewVariable('related_entries'));
43
        $this->assertEquals(count($this->viewVariable('columns')),
44
                count($this->viewVariable('related_entries')));
45
46
        foreach ($this->viewVariable('columns') as $column) {
47
            $this->assertNotEmpty($this->viewVariable("${column}_distinct_count"));
48
        }
49
50
        $this->assertNotEmpty($this->viewVariable('incidents'));
51
        $this->assertEquals(1, count($this->viewVariable('incidents')));
52
53
        $this->assertNotEmpty($this->viewVariable('incidents_with_description'));
54
        $this->assertEquals(1, count($this->viewVariable('incidents_with_description')));
55
56
        $this->assertNotEmpty($this->viewVariable('incidents_with_stacktrace'));
57
        $this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace')));
58
59
        $this->assertNotEmpty($this->viewVariable('related_reports'));
60
        $this->assertEquals(1, count($this->viewVariable('related_reports')));
61
62
        $this->get('/reports/view/3');
63
        $this->assertResponseContains('Invalid Report');
64
       // $this->assertResponseOk();
65
        //$this->setExpectedException('NotFoundException');
66
        //	$this->get('/reports/view');
67
    }
68
69
    public function testDataTables()
70
    {
71
        $this->get('/reports/data_tables?sEcho=1&iDisplayLength=25');
72
        $expected = array(
73
            'iTotalRecords' => 3,
74
            'iTotalDisplayRecords' => 3,
75
            'sEcho' => 1,
76
            'aaData' => array(
77
                array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', '4.0', 'New', 'js', '1'),
78
                array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', '4.0', 'New', 'js', '1'),
79
                array("<input type='checkbox' name='reports[]' value='4'/>", 4, 'error1', 'Lorem ipsum dolor sit amet', '3.8', 'New', 'js', '2'),
80
            ),
81
        );
82
        $this->assertEquals($expected, json_decode($this->_response->body(), true));
83
84
        $this->get('/reports/data_tables?sEcho=1&sSearch=error2&bSearchable_2=true&iSortCol_0=0&sSortDir_0=desc&bSortable_0=true&iSortingCols=2&iDisplayLength=25');
85
        $expected = array(
86
            'iTotalRecords' => 3,
87
            'iTotalDisplayRecords' => 2,
88
            'sEcho' => 1,
89
            'aaData' => array(
90
                array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', '4.0', 'New', 'js', '1'),
91
                array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', '4.0', 'New', 'js', '1'),
92
            ),
93
        );
94
        $result = json_decode($this->_response->body(), true);
95
        $this->assertEquals($expected, $result);
96
97
        $this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25');
98
        $expected = array(
99
            'iTotalRecords' => 3,
100
            'iTotalDisplayRecords' => 1,
101
            'sEcho' => 1,
102
            'aaData' => array(
103
                array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', '4.0', 'New', 'js', '1'),
104
            ),
105
        );
106
        $result = json_decode($this->_response->body(), true);
107
        $this->assertEquals($expected, $result);
108
109
        $this->get('/reports/data_tables?sEcho=1&sSearch_1=error&iDisplayLength=25');
110
        $expected = array(
111
            'iTotalRecords' => 3,
112
            'iTotalDisplayRecords' => 0,
113
            'sEcho' => 1,
114
            'aaData' => array(
115
            ),
116
        );
117
        $result = json_decode($this->_response->body(), true);
118
        $this->assertEquals($expected, $result);
119
    }
120
121 View Code Duplication
    public function testMarkRelatedTo() {
122
        $this->Reports->id = 2;
123
        $incidents = $this->Reports->getIncidents();
124
        $this->assertEquals(1, $incidents->count());
125
126
        $this->post(
127
            '/reports/mark_related_to/2',
128
            ['related_to' => 4]
129
        );
130
131
        $this->Reports->id = 2;
132
        $incidents = $this->Reports->getIncidents();
133
        $this->assertEquals(3, $incidents->count());
134
135
        $this->_testUnmarkRelatedTo();
136
    }
137
138
139
    /**
140
     * Don't run this as a separate test,
141
     * as the fixture tables are re-created and
142
     * we lose the previous related_to updations.
143
     *
144
     * So, call at the end of testMarkRelatedTo()
145
     */
146 View Code Duplication
    private function _testUnmarkRelatedTo() {
147
        $this->Reports->id = 2;
148
        $incidents = $this->Reports->getIncidents();
149
        $this->assertEquals(3, $incidents->count());
150
151
        $this->post('/reports/unmark_related_to/2');
152
153
        $this->Reports->id = 2;
154
        $incidents = $this->Reports->getIncidents();
155
        $this->assertEquals(1, $incidents->count());
156
    }
157
}
158