PageRenderTime 73ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/php/PHP/Depend/Visitor/DefaultListenerTest.php

https://github.com/Proudio-Interactive/pdepend
PHP | 317 lines | 181 code | 34 blank | 102 comment | 0 complexity | 2fdf39059e8cd22ba9b7428ecebe3c81 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2011, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package PHP_Depend
  41. * @subpackage Code
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://pdepend.org/
  47. */
  48. require_once dirname(__FILE__) . '/../AbstractTest.php';
  49. require_once dirname(__FILE__) . '/DefaultVisitorDummy.php';
  50. require_once dirname(__FILE__) . '/TestListener.php';
  51. /**
  52. * Test case for the default visit listener implementation.
  53. *
  54. * @category QualityAssurance
  55. * @package PHP_Depend
  56. * @subpackage Code
  57. * @author Manuel Pichler <mapi@pdepend.org>
  58. * @copyright 2008-2011 Manuel Pichler. All rights reserved.
  59. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  60. * @version Release: @package_version@
  61. * @link http://pdepend.org/
  62. *
  63. * @covers PHP_Depend_Visitor_AbstractListener
  64. * @group pdepend
  65. * @group pdepend::visitor
  66. * @group unittest
  67. */
  68. class PHP_Depend_Visitor_DefaultListenerTest extends PHP_Depend_AbstractTest
  69. {
  70. /**
  71. * testDefaultImplementationCallsListeners
  72. *
  73. * @return void
  74. */
  75. public function testDefaultImplementationCallsListeners()
  76. {
  77. $codeUri = self::createCodeResourceUriForTest();
  78. $packages = self::parseSource($codeUri);
  79. $listener = new PHP_Depend_Visitor_TestListener();
  80. $visitor = new PHP_Depend_Visitor_DefaultVisitorDummy();
  81. $visitor->addVisitListener($listener);
  82. $visitor->visitPackage($packages->current());
  83. $actual = $listener->nodes;
  84. $expected = array(
  85. $codeUri . '#start' => true,
  86. $codeUri . '#end' => true,
  87. 'package#start' => true,
  88. 'package#end' => true,
  89. 'clazz#start' => true,
  90. 'clazz#end' => true,
  91. 'func#start' => true,
  92. 'func#end' => true,
  93. 'interfs#start' => true,
  94. 'interfs#end' => true,
  95. 'm1#start' => true,
  96. 'm1#end' => true,
  97. 'm2#start' => true,
  98. 'm2#end' => true,
  99. 'm3#start' => true,
  100. 'm3#end' => true,
  101. 'm4#start' => true,
  102. 'm4#end' => true,
  103. '$_p1#start' => true,
  104. '$_p1#end' => true,
  105. );
  106. ksort($actual);
  107. ksort($expected);
  108. $this->assertEquals($expected, $actual);
  109. }
  110. /**
  111. * Tests that the default listener implementation delegates a class call
  112. * to the startVisitNode() and endVisitNode() methods.
  113. *
  114. * @return void
  115. */
  116. public function testListenerCallsStartNodeEndNodeForClass()
  117. {
  118. include_once 'PHP/Depend/Code/Class.php';
  119. $class = $this->getMock(
  120. 'PHP_Depend_Code_Class',
  121. array(
  122. 'getName', 'getSourceFile'
  123. ),
  124. array(__FUNCTION__)
  125. );
  126. $class->expects($this->atLeastOnce())
  127. ->method('getName')
  128. ->will($this->returnValue(__FUNCTION__));
  129. $class->expects($this->atLeastOnce())
  130. ->method('getSourceFile')
  131. ->will(
  132. $this->returnValue(
  133. $this->getMock('PHP_Depend_Code_File', array(), array(null))
  134. )
  135. );
  136. $listener = new PHP_Depend_Visitor_TestListener();
  137. $visitor = new PHP_Depend_Visitor_DefaultVisitorDummy();
  138. $visitor->addVisitListener($listener);
  139. $class->accept($visitor);
  140. $actual = $listener->nodes;
  141. $expected = array(
  142. __FUNCTION__ . '#start' => true,
  143. __FUNCTION__ . '#end' => true,
  144. );
  145. $this->assertEquals($expected, $actual);
  146. }
  147. /**
  148. * Tests that the default listener implementation delegates an interface
  149. * call to the startVisitNode() and endVisitNode() methods.
  150. *
  151. * @return void
  152. */
  153. public function testListenerCallsStartNodeEndNodeForInterface()
  154. {
  155. include_once 'PHP/Depend/Code/Interface.php';
  156. $interface = $this->getMock(
  157. 'PHP_Depend_Code_Interface',
  158. array(
  159. 'getName', 'getSourceFile'
  160. ),
  161. array(__FUNCTION__)
  162. );
  163. $interface->expects($this->atLeastOnce())
  164. ->method('getName')
  165. ->will($this->returnValue(__FUNCTION__));
  166. $interface->expects($this->atLeastOnce())
  167. ->method('getSourceFile')
  168. ->will(
  169. $this->returnValue(
  170. $this->getMock('PHP_Depend_Code_File', array(), array(null))
  171. )
  172. );
  173. $listener = new PHP_Depend_Visitor_TestListener();
  174. $visitor = new PHP_Depend_Visitor_DefaultVisitorDummy();
  175. $visitor->addVisitListener($listener);
  176. $interface->accept($visitor);
  177. $actual = $listener->nodes;
  178. $expected = array(
  179. __FUNCTION__ . '#start' => true,
  180. __FUNCTION__ . '#end' => true,
  181. );
  182. $this->assertEquals($expected, $actual);
  183. }
  184. /**
  185. * Tests that the default listener implementation delegates a function
  186. * call to the startVisitNode() and endVisitNode() methods.
  187. *
  188. * @return void
  189. */
  190. public function testListenerCallsStartNodeEndNodeForFunction()
  191. {
  192. include_once 'PHP/Depend/Code/Function.php';
  193. $function = $this->getMock(
  194. 'PHP_Depend_Code_Function',
  195. array(
  196. 'getName', 'getSourceFile', 'getParameters'
  197. ),
  198. array(__FUNCTION__)
  199. );
  200. $function->expects($this->atLeastOnce())
  201. ->method('getName')
  202. ->will($this->returnValue(__FUNCTION__));
  203. $function->expects($this->atLeastOnce())
  204. ->method('getSourceFile')
  205. ->will(
  206. $this->returnValue(
  207. $this->getMock('PHP_Depend_Code_File', array(), array(null))
  208. )
  209. );
  210. $function->expects($this->atLeastOnce())
  211. ->method('getParameters')
  212. ->will($this->returnValue(array()));
  213. $listener = new PHP_Depend_Visitor_TestListener();
  214. $visitor = new PHP_Depend_Visitor_DefaultVisitorDummy();
  215. $visitor->addVisitListener($listener);
  216. $function->accept($visitor);
  217. $actual = $listener->nodes;
  218. $expected = array(
  219. __FUNCTION__ . '#start' => true,
  220. __FUNCTION__ . '#end' => true,
  221. );
  222. $this->assertEquals($expected, $actual);
  223. }
  224. /**
  225. * Tests that the default listener implementation delegates a method call to
  226. * the startVisitNode() and endVisitNode() methods.
  227. *
  228. * @return void
  229. */
  230. public function testListenerCallsStartNodeEndNodeForMethod()
  231. {
  232. include_once 'PHP/Depend/Code/Method.php';
  233. $method = $this->getMock(
  234. 'PHP_Depend_Code_Method',
  235. array(
  236. 'getName', 'getSourceFile', 'getParameters'
  237. ),
  238. array(__FUNCTION__)
  239. );
  240. $method->expects($this->atLeastOnce())
  241. ->method('getName')
  242. ->will($this->returnValue(__FUNCTION__));
  243. $method->expects($this->atLeastOnce())
  244. ->method('getParameters')
  245. ->will($this->returnValue(array()));
  246. $listener = new PHP_Depend_Visitor_TestListener();
  247. $visitor = new PHP_Depend_Visitor_DefaultVisitorDummy();
  248. $visitor->addVisitListener($listener);
  249. $method->accept($visitor);
  250. $actual = $listener->nodes;
  251. $expected = array(
  252. __FUNCTION__ . '#start' => true,
  253. __FUNCTION__ . '#end' => true,
  254. );
  255. $this->assertEquals($expected, $actual);
  256. }
  257. /**
  258. * testListenerCallsStartVisitNodeForPassedParameterInstance
  259. *
  260. * @return void
  261. */
  262. public function testListenerCallsStartVisitNodeForPassedParameterInstance()
  263. {
  264. $listener = $this->getMock('PHP_Depend_Visitor_AbstractListener', array('startVisitNode'));
  265. $listener->expects($this->once())
  266. ->method('startVisitNode');
  267. $parameter = $this->getMock('PHP_Depend_Code_Parameter', array(), array(null), '', false);
  268. $listener->startVisitParameter($parameter);
  269. }
  270. /**
  271. * testListenerCallsEndVisitNodeForPassedParameterInstance
  272. *
  273. * @return void
  274. */
  275. public function testListenerCallsEndVisitNodeForPassedParameterInstance()
  276. {
  277. $listener = $this->getMock('PHP_Depend_Visitor_AbstractListener', array('endVisitNode'));
  278. $listener->expects($this->once())
  279. ->method('endVisitNode');
  280. $parameter = $this->getMock('PHP_Depend_Code_Parameter', array(), array(null), '', false);
  281. $listener->endVisitParameter($parameter);
  282. }
  283. }