aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Core/Command/Config/System/CastHelperTest.php
blob: 924887daaf7f94a4f22903237ce8232a2824c5af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace Core\Command\Config\System;

use OC\Core\Command\Config\System\CastHelper;
use Test\TestCase;

class CastHelperTest extends TestCase {
	private CastHelper $castHelper;

	protected function setUp(): void {
		parent::setUp();
		$this->castHelper = new CastHelper();
	}

	public static function castValueProvider(): array {
		return [
			[null, 'string', ['value' => '', 'readable-value' => 'empty string']],

			['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']],

			['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']],
			['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']],

			['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']],
			['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']],

			['', 'null', ['value' => null, 'readable-value' => 'null']],

			['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
			['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('castValueProvider')]
	public function testCastValue($value, $type, $expectedValue): void {
		$this->assertSame(
			$expectedValue,
			$this->castHelper->castValue($value, $type)
		);
	}

	public static function castValueInvalidProvider(): array {
		return [
			['123', 'foobar'],

			[null, 'integer'],
			['abc', 'integer'],
			['76ggg', 'double'],
			['true', 'float'],
			['foobar', 'boolean'],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('castValueInvalidProvider')]
	public function testCastValueInvalid($value, $type): void {
		$this->expectException(\InvalidArgumentException::class);

		$this->castHelper->castValue($value, $type);
	}
}