Completed
Push — master ( bc825a...1aba9a )
by William
06:03
created

IncidentsControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
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
0 ignored issues
show
Deprecated Code introduced by
The class Cake\TestSuite\IntegrationTestCase has been deprecated: 3.7.0 Use Cake\TestSuite\IntegrationTestTrait instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

9
class IncidentsControllerTest extends /** @scrutinizer ignore-deprecated */ IntegrationTestCase
Loading history...
10
{
11
    public $fixtures = [
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
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

20
        $this->Incidents = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Incidents');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The property Incidents does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        //$Session = new SessionComponent(new ComponentRegistry());
22
        $this->session(['Developer.id' => 1]);
23
        $this->Reports = TableRegistry::get('Reports');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\ORM\TableRegistry::get() has been deprecated: 3.6.0 Use \Cake\ORM\Locator\TableLocator::get() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
        $this->Reports = /** @scrutinizer ignore-deprecated */ TableRegistry::get('Reports');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The property Reports does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
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(
32
            'array',
33
            $this->viewVariable('incident')['stacktrace']
34
        );
35
        $this->assertInternalType(
36
            'array',
37
            $this->viewVariable('incident')['full_report']
38
        );
39
    }
40
41
    public function testJson()
42
    {
43
        $this->get('/incidents/json/1');
44
        $incident = json_decode($this->_response->body(), true);
0 ignored issues
show
Bug introduced by
The method body() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $incident = json_decode($this->_response->/** @scrutinizer ignore-call */ body(), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

44
        $incident = json_decode(/** @scrutinizer ignore-deprecated */ $this->_response->body(), true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
45
        $expected = [
46
            'id' => 1,
47
            'error_name' => 'Lorem ipsum dolor sit amet',
48
            'error_message' => 'Lorem ipsum dolor sit amet',
49
            'pma_version' => 'Lorem ipsum dolor sit amet',
50
            'php_version' => '5.5',
51
            'browser' => 'Lorem ipsum dolor sit amet',
52
            'user_os' => 'Lorem ipsum dolor sit amet',
53
            'locale' => 'Lorem ipsum dolor sit amet',
54
            'server_software' => 'Lorem ipsum dolor sit amet',
55
            'stackhash' => 'hash1',
56
            'configuration_storage' => 'Lorem ipsum dolor sit amet',
57
            'script_name' => 'Lorem ipsum dolor sit amet',
58
            'steps' => 'Lorem ipsum dolor sit amet',
59
            'stacktrace' => [['context' => ['test']]],
60
            'full_report' => [
61
                'pma_version' => '',
62
                'php_version' => '',
63
                'browser_name' => '',
64
                'browser_version' => '',
65
                'user_agent_string' => '',
66
                'server_software' => '',
67
                'locale' => '',
68
                'exception' => ['uri' => ''],
69
                'configuration_storage' => '',
70
                'microhistory' => '',
71
            ],
72
            'report_id' => 1,
73
            'created' => '2013-08-29T18:10:01+00:00',
74
            'modified' => '2013-08-29T18:10:01+00:00',
75
            'exception_type' => null,
76
        ];
77
78
        $this->assertEquals($expected, $incident);
79
    }
80
81
    public function testCreate()
82
    {
83
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
84
        $bugReportDecoded = json_decode($bugReport, true);
85
        $this->configRequest(['input' => $bugReport]);
86
        $this->post('/incidents/create');
87
88
        $report = $this->Reports->find(
89
            'all',
90
            ['order' => 'Reports.created desc']
91
        )->all()->first();
92
        $subject = 'A new report has been submitted '
0 ignored issues
show
Unused Code introduced by
The assignment to $subject is dead and can be removed.
Loading history...
93
            . 'on the Error Reporting Server: '
94
            . $report['id'];
95
        $this->assertEquals('4.5.4.1', $report['pma_version']);
96
97
        //FIXME: test email sending
98
        // Test notification email
99
        //$emailContent = Configure::read('test_transport_email');
100
101
        //$this->assertEquals(Configure::read('NotificationEmailsFrom'), $emailContent['headers']['From']);
102
        //$this->assertEquals(Configure::read('NotificationEmailsTo'), $emailContent['headers']['To']);
103
        //$this->assertEquals($subject, $emailContent['headers']['Subject']);
104
105
        //Configure::write('test_transport_email', null);
106
107
        $this->post('/incidents/create');
108
109
        $report = $this->Reports->find(
110
            'all',
111
            ['order' => 'Reports.created desc']
112
        )->all()->first();
113
        $this->Reports->id = $report['id'];
114
        $incidents = $this->Reports->getIncidents();
115
        $incidents = $incidents->hydrate(false)->toArray();
116
        $this->assertEquals(2, count($incidents));
117
        $this->assertEquals(
118
            $bugReportDecoded['exception']['message'],
119
            $report['error_message']
120
        );
121
        $this->assertEquals(
122
            $bugReportDecoded['exception']['name'],
123
            $report['error_name']
124
        );
125
        $this->assertEquals(
126
            $this->Incidents->getStrippedPmaVersion($bugReportDecoded['pma_version']),
127
            $report['pma_version']
128
        );
129
130
        $this->configRequest(['input' => '']);
131
        $this->post('/incidents/create');
132
        $result = json_decode($this->_response->body(), true);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

132
        $result = json_decode(/** @scrutinizer ignore-deprecated */ $this->_response->body(), true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
133
        $this->assertEquals(false, $result['success']);
134
135
        // Test invalid Notification email configuration
136
        Configure::write('NotificationEmailsTo', '');
137
138
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
139
        $bugReportDecoded = json_decode($bugReport, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $bugReportDecoded is dead and can be removed.
Loading history...
140
        $this->configRequest(['input' => $bugReport]);
141
        $this->post('/incidents/create');
142
143
        $report = $this->Reports->find(
144
            'all',
145
            ['order' => 'Reports.created desc']
146
        )->all()->first();
147
        $subject = 'A new report has been submitted '
148
            . 'on the Error Reporting Server: '
149
            . $report['id'];
150
        $this->assertEquals('4.5.4.1', $report['pma_version']);
151
152
        $emailContent = Configure::read('test_transport_email');
153
154
        // Since no email sent
155
        $this->assertEquals(null, $emailContent);
156
    }
157
}
158