Passed
Push — master ( 902a34...c826a7 )
by Kyle
53s queued 11s
created

Rule/Controversial/CamelCaseMethodNameTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Rule\Controversial;
19
20
use PHPMD\AbstractTest;
21
22
/**
23
 * Test case for the camel case method name rule.
24
 *
25
 * @covers \PHPMD\Rule\Controversial\CamelCaseMethodName
26
 */
27
class CamelCaseMethodNameTest extends AbstractTest
28
{
29
    /**
30
     * Tests that the rule does not apply for a valid method name.
31
     *
32
     * @return void
33
     */
34
    public function testRuleDoesNotApplyForValidMethodName()
35
    {
36
        //$method = $this->getMethod();
37
        $report = $this->getReportWithNoViolation();
38
39
        $rule = new CamelCaseMethodName();
40
        $rule->setReport($report);
41
        $rule->addProperty('allow-underscore', 'false');
42
        $rule->addProperty('allow-underscore-test', 'false');
43
        $rule->apply($this->getMethod());
44
    }
45
46
    /**
47
     * Tests that the rule does apply for an method name
48
     * starting with a capital.
49
     *
50
     * @return void
51
     */
52
    public function testRuleDoesApplyForMethodNameWithCapital()
53
    {
54
        // Test method name with capital at the beginning
55
        $method = $this->getMethod();
56
        $report = $this->getReportWithOneViolation();
57
58
        $rule = new CamelCaseMethodName();
59
        $rule->setReport($report);
60
        $rule->addProperty('allow-underscore', 'false');
61
        $rule->addProperty('allow-underscore-test', 'false');
62
        $rule->apply($method);
63
    }
64
65
    /**
66
     * Tests that the rule does apply for a method name
67
     * with underscores.
68
     *
69
     * @return void
70
     */
71
    public function testRuleDoesApplyForMethodNameWithUnderscores()
72
    {
73
        // Test method name with underscores
74
        $method = $this->getMethod();
75
        $report = $this->getReportWithOneViolation();
76
77
        $rule = new CamelCaseMethodName();
78
        $rule->setReport($report);
79
        $rule->addProperty('allow-underscore', 'false');
80
        $rule->addProperty('allow-underscore-test', 'false');
81
        $rule->apply($method);
82
    }
83
84
    /**
85
     * Tests that the rule does apply for a valid method name
86
     * with an underscore at the beginning when it is allowed.
87
     *
88
     * @return void
89
     */
90
    public function testRuleDoesApplyForValidMethodNameWithUnderscoreWhenNotAllowed()
91
    {
92
        $method = $this->getMethod();
93
        $report = $this->getReportWithOneViolation();
94
95
        $rule = new CamelCaseMethodName();
96
        $rule->setReport($report);
97
        $rule->addProperty('allow-underscore', 'false');
98
        $rule->addProperty('allow-underscore-test', 'false');
99
        $rule->apply($method);
100
    }
101
102
    /**
103
     * Tests that the rule does not apply for a valid method name
104
     * with an underscore at the beginning when it is not allowed.
105
     *
106
     * @return void
107
     */
108
    public function testRuleDoesNotApplyForValidMethodNameWithUnderscoreWhenAllowed()
109
    {
110
        $method = $this->getMethod();
111
        $report = $this->getReportWithNoViolation();
112
113
        $rule = new CamelCaseMethodName();
114
        $rule->setReport($report);
115
        $rule->addProperty('allow-underscore', 'true');
116
        $rule->addProperty('allow-underscore-test', 'false');
117
        $rule->apply($method);
118
    }
119
120
    /**
121
     * Tests that the rule does not apply for a valid method name
122
     * with an underscore at the beginning when it is not allowed.
123
     *
124
     * @return void
125
     */
126 View Code Duplication
    public function testRuleDoesNotApplyForMagicMethods()
127
    {
128
        $methods = $this->getClass()->getMethods();
129
130
        foreach ($methods as $method) {
131
            $report = $this->getReportMock($method->getName() === '__notAllowed' ? 1 : 0);
132
133
            $rule = new CamelCaseMethodName();
134
            $rule->setReport($report);
0 ignored issues
show
It seems like $report defined by $this->getReportMock($me...'__notAllowed' ? 1 : 0) on line 131 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PHPMD\AbstractRule::setReport() does only seem to accept object<PHPMD\Report>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
135
            $rule->addProperty('allow-underscore', 'false');
136
            $rule->addProperty('allow-underscore-test', 'false');
137
            $rule->apply($method);
138
        }
139
    }
140
141
    /**
142
     * Tests that the rule does apply for a valid test method name
143
     * with an underscore.
144
     *
145
     * @return void
146
     */
147
    public function testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed()
148
    {
149
        $method = $this->getMethod();
150
        $report = $this->getReportWithOneViolation();
151
152
        $rule = new CamelCaseMethodName();
153
        $rule->setReport($report);
154
        $rule->addProperty('allow-underscore', 'false');
155
        $rule->addProperty('allow-underscore-test', 'false');
156
        $rule->apply($method);
157
    }
158
159
    /**
160
     * Tests that the rule does not apply for a valid test method name
161
     * with an underscore when underscores are allowed.
162
     *
163
     * @return void
164
     */
165
    public function testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed()
166
    {
167
        $method = $this->getMethod();
168
        $report = $this->getReportWithNoViolation();
169
170
        $rule = new CamelCaseMethodName();
171
        $rule->setReport($report);
172
        $rule->addProperty('allow-underscore', 'false');
173
        $rule->addProperty('allow-underscore-test', 'true');
174
        $rule->apply($method);
175
    }
176
177
    /**
178
     * Tests that the rule does not apply for a valid test method name
179
     * with multiple underscores in different positions when underscores are allowed.
180
     *
181
     * @return void
182
     */
183
    public function testRuleDoesNotApplyForTestMethodWithMultipleUnderscoresWhenAllowed()
184
    {
185
        $method = $this->getMethod();
186
        $report = $this->getReportWithNoViolation();
187
188
        $rule = new CamelCaseMethodName();
189
        $rule->setReport($report);
190
        $rule->addProperty('allow-underscore', 'false');
191
        $rule->addProperty('allow-underscore-test', 'true');
192
        $rule->apply($method);
193
    }
194
195
    /**
196
     * Tests that the rule does apply for a test method name
197
     * with consecutive underscores even when underscores are allowed.
198
     *
199
     * @return void
200
     */
201
    public function testRuleAppliesToTestMethodWithTwoConsecutiveUnderscoresWhenAllowed()
202
    {
203
        $method = $this->getMethod();
204
        $report = $this->getReportWithOneViolation();
205
206
        $rule = new CamelCaseMethodName();
207
        $rule->setReport($report);
208
        $rule->addProperty('allow-underscore', 'false');
209
        $rule->addProperty('allow-underscore-test', 'true');
210
        $rule->apply($method);
211
    }
212
213
    /**
214
     * Tests that the rule does apply to for test method names that
215
     * have a capital after their single allowed underscore.
216
     *
217
     * @return void
218
     */
219
    public function testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital()
220
    {
221
        $method = $this->getMethod();
222
        $report = $this->getReportWithOneViolation();
223
224
        $rule = new CamelCaseMethodName();
225
        $rule->setReport($report);
226
        $rule->addProperty('allow-underscore', 'false');
227
        $rule->addProperty('allow-underscore-test', 'true');
228
        $rule->apply($method);
229
    }
230
231
    /**
232
     * Returns the first method found in a source file related to the calling
233
     * test method.
234
     *
235
     * @return \PHPMD\Node\MethodNode
236
     */
237
    protected function getMethod()
238
    {
239
        $methods = $this->getClass()->getMethods();
240
241
        return reset($methods);
242
    }
243
}
244