FunctionBuilderTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @author Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. namespace Test\DB\QueryBuilder;
  21. use OC\DB\QueryBuilder\Literal;
  22. use OCP\DB\QueryBuilder\IQueryBuilder;
  23. use Test\TestCase;
  24. /**
  25. * Class FunctionBuilderTest
  26. *
  27. * @group DB
  28. *
  29. * @package Test\DB\QueryBuilder
  30. */
  31. class FunctionBuilderTest extends TestCase {
  32. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  33. protected $connection;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->connection = \OC::$server->getDatabaseConnection();
  37. }
  38. public function testConcat() {
  39. $query = $this->connection->getQueryBuilder();
  40. $query->select($query->func()->concat($query->createNamedParameter('foo'), new Literal("'bar'")));
  41. $query->from('appconfig')
  42. ->setMaxResults(1);
  43. $this->assertEquals('foobar', $query->execute()->fetchColumn());
  44. }
  45. public function testMd5() {
  46. $query = $this->connection->getQueryBuilder();
  47. $query->select($query->func()->md5($query->createNamedParameter('foobar')));
  48. $query->from('appconfig')
  49. ->setMaxResults(1);
  50. $this->assertEquals(md5('foobar'), $query->execute()->fetchColumn());
  51. }
  52. public function testSubstring() {
  53. $query = $this->connection->getQueryBuilder();
  54. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2), $query->createNamedParameter(2)));
  55. $query->from('appconfig')
  56. ->setMaxResults(1);
  57. $this->assertEquals('oo', $query->execute()->fetchColumn());
  58. }
  59. public function testSubstringNoLength() {
  60. $query = $this->connection->getQueryBuilder();
  61. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2)));
  62. $query->from('appconfig')
  63. ->setMaxResults(1);
  64. $this->assertEquals('oobar', $query->execute()->fetchColumn());
  65. }
  66. public function testLower() {
  67. $query = $this->connection->getQueryBuilder();
  68. $query->select($query->func()->lower($query->createNamedParameter('FooBar')));
  69. $query->from('appconfig')
  70. ->setMaxResults(1);
  71. $this->assertEquals('foobar', $query->execute()->fetchColumn());
  72. }
  73. public function testAdd() {
  74. $query = $this->connection->getQueryBuilder();
  75. $query->select($query->func()->add($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  76. $query->from('appconfig')
  77. ->setMaxResults(1);
  78. $this->assertEquals(3, $query->execute()->fetchColumn());
  79. }
  80. public function testSubtract() {
  81. $query = $this->connection->getQueryBuilder();
  82. $query->select($query->func()->subtract($query->createNamedParameter(2, IQueryBuilder::PARAM_INT), new Literal(1)));
  83. $query->from('appconfig')
  84. ->setMaxResults(1);
  85. $this->assertEquals(1, $query->execute()->fetchColumn());
  86. }
  87. }