File: ReportingTest.php

package info (click to toggle)
php-codesniffer 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,092 kB
  • sloc: php: 30,445; xml: 3,768; makefile: 15; pascal: 8; sh: 6
file content (234 lines) | stat: -rw-r--r-- 9,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
 * Tests for the PHP_CodeSniffer_Reporting class.
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Gabriele Santini <gsantini@sqli.com>
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @copyright 2009 SQLI <www.sqli.com>
 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */

require_once 'PHPUnit/Framework/TestCase.php';

/**
 * Tests for the PHP_CodeSniffer reporting system.
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Gabriele Santini <gsantini@sqli.com>
 * @author    Greg Sherwood <gsherwood@squiz.net>
 * @copyright 2009 SQLI <www.sqli.com>
 * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
 * @version   Release: 1.3.4
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */
class Core_ReportingTest extends PHPUnit_Framework_TestCase
{

    /**
     * Called class
     *
     * @var PHP_CodeSniffer_Reporting
     */
    protected $reporting;

    /**
     * Report error fixtures.
     * 
     * @var array
     */
    protected $fixtureErrors = array(
                                10 => array(
                                       1 => array(
                                             array(
                                              'message'  => 'Fourth error message',
                                              'source'   => 'MyStandard.MyType.Mysniff1.Mycode1',
                                              'severity' => 5,
                                             ),
                                            )
                                      ),
                                1  => array(
                                       10 => array(
                                              array(
                                               'message'  => 'Second error message',
                                               'source'   => 'MyStandard.MyType.Mysniff2.Mycode1',
                                               'severity' => 5,
                                              ),
                                              array(
                                               'message'  => 'Third error message',
                                               'source'   => 'MyStandard.MyType.Mysniff1.Mycode2',
                                               'severity' => 5,
                                              ),
                                             ),
                                       1  => array(
                                              array(
                                               'message'  => 'First error message',
                                               'source'   => 'MyStandard.MyType.Mysniff1.Mycode2',
                                               'severity' => 5,
                                              ),
                                             )
                                      )
                               );

    /**
     * Report warning fixtures.
     * 
     * @var array
     */
    protected $fixtureWarnings = array(
                                  10 => array(
                                         1 => array(
                                               array(
                                                'message'  => 'First warning message',
                                                'source'   => 'MyStandard.MyType.Mysniff1.Mycode3',
                                                'severity' => 5,
                                               ),
                                              )
                                        ),
                                  5  => array(
                                         1 => array(
                                               array(
                                                'message'  => 'Second warning message',
                                                'source'   => 'MyStandard.MyType.Mysniff2.Mycode2',
                                                'severity' => 5,
                                               ),
                                              )
                                        ),
                                 );


    /**
     * Gives a Reporting instance.
     * 
     * @return void
     */
    public function setUp()
    {
        $this->reporting = new PHP_CodeSniffer_Reporting();

    }//end setUp()


    /**
     * Test report factory method.
     *
     * @return void
     */
    public function testFactory()
    {
        $type        = 'checkstyle';
        $reportClass = $this->reporting->factory($type);
        $this->assertInstanceOf('PHP_CodeSniffer_Report', $reportClass);
        $this->assertInstanceOf('PHP_CodeSniffer_Reports_Checkstyle', $reportClass);

        $this->setExpectedException('PHP_CodeSniffer_Exception');
        $type        = 'foo';
        $reportClass = $this->reporting->factory($type);

    }//end testFactory()


    /**
     * Compose fixture violations.
     * 
     * @return array
     */
    protected function getFixtureFilesViolations()
    {
        return array(
                'foo' => array(
                          'errors'      => $this->fixtureErrors,
                          'warnings'    => $this->fixtureWarnings,
                          'numErrors'   => 4,
                          'numWarnings' => 2,
                         ),
                'bar' => array(
                          'errors'      => $this->fixtureErrors,
                          'warnings'    => array(),
                          'numErrors'   => 4,
                          'numWarnings' => 0,
                         ),
                'baz' => array(
                          'errors'      => array(),
                          'warnings'    => array(),
                          'numErrors'   => 0,
                          'numWarnings' => 0,
                         )
               );

    }//end getFixtureFilesViolations()


    /**
     * Test prepare report method.
     * 
     * @return void
     */
    public function testPrepare()
    {
        $fixtureFilesViolations = $this->getFixtureFilesViolations();
        $reports = $this->reporting->prepare($fixtureFilesViolations);

        $this->assertArrayHasKey('files', $reports);
        $this->assertEquals(3, count($reports['files']));

        $this->assertArrayHasKey('foo', $reports['files']);
        $this->assertArrayHasKey('errors', $reports['files']['foo']);
        $this->assertEquals(4, $reports['files']['foo']['errors']);
        $this->assertArrayHasKey('warnings', $reports['files']['foo']);
        $this->assertEquals(2, $reports['files']['foo']['warnings']);

        // Two errors on line 1 column 10.
        $this->assertArrayHasKey('messages', $reports['files']['foo']);
        $fooMessages = $reports['files']['foo']['messages'];

        $this->assertArrayHasKey(1, $fooMessages, 'messages on line 1');
        $this->assertArrayHasKey(10, $fooMessages[1], 'messages on line 1 column 10');
        $this->assertEquals(2, count($fooMessages[1][10]), '2 messages on line 1 column 10');

        // One error one warning on line 10 column 1.
        $this->assertArrayHasKey(10, $fooMessages, 'messages on line 10');
        $this->assertArrayHasKey(1, $fooMessages[10], 'messages on line 10 column 1');
        $this->assertEquals(2, count($fooMessages[10][1]), '2 messages on line 10 column 1');

        // Empty file has structure without data.
        $this->assertArrayHasKey('baz', $reports['files']);
        $this->assertArrayHasKey('messages', $reports['files']['baz']);
        $this->assertEquals(0, count($reports['files']['baz']['messages']));

        // Totals.
        $this->assertArrayHasKey('totals', $reports, 'report totals exist');
        $this->assertArrayHasKey('errors', $reports['totals'], 'errors total exists');
        $this->assertEquals(8, $reports['totals']['errors'], 'errors total is well calculated');
        $this->assertArrayHasKey('warnings', $reports['totals'], 'warnings total exists');
        $this->assertEquals(2, $reports['totals']['warnings'], 'warnings total is well calculated');

        // Files Order.
        reset($reports['files']);
        $this->assertEquals('bar', key($reports['files']), 'report files ordered by name');
        next($reports['files']);
        $this->assertEquals('baz', key($reports['files']), 'report files ordered by name');

        // Violations Order.
        reset($fooMessages);
        $this->assertEquals(1, key($fooMessages), 'line level violations order');
        next($fooMessages);
        $this->assertEquals(5, key($fooMessages), 'line level violations order');
        reset($fooMessages[1]);
        $this->assertEquals(1, key($fooMessages[1]), 'column level violations order');
        next($fooMessages[1]);
        $this->assertEquals(10, key($fooMessages[1]), 'column level violations order');

    }//end testPrepare()


}//end class

?>