JoinConditionTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\DB\QueryBuilder\Partitioned;
  8. use OC\DB\ConnectionAdapter;
  9. use OC\DB\QueryBuilder\Partitioned\JoinCondition;
  10. use OC\DB\QueryBuilder\QueryBuilder;
  11. use OC\SystemConfig;
  12. use OCP\DB\QueryBuilder\IQueryBuilder;
  13. use OCP\IDBConnection;
  14. use Psr\Log\LoggerInterface;
  15. use Test\TestCase;
  16. class JoinConditionTest extends TestCase {
  17. protected function setUp(): void {
  18. parent::setUp();
  19. }
  20. public function platformProvider(): array {
  21. return [
  22. [IDBConnection::PLATFORM_SQLITE],
  23. [IDBConnection::PLATFORM_POSTGRES],
  24. [IDBConnection::PLATFORM_MYSQL],
  25. [IDBConnection::PLATFORM_ORACLE],
  26. ];
  27. }
  28. private function getBuilder(string $platform): IQueryBuilder {
  29. $connection = $this->createMock(ConnectionAdapter::class);
  30. $connection->method('getDatabaseProvider')->willReturn($platform);
  31. return new QueryBuilder(
  32. $connection,
  33. $this->createMock(SystemConfig::class),
  34. $this->createMock(LoggerInterface::class)
  35. );
  36. }
  37. /**
  38. * @dataProvider platformProvider
  39. */
  40. public function testParseCondition(string $platform): void {
  41. $query = $this->getBuilder($platform);
  42. $param1 = $query->createNamedParameter('files');
  43. $param2 = $query->createNamedParameter('test');
  44. $condition = $query->expr()->andX(
  45. $query->expr()->eq('tagmap.categoryid', 'tag.id'),
  46. $query->expr()->eq('tag.type', $param1),
  47. $query->expr()->eq('tag.uid', $param2)
  48. );
  49. $parsed = JoinCondition::parse($condition, 'vcategory', 'tag', 'tagmap');
  50. $this->assertEquals('tagmap.categoryid', $parsed->fromColumn);
  51. $this->assertEquals('tag.id', $parsed->toColumn);
  52. $this->assertEquals([], $parsed->fromConditions);
  53. $this->assertEquals([
  54. $query->expr()->eq('tag.type', $param1),
  55. $query->expr()->eq('tag.uid', $param2),
  56. ], $parsed->toConditions);
  57. }
  58. /**
  59. * @dataProvider platformProvider
  60. */
  61. public function testParseCastCondition(string $platform): void {
  62. $query = $this->getBuilder($platform);
  63. $condition = $query->expr()->eq($query->expr()->castColumn('m.objectid', IQueryBuilder::PARAM_INT), 'f.fileid');
  64. $parsed = JoinCondition::parse($condition, 'filecache', 'f', 'm');
  65. $this->assertEquals('m.objectid', $parsed->fromColumn);
  66. $this->assertEquals('f.fileid', $parsed->toColumn);
  67. $this->assertEquals([], $parsed->fromConditions);
  68. }
  69. }