FunctionsTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Core;
  17. use Cake\Core\Configure;
  18. use Cake\Http\Response;
  19. use Cake\ORM\Entity;
  20. use Cake\TestSuite\TestCase;
  21. use PHPUnit\Framework\Attributes\DataProvider;
  22. use stdClass;
  23. use function Cake\Core\deprecationWarning;
  24. use function Cake\Core\env;
  25. use function Cake\Core\h;
  26. use function Cake\Core\namespaceSplit;
  27. use function Cake\Core\pathCombine;
  28. use function Cake\Core\pluginSplit;
  29. use function Cake\Core\toBool;
  30. use function Cake\Core\toFloat;
  31. use function Cake\Core\toInt;
  32. use function Cake\Core\toString;
  33. use function Cake\Core\triggerWarning;
  34. /**
  35. * Test cases for functions in Core\functions.php
  36. */
  37. class FunctionsTest extends TestCase
  38. {
  39. public function testPathCombine(): void
  40. {
  41. $this->assertSame('', pathCombine([]));
  42. $this->assertSame('', pathCombine(['']));
  43. $this->assertSame('', pathCombine(['', '']));
  44. $this->assertSame('/', pathCombine(['/', '/']));
  45. $this->assertSame('path/to/file', pathCombine(['path', 'to', 'file']));
  46. $this->assertSame('path/to/file', pathCombine(['path/', 'to', 'file']));
  47. $this->assertSame('path/to/file', pathCombine(['path', 'to/', 'file']));
  48. $this->assertSame('path/to/file', pathCombine(['path/', 'to/', 'file']));
  49. $this->assertSame('path/to/file', pathCombine(['path/', '/to/', 'file']));
  50. $this->assertSame('/path/to/file', pathCombine(['/', 'path', 'to', 'file']));
  51. $this->assertSame('/path/to/file', pathCombine(['/', '/path', 'to', 'file']));
  52. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file/']));
  53. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file', '/']));
  54. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file/', '/']));
  55. // Test adding trailing slash
  56. $this->assertSame('/', pathCombine([], trailing: true));
  57. $this->assertSame('/', pathCombine([''], trailing: true));
  58. $this->assertSame('/', pathCombine(['/'], trailing: true));
  59. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file/'], trailing: true));
  60. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file/', '/'], trailing: true));
  61. // Test removing trailing slash
  62. $this->assertSame('', pathCombine([''], trailing: false));
  63. $this->assertSame('', pathCombine(['/'], trailing: false));
  64. $this->assertSame('/path/to/file', pathCombine(['/path', 'to', 'file/'], trailing: false));
  65. $this->assertSame('/path/to/file', pathCombine(['/path', 'to', 'file/', '/'], trailing: false));
  66. // Test Windows-style backslashes
  67. $this->assertSame('/path/to\\file', pathCombine(['/', '\\path', 'to', '\\file']));
  68. $this->assertSame('/path\\to\\file/', pathCombine(['/', 'path', '\\to\\', 'file'], trailing: true));
  69. $this->assertSame('/path\\to\\file\\', pathCombine(['/', 'path', '\\to\\', 'file', '\\'], trailing: true));
  70. $this->assertSame('/path\\to\\file', pathCombine(['/', 'path', '\\to\\', 'file'], trailing: false));
  71. $this->assertSame('/path\\to\\file', pathCombine(['/', 'path', '\\to\\', 'file', '\\'], trailing: false));
  72. }
  73. /**
  74. * Test cases for env()
  75. */
  76. public function testEnv(): void
  77. {
  78. $_ENV['DOES_NOT_EXIST'] = null;
  79. $this->assertNull(env('DOES_NOT_EXIST'));
  80. $this->assertSame('default', env('DOES_NOT_EXIST', 'default'));
  81. $_ENV['DOES_EXIST'] = 'some value';
  82. $this->assertSame('some value', env('DOES_EXIST'));
  83. $this->assertSame('some value', env('DOES_EXIST', 'default'));
  84. $_ENV['EMPTY_VALUE'] = '';
  85. $this->assertSame('', env('EMPTY_VALUE'));
  86. $this->assertSame('', env('EMPTY_VALUE', 'default'));
  87. $_ENV['ZERO'] = '0';
  88. $this->assertSame('0', env('ZERO'));
  89. $this->assertSame('0', env('ZERO', '1'));
  90. $_ENV['ZERO'] = 0;
  91. $this->assertSame(0, env('ZERO'));
  92. $this->assertSame(0, env('ZERO', 1));
  93. $_ENV['ZERO'] = 0.0;
  94. $this->assertSame(0.0, env('ZERO'));
  95. $this->assertSame(0.0, env('ZERO', 1));
  96. $this->assertSame('', env('DOCUMENT_ROOT'));
  97. $this->assertStringContainsString('phpunit', env('PHP_SELF'));
  98. }
  99. public function testEnv2(): void
  100. {
  101. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  102. $server = $_SERVER;
  103. $env = $_ENV;
  104. $_SERVER = $_ENV = [];
  105. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  106. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  107. $_SERVER = $_ENV = [];
  108. $_ENV['CGI_MODE'] = 'BINARY';
  109. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  110. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  111. $_SERVER = $_ENV = [];
  112. $this->assertFalse(env('HTTPS'));
  113. $_SERVER['HTTPS'] = 'on';
  114. $this->assertTrue(env('HTTPS'));
  115. $_SERVER['HTTPS'] = '1';
  116. $this->assertTrue(env('HTTPS'));
  117. $_SERVER['HTTPS'] = 'I am not empty';
  118. $this->assertTrue(env('HTTPS'));
  119. $_SERVER['HTTPS'] = 1;
  120. $this->assertTrue(env('HTTPS'));
  121. $_SERVER['HTTPS'] = 'off';
  122. $this->assertFalse(env('HTTPS'));
  123. $_SERVER['HTTPS'] = false;
  124. $this->assertFalse(env('HTTPS'));
  125. $_SERVER['HTTPS'] = '';
  126. $this->assertFalse(env('HTTPS'));
  127. $_SERVER = [];
  128. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  129. $this->assertTrue(env('HTTPS'));
  130. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  131. $this->assertFalse(env('HTTPS'));
  132. $_SERVER = $_ENV = [];
  133. $this->assertNull(env('TEST_ME'));
  134. $_ENV['TEST_ME'] = 'a';
  135. $this->assertSame(env('TEST_ME'), 'a');
  136. $_SERVER['TEST_ME'] = 'b';
  137. $this->assertSame(env('TEST_ME'), 'b');
  138. unset($_ENV['TEST_ME']);
  139. $this->assertSame(env('TEST_ME'), 'b');
  140. $_SERVER = $server;
  141. $_ENV = $env;
  142. }
  143. /**
  144. * Test cases for h()
  145. *
  146. * @param mixed $value
  147. * @param mixed $expected
  148. */
  149. #[DataProvider('hInputProvider')]
  150. public function testH($value, $expected): void
  151. {
  152. $result = h($value);
  153. $this->assertSame($expected, $result);
  154. }
  155. public static function hInputProvider(): array
  156. {
  157. return [
  158. ['i am clean', 'i am clean'],
  159. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  160. [null, null],
  161. [1, 1],
  162. [1.1, 1.1],
  163. [new stdClass(), '(object)stdClass'],
  164. [new Response(), ''],
  165. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  166. ];
  167. }
  168. public function testH2(): void
  169. {
  170. $string = '<foo>';
  171. $result = h($string);
  172. $this->assertSame('&lt;foo&gt;', $result);
  173. $in = ['this & that', '<p>Which one</p>'];
  174. $result = h($in);
  175. $expected = ['this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;'];
  176. $this->assertSame($expected, $result);
  177. $string = '<foo> & &nbsp;';
  178. $result = h($string);
  179. $this->assertSame('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  180. $string = '<foo> & &nbsp;';
  181. $result = h($string, false);
  182. $this->assertSame('&lt;foo&gt; &amp; &nbsp;', $result);
  183. $string = "An invalid\x80string";
  184. $result = h($string);
  185. $this->assertStringContainsString('string', $result);
  186. $arr = ['<foo>', '&nbsp;'];
  187. $result = h($arr);
  188. $expected = [
  189. '&lt;foo&gt;',
  190. '&amp;nbsp;',
  191. ];
  192. $this->assertSame($expected, $result);
  193. $arr = ['<foo>', '&nbsp;'];
  194. $result = h($arr, false);
  195. $expected = [
  196. '&lt;foo&gt;',
  197. '&nbsp;',
  198. ];
  199. $this->assertSame($expected, $result);
  200. $arr = ['f' => '<foo>', 'n' => '&nbsp;'];
  201. $result = h($arr, false);
  202. $expected = [
  203. 'f' => '&lt;foo&gt;',
  204. 'n' => '&nbsp;',
  205. ];
  206. $this->assertSame($expected, $result);
  207. $arr = ['invalid' => "\x99An invalid\x80string", 'good' => 'Good string'];
  208. $result = h($arr);
  209. $this->assertStringContainsString('An invalid', $result['invalid']);
  210. $this->assertSame('Good string', $result['good']);
  211. // Test that boolean values are not converted to strings
  212. $result = h(false);
  213. $this->assertFalse($result);
  214. $arr = ['foo' => false, 'bar' => true];
  215. $result = h($arr);
  216. $this->assertFalse($result['foo']);
  217. $this->assertTrue($result['bar']);
  218. $obj = new stdClass();
  219. $result = h($obj);
  220. $this->assertSame('(object)stdClass', $result);
  221. $obj = new Response(['body' => 'Body content']);
  222. $result = h($obj);
  223. $this->assertSame('Body content', $result);
  224. }
  225. /**
  226. * Test splitting plugin names.
  227. */
  228. public function testPluginSplit(): void
  229. {
  230. $result = pluginSplit('Something.else');
  231. $this->assertSame(['Something', 'else'], $result);
  232. $result = pluginSplit('Something.else.more.dots');
  233. $this->assertSame(['Something', 'else.more.dots'], $result);
  234. $result = pluginSplit('Somethingelse');
  235. $this->assertSame([null, 'Somethingelse'], $result);
  236. $result = pluginSplit('Something.else', true);
  237. $this->assertSame(['Something.', 'else'], $result);
  238. $result = pluginSplit('Something.else.more.dots', true);
  239. $this->assertSame(['Something.', 'else.more.dots'], $result);
  240. $result = pluginSplit('Post', false, 'Blog');
  241. $this->assertSame(['Blog', 'Post'], $result);
  242. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  243. $this->assertSame(['Blog', 'Post'], $result);
  244. }
  245. /**
  246. * test namespaceSplit
  247. */
  248. public function testNamespaceSplit(): void
  249. {
  250. $result = namespaceSplit('Something');
  251. $this->assertSame(['', 'Something'], $result);
  252. $result = namespaceSplit('\Something');
  253. $this->assertSame(['', 'Something'], $result);
  254. $result = namespaceSplit('Cake\Something');
  255. $this->assertSame(['Cake', 'Something'], $result);
  256. $result = namespaceSplit('Cake\Test\Something');
  257. $this->assertSame(['Cake\Test', 'Something'], $result);
  258. }
  259. /**
  260. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  261. */
  262. public function testDeprecationWarningEnabled(): void
  263. {
  264. $this->expectDeprecationMessageMatches('/Since 5.0.0: This is going away\n(.*?)[\/\\\]FunctionsTest.php, line\: \d+/', function () {
  265. $this->withErrorReporting(E_ALL, function (): void {
  266. deprecationWarning('5.0.0', 'This is going away', 2);
  267. });
  268. });
  269. }
  270. /**
  271. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  272. */
  273. public function testDeprecationWarningEnabledDefaultFrame(): void
  274. {
  275. $this->expectDeprecationMessageMatches('/Since 5.0.0: This is going away too\n(.*?)[\/\\\]TestCase.php, line\: \d+/', function () {
  276. $this->withErrorReporting(E_ALL, function (): void {
  277. deprecationWarning('5.0.0', 'This is going away too');
  278. });
  279. });
  280. }
  281. /**
  282. * Test no error when deprecation matches ignore paths.
  283. */
  284. public function testDeprecationWarningPathDisabled(): void
  285. {
  286. $this->expectNotToPerformAssertions();
  287. Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
  288. $this->withErrorReporting(E_ALL, function (): void {
  289. deprecationWarning('5.0.0', 'This will be gone soon');
  290. });
  291. }
  292. /**
  293. * Test no error when deprecated level is off.
  294. */
  295. public function testDeprecationWarningLevelDisabled(): void
  296. {
  297. $this->expectNotToPerformAssertions();
  298. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
  299. deprecationWarning('5.0.0', 'This is leaving');
  300. });
  301. }
  302. /**
  303. * Test error messages coming out when warning level is on.
  304. */
  305. public function testTriggerWarningEnabled(): void
  306. {
  307. $this->expectWarningMessageMatches('/This will be gone one day - (.*?)[\/\\\]TestCase.php, line\: \d+/', function () {
  308. $this->withErrorReporting(E_ALL, function (): void {
  309. triggerWarning('This will be gone one day');
  310. $this->assertTrue(true);
  311. });
  312. });
  313. }
  314. /**
  315. * Test no error when warning level is off.
  316. */
  317. public function testTriggerWarningLevelDisabled(): void
  318. {
  319. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function (): void {
  320. triggerWarning('This was a mistake.');
  321. $this->assertTrue(true);
  322. });
  323. }
  324. #[DataProvider('toStringProvider')]
  325. public function testToString(mixed $rawValue, ?string $expected): void
  326. {
  327. $this->assertSame($expected, toString($rawValue));
  328. }
  329. /**
  330. * @return array The array of test cases.
  331. */
  332. public static function toStringProvider(): array
  333. {
  334. return [
  335. // input like string
  336. '(string) empty' => ['', ''],
  337. '(string) space' => [' ', ' '],
  338. '(string) dash' => ['-', '-'],
  339. '(string) zero' => ['0', '0'],
  340. '(string) number' => ['55', '55'],
  341. '(string) partially2 number' => ['5x', '5x'],
  342. // input like int
  343. '(int) number' => [55, '55'],
  344. '(int) negative number' => [-5, '-5'],
  345. '(int) PHP_INT_MAX + 2' => [9223372036854775809, '9223372036854775808'], //is float: see IEEE 754
  346. '(int) PHP_INT_MAX + 1' => [9223372036854775808, '9223372036854775808'], //is float: see IEEE 754
  347. '(int) PHP_INT_MAX + 0' => [9223372036854775807, '9223372036854775807'],
  348. '(int) PHP_INT_MAX - 1' => [9223372036854775806, '9223372036854775806'],
  349. '(int) PHP_INT_MIN + 1' => [-9223372036854775807, '-9223372036854775807'],
  350. '(int) PHP_INT_MIN + 0' => [-9223372036854775808, '-9223372036854775808'],
  351. '(int) PHP_INT_MIN - 1' => [-9223372036854775809, '-9223372036854775808'], //is float: see IEEE 754
  352. '(int) PHP_INT_MIN - 2' => [-9223372036854775810, '-9223372036854775808'], //is float: see IEEE 754
  353. // input like float
  354. '(float) zero' => [0.0, '0'],
  355. '(float) positive' => [5.5, '5.5'],
  356. '(float) round' => [5.0, '5'],
  357. '(float) negative' => [-5.5, '-5.5'],
  358. '(float) round negative' => [-5.0, '-5'],
  359. '(float) small' => [0.000000000003, '0.000000000003'],
  360. '(float) small2' => [64321.0000003, '64321.0000003'],
  361. '(float) fractions' => [-9223372036778.2233, '-9223372036778.223'], //is float: see IEEE 754
  362. '(float) NaN' => [acos(8), null],
  363. '(float) INF' => [INF, null],
  364. '(float) -INF' => [-INF, null],
  365. // boolean input types
  366. '(bool) true' => [true, '1'],
  367. '(bool) false' => [false, '0'],
  368. // other input types
  369. '(other) null' => [null, null],
  370. '(other) empty-array' => [[], null],
  371. '(other) int-array' => [[5], null],
  372. '(other) string-array' => [['5'], null],
  373. '(other) simple object' => [new stdClass(), null],
  374. '(other) Stringable object' => [new Entity(), '[]'],
  375. ];
  376. }
  377. #[DataProvider('toIntProvider')]
  378. public function testToInt(mixed $rawValue, null|int $expected): void
  379. {
  380. $this->assertSame($expected, toInt($rawValue));
  381. }
  382. /**
  383. * @return array The array of test cases.
  384. */
  385. public static function toIntProvider(): array
  386. {
  387. return [
  388. // string input types
  389. '(string) empty' => ['', null],
  390. '(string) space' => [' ', null],
  391. '(string) null' => ['null', null],
  392. '(string) dash' => ['-', null],
  393. '(string) ctz' => ['čťž', null],
  394. '(string) hex' => ['0x539', null],
  395. '(string) binary' => ['0b10100111001', null],
  396. '(string) scientific e' => ['1.2e+2', null],
  397. '(string) scientific E' => ['1.2E+2', null],
  398. '(string) octal old' => ['0123', null],
  399. '(string) octal new' => ['0o123', null],
  400. '(string) decimal php74' => ['1_234_567', null],
  401. '(string) zero' => ['0', 0],
  402. '(string) number' => ['55', 55],
  403. '(string) number_space_before' => [' 55', 55],
  404. '(string) number_space_after' => ['55 ', 55],
  405. '(string) negative number' => ['-5', -5],
  406. '(string) float round' => ['5.0', null],
  407. '(string) float round negative' => ['-5.0', null],
  408. '(string) float real' => ['5.1', null],
  409. '(string) float round slovak' => ['5,0', null],
  410. '(string) money' => ['5 €', null],
  411. '(string) PHP_INT_MAX + 1' => ['9223372036854775808', null],
  412. '(string) PHP_INT_MAX + 0' => ['9223372036854775807', 9223372036854775807],
  413. '(string) PHP_INT_MAX - 1' => ['9223372036854775806', 9223372036854775806],
  414. '(string) PHP_INT_MIN + 1' => ['-9223372036854775807', -9223372036854775807],
  415. '(string) PHP_INT_MIN + 0' => ['-9223372036854775808', null],
  416. '(string) PHP_INT_MIN - 1' => ['-9223372036854775809', null],
  417. '(string) string' => ['f', null],
  418. '(string) partially1 number' => ['5 5', null],
  419. '(string) partially2 number' => ['5x', null],
  420. '(string) partially3 number' => ['x4', null],
  421. '(string) double dot' => ['5.1.0', null],
  422. // int input types
  423. '(int) number' => [55, 55],
  424. '(int) negative number' => [-5, -5],
  425. '(int) PHP_INT_MAX + 1' => [9223372036854775808, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  426. '(int) PHP_INT_MAX + 0' => [9223372036854775807, 9223372036854775807],
  427. '(int) PHP_INT_MAX - 1' => [9223372036854775806, 9223372036854775806],
  428. '(int) PHP_INT_MIN + 1' => [-9223372036854775807, -9223372036854775807],
  429. // PHP_INT_MIN is float -> PHP inconsistency https://bugs.php.net/bug.php?id=53934
  430. '(int) PHP_INT_MIN + 0' => [-9223372036854775808, -9223372036854775807 - 1], // ¯\_(ツ)_/¯,
  431. '(int) PHP_INT_MIN - 1' => [-9223372036854775809, -9223372036854775807 - 1], // ¯\_(ツ)_/¯,
  432. // float input types
  433. '(float) zero' => [0.0, 0],
  434. '(float) positive' => [5.5, 5],
  435. '(float) round' => [5.0, 5],
  436. '(float) negative' => [-5.5, -5],
  437. '(float) round negative' => [-5.0, -5],
  438. '(float) PHP_INT_MAX + 1' => [9223372036854775808.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  439. '(float) PHP_INT_MAX + 0' => [9223372036854775807.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  440. '(float) PHP_INT_MAX - 1' => [9223372036854775806.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  441. '(float) PHP_INT_MIN + 1' => [-9223372036854775807.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  442. '(float) PHP_INT_MIN + 0' => [-9223372036854775808.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  443. '(float) PHP_INT_MIN - 1' => [-9223372036854775809.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  444. '(float) 2^53 + 2' => [9007199254740994.0, 9007199254740994],
  445. '(float) 2^53 + 1' => [9007199254740993.0, 9007199254740992], // see IEEE 754
  446. '(float) 2^53 + 0' => [9007199254740992.0, 9007199254740992],
  447. '(float) 2^53 - 1' => [9007199254740991.0, 9007199254740991],
  448. '(float) 2^53 - 2' => [9007199254740990.0, 9007199254740990],
  449. '(float) -(2^53) + 2' => [-9007199254740990.0, -9007199254740990],
  450. '(float) -(2^53) + 1' => [-9007199254740991.0, -9007199254740991],
  451. '(float) -(2^53) + 0' => [-9007199254740992.0, -9007199254740992],
  452. '(float) -(2^53) - 1' => [-9007199254740993.0, -9007199254740992], // see IEEE 754
  453. '(float) -(2^53) - 2' => [-9007199254740994.0, -9007199254740994],
  454. '(float) NaN' => [acos(8), null],
  455. '(float) INF' => [INF, null],
  456. '(float) -INF' => [-INF, null],
  457. // boolean input types
  458. '(bool) true' => [true, 1],
  459. '(bool) false' => [false, 0],
  460. // other input types
  461. '(other) null' => [null, null],
  462. '(other) empty-array' => [[], null],
  463. '(other) int-array' => [[5], null],
  464. '(other) string-array' => [['5'], null],
  465. '(other) simple object' => [new stdClass(), null],
  466. ];
  467. }
  468. #[DataProvider('toFloatProvider')]
  469. public function testToFloat(mixed $rawValue, null|float $expected): void
  470. {
  471. $this->assertSame($expected, toFloat($rawValue));
  472. }
  473. /**
  474. * @return array The array of test cases.
  475. */
  476. public static function toFloatProvider(): array
  477. {
  478. return [
  479. // string input types
  480. '(string) empty' => ['', null],
  481. '(string) space' => [' ', null],
  482. '(string) null' => ['null', null],
  483. '(string) dash' => ['-', null],
  484. '(string) ctz' => ['čťž', null],
  485. '(string) hex' => ['0x539', null],
  486. '(string) binary' => ['0b10100111001', null],
  487. '(string) scientific e' => ['1.2e+2', 120.0],
  488. '(string) scientific E' => ['1.2E+2', 120.],
  489. '(string) octal old' => ['0123', 123.0],
  490. '(string) octal new' => ['0o123', null],
  491. '(string) decimal php74' => ['1_234_567', null],
  492. '(string) zero' => ['0', 0.0],
  493. '(string) number' => ['55', 55.0],
  494. '(string) number_space_before' => [' 55', 55.0],
  495. '(string) number_space_after' => ['55 ', 55.0],
  496. '(string) negative number' => ['-5', -5.0],
  497. '(string) float round' => ['5.0', 5.0],
  498. '(string) float round negative' => ['-5.0', -5.0],
  499. '(string) float real' => ['5.1', 5.1],
  500. '(string) float round slovak' => ['5,0', null],
  501. '(string) money' => ['5 €', null],
  502. '(string) PHP_INT_MAX + 1' => ['9223372036854775808', PHP_INT_MAX],
  503. '(string) PHP_INT_MAX + 0' => ['9223372036854775807', 9223372036854775807],
  504. '(string) PHP_INT_MAX - 1' => ['9223372036854775806', 9223372036854775806],
  505. '(string) PHP_INT_MIN + 1' => ['-9223372036854775807', -9223372036854775807],
  506. '(string) PHP_INT_MIN + 0' => ['-9223372036854775808', -9223372036854775807],
  507. '(string) PHP_INT_MIN - 1' => ['-9223372036854775809', -9223372036854775807],
  508. '(string) string' => ['f', null],
  509. '(string) partially1 number' => ['5 5', null],
  510. '(string) partially2 number' => ['5x', null],
  511. '(string) partially3 number' => ['x4', null],
  512. '(string) double dot' => ['5.1.0', null],
  513. // int input types
  514. '(int) number' => [55, 55.0],
  515. '(int) negative number' => [-5, -5.0],
  516. '(int) PHP_INT_MAX + 1' => [9223372036854775808, 9223372036854775807 - 1],
  517. '(int) PHP_INT_MAX + 0' => [9223372036854775807, 9223372036854775807],
  518. '(int) PHP_INT_MAX - 1' => [9223372036854775806, 9223372036854775806],
  519. '(int) PHP_INT_MIN + 1' => [-9223372036854775807, -9223372036854775807],
  520. // PHP_INT_MIN is float -> PHP inconsistency https://bugs.php.net/bug.php?id=53934
  521. '(int) PHP_INT_MIN + 0' => [-9223372036854775808, -9223372036854775807 - 1], // ¯\_(ツ)_/¯,
  522. '(int) PHP_INT_MIN - 1' => [-9223372036854775809, -9223372036854775807 - 1], // ¯\_(ツ)_/¯,
  523. // float input types
  524. '(float) zero' => [0.0, 0.0],
  525. '(float) positive' => [5.5, 5.5],
  526. '(float) round' => [5.0, 5.0],
  527. '(float) negative' => [-5.5, -5.5],
  528. '(float) round negative' => [-5.0, -5.0],
  529. '(float) PHP_INT_MAX + 1' => [9223372036854775808.0, 9223372036854775807 - 1],
  530. '(float) PHP_INT_MAX + 0' => [9223372036854775807.0, 9223372036854775807 - 1],
  531. '(float) PHP_INT_MAX - 1' => [9223372036854775806.0, 9223372036854775807 - 1],
  532. '(float) PHP_INT_MIN + 1' => [-9223372036854775807.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  533. '(float) PHP_INT_MIN + 0' => [-9223372036854775808.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  534. '(float) PHP_INT_MIN - 1' => [-9223372036854775809.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  535. '(float) 2^53 + 2' => [9007199254740994.0, 9007199254740994],
  536. '(float) 2^53 + 1' => [9007199254740993.0, 9007199254740992], // see IEEE 754
  537. '(float) 2^53 + 0' => [9007199254740992.0, 9007199254740992],
  538. '(float) 2^53 - 1' => [9007199254740991.0, 9007199254740991],
  539. '(float) 2^53 - 2' => [9007199254740990.0, 9007199254740990],
  540. '(float) -(2^53) + 2' => [-9007199254740990.0, -9007199254740990],
  541. '(float) -(2^53) + 1' => [-9007199254740991.0, -9007199254740991],
  542. '(float) -(2^53) + 0' => [-9007199254740992.0, -9007199254740992],
  543. '(float) -(2^53) - 1' => [-9007199254740993.0, -9007199254740992], // see IEEE 754
  544. '(float) -(2^53) - 2' => [-9007199254740994.0, -9007199254740994],
  545. '(float) NaN' => [acos(8), null],
  546. '(float) INF' => [INF, null],
  547. '(float) -INF' => [-INF, null],
  548. // boolean input types
  549. '(bool) true' => [true, 1.0],
  550. '(bool) false' => [false, 0.0],
  551. // other input types
  552. '(other) null' => [null, null],
  553. '(other) empty-array' => [[], null],
  554. '(other) int-array' => [[5], null],
  555. '(other) string-array' => [['5'], null],
  556. '(other) simple object' => [new stdClass(), null],
  557. ];
  558. }
  559. #[DataProvider('toBoolProvider')]
  560. public function testToBool(mixed $rawValue, ?bool $expected): void
  561. {
  562. $this->assertSame($expected, toBool($rawValue));
  563. }
  564. /**
  565. * @return array The array of test cases.
  566. */
  567. public static function toBoolProvider(): array
  568. {
  569. return [
  570. // string input types
  571. '(string) empty string' => ['', null],
  572. '(string) space' => [' ', null],
  573. '(string) some word' => ['abc', null],
  574. '(string) double 0' => ['00', null],
  575. '(string) single 0' => ['0', false],
  576. '(string) false' => ['false', null],
  577. '(string) double 1' => ['11', null],
  578. '(string) single 1' => ['1', true],
  579. '(string) true-string' => ['true', null],
  580. // int input types
  581. '(int) 0' => [0, false],
  582. '(int) 1' => [1, true],
  583. '(int) -1' => [-1, null],
  584. '(int) 55' => [55, null],
  585. '(int) negative number' => [-5, null],
  586. // float input types
  587. '(float) positive' => [5.5, null],
  588. '(float) round' => [5.0, null],
  589. '(float) 0.0' => [0.0, false],
  590. '(float) 1.0' => [1.0, true],
  591. '(float) NaN' => [acos(8), null],
  592. '(float) INF' => [INF, null],
  593. '(float) -INF' => [-INF, null],
  594. // boolean input types
  595. '(bool) true' => [true, true],
  596. '(bool) false' => [false, false],
  597. // other input types
  598. '(other) null' => [null, null],
  599. '(other) empty-array' => [[], null],
  600. '(other) int-array' => [[5], null],
  601. '(other) string-array' => [['5'], null],
  602. '(other) simple object' => [new stdClass(), null],
  603. ];
  604. }
  605. }