PageRenderTime 61ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Query/Mysql/TrigTest.php

https://github.com/beberlei/DoctrineExtensions
PHP | 204 lines | 156 code | 46 blank | 2 comment | 8 complexity | 3dddee6c490c245e93320f5d0b28e6cf MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace DoctrineExtensions\Tests\Query\Mysql;
  3. class TrigTest extends \DoctrineExtensions\Tests\Query\MysqlTestCase
  4. {
  5. public function setUp(): void
  6. {
  7. parent::setUp();
  8. $this->entity = 'DoctrineExtensions\Tests\Entities\BlogPost';
  9. }
  10. public function testSin()
  11. {
  12. $this->_assertFirstQuery('SIN');
  13. $this->_assertSecondQuery('SIN');
  14. $dql = "SELECT SIN(RADIANS(p.latitude)) FROM {$this->entity} p";
  15. $q = $this->entityManager->createQuery($dql);
  16. $sql = 'SELECT SIN(RADIANS(b0_.latitude)) AS sclr_0 FROM BlogPost b0_';
  17. $this->assertEquals($sql, $q->getSql());
  18. $dql = "SELECT SIN(p.latitude * p.longitude) FROM {$this->entity} p";
  19. $q = $this->entityManager->createQuery($dql);
  20. $sql = 'SELECT SIN(b0_.latitude * b0_.longitude) AS sclr_0 FROM BlogPost b0_';
  21. $this->assertEquals($sql, $q->getSql());
  22. $dql = "SELECT SIN(RADIANS(p.latitude) * RADIANS(p.longitude)) FROM {$this->entity} p";
  23. $q = $this->entityManager->createQuery($dql);
  24. $sql = 'SELECT SIN(RADIANS(b0_.latitude) * RADIANS(b0_.longitude)) AS sclr_0 FROM BlogPost b0_';
  25. $this->assertEquals($sql, $q->getSql());
  26. if (\Doctrine\ORM\Version::compare('2.4.0') <= 0) {
  27. $dql = "SELECT p FROM {$this->entity} p WHERE p.longitude = SIN(RADIANS(p.latitude)) * RADIANS(p.longitude)";
  28. } else {
  29. $dql = "SELECT p FROM {$this->entity} p WHERE p.longitude = (SIN(RADIANS(p.latitude)) * RADIANS(p.longitude))";
  30. }
  31. $q = $this->entityManager->createQuery($dql);
  32. $sql = 'SELECT b0_.id AS id_0, b0_.created AS created_1, b0_.longitude AS longitude_2, b0_.latitude AS latitude_3 FROM BlogPost b0_ WHERE b0_.longitude = SIN(RADIANS(b0_.latitude)) * RADIANS(b0_.longitude)';
  33. $this->assertEquals($sql, $q->getSql());
  34. $dql = "SELECT p FROM {$this->entity} p WHERE SIN(RADIANS(p.latitude)) * SIN(RADIANS(p.longitude)) = 1";
  35. $q = $this->entityManager->createQuery($dql);
  36. $sql = 'SELECT b0_.id AS id_0, b0_.created AS created_1, b0_.longitude AS longitude_2, b0_.latitude AS latitude_3 FROM BlogPost b0_ WHERE SIN(RADIANS(b0_.latitude)) * SIN(RADIANS(b0_.longitude)) = 1';
  37. $this->assertEquals($sql, $q->getSql());
  38. if (\Doctrine\ORM\Version::compare('2.4.0') <= 0) {
  39. $dql = "SELECT SIN(RADIANS(p.latitude)) * SIN(RADIANS(p.longitude)) FROM {$this->entity} p ";
  40. } else {
  41. $dql = "SELECT (SIN(RADIANS(p.latitude)) * SIN(RADIANS(p.longitude))) FROM {$this->entity} p ";
  42. }
  43. $q = $this->entityManager->createQuery($dql);
  44. $sql = 'SELECT SIN(RADIANS(b0_.latitude)) * SIN(RADIANS(b0_.longitude)) AS sclr_0 FROM BlogPost b0_';
  45. $this->assertEquals($sql, $q->getSql());
  46. }
  47. public function testAsin()
  48. {
  49. $this->_assertFirstQuery('ASIN');
  50. $this->_assertSecondQuery('ASIN');
  51. }
  52. public function testAcos()
  53. {
  54. $this->_assertFirstQuery('ACOS');
  55. $this->_assertSecondQuery('ACOS');
  56. if (\Doctrine\ORM\Version::compare('2.4.0') <= 0) {
  57. $dql = "SELECT ACOS(SIN(RADIANS(p.latitude)) + SIN(RADIANS(p.longitude))) * 1 FROM {$this->entity} p";
  58. } else {
  59. $dql = "SELECT (ACOS(SIN(RADIANS(p.latitude)) + SIN(RADIANS(p.longitude))) * 1) FROM {$this->entity} p";
  60. }
  61. $q = $this->entityManager->createQuery($dql);
  62. $sql = 'SELECT ACOS(SIN(RADIANS(b0_.latitude)) + SIN(RADIANS(b0_.longitude))) * 1 AS sclr_0 FROM BlogPost b0_';
  63. $this->assertEquals($sql, $q->getSql());
  64. }
  65. public function testCos()
  66. {
  67. $this->_assertFirstQuery('COS');
  68. $this->_assertSecondQuery('COS');
  69. }
  70. public function testCot()
  71. {
  72. $this->_assertFirstQuery('COT');
  73. $this->_assertSecondQuery('COT');
  74. }
  75. public function testDegrees()
  76. {
  77. $this->_assertFirstQuery('DEGREES');
  78. $this->_assertSecondQuery('DEGREES');
  79. }
  80. public function testRadians()
  81. {
  82. $this->_assertFirstQuery('RADIANS');
  83. $this->_assertSecondQuery('RADIANS');
  84. }
  85. public function testTan()
  86. {
  87. $this->_assertFirstQuery('TAN');
  88. $this->_assertSecondQuery('TAN');
  89. }
  90. public function testAtan()
  91. {
  92. // test with 1 argument
  93. $this->_assertFirstQuery('ATAN');
  94. $this->_assertSecondQuery('ATAN');
  95. // test with 2 arguments
  96. $dql = "SELECT ATAN(p.latitude, p.longitude) FROM {$this->entity} p ";
  97. $q = $this->entityManager->createQuery($dql);
  98. $sql = 'SELECT ATAN(b0_.latitude, b0_.longitude) AS sclr_0 FROM BlogPost b0_';
  99. $this->assertEquals($sql, $q->getSql());
  100. }
  101. public function testAtan2()
  102. {
  103. $dql = "SELECT ATAN2(p.latitude, p.longitude) FROM {$this->entity} p";
  104. $q = $this->entityManager->createQuery($dql);
  105. $sql = 'SELECT ATAN2(b0_.latitude, b0_.longitude) AS sclr_0 FROM BlogPost b0_';
  106. $this->assertEquals($sql, $q->getSql());
  107. }
  108. public function testCosineLaw()
  109. {
  110. $lat = 0.0;
  111. $lng = 0.0;
  112. $radiusOfEarth = 6371;
  113. $cosineLaw = 'ACOS(SIN(' . deg2rad($lat) . ') * SIN(RADIANS(p.latitude)) '
  114. . '+ COS(' . deg2rad($lat) . ') * COS(RADIANS(p.latitude)) '
  115. . '* COS(RADIANS(p.longitude) - ' . deg2rad($lng) . ')'
  116. . ') * ' . $radiusOfEarth;
  117. if (\Doctrine\ORM\Version::compare('2.4.0') <= 0) {
  118. $dql = 'SELECT ' . $cosineLaw . " FROM {$this->entity} p";
  119. } else {
  120. $dql = 'SELECT (' . $cosineLaw . ") FROM {$this->entity} p";
  121. }
  122. $q = $this->entityManager->createQuery($dql);
  123. $sql = 'SELECT ACOS(SIN(0) * SIN(RADIANS(b0_.latitude)) + COS(0) * COS(RADIANS(b0_.latitude)) * COS(RADIANS(b0_.longitude) - 0)) * 6371 AS sclr_0 FROM BlogPost b0_';
  124. $this->assertEquals($sql, $q->getSql());
  125. }
  126. protected function _assertFirstQuery($func)
  127. {
  128. $q = $this->_getFirstDqlQuery($func);
  129. $sql = $this->_getFirstSqlQuery($func);
  130. $this->assertEquals($sql, $q->getSql());
  131. }
  132. protected function _assertSecondQuery($func)
  133. {
  134. $q = $this->_getSecondDqlQuery($func);
  135. $sql = $this->_getSecondSqlQuery($func);
  136. $this->assertEquals($sql, $q->getSql());
  137. }
  138. protected function _getFirstDqlQuery($func)
  139. {
  140. $dql = "SELECT p FROM {$this->entity} p WHERE " . $func . '(p.latitude) = 1';
  141. return $this->entityManager->createQuery($dql);
  142. }
  143. protected function _getFirstSqlQuery($func)
  144. {
  145. return 'SELECT b0_.id AS id_0, b0_.created AS created_1, '
  146. . 'b0_.longitude AS longitude_2, b0_.latitude AS latitude_3 '
  147. . 'FROM BlogPost b0_ WHERE ' . $func . '(b0_.latitude) = 1';
  148. }
  149. protected function _getSecondDqlQuery($func)
  150. {
  151. $dql = 'SELECT ' . $func . "(p.latitude) FROM {$this->entity} p";
  152. return $this->entityManager->createQuery($dql);
  153. }
  154. protected function _getSecondSqlQuery($func)
  155. {
  156. return 'SELECT ' . $func . '(b0_.latitude) AS sclr_0 FROM BlogPost b0_';
  157. }
  158. }