FunctionBuilderTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 Test\TestCase;
  23. /**
  24. * Class FunctionBuilderTest
  25. *
  26. * @group DB
  27. *
  28. * @package Test\DB\QueryBuilder
  29. */
  30. class FunctionBuilderTest extends TestCase {
  31. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  32. protected $connection;
  33. protected function setUp() {
  34. parent::setUp();
  35. $this->connection = \OC::$server->getDatabaseConnection();
  36. }
  37. public function testConcat() {
  38. $query = $this->connection->getQueryBuilder();
  39. $query->select($query->func()->concat($query->createNamedParameter('foo'), new Literal("'bar'")));
  40. $query->from('appconfig')
  41. ->setMaxResults(1);
  42. $this->assertEquals('foobar', $query->execute()->fetchColumn());
  43. }
  44. public function testMd5() {
  45. $query = $this->connection->getQueryBuilder();
  46. $query->select($query->func()->md5($query->createNamedParameter('foobar')));
  47. $query->from('appconfig')
  48. ->setMaxResults(1);
  49. $this->assertEquals(md5('foobar'), $query->execute()->fetchColumn());
  50. }
  51. public function testSubstring() {
  52. $query = $this->connection->getQueryBuilder();
  53. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2), $query->createNamedParameter(2)));
  54. $query->from('appconfig')
  55. ->setMaxResults(1);
  56. $this->assertEquals('oo', $query->execute()->fetchColumn());
  57. }
  58. public function testSubstringNoLength() {
  59. $query = $this->connection->getQueryBuilder();
  60. $query->select($query->func()->substring($query->createNamedParameter('foobar'), new Literal(2)));
  61. $query->from('appconfig')
  62. ->setMaxResults(1);
  63. $this->assertEquals('oobar', $query->execute()->fetchColumn());
  64. }
  65. }