| 1 |
143
|
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 |
143
|
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 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 42 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 43 |
143
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 44 |
↓
|
mapi |
* @version SVN: $Id$ |
|
| 45 |
174
|
mapi |
* @link http://phpmd.org |
|
| 46 |
143
|
mapi |
*/ |
|
| 47 |
↓
|
mapi |
|
|
| 48 |
↓
|
mapi |
require_once dirname(__FILE__) . '/AbstractTest.php'; |
|
| 49 |
↓
|
mapi |
|
|
| 50 |
↓
|
mapi |
require_once 'PHP/PMD/Parser.php'; |
|
| 51 |
↓
|
mapi |
|
|
| 52 |
↓
|
mapi |
require_once 'PHP/Depend/ProcessListenerI.php'; |
|
| 53 |
↓
|
mapi |
|
|
| 54 |
↓
|
mapi |
/** |
|
| 55 |
↓
|
mapi |
* Test case for the PHP_Depend backend adapter class. |
|
| 56 |
↓
|
mapi |
* |
|
| 57 |
↓
|
mapi |
* @category PHP |
|
| 58 |
↓
|
mapi |
* @package PHP_PMD |
|
| 59 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 60 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 61 |
143
|
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 |
143
|
mapi |
*/ |
|
| 65 |
↓
|
mapi |
class PHP_PMD_ParserTest extends PHP_PMD_AbstractTest |
|
| 66 |
↓
|
mapi |
{ |
|
| 67 |
↓
|
mapi |
/** |
|
| 68 |
↓
|
mapi |
* Tests that the metrics adapter delegates a node to a registered rule-set. |
|
| 69 |
↓
|
mapi |
* |
|
| 70 |
↓
|
mapi |
* @return void |
|
| 71 |
↓
|
mapi |
* @covers PHP_PMD_Parser |
|
| 72 |
↓
|
mapi |
* @group phpmd |
|
| 73 |
↓
|
mapi |
* @group unittest |
|
| 74 |
↓
|
mapi |
*/ |
|
| 75 |
↓
|
mapi |
public function testAdapterDelegatesClassNodeToRuleSet() |
|
| 76 |
↓
|
mapi |
{ |
|
| 77 |
205
|
mapi |
$mock = $this->getPHPDependClassMock(); |
|
| 78 |
↓
|
mapi |
$mock->expects($this->once()) |
|
| 79 |
↓
|
mapi |
->method('isUserDefined') |
|
| 80 |
↓
|
mapi |
->will($this->returnValue(true)); |
|
| 81 |
↓
|
mapi |
|
|
| 82 |
143
|
mapi |
$adapter = new PHP_PMD_Parser($this->_getPHPDependMock()); |
|
| 83 |
200
|
mapi |
$adapter->addRuleSet($this->getRuleSetMock('PHP_PMD_Node_Class')); |
|
| 84 |
143
|
mapi |
$adapter->setReport($this->getReportMock(0)); |
|
| 85 |
205
|
mapi |
$adapter->visitClass($mock); |
|
| 86 |
143
|
mapi |
} |
|
| 87 |
↓
|
mapi |
|
|
| 88 |
↓
|
mapi |
/** |
|
| 89 |
↓
|
mapi |
* Tests that the metrics adapter does not delegate a node without source |
|
| 90 |
↓
|
mapi |
* code file to a registered rule-set. |
|
| 91 |
↓
|
mapi |
* |
|
| 92 |
↓
|
mapi |
* @return void |
|
| 93 |
↓
|
mapi |
* @covers PHP_PMD_Parser |
|
| 94 |
↓
|
mapi |
* @group phpmd |
|
| 95 |
↓
|
mapi |
* @group unittest |
|
| 96 |
↓
|
mapi |
*/ |
|
| 97 |
↓
|
mapi |
public function testAdapterDoesNotDelegateNonSourceClassNodeToRuleSet() |
|
| 98 |
↓
|
mapi |
{ |
|
| 99 |
205
|
mapi |
$mock = $this->getPHPDependClassMock(); |
|
| 100 |
↓
|
mapi |
$mock->expects($this->once()) |
|
| 101 |
↓
|
mapi |
->method('isUserDefined') |
|
| 102 |
↓
|
mapi |
->will($this->returnValue(false)); |
|
| 103 |
↓
|
mapi |
|
|
| 104 |
143
|
mapi |
$adapter = new PHP_PMD_Parser($this->_getPHPDependMock()); |
|
| 105 |
↓
|
mapi |
$adapter->addRuleSet($this->getRuleSetMock()); |
|
| 106 |
↓
|
mapi |
$adapter->setReport($this->getReportMock(0)); |
|
| 107 |
205
|
mapi |
$adapter->visitClass($mock); |
|
| 108 |
143
|
mapi |
} |
|
| 109 |
↓
|
mapi |
|
|
| 110 |
↓
|
mapi |
/** |
|
| 111 |
↓
|
mapi |
* Tests that the metrics adapter delegates a node to a registered rule-set. |
|
| 112 |
↓
|
mapi |
* |
|
| 113 |
↓
|
mapi |
* @return void |
|
| 114 |
↓
|
mapi |
* @covers PHP_PMD_Parser |
|
| 115 |
↓
|
mapi |
* @group phpmd |
|
| 116 |
↓
|
mapi |
* @group unittest |
|
| 117 |
↓
|
mapi |
*/ |
|
| 118 |
↓
|
mapi |
public function testAdapterDelegatesMethodNodeToRuleSet() |
|
| 119 |
↓
|
mapi |
{ |
|
| 120 |
↓
|
mapi |
$adapter = new PHP_PMD_Parser($this->_getPHPDependMock()); |
|
| 121 |
200
|
mapi |
$adapter->addRuleSet($this->getRuleSetMock('PHP_PMD_Node_Method')); |
|
| 122 |
143
|
mapi |
$adapter->setReport($this->getReportMock(0)); |
|
| 123 |
↓
|
mapi |
$adapter->visitMethod($this->getPHPDependMethodMock()); |
|
| 124 |
↓
|
mapi |
} |
|
| 125 |
↓
|
mapi |
|
|
| 126 |
↓
|
mapi |
/** |
|
| 127 |
↓
|
mapi |
* Tests that the metrics adapter does not delegate a node without source |
|
| 128 |
↓
|
mapi |
* code file to a registered rule-set. |
|
| 129 |
↓
|
mapi |
* |
|
| 130 |
↓
|
mapi |
* @return void |
|
| 131 |
↓
|
mapi |
* @covers PHP_PMD_Parser |
|
| 132 |
↓
|
mapi |
* @group phpmd |
|
| 133 |
↓
|
mapi |
* @group unittest |
|
| 134 |
↓
|
mapi |
*/ |
|
| 135 |
↓
|
mapi |
public function testAdapterDoesNotDelegateNonSourceMethodNodeToRuleSet() |
|
| 136 |
↓
|
mapi |
{ |
|
| 137 |
↓
|
mapi |
$adapter = new PHP_PMD_Parser($this->_getPHPDependMock()); |
|
| 138 |
↓
|
mapi |
$adapter->addRuleSet($this->getRuleSetMock()); |
|
| 139 |
↓
|
mapi |
$adapter->setReport($this->getReportMock(0)); |
|
| 140 |
↓
|
mapi |
$adapter->visitMethod($this->getPHPDependMethodMock(null)); |
|
| 141 |
↓
|
mapi |
} |
|
| 142 |
↓
|
mapi |
|
|
| 143 |
↓
|
mapi |
/** |
|
| 144 |
↓
|
mapi |
* Tests that the metrics adapter delegates a node to a registered rule-set. |
|
| 145 |
↓
|
mapi |
* |
|
| 146 |
↓
|
mapi |
* @return void |
|
| 147 |
↓
|
mapi |
* @covers PHP_PMD_Parser |
|
| 148 |
↓
|
mapi |
* @group phpmd |
|
| 149 |
↓
|
mapi |
* @group unittest |
|
| 150 |
↓
|
mapi |
*/ |
|
| 151 |
↓
|
mapi |
public function testAdapterDelegatesFunctionNodeToRuleSet() |
|
| 152 |
↓
|
mapi |
{ |
|
| 153 |
↓
|
mapi |
$adapter = new PHP_PMD_Parser($this->_getPHPDependMock()); |
|
| 154 |
200
|
mapi |
$adapter->addRuleSet($this->getRuleSetMock('PHP_PMD_Node_Function')); |
|
| 155 |
143
|
mapi |
$adapter->setReport($this->getReportMock(0)); |
|
| 156 |
↓
|
mapi |
$adapter->visitFunction($this->getPHPDependFunctionMock()); |
|
| 157 |
↓
|
mapi |
} |
|
| 158 |
↓
|
mapi |
|
|
| 159 |
↓
|
mapi |
/** |
|
| 160 |
↓
|
mapi |
* Tests that the metrics adapter does not delegate a node without source |
|
| 161 |
↓
|
mapi |
* code file to a registered rule-set. |
|
| 162 |
↓
|
mapi |
* |
|
| 163 |
↓
|
mapi |
* @return void |
|
| 164 |
↓
|
mapi |
* @covers PHP_PMD_Parser |
|
| 165 |
↓
|
mapi |
* @group phpmd |
|
| 166 |
↓
|
mapi |
* @group unittest |
|
| 167 |
↓
|
mapi |
*/ |
|
| 168 |
↓
|
mapi |
public function testAdapterDoesNotDelegateNonSourceFunctionNodeToRuleSet() |
|
| 169 |
↓
|
mapi |
{ |
|
| 170 |
↓
|
mapi |
$adapter = new PHP_PMD_Parser($this->_getPHPDependMock()); |
|
| 171 |
↓
|
mapi |
$adapter->addRuleSet($this->getRuleSetMock()); |
|
| 172 |
↓
|
mapi |
$adapter->setReport($this->getReportMock(0)); |
|
| 173 |
↓
|
mapi |
$adapter->visitFunction($this->getPHPDependFunctionMock(null)); |
|
| 174 |
↓
|
mapi |
} |
|
| 175 |
↓
|
mapi |
|
|
| 176 |
↓
|
mapi |
/** |
|
| 177 |
↓
|
mapi |
* Creates a mocked PHP_Depend instance. |
|
| 178 |
↓
|
mapi |
* |
|
| 179 |
↓
|
mapi |
* @return PHP_Depend |
|
| 180 |
↓
|
mapi |
*/ |
|
| 181 |
↓
|
mapi |
private function _getPHPDependMock() |
|
| 182 |
↓
|
mapi |
{ |
|
| 183 |
↓
|
mapi |
include_once 'PHP/Depend.php'; |
|
| 184 |
↓
|
mapi |
|
|
| 185 |
↓
|
mapi |
return $this->getMock('PHP_Depend'); |
|
| 186 |
↓
|
mapi |
} |
|
| 187 |
↓
|
mapi |
|
|
| 188 |
↓
|
mapi |
/** |
|
| 189 |
↓
|
mapi |
* Creates a mocked PHP_Depend class instance. |
|
| 190 |
↓
|
mapi |
* |
|
| 191 |
↓
|
mapi |
* @return PHP_Depend_Code_Class |
|
| 192 |
↓
|
mapi |
*/ |
|
| 193 |
205
|
mapi |
protected function getPHPDependClassMock() |
|
| 194 |
143
|
mapi |
{ |
|
| 195 |
↓
|
mapi |
include_once 'PHP/Depend/Code/Class.php'; |
|
| 196 |
↓
|
mapi |
|
|
| 197 |
↓
|
mapi |
$class = $this->getMock('PHP_Depend_Code_Class', |
|
| 198 |
↓
|
mapi |
array(), |
|
| 199 |
↓
|
mapi |
array(null)); |
|
| 200 |
205
|
mapi |
$class->expects($this->any()) |
|
| 201 |
143
|
mapi |
->method('getSourceFile') |
|
| 202 |
205
|
mapi |
->will($this->returnValue($this->getPHPDependFileMock('foo.php'))); |
|
| 203 |
143
|
mapi |
$class->expects($this->any()) |
|
| 204 |
↓
|
mapi |
->method('getConstants') |
|
| 205 |
↓
|
mapi |
->will($this->returnValue(new ArrayIterator(array()))); |
|
| 206 |
↓
|
mapi |
$class->expects($this->any()) |
|
| 207 |
↓
|
mapi |
->method('getProperties') |
|
| 208 |
↓
|
mapi |
->will($this->returnValue(new ArrayIterator(array()))); |
|
| 209 |
↓
|
mapi |
$class->expects($this->any()) |
|
| 210 |
↓
|
mapi |
->method('getMethods') |
|
| 211 |
↓
|
mapi |
->will($this->returnValue(new ArrayIterator(array()))); |
|
| 212 |
↓
|
mapi |
|
|
| 213 |
↓
|
mapi |
return $class; |
|
| 214 |
↓
|
mapi |
} |
|
| 215 |
↓
|
mapi |
|
|
| 216 |
↓
|
mapi |
/** |
|
| 217 |
↓
|
mapi |
* Creates a mocked PHP_Depend function instance. |
|
| 218 |
↓
|
mapi |
* |
|
| 219 |
↓
|
mapi |
* @return PHP_Depend_Code_Function |
|
| 220 |
↓
|
mapi |
*/ |
|
| 221 |
↓
|
mapi |
protected function getPHPDependFunctionMock($fileName = '/foo/bar.php') |
|
| 222 |
↓
|
mapi |
{ |
|
| 223 |
↓
|
mapi |
include_once 'PHP/Depend/Code/Function.php'; |
|
| 224 |
↓
|
mapi |
|
|
| 225 |
↓
|
mapi |
$function = $this->getMock('PHP_Depend_Code_Function', |
|
| 226 |
↓
|
mapi |
array(), |
|
| 227 |
↓
|
mapi |
array(null)); |
|
| 228 |
↓
|
mapi |
$function->expects($this->atLeastOnce()) |
|
| 229 |
↓
|
mapi |
->method('getSourceFile') |
|
| 230 |
↓
|
mapi |
->will($this->returnValue($this->getPHPDependFileMock($fileName))); |
|
| 231 |
↓
|
mapi |
|
|
| 232 |
↓
|
mapi |
return $function; |
|
| 233 |
↓
|
mapi |
} |
|
| 234 |
↓
|
mapi |
|
|
| 235 |
↓
|
mapi |
/** |
|
| 236 |
↓
|
mapi |
* Creates a mocked PHP_Depend method instance. |
|
| 237 |
↓
|
mapi |
* |
|
| 238 |
196
|
mapi |
* @return PHP_Depend_Code_CodeMethod |
|
| 239 |
143
|
mapi |
*/ |
|
| 240 |
↓
|
mapi |
protected function getPHPDependMethodMock($fileName = '/foo/bar.php') |
|
| 241 |
↓
|
mapi |
{ |
|
| 242 |
↓
|
mapi |
include_once 'PHP/Depend/Code/Method.php'; |
|
| 243 |
↓
|
mapi |
|
|
| 244 |
↓
|
mapi |
$method = $this->getMock('PHP_Depend_Code_Method', |
|
| 245 |
↓
|
mapi |
array(), |
|
| 246 |
↓
|
mapi |
array(null)); |
|
| 247 |
↓
|
mapi |
$method->expects($this->atLeastOnce()) |
|
| 248 |
↓
|
mapi |
->method('getSourceFile') |
|
| 249 |
↓
|
mapi |
->will($this->returnValue($this->getPHPDependFileMock($fileName))); |
|
| 250 |
↓
|
mapi |
|
|
| 251 |
↓
|
mapi |
return $method; |
|
| 252 |
↓
|
mapi |
} |
|
| 253 |
↓
|
mapi |
|
|
| 254 |
↓
|
mapi |
/** |
|
| 255 |
↓
|
mapi |
* Creates a mocked PHP_Depend file instance. |
|
| 256 |
↓
|
mapi |
* |
|
| 257 |
↓
|
mapi |
* @param string $fileName The temporary file name. |
|
| 258 |
↓
|
mapi |
* |
|
| 259 |
↓
|
mapi |
* @return PHP_Depend_Code_File |
|
| 260 |
↓
|
mapi |
*/ |
|
| 261 |
↓
|
mapi |
protected function getPHPDependFileMock($fileName) |
|
| 262 |
↓
|
mapi |
{ |
|
| 263 |
↓
|
mapi |
include_once 'PHP/Depend/Code/File.php'; |
|
| 264 |
↓
|
mapi |
|
|
| 265 |
↓
|
mapi |
$file = $this->getMock('PHP_Depend_Code_File', array(), array(null)); |
|
| 266 |
↓
|
mapi |
$file->expects($this->any()) |
|
| 267 |
↓
|
mapi |
->method('getFileName') |
|
| 268 |
↓
|
mapi |
->will($this->returnValue($fileName)); |
|
| 269 |
↓
|
mapi |
|
|
| 270 |
↓
|
mapi |
return $file; |
|
| 271 |
↓
|
mapi |
} |
|
| 272 |
↓
|
mapi |
} |
|