Completed
Pull Request — master (#153)
by Deven
16:32
created

IncidentsControllerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 131
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testView() 0 10 1
B testJson() 0 39 1
A testCreate() 0 62 1
1
<?php
2
3
namespace App\Test\TestCase\Controller;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\IntegrationTestCase;
7
8
class IncidentsControllerTest 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->Incidents = TableRegistry::get('Incidents');
0 ignored issues
show
Bug introduced by
The property Incidents 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
        //$Session = new SessionComponent(new ComponentRegistry());
21
        $this->session(array('Developer.id' => 1));
22
        $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...
23
    }
24
25
    public function testView()
26
    {
27
        $this->get('/incidents/view/1');
28
29
        $this->assertNotEmpty($this->viewVariable('incident'));
30
        $this->assertInternalType('array',
31
                $this->viewVariable('incident')['stacktrace']);
32
        $this->assertInternalType('array',
33
                $this->viewVariable('incident')['full_report']);
34
    }
35
36
    public function testJson()
37
    {
38
        $this->get('/incidents/json/1');
39
        $incident = json_decode($this->_response->body(), true);
40
        $expected = array(
41
                'id' => 1,
42
                'error_name' => 'Lorem ipsum dolor sit amet',
43
                'error_message' => 'Lorem ipsum dolor sit amet',
44
                'pma_version' => 'Lorem ipsum dolor sit amet',
45
                'php_version' => '5.5',
46
                'browser' => 'Lorem ipsum dolor sit amet',
47
                'user_os' => 'Lorem ipsum dolor sit amet',
48
                'locale' => 'Lorem ipsum dolor sit amet',
49
                'server_software' => 'Lorem ipsum dolor sit amet',
50
                'stackhash' => 'hash1',
51
                'configuration_storage' => 'Lorem ipsum dolor sit amet',
52
                'script_name' => 'Lorem ipsum dolor sit amet',
53
                'steps' => 'Lorem ipsum dolor sit amet',
54
                'stacktrace' => array(array('context' => array('test'))),
55
                'full_report' => array(
56
                    'pma_version' => '',
57
          'php_version' => '',
58
          'browser_name' => '',
59
          'browser_version' => '',
60
          'user_agent_string' => '',
61
          'server_software' => '',
62
          'locale' => '',
63
          'exception' => array('uri' => ''),
64
          'configuration_storage' => '',
65
          'microhistory' => '',
66
                ),
67
                'report_id' => 1,
68
                'created' => '2013-08-29T18:10:01',
69
                'modified' => '2013-08-29T18:10:01',
70
                'exception_type' => null,
71
        );
72
73
        $this->assertEquals($expected, $incident);
74
    }
75
76
    public function testCreate()
77
    {
78
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
79
        $bugReportDecoded = json_decode($bugReport, true);
80
        $this->configRequest(array('input' => $bugReport));
81
        $this->post('/incidents/create');
82
83
        $report = $this->Reports->find('all',
84
            array('order' => 'Reports.created desc'))->all()->first();
85
        $subject = 'A new report has been submitted '
86
            . 'on the Error Reporting Server: '
87
            . $report['id'];
88
89
        // Test notification email
90
        $emailContent = Configure::read('test_transport_email');
91
92
        $this->assertEquals('[email protected]', $emailContent['headers']['From']);
93
        $this->assertEquals('[email protected]', $emailContent['headers']['To']);
94
        $this->assertEquals($subject, $emailContent['headers']['Subject']);
95
96
        $this->post('/incidents/create');
97
98
        $report = $this->Reports->find('all',
99
                array('order' => 'Reports.created desc'))->all()->first();
100
        //$report = $report->hydrate(false)->toArray();
101
        $this->Reports->id = $report['id'];
102
        $incidents = $this->Reports->getIncidents();
103
        $incidents = $incidents->hydrate(false)->toArray();
104
        $this->assertEquals(2, count($incidents));
105
        $this->assertEquals($bugReportDecoded['exception']['message'],
106
                $report['error_message']);
107
        $this->assertEquals($bugReportDecoded['exception']['name'],
108
                $report['error_name']);
109
        $this->assertEquals($bugReportDecoded['pma_version'],
110
                $report['pma_version']);
111
        $this->configRequest(array('input' => array()));
112
        $this->post('/incidents/create');
113
        $result = json_decode($this->_response->body(), true);
114
        $this->assertEquals(false, $result['success']);
115
116
117
118
        // Test invalid Notification email configuration
119
        Configure::write('NotificationEmailsTo', '');
120
121
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
122
        $bugReportDecoded = json_decode($bugReport, true);
0 ignored issues
show
Unused Code introduced by
$bugReportDecoded 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...
123
        $this->configRequest(array('input' => $bugReport));
124
        $this->post('/incidents/create');
125
126
        $report = $this->Reports->find('all',
127
            array('order' => 'Reports.created desc'))->all()->first();
128
        $subject = 'A new report has been submitted '
0 ignored issues
show
Unused Code introduced by
$subject 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...
129
            . 'on the Error Reporting Server: '
130
            . $report['id'];
131
132
        $emailContent = Configure::read('test_transport_email');
133
134
        // Since no email sent
135
        $this->assertEquals(null, $emailContent);
136
137
    }
138
}
139