Completed
Pull Request — master (#153)
by Deven
02:47
created

IncidentsControllerTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 62
rs 9.4743
cc 1
eloc 43
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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