Completed
Pull Request — master (#153)
by Deven
03:59
created

IncidentsControllerTest::testCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 64
rs 9.3956
cc 1
eloc 44
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
        Configure::write('test_transport_email', NULL);
98
99
        $this->post('/incidents/create');
100
101
        $report = $this->Reports->find('all',
102
                array('order' => 'Reports.created desc'))->all()->first();
103
        //$report = $report->hydrate(false)->toArray();
104
        $this->Reports->id = $report['id'];
105
        $incidents = $this->Reports->getIncidents();
106
        $incidents = $incidents->hydrate(false)->toArray();
107
        $this->assertEquals(2, count($incidents));
108
        $this->assertEquals($bugReportDecoded['exception']['message'],
109
                $report['error_message']);
110
        $this->assertEquals($bugReportDecoded['exception']['name'],
111
                $report['error_name']);
112
        $this->assertEquals($bugReportDecoded['pma_version'],
113
                $report['pma_version']);
114
        $this->configRequest(array('input' => array()));
115
        $this->post('/incidents/create');
116
        $result = json_decode($this->_response->body(), true);
117
        $this->assertEquals(false, $result['success']);
118
119
120
121
        // Test invalid Notification email configuration
122
        Configure::write('NotificationEmailsTo', '');
123
124
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
125
        $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...
126
        $this->configRequest(array('input' => $bugReport));
127
        $this->post('/incidents/create');
128
129
        $report = $this->Reports->find('all',
130
            array('order' => 'Reports.created desc'))->all()->first();
131
        $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...
132
            . 'on the Error Reporting Server: '
133
            . $report['id'];
134
135
        $emailContent = Configure::read('test_transport_email');
136
137
        // Since no email sent
138
        $this->assertEquals(NULL, $emailContent);
139
140
    }
141
}
142