Files
predis/tests/Predis/Command/Utils/CommandUtilityTest.php
T
Vladyslav Vildanov f11e855047 Added experimental support for FT.HYBRID (#1607)
* Added basic structures and test coverage

* Added most of the test coverage

* Added more test coverage

* Codestyle fixes

* Fixed static analysis

* ANother static analysis fix

* Codestyle fixes

* Fixed version constraints

* Added missing annotations

* Use class context

* Added more test coverage

* Codestyle fixes

* Added experimental mentioning
2025-11-11 15:31:10 +02:00

93 lines
2.7 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2025 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Utils;
use Predis\Command\Redis\Utils\CommandUtility;
use PredisTestCase;
use UnexpectedValueException;
class CommandUtilityTest extends PredisTestCase
{
/**
* @dataProvider arrayProvider
* @param array $actual
* @param array $expected
* @param callable|null $callback
* @param bool $recursive
* @return void
*/
public function testArrayToDictionary(array $actual, array $expected, ?callable $callback, bool $recursive = true)
{
$this->assertSame($expected, CommandUtility::arrayToDictionary($actual, $callback, $recursive));
}
/**
* @return void
*/
public function testArrayToDictionaryThrowsExceptionOnOddNumberOfElements()
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Array must have an even number of arguments');
CommandUtility::arrayToDictionary(['key1', 'value1', 'key1']);
}
/**
* @return void
*/
public function testDictionaryToArray(): void
{
$dict = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
];
$this->assertSame(
['key1', 'value1', 'key2', 'value2', 'key3', 'value3'],
CommandUtility::dictionaryToArray($dict)
);
}
public function arrayProvider(): array
{
return [
'without nesting arrays' => [
['key1', 'value1', 'key2', 'value2', 'key3', 'value3'],
['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'],
null,
],
'with nesting arrays' => [
['key1', ['key2', ['key3', 'value3']]],
['key1' => ['key2' => ['key3' => 'value3']]],
null,
],
'with callback applied' => [
['key1', ['key2', ['key3', '0.1']]],
['key1' => ['key2' => ['key3' => 0.1]]],
function ($key, $value) {
return [$key, (float) $value];
},
],
'with non-recursive approach' => [
['key1', ['key2', ['key3', '0.1']]],
['key1' => ['key2', ['key3', '0.1']]],
function ($key, $value) {
return [$key, (float) $value];
},
false,
],
];
}
}