PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/tests/Doctrine/Tests/ORM/Query/LexerTest.php

https://bitbucket.org/cryofrost/portal
PHP | 234 lines | 206 code | 27 blank | 1 comment | 0 complexity | 6c3461078239509b0e0375343fb833ed MD5 | raw file
Possible License(s): Apache-2.0, JSON, LGPL-2.1, LGPL-2.0, LGPL-3.0, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. namespace Doctrine\Tests\ORM\Query;
  3. use Doctrine\ORM\Query\Lexer;
  4. require_once __DIR__ . '/../../TestInit.php';
  5. class LexerTest extends \Doctrine\Tests\OrmTestCase
  6. {
  7. //private $_lexer;
  8. protected function setUp() {
  9. }
  10. public function testScannerRecognizesIdentifierWithLengthOfOneCharacter()
  11. {
  12. $lexer = new Lexer('u');
  13. $lexer->moveNext();
  14. $token = $lexer->lookahead;
  15. $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
  16. $this->assertEquals('u', $token['value']);
  17. }
  18. public function testScannerRecognizesIdentifierConsistingOfLetters()
  19. {
  20. $lexer = new Lexer('someIdentifier');
  21. $lexer->moveNext();
  22. $token = $lexer->lookahead;
  23. $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
  24. $this->assertEquals('someIdentifier', $token['value']);
  25. }
  26. public function testScannerRecognizesIdentifierIncludingDigits()
  27. {
  28. $lexer = new Lexer('s0m31d3nt1f13r');
  29. $lexer->moveNext();
  30. $token = $lexer->lookahead;
  31. $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
  32. $this->assertEquals('s0m31d3nt1f13r', $token['value']);
  33. }
  34. public function testScannerRecognizesIdentifierIncludingUnderscore()
  35. {
  36. $lexer = new Lexer('some_identifier');
  37. $lexer->moveNext();
  38. $token = $lexer->lookahead;
  39. $this->assertEquals(Lexer::T_IDENTIFIER, $token['type']);
  40. $this->assertEquals('some_identifier', $token['value']);
  41. }
  42. public function testScannerRecognizesDecimalInteger()
  43. {
  44. $lexer = new Lexer('1234');
  45. $lexer->moveNext();
  46. $token = $lexer->lookahead;
  47. $this->assertEquals(Lexer::T_INTEGER, $token['type']);
  48. $this->assertEquals(1234, $token['value']);
  49. }
  50. public function testScannerRecognizesFloat()
  51. {
  52. $lexer = new Lexer('1.234');
  53. $lexer->moveNext();
  54. $token = $lexer->lookahead;
  55. $this->assertEquals(Lexer::T_FLOAT, $token['type']);
  56. $this->assertEquals(1.234, $token['value']);
  57. }
  58. public function testScannerRecognizesFloatWithExponent()
  59. {
  60. $lexer = new Lexer('1.2e3');
  61. $lexer->moveNext();
  62. $token = $lexer->lookahead;
  63. $this->assertEquals(Lexer::T_FLOAT, $token['type']);
  64. $this->assertEquals(1.2e3, $token['value']);
  65. }
  66. public function testScannerRecognizesFloatWithExponent2()
  67. {
  68. $lexer = new Lexer('0.2e3');
  69. $lexer->moveNext();
  70. $token = $lexer->lookahead;
  71. $this->assertEquals(Lexer::T_FLOAT, $token['type']);
  72. $this->assertEquals(.2e3, $token['value']);
  73. }
  74. public function testScannerRecognizesFloatWithNegativeExponent()
  75. {
  76. $lexer = new Lexer('7E-10');
  77. $lexer->moveNext();
  78. $token = $lexer->lookahead;
  79. $this->assertEquals(Lexer::T_FLOAT, $token['type']);
  80. $this->assertEquals(7E-10, $token['value']);
  81. }
  82. public function testScannerRecognizesFloatBig()
  83. {
  84. $lexer = new Lexer('123456789.01');
  85. $lexer->moveNext();
  86. $token = $lexer->lookahead;
  87. $this->assertEquals(Lexer::T_FLOAT, $token['type']);
  88. $this->assertEquals(1.2345678901e8, $token['value']);
  89. }
  90. public function testScannerRecognizesFloatContainingWhitespace()
  91. {
  92. $lexer = new Lexer('- 1.234e2');
  93. $lexer->moveNext();
  94. $token = $lexer->lookahead;
  95. $this->assertEquals(Lexer::T_MINUS, $token['type']);
  96. $this->assertEquals('-', $token['value']);
  97. $lexer->moveNext();
  98. $token = $lexer->lookahead;
  99. $this->assertEquals(Lexer::T_FLOAT, $token['type']);
  100. $this->assertNotEquals(-1.234e2, $token['value']);
  101. $this->assertEquals(1.234e2, $token['value']);
  102. }
  103. public function testScannerRecognizesStringContainingWhitespace()
  104. {
  105. $lexer = new Lexer("'This is a string.'");
  106. $lexer->moveNext();
  107. $token = $lexer->lookahead;
  108. $this->assertEquals(Lexer::T_STRING, $token['type']);
  109. $this->assertEquals("This is a string.", $token['value']);
  110. }
  111. public function testScannerRecognizesStringContainingSingleQuotes()
  112. {
  113. $lexer = new Lexer("'abc''defg'''");
  114. $lexer->moveNext();
  115. $token = $lexer->lookahead;
  116. $this->assertEquals(Lexer::T_STRING, $token['type']);
  117. $this->assertEquals("abc'defg'", $token['value']);
  118. }
  119. public function testScannerRecognizesInputParameter()
  120. {
  121. $lexer = new Lexer('?1');
  122. $lexer->moveNext();
  123. $token = $lexer->lookahead;
  124. $this->assertEquals(Lexer::T_INPUT_PARAMETER, $token['type']);
  125. $this->assertEquals('?1', $token['value']);
  126. }
  127. public function testScannerRecognizesNamedInputParameter()
  128. {
  129. $lexer = new Lexer(':name');
  130. $lexer->moveNext();
  131. $token = $lexer->lookahead;
  132. $this->assertEquals(Lexer::T_INPUT_PARAMETER, $token['type']);
  133. $this->assertEquals(':name', $token['value']);
  134. }
  135. public function testScannerTokenizesASimpleQueryCorrectly()
  136. {
  137. $dql = "SELECT u FROM My\Namespace\User u WHERE u.name = 'Jack O''Neil'";
  138. $lexer = new Lexer($dql);
  139. $tokens = array(
  140. array(
  141. 'value' => 'SELECT',
  142. 'type' => Lexer::T_SELECT,
  143. 'position' => 0
  144. ),
  145. array(
  146. 'value' => 'u',
  147. 'type' => Lexer::T_IDENTIFIER,
  148. 'position' => 7
  149. ),
  150. array(
  151. 'value' => 'FROM',
  152. 'type' => Lexer::T_FROM,
  153. 'position' => 9
  154. ),
  155. array(
  156. 'value' => 'My\Namespace\User',
  157. 'type' => Lexer::T_IDENTIFIER,
  158. 'position' => 14
  159. ),
  160. array(
  161. 'value' => 'u',
  162. 'type' => Lexer::T_IDENTIFIER,
  163. 'position' => 32
  164. ),
  165. array(
  166. 'value' => 'WHERE',
  167. 'type' => Lexer::T_WHERE,
  168. 'position' => 34
  169. ),
  170. array(
  171. 'value' => 'u',
  172. 'type' => Lexer::T_IDENTIFIER,
  173. 'position' => 40
  174. ),
  175. array(
  176. 'value' => '.',
  177. 'type' => Lexer::T_DOT,
  178. 'position' => 41
  179. ),
  180. array(
  181. 'value' => 'name',
  182. 'type' => Lexer::T_IDENTIFIER,
  183. 'position' => 42
  184. ),
  185. array(
  186. 'value' => '=',
  187. 'type' => Lexer::T_EQUALS,
  188. 'position' => 47
  189. ),
  190. array(
  191. 'value' => "Jack O'Neil",
  192. 'type' => Lexer::T_STRING,
  193. 'position' => 49
  194. )
  195. );
  196. foreach ($tokens as $expected) {
  197. $lexer->moveNext();
  198. $actual = $lexer->lookahead;
  199. $this->assertEquals($expected['value'], $actual['value']);
  200. $this->assertEquals($expected['type'], $actual['type']);
  201. $this->assertEquals($expected['position'], $actual['position']);
  202. }
  203. $this->assertFalse($lexer->moveNext());
  204. }
  205. }