ExpressionBuilderDBTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\DB\QueryBuilder;
  22. use OC\DB\QueryBuilder\Literal;
  23. use Test\TestCase;
  24. /**
  25. * @group DB
  26. */
  27. class ExpressionBuilderDBTest extends TestCase {
  28. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  29. protected $connection;
  30. protected function setUp() {
  31. parent::setUp();
  32. $this->connection = \OC::$server->getDatabaseConnection();
  33. }
  34. public function likeProvider() {
  35. $connection = \OC::$server->getDatabaseConnection();
  36. return [
  37. ['foo', 'bar', false],
  38. ['foo', 'foo', true],
  39. ['foo', 'f%', true],
  40. ['foo', '%o', true],
  41. ['foo', '%', true],
  42. ['foo', 'fo_', true],
  43. ['foo', 'foo_', false],
  44. ['foo', $connection->escapeLikeParameter('fo_'), false],
  45. ['foo', $connection->escapeLikeParameter('f%'), false],
  46. ];
  47. }
  48. /**
  49. * @dataProvider likeProvider
  50. *
  51. * @param string $param1
  52. * @param string $param2
  53. * @param boolean $match
  54. */
  55. public function testLike($param1, $param2, $match) {
  56. $query = $this->connection->getQueryBuilder();
  57. $query->select(new Literal('1'))
  58. ->from('users')
  59. ->where($query->expr()->like($query->createNamedParameter($param1), $query->createNamedParameter($param2)));
  60. $this->assertEquals($match, $query->execute()->fetchColumn());
  61. }
  62. public function ilikeProvider() {
  63. $connection = \OC::$server->getDatabaseConnection();
  64. return [
  65. ['foo', 'bar', false],
  66. ['foo', 'foo', true],
  67. ['foo', 'Foo', true],
  68. ['foo', 'f%', true],
  69. ['foo', '%o', true],
  70. ['foo', '%', true],
  71. ['foo', 'fo_', true],
  72. ['foo', 'foo_', false],
  73. ['foo', $connection->escapeLikeParameter('fo_'), false],
  74. ['foo', $connection->escapeLikeParameter('f%'), false],
  75. ];
  76. }
  77. /**
  78. * @dataProvider ilikeProvider
  79. *
  80. * @param string $param1
  81. * @param string $param2
  82. * @param boolean $match
  83. */
  84. public function testILike($param1, $param2, $match) {
  85. $query = $this->connection->getQueryBuilder();
  86. $query->select(new Literal('1'))
  87. ->from('users')
  88. ->where($query->expr()->iLike($query->createNamedParameter($param1), $query->createNamedParameter($param2)));
  89. $this->assertEquals($match, $query->execute()->fetchColumn());
  90. }
  91. }