|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Tables\Service\ColumnTypes; |
| 9 | + |
| 10 | +use OCA\Tables\Db\Column; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Psr\Log\LoggerInterface; |
| 13 | + |
| 14 | +class SelectionBusinessTest extends TestCase { |
| 15 | + |
| 16 | + private SelectionBusiness $selectionBusiness; |
| 17 | + private LoggerInterface $logger; |
| 18 | + private Column $column; |
| 19 | + |
| 20 | + public function setUp(): void { |
| 21 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 22 | + $this->selectionBusiness = new SelectionBusiness($this->logger); |
| 23 | + |
| 24 | + $this->column = $this->createMock(Column::class); |
| 25 | + $this->column->method('getSelectionOptionsArray') |
| 26 | + ->willReturn([ |
| 27 | + ['id' => 1, 'label' => 'Option 1'], |
| 28 | + ['id' => 2, 'label' => 'Option 2'], |
| 29 | + ['id' => 3, 'label' => 'Option 3'], |
| 30 | + ['id' => 4, 'label' => '1'], |
| 31 | + ]); |
| 32 | + } |
| 33 | + |
| 34 | + public function parseValueProvider(): array { |
| 35 | + return [ |
| 36 | + 'valid integer value' => [2, '2'], |
| 37 | + 'valid string value' => ['2', '2'], |
| 38 | + 'valid string value for numeric option' => ['4', '4'], |
| 39 | + 'invalid value' => [5, ''], |
| 40 | + 'null value' => [null, ''], |
| 41 | + 'empty string' => ['', ''], |
| 42 | + 'float value' => [1.5, ''], |
| 43 | + 'boolean value' => [true, ''], |
| 44 | + 'array value' => [[1], ''], |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider parseValueProvider |
| 50 | + */ |
| 51 | + public function testParseValue($value, string $expected): void { |
| 52 | + $result = $this->selectionBusiness->parseValue($value, $this->column); |
| 53 | + $this->assertEquals($expected, $result); |
| 54 | + } |
| 55 | + |
| 56 | + public function parseDisplayValueProvider(): array { |
| 57 | + return [ |
| 58 | + 'valid label' => ['Option 2', '2'], |
| 59 | + 'invalid label' => ['Invalid Option', ''], |
| 60 | + 'valid label for numeric option' => ['1', '4'], |
| 61 | + 'null value' => [null, ''], |
| 62 | + 'empty string' => ['', ''], |
| 63 | + 'boolean value' => [true, ''], |
| 64 | + 'array value' => [[1], ''], |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @dataProvider parseDisplayValueProvider |
| 70 | + */ |
| 71 | + public function testParseDisplayValue($value, string $expected): void { |
| 72 | + $result = $this->selectionBusiness->parseDisplayValue($value, $this->column); |
| 73 | + $this->assertEquals($expected, $result); |
| 74 | + } |
| 75 | + |
| 76 | + public function canBeParsedProvider(): array { |
| 77 | + return [ |
| 78 | + 'valid integer 1' => [1, true], |
| 79 | + 'valid string 4' => ['4', true], |
| 80 | + 'invalid integer' => [5, false], |
| 81 | + 'invalid integer 0' => [0, false], |
| 82 | + 'null value' => [null, true], |
| 83 | + 'empty string' => ['', false], |
| 84 | + 'float value' => [1.5, false], |
| 85 | + 'boolean value' => [true, false], |
| 86 | + 'array value' => [[1], false], |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @dataProvider canBeParsedProvider |
| 92 | + */ |
| 93 | + public function testCanBeParsed($value, bool $expected): void { |
| 94 | + $result = $this->selectionBusiness->canBeParsed($value, $this->column); |
| 95 | + $this->assertEquals($expected, $result); |
| 96 | + } |
| 97 | + |
| 98 | + public function canBeParsedDisplayValueProvider(): array { |
| 99 | + return [ |
| 100 | + 'valid label' => ['Option 2', true], |
| 101 | + 'invalid label' => ['Invalid Option', false], |
| 102 | + 'valid label for numeric option' => ['1', true], |
| 103 | + 'null value' => [null, true], |
| 104 | + 'empty string' => ['', false], |
| 105 | + 'boolean value' => [true, false], |
| 106 | + 'array value' => [[1], false], |
| 107 | + ]; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @dataProvider canBeParsedDisplayValueProvider |
| 112 | + */ |
| 113 | + public function testCanBeParsedDisplayValue($value, bool $expected): void { |
| 114 | + $result = $this->selectionBusiness->canBeParsedDisplayValue($value, $this->column); |
| 115 | + $this->assertEquals($expected, $result); |
| 116 | + } |
| 117 | + |
| 118 | + public function withoutColumnProvider(): array { |
| 119 | + return [ |
| 120 | + 'parseValue' => ['parseValue', 1, ''], |
| 121 | + 'parseDisplayValue' => ['parseDisplayValue', 'Option 1', ''], |
| 122 | + 'canBeParsed' => ['canBeParsed', 1, false], |
| 123 | + 'canBeParsedDisplayValue' => ['canBeParsedDisplayValue', 'Option 1', false], |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @dataProvider withoutColumnProvider |
| 129 | + */ |
| 130 | + public function testMethodsWithoutColumn(string $method, $value, $expected): void { |
| 131 | + $result = $this->selectionBusiness->$method($value, null); |
| 132 | + $this->assertEquals($expected, $result); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments