PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/DocBlox/Token/IteratorTestDocBlox.php

https://github.com/androa/Docblox
PHP | 290 lines | 189 code | 40 blank | 61 comment | 1 complexity | 40efff6399caaedc2242127f1a9f41a7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Tokens
  9. * @subpackage Tests
  10. * @author Mike van Riel <mike.vanriel@naenius.com>
  11. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT
  13. * @link http://docblox-project.org
  14. */
  15. /**
  16. * TokenIterator mock class.
  17. *
  18. * @category DocBlox
  19. * @package Tokens
  20. * @subpackage Tests
  21. * @author Mike van Riel <mike.vanriel@naenius.com>
  22. * @license http://www.opensource.org/licenses/mit-license.php MIT
  23. * @link http://docblox-project.org
  24. */
  25. class DocBlox_Token_IteratorMock extends DocBlox_Token_Iterator {
  26. public function gotoUpByType() {
  27. $this->gotoTokenByTypeInDirection('{', 'up');
  28. }
  29. public function getBrokenTokenIdsOfBracePair() {
  30. return $this->getTokenIdsBetweenPair('}', '{');
  31. }
  32. }
  33. /**
  34. * Test class for DocBlox_Token_Iterator.
  35. *
  36. * @category DocBlox
  37. * @package Tokens
  38. * @subpackage Tests
  39. * @author Mike van Riel <mike.vanriel@naenius.com>
  40. * @license http://www.opensource.org/licenses/mit-license.php MIT
  41. * @link http://docblox-project.org
  42. */
  43. class DocBlox_Token_IteratorTest extends PHPUnit_Framework_TestCase
  44. {
  45. /** @var array[] tokens returned by token_get_all */
  46. protected $tokens = array();
  47. protected $fixture = <<<FIXTURE
  48. <?php
  49. namespace myBla;
  50. class Bla
  51. {
  52. public function testBla()
  53. {
  54. }
  55. }
  56. FIXTURE;
  57. /**
  58. * @var DocBlox_Token_Iterator
  59. */
  60. protected $object;
  61. /**
  62. * Sets up the fixture.
  63. *
  64. * This method is called before a test is executed.
  65. *
  66. * @return void
  67. */
  68. protected function setUp() {
  69. $this->tokens = token_get_all($this->fixture);
  70. $this->object = new DocBlox_Token_Iterator($this->tokens);
  71. $this->object->seek(0);
  72. }
  73. /**
  74. * Tests whether the token iterator is properly constructed and contains
  75. * only DocBlox_Token objects.
  76. *
  77. * @return void
  78. */
  79. public function testConstruct() {
  80. $this->assertGreaterThan(
  81. 0, count($this->object),
  82. 'Expected DocBlox_Token_Iterator to contain more than 0 items'
  83. );
  84. $this->assertEquals(
  85. count($this->tokens),
  86. count($this->object),
  87. 'Expected DocBlox_Token_Iterator to contain the same amount of '
  88. . 'items as the output of the tokenizer'
  89. );
  90. foreach ($this->object as $token)
  91. {
  92. if (!($token instanceof DocBlox_Token)) {
  93. $this->fail('All tokens in the DocBlox_Token_Iterator are '
  94. . 'expected to be of type DocBlox_Token, found: '
  95. . print_r($token, true));
  96. }
  97. }
  98. // test by inserting an array of Tokens ($this->object is
  99. // effectively an array of objects)
  100. $test_array = array(
  101. $this->object[0], $this->object[1], $this->object[2]
  102. );
  103. $other_object = new DocBlox_Token_Iterator($test_array);
  104. $this->assertGreaterThan(
  105. 0, count($other_object),
  106. 'Expected DocBlox_Token_Iterator to contain more than 0 items'
  107. );
  108. $this->assertEquals(
  109. count($test_array),
  110. count($other_object),
  111. 'Expected DocBlox_Token_Iterator to contain the same amount of '
  112. . 'items as the given array of Tokens'
  113. );
  114. }
  115. public function testGotoTokenByTypeInDirection() {
  116. $tokens = token_get_all($this->fixture);
  117. $mock = new DocBlox_Token_IteratorMock($tokens);
  118. $this->setExpectedException('InvalidArgumentException');
  119. // expect an exception because this stub tries to use a wrong direction
  120. $mock->gotoUpByType();
  121. }
  122. /**
  123. * Tests the gotoNextByType method
  124. */
  125. public function testGotoNextByType() {
  126. $this->object->seek(0);
  127. try
  128. {
  129. $token = $this->object->gotoNextByType(T_CLASS, -1);
  130. $this->fail('Expected an InvalidArgumentException when passing a negative number for the max_count argument');
  131. } catch (InvalidArgumentException $e)
  132. {
  133. }
  134. $this->object->seek(0);
  135. $token = $this->object->gotoNextByType(T_CLASS, 0);
  136. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset');
  137. $this->assertNotEquals(0, $this->object->key(), 'Expected the key to have a different position');
  138. $this->object->seek(0);
  139. $token = $this->object->gotoNextByType(T_CLASS, 10);
  140. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within 40 tokens');
  141. $this->assertNotEquals(0, $this->object->key(), 'Expected the key to have a different position');
  142. $this->object->seek(0);
  143. $token = $this->object->gotoNextByType(T_CLASS, 10, T_REQUIRE);
  144. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within 40 tokens before a T_REQUIRE is encountered');
  145. $this->assertNotEquals(0, $this->object->key(), 'Expected the key to have a different position');
  146. $this->object->seek(0);
  147. $token = $this->object->gotoNextByType(T_CLASS, 10, T_NAMESPACE);
  148. $this->assertFalse($token, 'Expected to fail finding a T_CLASS in the dataset within 40 tokens before a T_NAMESPACE is encountered');
  149. $this->assertEquals(0, $this->object->key(), 'Expected the key to be at the starting position');
  150. }
  151. /**
  152. * Tests the gotoNextByType method
  153. */
  154. public function testGotoPreviousByType()
  155. {
  156. $pos = 20;
  157. $this->object->seek($pos);
  158. $token = $this->object->gotoPreviousByType(T_CLASS, 0);
  159. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset');
  160. $this->assertNotEquals($pos, $this->object->key(), 'Expected the key to have a different position');
  161. $this->object->seek($pos);
  162. $token = $this->object->gotoPreviousByType(T_CLASS, $pos);
  163. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within ' . $pos . ' tokens');
  164. $this->assertNotEquals($pos, $this->object->key(), 'Expected the key to have a different position');
  165. $this->object->seek($pos);
  166. $token = $this->object->gotoPreviousByType(T_CLASS, $pos, T_NAMESPACE);
  167. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within ' . $pos . ' tokens before a T_NAMESPACE is encountered');
  168. $this->assertNotEquals($pos, $this->object->key(), 'Expected the key to have a different position');
  169. $this->object->seek($pos);
  170. $token = $this->object->gotoPreviousByType(T_CLASS, $pos, T_FUNCTION);
  171. $this->assertFalse($token, 'Expected to fail finding a T_CLASS in the dataset within ' . $pos . ' tokens before a T_FUNCTION is encountered');
  172. $this->assertEquals($pos, $this->object->key(), 'Expected the key to be at the starting position');
  173. }
  174. public function testFindNextByType() {
  175. $this->object->seek(0);
  176. try
  177. {
  178. $token = $this->object->findNextByType(T_CLASS, -1);
  179. $this->fail('Expected an InvalidArgumentException when passing a negative number for the max_count argument');
  180. } catch (InvalidArgumentException $e)
  181. {
  182. }
  183. $this->object->seek(0);
  184. $token = $this->object->findNextByType(T_CLASS, 0);
  185. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset');
  186. $this->assertEquals(0, $this->object->key(), 'Expected the key to equal the starting position');
  187. $this->object->seek(0);
  188. $token = $this->object->findNextByType(T_CLASS, 40);
  189. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within 40 tokens');
  190. $this->assertEquals(0, $this->object->key(), 'Expected the key to equal the starting position');
  191. $this->object->seek(0);
  192. $token = $this->object->findNextByType(T_CLASS, 40, T_REQUIRE);
  193. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within 40 tokens before a T_REQUIRE is encountered');
  194. $this->assertEquals(0, $this->object->key(), 'Expected the key to equal the starting position');
  195. $this->object->seek(0);
  196. $token = $this->object->findNextByType(T_CLASS, 40, T_NAMESPACE);
  197. $this->assertFalse($token, 'Expected to fail finding a T_CLASS in the dataset within 40 tokens before a T_NAMESPACE is encountered');
  198. $this->assertEquals(0, $this->object->key(), 'Expected the key to be at the starting position');
  199. }
  200. public function testFindPreviousByType() {
  201. $pos = 20;
  202. $this->object->seek($pos);
  203. $token = $this->object->findPreviousByType(T_CLASS, 0);
  204. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset');
  205. $this->assertEquals($pos, $this->object->key(), 'Expected the key to have a different position');
  206. $this->object->seek($pos);
  207. $token = $this->object->findPreviousByType(T_CLASS, $pos);
  208. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within ' . $pos . ' tokens');
  209. $this->assertEquals($pos, $this->object->key(), 'Expected the key to have a different position');
  210. $this->object->seek($pos);
  211. $token = $this->object->findPreviousByType(T_CLASS, $pos, T_NAMESPACE);
  212. $this->assertInstanceOf('DocBlox_Token', $token, 'Expected to find a T_CLASS in the dataset within ' . $pos . ' tokens before a T_NAMESPACE is encountered');
  213. $this->assertEquals($pos, $this->object->key(), 'Expected the key to have a different position');
  214. $this->object->seek($pos);
  215. $token = $this->object->findPreviousByType(T_CLASS, $pos, T_FUNCTION);
  216. $this->assertFalse($token, 'Expected to fail finding a T_CLASS in the dataset within ' . $pos . ' tokens before a T_FUNCTION is encountered');
  217. $this->assertEquals($pos, $this->object->key(), 'Expected the key to be at the starting position');
  218. }
  219. public function testGetBrokenTokenIdsOfBracePair() {
  220. $tokens = token_get_all(file_get_contents(dirname(__FILE__) . '/../../../data/TokenIteratorTestFixture.php'));
  221. $mock = new DocBlox_Token_IteratorMock($tokens);
  222. // because we have switched the { and } in the stub method it should immediately find a closing brace and thus
  223. // return null,null
  224. $mock->seek(0);
  225. $mock->gotoNextByType(T_CLASS, 0);
  226. $this->assertEquals(array(null, null), $mock->getBrokenTokenIdsOfBracePair());
  227. }
  228. public function testGetTokenIdsOfBracePair()
  229. {
  230. $this->object->seek(0);
  231. $this->object->gotoNextByType(T_CLASS, 0);
  232. $result = $this->object->getTokenIdsOfBracePair();
  233. $this->assertInternalType('array', $result, 'Expected result to be an array');
  234. $this->assertArrayHasKey(0, $result, 'Expected result to have a start element');
  235. $this->assertArrayHasKey(1, $result, 'Expected result to have an end element');
  236. $this->assertEquals(10, $result[0], 'Expected the first brace to be at token id 10');
  237. $this->assertEquals(24, $result[1], 'Expected the closing brace to be at token id 24');
  238. }
  239. public function testGetTokenIdsOfParenthesisPair()
  240. {
  241. $this->object->seek(0);
  242. $this->object->gotoNextByType(T_FUNCTION, 0);
  243. $result = $this->object->getTokenIdsOfParenthesisPair();
  244. $this->assertInternalType('array', $result, 'Expected result to be an array');
  245. $this->assertArrayHasKey(0, $result, 'Expected result to have a start element');
  246. $this->assertArrayHasKey(1, $result, 'Expected result to have an end element');
  247. $this->assertEquals(17, $result[0], 'Expected the first brace to be at token id 17');
  248. $this->assertEquals(18, $result[1], 'Expected the closing brace to be at token id 18');
  249. }
  250. }