Completed
Pull Request — master (#149)
by Deven
03:49
created

IncidentsTableTest::testGetSchematizedIncident()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 94
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 94
rs 8.4378
c 1
b 0
f 0
cc 1
eloc 76
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\Model\Table;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\TestCase;
7
8
class IncidentsTableTest extends TestCase
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
        parent::setUp();
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
    }
22
23
    public function testGetStackHash()
24
    {
25
        //$method = new ReflectionMethod('Incident', 'getStackHash');
26
        //$method->setAccessible(true);
27
28
        $stacktrace1 = array(
29
            array(
30
                'filename' => 'file1',
31
                'line' => 300,
32
            ),
33
            array(
34
                'filename' => 'file2',
35
                'line' => 200,
36
            ),
37
        );
38
39
        $stacktrace2 = array(
40
            array(
41
                'line' => 300,
42
                'filename' => 'file1',
43
            ),
44
            array(
45
                'line' => 200,
46
                'filename' => 'file2',
47
            ),
48
        );
49
50
        $result = $this->Incidents->getStackHash($stacktrace1);
51
        $this->assertEquals('a441639902837d88db25214812c0cd83', $result);
52
53
        $result = $this->Incidents->getStackHash($stacktrace2);
54
        $this->assertEquals('a441639902837d88db25214812c0cd83', $result);
55
    }
56
57
    public function testGetServer()
58
    {
59
        $method = new \ReflectionMethod($this->Incidents, '_getServer');
60
        $method->setAccessible(true);
61
62
        $result = $method->invoke($this->Incidents,
63
                'some random/data Apache/2.1.7;other.random/data');
64
        $this->assertEquals('Apache/2.1', $result);
65
66
        $result = $method->invoke($this->Incidents,
67
                'some random/data Nginx/1.0.7;other.random/data');
68
        $this->assertEquals('Nginx/1.0', $result);
69
70
        $result = $method->invoke($this->Incidents,
71
                'some random/data Lighttpd/4.6.7;other.random/data');
72
        $this->assertEquals('Lighttpd/4.6', $result);
73
74
        $result = $method->invoke($this->Incidents,
75
                'some random/data DoesNotExist/3.5;other.random/data');
76
        $this->assertEquals('UNKNOWN', $result);
77
    }
78
79
    public function testGetSimpleVersion()
80
    {
81
        $method = new \ReflectionMethod($this->Incidents, '_getSimpleVersion');
82
        $method->setAccessible(true);
83
84
        $result = $method->invoke($this->Incidents,
85
                '15.3.12.17', 1);
86
        $this->assertEquals('15', $result);
87
88
        $result = $method->invoke($this->Incidents,
89
                '15.3.12.17', 2);
90
        $this->assertEquals('15.3', $result);
91
92
        $result = $method->invoke($this->Incidents,
93
                '15.3.12.17', 3);
94
        $this->assertEquals('15.3.12', $result);
95
96
        $result = $method->invoke($this->Incidents,
97
                '15.3.12.17', 'wrong argument');
98
        $this->assertEquals('15', $result);
99
100
        $result = $method->invoke($this->Incidents,
101
                '15.3.12.17', -1);
102
        $this->assertEquals('15', $result);
103
    }
104
105
    public function testGetIdentifyingLocation()
106
    {
107
        $method = new \ReflectionMethod($this->Incidents, '_getIdentifyingLocation');
108
        $method->setAccessible(true);
109
110
        $stacktrace = array(
111
            array(
112
                'filename' => 'file1',
113
                'line' => 300,
114
            ),
115
            array(
116
                'filename' => 'file2',
117
                'line' => 200,
118
            ),
119
        );
120
121
        $stacktrace_script = array(
122
            array(
123
                'scriptname' => 'script1',
124
                'line' => 300,
125
            ),
126
            array(
127
                'filename' => 'file2',
128
                'line' => 200,
129
            ),
130
        );
131
132
        $stacktrace_uri = array(
133
            array(
134
                'uri' => 'sql.php',
135
                'line' => 5,
136
            ),
137
            array(
138
                'filename' => 'file2',
139
                'line' => 200,
140
            ),
141
        );
142
143
        $result = $method->invoke($this->Incidents,
144
                $stacktrace_script);
145
        $this->assertEquals(array('script1', 300), $result);
146
147
        $result = $method->invoke($this->Incidents,
148
                $stacktrace);
149
        $this->assertEquals(array('file1', 300), $result);
150
151
        $stacktrace[0]['filename'] = 'tracekit/tracekit.js';
152
        $result = $method->invoke($this->Incidents,
153
                $stacktrace);
154
        $this->assertEquals(array('file2', 200), $result);
155
156
        $stacktrace[0]['filename'] = 'error_report.js';
157
        $result = $method->invoke($this->Incidents,
158
                $stacktrace);
159
        $this->assertEquals(array('file2', 200), $result);
160
161
        $stacktrace[1] = null;
162
        $result = $method->invoke($this->Incidents,
163
                $stacktrace);
164
        $this->assertEquals(array('error_report.js', 300), $result);
165
166
        $result = $method->invoke($this->Incidents,
167
                $stacktrace_uri);
168
        $this->assertEquals(array('file2', 200), $result);
169
    }
170
171
    public function testGetSchematizedIncident()
172
    {
173
        $method = new \ReflectionMethod($this->Incidents, '_getSchematizedIncidents');
174
        $method->setAccessible(true);
175
176
        // Case-1: JavaScript Report
177
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
178
        $bugReport = json_decode($bugReport, true);
179
180
        $result = $method->invoke($this->Incidents,
181
                $bugReport);
182
183
        $expected = array(
184
            array(
185
                'pma_version' => '4.0',
186
                'php_version' => '5.2',
187
                'steps' => '<script>test steps',
188
                'error_message' => 'a is not defined',
189
                'error_name' => 'ReferenceError',
190
                'browser' => 'FIREFOX 17',
191
                'user_os' => 'Windows',
192
                'locale' => 'en',
193
                'script_name' => 'tbl_relation.php',
194
                'configuration_storage' => 'enabled',
195
                'server_software' => 'NginX/1.17',
196
                'stackhash' => '9db5408094f1e76ef7161b7bbf3ddfe4',
197
                'full_report' => json_encode($bugReport),
198
                'stacktrace' => json_encode($bugReport['exception']['stack']),
199
                'exception_type' => 0,
200
            ),
201
        );
202
203
        $this->assertEquals($expected, $result);
204
205
        // Case-2: php Report
206
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
207
        $bugReport = json_decode($bugReport, true);
208
209
        $result = $method->invoke($this->Incidents,
210
                $bugReport);
211
212
        $expected = array(
213
            array(
214
                'pma_version' => '4.3.0-dev',
215
                'php_version' => '5.5',
216
                'error_message' => 'Undefined variable: haha',
217
                'error_name' => 'Notice',
218
                'browser' => 'CHROME 27',
219
                'user_os' => 'Linux',
220
                'locale' => 'en',
221
                'script_name' => './libraries/Config.class.php',
222
                'configuration_storage' => 'disabled',
223
                'server_software' => 'Apache/2.4',
224
                'stackhash' => '5063bbe81a2daa6a6ad39c5cd315701c',
225
                'full_report' => json_encode($bugReport),
226
                'stacktrace' => json_encode($bugReport['errors'][0]['stackTrace']),
227
                'exception_type' => 1,
228
            ),
229
            array(
230
                'pma_version' => '4.3.0-dev',
231
                'php_version' => '5.5',
232
                'error_message' => 'Undefined variable: hihi',
233
                'error_name' => 'Notice',
234
                'browser' => 'CHROME 27',
235
                'user_os' => 'Linux',
236
                'locale' => 'en',
237
                'script_name' => './libraries/Util.class.php',
238
                'configuration_storage' => 'disabled',
239
                'server_software' => 'Apache/2.4',
240
                'stackhash' => 'e911a21765eae766463612e033773716',
241
                'full_report' => json_encode($bugReport),
242
                'stacktrace' => json_encode($bugReport['errors'][1]['stackTrace']),
243
                'exception_type' => 1,
244
            ),
245
            array(
246
                'pma_version' => '4.3.0-dev',
247
                'php_version' => '5.5',
248
                'error_message' => 'Undefined variable: hehe',
249
                'error_name' => 'Notice',
250
                'browser' => 'CHROME 27',
251
                'user_os' => 'Linux',
252
                'locale' => 'en',
253
                'script_name' => './index.php',
254
                'configuration_storage' => 'disabled',
255
                'server_software' => 'Apache/2.4',
256
                'stackhash' => '37848b23bdd6e737273516b9575fe407',
257
                'full_report' => json_encode($bugReport),
258
                'stacktrace' => json_encode($bugReport['errors'][2]['stackTrace']),
259
                'exception_type' => 1,
260
            ),
261
        );
262
263
        $this->assertEquals($expected, $result);
264
    }
265
266
    public function testGetReportDetails()
267
    {
268
        $method = new \ReflectionMethod($this->Incidents, '_getReportDetails');
269
        $method->setAccessible(true);
270
271
        // case-1: JavaScript BugReport
272
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
273
        $bugReport = json_decode($bugReport, true);
274
275
        $result = $method->invoke($this->Incidents,
276
                $bugReport);
277
278
        $expected = array(
279
            'error_message' => 'a is not defined',
280
            'error_name' => 'ReferenceError',
281
            'status' => 'new',
282
            'location' => 'error.js',
283
            'linenumber' => (int) 312,
284
            'pma_version' => '4.0',
285
            'exception_type' => 0,
286
        );
287
288
        $this->assertEquals($expected, $result);
289
290
        // case-2: php BugReport
291
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
292
        $bugReport = json_decode($bugReport, true);
293
294
        $result = $method->invoke($this->Incidents,
295
                $bugReport, 1);
296
297
        $expected = array(
298
            'error_message' => 'Undefined variable: hihi',
299
            'error_name' => 'Notice',
300
            'status' => 'new',
301
            'location' => './libraries/Util.class.php',
302
            'linenumber' => (int) 557,
303
            'pma_version' => '4.3.0-dev',
304
            'exception_type' => 1,
305
        );
306
        $this->assertEquals($expected, $result);
307
    }
308
309
    public function testGetClosestReport()
310
    {
311
        $method = new \ReflectionMethod($this->Incidents, '_getClosestReport');
312
        $method->setAccessible(true);
313
314
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
315
        $bugReport = json_decode($bugReport, true);
316
317
        $returnedReport = null;
318
319
        $result = $method->invoke($this->Incidents,
320
                $bugReport);
321
322
        $this->assertEquals($returnedReport, $result);
323
    }
324
325
    public function testCreateIncidentFromBugReport()
326
    {
327
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
328
        $bugReport = json_decode($bugReport, true);
329
330
        // Case-1: 'js' report
331
332
        // Case-1.1: closest report = null
333
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
334
        $this->assertEquals(1, count($result));
335
336
        // Case-1.2: closest report = some report.
337
        // Previously(in Case-1.1) inserted Reports serve as ClosestReports.
338
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
339
        $this->assertEquals(1, count($result));
340
        $incident = $this->Incidents->get($result[0]);
341
        // check the incident has been reported under the same 'Report'
342
        $result = TableRegistry::get('Incidents')->find('all', array('conditions' => array('report_id = ' . $incident->report_id)));
343
        $result = $result->hydrate(false)->toArray();
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Query::hydrate() has been deprecated with message: 3.4.0 Use enableHydration()/isHydrationEnabled() instead.

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

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

Loading history...
344
        $this->assertEquals(2, count($result));
345
346
        // Case-2: for 'php' reports
347
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
348
        $bugReport = json_decode($bugReport, true);
349
        // Case-2.1: closest report = null.
350
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
351
        $this->assertEquals(3, count($result));
352
353
        // Case-2.2: closest report = some report.
354
        // Previously(in Case-2.1) inserted Reports serve as ClosestReports.
355
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
356
        $this->assertEquals(3, count($result));
357
        // check the incidents have been reported under the same 'Report's
358
        $incident = $this->Incidents->get($result[0]);
359
        $result = TableRegistry::get('Incidents')->find('all', array('conditions' => array('report_id = ' . $incident->report_id)));
360
        $result = $result->hydrate(false)->toArray();
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Query::hydrate() has been deprecated with message: 3.4.0 Use enableHydration()/isHydrationEnabled() instead.

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

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

Loading history...
361
        $this->assertEquals(2, count($result));
362
363
364
        // Case 3.1: Long PHP report submission
365
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_php.json');
366
        $bugReport = json_decode($bugReport, true);
367
368
        // Forcefully inflate the report by inflating $bugReport['errors']
369 View Code Duplication
        for ($i = 0; $i < 2000; $i++) {
370
            $bugReport['errors'] = array_push($bugReport['errors'], $bugReport['errors'][0]);
371
        }
372
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
373
374
        $this->assertEquals(true, in_array(false, $result));
375
376
377
        // Case 3.2: Long JS report submission
378
        $bugReport = file_get_contents(TESTS . 'Fixture' . DS . 'report_js.json');
379
        $bugReport = json_decode($bugReport, true);
380
381
        // Forcefully inflate the report by inflating $bugReport['errors']
382 View Code Duplication
        for ($i = 0; $i < 1500; $i++) {
383
            $bugReport['exception']['stack'] .= array_push($bugReport['exception']['stack'][0]);
384
        }
385
        $result = $this->Incidents->createIncidentFromBugReport($bugReport);
386
387
        $this->assertEquals(true, in_array(false, $result));
388
    }
389
390
    /**
391
     * @dataProvider versionsStripping
392
     */
393
    public function testStripversion($version, $expected)
394
    {
395
        $this->assertEquals($expected, $this->Incidents->getStrippedPmaVersion($version));
396
    }
397
398
    public function versionsStripping()
399
    {
400
        return array(
401
            array('1.2.3', '1.2.3'),
402
            array('1.2.3-rc1', '1.2.3-rc1'),
403
            array('4.1-dev', '4.1-dev'),
404
            array('4.1.6deb0ubuntu1ppa1', '4.1.6'),
405
            array('4.2.3deb1.trusty~ppa.1', '4.2.3'),
406
            array('4.2.9deb0.1', '4.2.9'),
407
        );
408
    }
409
}
410