Arbit - project tracking

PHPMD

Browse source code

File: / test/ PHP/ PMD/ Rule/ Design/ TooManyMethodsTest.php

Type
text/plain text/plain
Last Author
mapi
Version
200
Line Rev. Author Source
1 98 mapi <?php
2 mapi /**
3 mapi * This file is part of PHP_PMD.
4 mapi *
5 mapi * PHP Version 5
6 mapi *
7 174 mapi * Copyright (c) 2009-2010, Manuel Pichler <mapi@phpmd.org>.
8 98 mapi * All rights reserved.
9 mapi *
10 mapi * Redistribution and use in source and binary forms, with or without
11 mapi * modification, are permitted provided that the following conditions
12 mapi * are met:
13 mapi *
14 mapi * * Redistributions of source code must retain the above copyright
15 mapi * notice, this list of conditions and the following disclaimer.
16 mapi *
17 mapi * * Redistributions in binary form must reproduce the above copyright
18 mapi * notice, this list of conditions and the following disclaimer in
19 mapi * the documentation and/or other materials provided with the
20 mapi * distribution.
21 mapi *
22 mapi * * Neither the name of Manuel Pichler nor the names of his
23 mapi * contributors may be used to endorse or promote products derived
24 mapi * from this software without specific prior written permission.
25 mapi *
26 mapi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 mapi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 mapi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 mapi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 mapi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 mapi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 mapi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 mapi * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 mapi * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 mapi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 mapi * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 mapi * POSSIBILITY OF SUCH DAMAGE.
38 mapi *
39 mapi * @category PHP
40 mapi * @package PHP_PMD
41 mapi * @subpackage Rule_Design
42 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
43 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
44 98 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
45 mapi * @version SVN: $Id$
46 174 mapi * @link http://phpmd.org
47 98 mapi */
48 mapi
49 mapi require_once dirname(__FILE__) . '/../../AbstractTest.php';
50 mapi
51 mapi require_once 'PHP/PMD/Rule/Design/TooManyMethods.php';
52 mapi
53 mapi /**
54 mapi * Test case for the too many methods rule.
55 mapi *
56 mapi * @category PHP
57 mapi * @package PHP_PMD
58 mapi * @subpackage Rule_Design
59 174 mapi * @author Manuel Pichler <mapi@phpmd.org>
60 150 mapi * @copyright 2009-2010 Manuel Pichler. All rights reserved.
61 98 mapi * @license http://www.opensource.org/licenses/bsd-license.php BSD License
62 mapi * @version Release: @package_version@
63 174 mapi * @link http://phpmd.org
64 98 mapi */
65 mapi class PHP_PMD_Rule_Design_TooManyMethodsTest extends PHP_PMD_AbstractTest
66 mapi {
67 mapi /**
68 mapi * testRuleDoesNotApplyToClassesWithLessMethodsThanThreshold
69 mapi *
70 127 mapi * @return void
71 98 mapi * @covers PHP_PMD_Rule_Design_TooManyMethods
72 mapi * @group phpmd
73 127 mapi * @group phpmd::rule
74 mapi * @group phpmd::rule::design
75 98 mapi * @group unittest
76 mapi */
77 mapi public function testRuleDoesNotApplyToClassesWithLessMethodsThanThreshold()
78 mapi {
79 mapi $rule = new PHP_PMD_Rule_Design_TooManyMethods();
80 mapi $rule->setReport($this->getReportMock(0));
81 mapi $rule->addProperty('maxmethods', '42');
82 mapi $rule->apply($this->_createClassMock(23));
83 mapi }
84 mapi
85 mapi /**
86 100 mapi * testRuleDoesNotApplyToClassesWithSameNumberOfMethodsAsThreshold
87 98 mapi *
88 127 mapi * @return void
89 98 mapi * @covers PHP_PMD_Rule_Design_TooManyMethods
90 mapi * @group phpmd
91 127 mapi * @group phpmd::rule
92 mapi * @group phpmd::rule::design
93 98 mapi * @group unittest
94 mapi */
95 100 mapi public function testRuleDoesNotApplyToClassesWithSameNumberOfMethodsAsThreshold()
96 98 mapi {
97 mapi $rule = new PHP_PMD_Rule_Design_TooManyMethods();
98 100 mapi $rule->setReport($this->getReportMock(0));
99 98 mapi $rule->addProperty('maxmethods', '42');
100 100 mapi $rule->apply($this->_createClassMock(42));
101 98 mapi }
102 mapi
103 mapi /**
104 mapi * testRuleAppliesToClassesWithMoreMethodsThanThreshold
105 mapi *
106 127 mapi * @return void
107 98 mapi * @covers PHP_PMD_Rule_Design_TooManyMethods
108 mapi * @group phpmd
109 127 mapi * @group phpmd::rule
110 mapi * @group phpmd::rule::design
111 98 mapi * @group unittest
112 mapi */
113 mapi public function testRuleAppliesToClassesWithMoreMethodsThanThreshold()
114 mapi {
115 mapi $rule = new PHP_PMD_Rule_Design_TooManyMethods();
116 mapi $rule->setReport($this->getReportMock(1));
117 mapi $rule->addProperty('maxmethods', '23');
118 mapi $rule->apply($this->_createClassMock(42, array_fill(0, 42, __FUNCTION__)));
119 mapi }
120 mapi
121 mapi /**
122 mapi * testRuleIgnoresGetterMethodsInTest
123 mapi *
124 127 mapi * @return void
125 98 mapi * @covers PHP_PMD_Rule_Design_TooManyMethods
126 mapi * @group phpmd
127 127 mapi * @group phpmd::rule
128 mapi * @group phpmd::rule::design
129 98 mapi * @group unittest
130 mapi */
131 mapi public function testRuleIgnoresGetterMethodsInTest()
132 mapi {
133 mapi $rule = new PHP_PMD_Rule_Design_TooManyMethods();
134 mapi $rule->setReport($this->getReportMock(0));
135 100 mapi $rule->addProperty('maxmethods', '1');
136 98 mapi $rule->apply($this->_createClassMock(2, array('invoke', 'getClass')));
137 mapi }
138 mapi
139 mapi /**
140 mapi * testRuleIgnoresSetterMethodsInTest
141 mapi *
142 127 mapi * @return void
143 98 mapi * @covers PHP_PMD_Rule_Design_TooManyMethods
144 mapi * @group phpmd
145 127 mapi * @group phpmd::rule
146 mapi * @group phpmd::rule::design
147 98 mapi * @group unittest
148 mapi */
149 mapi public function testRuleIgnoresSetterMethodsInTest()
150 mapi {
151 mapi $rule = new PHP_PMD_Rule_Design_TooManyMethods();
152 mapi $rule->setReport($this->getReportMock(0));
153 100 mapi $rule->addProperty('maxmethods', '1');
154 98 mapi $rule->apply($this->_createClassMock(2, array('invoke', 'setClass')));
155 mapi }
156 mapi
157 mapi /**
158 mapi * testRuleIgnoresGetterAndSetterMethodsInTest
159 mapi *
160 127 mapi * @return void
161 98 mapi * @covers PHP_PMD_Rule_Design_TooManyMethods
162 mapi * @group phpmd
163 127 mapi * @group phpmd::rule
164 mapi * @group phpmd::rule::design
165 98 mapi * @group unittest
166 mapi */
167 mapi public function testRuleIgnoresGetterAndSetterMethodsInTest()
168 mapi {
169 mapi $rule = new PHP_PMD_Rule_Design_TooManyMethods();
170 mapi $rule->setReport($this->getReportMock(0));
171 100 mapi $rule->addProperty('maxmethods', '2');
172 98 mapi $rule->apply($this->_createClassMock(3, array('invoke', 'getClass', 'setClass')));
173 mapi }
174 mapi
175 mapi /**
176 mapi * Creates a prepared class node mock
177 mapi *
178 mapi * @param integer $numberOfMethods Number of methods metric value.
179 mapi * @param array(string) $methodNames Name of all methods.
180 mapi *
181 200 mapi * @return PHP_PMD_Node_Class
182 98 mapi */
183 mapi private function _createClassMock($numberOfMethods, array $methodNames = null)
184 mapi {
185 mapi $class = $this->getClassMock('nom', $numberOfMethods);
186 mapi
187 mapi if (is_array($methodNames)) {
188 mapi $class->expects($this->once())
189 mapi ->method('getMethodNames')
190 mapi ->will($this->returnValue($methodNames));
191 mapi }
192 mapi return $class;
193 mapi }
194 mapi }