|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OCA\ProfileFields\Tests\Unit\Db; |
| 11 | + |
| 12 | +use OCA\ProfileFields\Db\FieldValueMapper; |
| 13 | +use OCP\DB\IResult; |
| 14 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 15 | +use OCP\IDBConnection; |
| 16 | +use PDO; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class FieldValueMapperTest extends TestCase { |
| 21 | + private IDBConnection&MockObject $db; |
| 22 | + private IQueryBuilder&MockObject $queryBuilder; |
| 23 | + private IResult&MockObject $result; |
| 24 | + private FieldValueMapper $mapper; |
| 25 | + |
| 26 | + protected function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + $this->db = $this->createMock(IDBConnection::class); |
| 29 | + $this->queryBuilder = $this->createMock(IQueryBuilder::class); |
| 30 | + $this->result = $this->createMock(IResult::class); |
| 31 | + $this->mapper = new FieldValueMapper($this->db); |
| 32 | + } |
| 33 | + |
| 34 | + public function testFindDistinctUserUidsUsesFetchAllColumnModeForCompatibility(): void { |
| 35 | + $this->db->expects($this->once()) |
| 36 | + ->method('getQueryBuilder') |
| 37 | + ->willReturn($this->queryBuilder); |
| 38 | + |
| 39 | + $this->queryBuilder->expects($this->once()) |
| 40 | + ->method('selectDistinct') |
| 41 | + ->with('user_uid') |
| 42 | + ->willReturnSelf(); |
| 43 | + $this->queryBuilder->expects($this->once()) |
| 44 | + ->method('from') |
| 45 | + ->with('profile_fields_values') |
| 46 | + ->willReturnSelf(); |
| 47 | + $this->queryBuilder->expects($this->once()) |
| 48 | + ->method('orderBy') |
| 49 | + ->with('user_uid', 'ASC') |
| 50 | + ->willReturnSelf(); |
| 51 | + $this->queryBuilder->expects($this->once()) |
| 52 | + ->method('executeQuery') |
| 53 | + ->willReturn($this->result); |
| 54 | + |
| 55 | + $this->result->expects($this->once()) |
| 56 | + ->method('fetchAll') |
| 57 | + ->with(PDO::FETCH_COLUMN) |
| 58 | + ->willReturn(['alice', 7, 'bob']); |
| 59 | + $this->result->expects($this->never()) |
| 60 | + ->method('fetchFirstColumn'); |
| 61 | + $this->result->expects($this->once()) |
| 62 | + ->method('closeCursor') |
| 63 | + ->willReturn(true); |
| 64 | + |
| 65 | + $this->assertSame(['alice', '7', 'bob'], $this->mapper->findDistinctUserUids()); |
| 66 | + } |
| 67 | +} |
0 commit comments