@@ -1435,4 +1435,82 @@ private static function getConnectionToFullDB(bool $emulate_prepares = true, boo
14351435
14361436 return $ pdo ;
14371437 }
1438+
1439+ /**
1440+ * @dataProvider leastArgumentsProvider
1441+ * @param array $args
1442+ * @param string|int|float|null $expected_value
1443+ */
1444+ public function testLeast ($ args , $ expected_value ): void
1445+ {
1446+ // Get a PDO instance for MySQL.
1447+ $ pdo = self ::getPdo ('mysql:host=localhost;dbname=testdb ' );
1448+ $ pdo ->setAttribute (\PDO ::ATTR_EMULATE_PREPARES , false );
1449+
1450+ $ args_str = implode (', ' , array_map (fn ($ arg ) => is_null ($ arg ) ? 'null ' : strval ($ arg ), $ args ));
1451+ $ query = $ pdo ->prepare (sprintf ('SELECT LEAST(%s) as result ' , $ args_str ),);
1452+ $ query ->execute ();
1453+
1454+ $ result = $ query ->fetch (\PDO ::FETCH_ASSOC );
1455+ $ this ->assertEquals (
1456+ ['result ' => $ expected_value ], $ result ,
1457+ sprintf ('Actual result is does not match the expected. Actual is: %s ' , print_r ($ result , true )));
1458+ }
1459+
1460+ public function leastArgumentsProvider (): iterable
1461+ {
1462+ yield 'Should properly work with at least one \'null \' argument ' => [
1463+ 'args ' => [1 ,2 ,null ,42 ],
1464+ 'expected_value ' => null
1465+ ];
1466+ yield 'Should properly get the least integer argument ' => [
1467+ 'args ' => [-1 , 1 ,2 ,42 ],
1468+ 'expected_value ' => '-1 '
1469+ ];
1470+ yield 'Should properly work with decimal argument ' => [
1471+ 'args ' => [0.00 , 0.1 ,2 ,42 , -0.001 ],
1472+ 'expected_value ' => '-0.001 '
1473+ ];
1474+ yield 'Should return proper precision if any argument is a float ' => [
1475+ 'args ' => [1 , 2.0001 , 42 , 1.001 ],
1476+ 'expected_value ' => '1.0000 '
1477+ ];
1478+ yield 'Should properly work with at least one string argument ' => [
1479+ 'args ' => [1 ,2 , "'null' " , "'nulla' " ],
1480+ 'expected_value ' => '1 '
1481+ ];
1482+ yield 'Should properly work all string args ' => [
1483+ 'args ' => ["'A' " ,"'B' " ,"'C' " ],
1484+ 'expected_value ' => 'A '
1485+ ];
1486+ yield 'Should lexicographically compare #1 ' => [
1487+ 'args ' => ["'AA' " ,"'AB' " ,"'AC' " ],
1488+ 'expected_value ' => 'AA '
1489+ ];
1490+ yield 'Should lexicographically compare #2 ' => [
1491+ 'args ' => ["'AA' " ,"'AB' " ,"'AC' " , 1 ],
1492+ 'expected_value ' => '1 '
1493+ ];
1494+ }
1495+
1496+ /** @dataProvider leastWithExceptionProvider */
1497+ public function testLeastThrowsExceptionWithWrongArgumentCount (array $ args ): void
1498+ {
1499+ $ this ->expectException (\UnexpectedValueException::class);
1500+ $ this ->expectExceptionMessage ('Incorrect parameter count in the call to native function \'LEAST \'' );
1501+
1502+ $ pdo = self ::getPdo ('mysql:host=localhost;dbname=testdb ' );
1503+ $ pdo ->setAttribute (\PDO ::ATTR_EMULATE_PREPARES , false );
1504+
1505+ $ args_str = implode (', ' , array_map (fn ($ arg ) => strval ($ arg ), $ args ));
1506+ $ query = $ pdo ->prepare (sprintf ('SELECT LEAST(%s) ' , $ args_str ),);
1507+
1508+ $ query ->execute ();
1509+ }
1510+
1511+ public function leastWithExceptionProvider (): iterable
1512+ {
1513+ yield ['Should fail with single argument ' => [1 ]];
1514+ yield ['Should fail without any arguments ' => []];
1515+ }
14381516}
0 commit comments