FunctionsTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 stdClass;
  22. use function Cake\Core\deprecationWarning;
  23. use function Cake\Core\env;
  24. use function Cake\Core\h;
  25. use function Cake\Core\namespaceSplit;
  26. use function Cake\Core\pathCombine;
  27. use function Cake\Core\pluginSplit;
  28. use function Cake\Core\toBool;
  29. use function Cake\Core\toInt;
  30. use function Cake\Core\toString;
  31. use function Cake\Core\triggerWarning;
  32. /**
  33. * Test cases for functions in Core\functions.php
  34. */
  35. class FunctionsTest extends TestCase
  36. {
  37. public function testPathCombine(): void
  38. {
  39. $this->assertSame('', pathCombine([]));
  40. $this->assertSame('', pathCombine(['']));
  41. $this->assertSame('', pathCombine(['', '']));
  42. $this->assertSame('/', pathCombine(['/', '/']));
  43. $this->assertSame('path/to/file', pathCombine(['path', 'to', 'file']));
  44. $this->assertSame('path/to/file', pathCombine(['path/', 'to', 'file']));
  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. // Test adding trailing slash
  54. $this->assertSame('/', pathCombine([], trailing: true));
  55. $this->assertSame('/', pathCombine([''], trailing: true));
  56. $this->assertSame('/', pathCombine(['/'], trailing: true));
  57. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file/'], trailing: true));
  58. $this->assertSame('/path/to/file/', pathCombine(['/path', 'to', 'file/', '/'], trailing: true));
  59. // Test removing trailing slash
  60. $this->assertSame('', pathCombine([''], trailing: false));
  61. $this->assertSame('', pathCombine(['/'], trailing: false));
  62. $this->assertSame('/path/to/file', pathCombine(['/path', 'to', 'file/'], trailing: false));
  63. $this->assertSame('/path/to/file', pathCombine(['/path', 'to', 'file/', '/'], trailing: false));
  64. // Test Windows-style backslashes
  65. $this->assertSame('/path/to\\file', pathCombine(['/', '\\path', 'to', '\\file']));
  66. $this->assertSame('/path\\to\\file/', pathCombine(['/', 'path', '\\to\\', 'file'], trailing: true));
  67. $this->assertSame('/path\\to\\file\\', pathCombine(['/', 'path', '\\to\\', 'file', '\\'], trailing: true));
  68. $this->assertSame('/path\\to\\file', pathCombine(['/', 'path', '\\to\\', 'file'], trailing: false));
  69. $this->assertSame('/path\\to\\file', pathCombine(['/', 'path', '\\to\\', 'file', '\\'], trailing: false));
  70. }
  71. /**
  72. * Test cases for env()
  73. */
  74. public function testEnv(): void
  75. {
  76. $_ENV['DOES_NOT_EXIST'] = null;
  77. $this->assertNull(env('DOES_NOT_EXIST'));
  78. $this->assertSame('default', env('DOES_NOT_EXIST', 'default'));
  79. $_ENV['DOES_EXIST'] = 'some value';
  80. $this->assertSame('some value', env('DOES_EXIST'));
  81. $this->assertSame('some value', env('DOES_EXIST', 'default'));
  82. $_ENV['EMPTY_VALUE'] = '';
  83. $this->assertSame('', env('EMPTY_VALUE'));
  84. $this->assertSame('', env('EMPTY_VALUE', 'default'));
  85. $_ENV['ZERO'] = '0';
  86. $this->assertSame('0', env('ZERO'));
  87. $this->assertSame('0', env('ZERO', '1'));
  88. $_ENV['ZERO'] = 0;
  89. $this->assertSame(0, env('ZERO'));
  90. $this->assertSame(0, env('ZERO', 1));
  91. $_ENV['ZERO'] = 0.0;
  92. $this->assertSame(0.0, env('ZERO'));
  93. $this->assertSame(0.0, env('ZERO', 1));
  94. $this->assertSame('', env('DOCUMENT_ROOT'));
  95. $this->assertStringContainsString('phpunit', env('PHP_SELF'));
  96. }
  97. public function testEnv2(): void
  98. {
  99. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  100. $server = $_SERVER;
  101. $env = $_ENV;
  102. $_SERVER = $_ENV = [];
  103. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  104. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  105. $_SERVER = $_ENV = [];
  106. $_ENV['CGI_MODE'] = 'BINARY';
  107. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  108. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  109. $_SERVER = $_ENV = [];
  110. $this->assertFalse(env('HTTPS'));
  111. $_SERVER['HTTPS'] = 'on';
  112. $this->assertTrue(env('HTTPS'));
  113. $_SERVER['HTTPS'] = '1';
  114. $this->assertTrue(env('HTTPS'));
  115. $_SERVER['HTTPS'] = 'I am not empty';
  116. $this->assertTrue(env('HTTPS'));
  117. $_SERVER['HTTPS'] = 1;
  118. $this->assertTrue(env('HTTPS'));
  119. $_SERVER['HTTPS'] = 'off';
  120. $this->assertFalse(env('HTTPS'));
  121. $_SERVER['HTTPS'] = false;
  122. $this->assertFalse(env('HTTPS'));
  123. $_SERVER['HTTPS'] = '';
  124. $this->assertFalse(env('HTTPS'));
  125. $_SERVER = [];
  126. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  127. $this->assertTrue(env('HTTPS'));
  128. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  129. $this->assertFalse(env('HTTPS'));
  130. $_SERVER = $_ENV = [];
  131. $this->assertNull(env('TEST_ME'));
  132. $_ENV['TEST_ME'] = 'a';
  133. $this->assertSame(env('TEST_ME'), 'a');
  134. $_SERVER['TEST_ME'] = 'b';
  135. $this->assertSame(env('TEST_ME'), 'b');
  136. unset($_ENV['TEST_ME']);
  137. $this->assertSame(env('TEST_ME'), 'b');
  138. $_SERVER = $server;
  139. $_ENV = $env;
  140. }
  141. /**
  142. * Test cases for h()
  143. *
  144. * @dataProvider hInputProvider
  145. * @param mixed $value
  146. * @param mixed $expected
  147. */
  148. public function testH($value, $expected): void
  149. {
  150. $result = h($value);
  151. $this->assertSame($expected, $result);
  152. }
  153. public static function hInputProvider(): array
  154. {
  155. return [
  156. ['i am clean', 'i am clean'],
  157. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  158. [null, null],
  159. [1, 1],
  160. [1.1, 1.1],
  161. [new stdClass(), '(object)stdClass'],
  162. [new Response(), ''],
  163. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  164. ];
  165. }
  166. public function testH2(): void
  167. {
  168. $string = '<foo>';
  169. $result = h($string);
  170. $this->assertSame('&lt;foo&gt;', $result);
  171. $in = ['this & that', '<p>Which one</p>'];
  172. $result = h($in);
  173. $expected = ['this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;'];
  174. $this->assertSame($expected, $result);
  175. $string = '<foo> & &nbsp;';
  176. $result = h($string);
  177. $this->assertSame('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  178. $string = '<foo> & &nbsp;';
  179. $result = h($string, false);
  180. $this->assertSame('&lt;foo&gt; &amp; &nbsp;', $result);
  181. $string = "An invalid\x80string";
  182. $result = h($string);
  183. $this->assertStringContainsString('string', $result);
  184. $arr = ['<foo>', '&nbsp;'];
  185. $result = h($arr);
  186. $expected = [
  187. '&lt;foo&gt;',
  188. '&amp;nbsp;',
  189. ];
  190. $this->assertSame($expected, $result);
  191. $arr = ['<foo>', '&nbsp;'];
  192. $result = h($arr, false);
  193. $expected = [
  194. '&lt;foo&gt;',
  195. '&nbsp;',
  196. ];
  197. $this->assertSame($expected, $result);
  198. $arr = ['f' => '<foo>', 'n' => '&nbsp;'];
  199. $result = h($arr, false);
  200. $expected = [
  201. 'f' => '&lt;foo&gt;',
  202. 'n' => '&nbsp;',
  203. ];
  204. $this->assertSame($expected, $result);
  205. $arr = ['invalid' => "\x99An invalid\x80string", 'good' => 'Good string'];
  206. $result = h($arr);
  207. $this->assertStringContainsString('An invalid', $result['invalid']);
  208. $this->assertSame('Good string', $result['good']);
  209. // Test that boolean values are not converted to strings
  210. $result = h(false);
  211. $this->assertFalse($result);
  212. $arr = ['foo' => false, 'bar' => true];
  213. $result = h($arr);
  214. $this->assertFalse($result['foo']);
  215. $this->assertTrue($result['bar']);
  216. $obj = new stdClass();
  217. $result = h($obj);
  218. $this->assertSame('(object)stdClass', $result);
  219. $obj = new Response(['body' => 'Body content']);
  220. $result = h($obj);
  221. $this->assertSame('Body content', $result);
  222. }
  223. /**
  224. * Test splitting plugin names.
  225. */
  226. public function testPluginSplit(): void
  227. {
  228. $result = pluginSplit('Something.else');
  229. $this->assertSame(['Something', 'else'], $result);
  230. $result = pluginSplit('Something.else.more.dots');
  231. $this->assertSame(['Something', 'else.more.dots'], $result);
  232. $result = pluginSplit('Somethingelse');
  233. $this->assertSame([null, 'Somethingelse'], $result);
  234. $result = pluginSplit('Something.else', true);
  235. $this->assertSame(['Something.', 'else'], $result);
  236. $result = pluginSplit('Something.else.more.dots', true);
  237. $this->assertSame(['Something.', 'else.more.dots'], $result);
  238. $result = pluginSplit('Post', false, 'Blog');
  239. $this->assertSame(['Blog', 'Post'], $result);
  240. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  241. $this->assertSame(['Blog', 'Post'], $result);
  242. }
  243. /**
  244. * test namespaceSplit
  245. */
  246. public function testNamespaceSplit(): void
  247. {
  248. $result = namespaceSplit('Something');
  249. $this->assertSame(['', 'Something'], $result);
  250. $result = namespaceSplit('\Something');
  251. $this->assertSame(['', 'Something'], $result);
  252. $result = namespaceSplit('Cake\Something');
  253. $this->assertSame(['Cake', 'Something'], $result);
  254. $result = namespaceSplit('Cake\Test\Something');
  255. $this->assertSame(['Cake\Test', 'Something'], $result);
  256. }
  257. /**
  258. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  259. */
  260. public function testDeprecationWarningEnabled(): void
  261. {
  262. $this->expectDeprecationMessageMatches('/Since 5.0.0: This is going away\n(.*?)[\/\\\]FunctionsTest.php, line\: \d+/', function () {
  263. $this->withErrorReporting(E_ALL, function (): void {
  264. deprecationWarning('5.0.0', 'This is going away', 2);
  265. });
  266. });
  267. }
  268. /**
  269. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  270. */
  271. public function testDeprecationWarningEnabledDefaultFrame(): void
  272. {
  273. $this->expectDeprecationMessageMatches('/Since 5.0.0: This is going away too\n(.*?)[\/\\\]TestCase.php, line\: \d+/', function () {
  274. $this->withErrorReporting(E_ALL, function (): void {
  275. deprecationWarning('5.0.0', 'This is going away too');
  276. });
  277. });
  278. }
  279. /**
  280. * Test no error when deprecation matches ignore paths.
  281. */
  282. public function testDeprecationWarningPathDisabled(): void
  283. {
  284. $this->expectNotToPerformAssertions();
  285. Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
  286. $this->withErrorReporting(E_ALL, function (): void {
  287. deprecationWarning('5.0.0', 'This will be gone soon');
  288. });
  289. }
  290. /**
  291. * Test no error when deprecated level is off.
  292. */
  293. public function testDeprecationWarningLevelDisabled(): void
  294. {
  295. $this->expectNotToPerformAssertions();
  296. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
  297. deprecationWarning('5.0.0', 'This is leaving');
  298. });
  299. }
  300. /**
  301. * Test error messages coming out when warning level is on.
  302. */
  303. public function testTriggerWarningEnabled(): void
  304. {
  305. $this->expectWarningMessageMatches('/This will be gone one day - (.*?)[\/\\\]TestCase.php, line\: \d+/', function () {
  306. $this->withErrorReporting(E_ALL, function (): void {
  307. triggerWarning('This will be gone one day');
  308. $this->assertTrue(true);
  309. });
  310. });
  311. }
  312. /**
  313. * Test no error when warning level is off.
  314. */
  315. public function testTriggerWarningLevelDisabled(): void
  316. {
  317. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function (): void {
  318. triggerWarning('This was a mistake.');
  319. $this->assertTrue(true);
  320. });
  321. }
  322. /**
  323. * @dataProvider toStringProvider
  324. */
  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. /**
  378. * @dataProvider toIntProvider
  379. */
  380. public function testToInt(mixed $rawValue, null|int $expected): void
  381. {
  382. $this->assertSame($expected, toInt($rawValue));
  383. }
  384. /**
  385. * @return array The array of test cases.
  386. */
  387. public static function toIntProvider(): array
  388. {
  389. return [
  390. // string input types
  391. '(string) empty' => ['', null],
  392. '(string) space' => [' ', null],
  393. '(string) null' => ['null', null],
  394. '(string) dash' => ['-', null],
  395. '(string) ctz' => ['čťž', null],
  396. '(string) hex' => ['0x539', null],
  397. '(string) binary' => ['0b10100111001', null],
  398. '(string) scientific e' => ['1.2e+2', null],
  399. '(string) scientific E' => ['1.2E+2', null],
  400. '(string) octal old' => ['0123', null],
  401. '(string) octal new' => ['0o123', null],
  402. '(string) decimal php74' => ['1_234_567', null],
  403. '(string) zero' => ['0', 0],
  404. '(string) number' => ['55', 55],
  405. '(string) number_space_before' => [' 55', 55],
  406. '(string) number_space_after' => ['55 ', 55],
  407. '(string) negative number' => ['-5', -5],
  408. '(string) float round' => ['5.0', null],
  409. '(string) float round negative' => ['-5.0', null],
  410. '(string) float real' => ['5.1', null],
  411. '(string) float round slovak' => ['5,0', null],
  412. '(string) money' => ['5 €', null],
  413. '(string) PHP_INT_MAX + 1' => ['9223372036854775808', null],
  414. '(string) PHP_INT_MAX + 0' => ['9223372036854775807', 9223372036854775807],
  415. '(string) PHP_INT_MAX - 1' => ['9223372036854775806', 9223372036854775806],
  416. '(string) PHP_INT_MIN + 1' => ['-9223372036854775807', -9223372036854775807],
  417. '(string) PHP_INT_MIN + 0' => ['-9223372036854775808', null],
  418. '(string) PHP_INT_MIN - 1' => ['-9223372036854775809', null],
  419. '(string) string' => ['f', null],
  420. '(string) partially1 number' => ['5 5', null],
  421. '(string) partially2 number' => ['5x', null],
  422. '(string) partially3 number' => ['x4', null],
  423. '(string) double dot' => ['5.1.0', null],
  424. // int input types
  425. '(int) number' => [55, 55],
  426. '(int) negative number' => [-5, -5],
  427. '(int) PHP_INT_MAX + 1' => [9223372036854775808, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  428. '(int) PHP_INT_MAX + 0' => [9223372036854775807, 9223372036854775807],
  429. '(int) PHP_INT_MAX - 1' => [9223372036854775806, 9223372036854775806],
  430. '(int) PHP_INT_MIN + 1' => [-9223372036854775807, -9223372036854775807],
  431. // PHP_INT_MIN is float -> PHP inconsistency https://bugs.php.net/bug.php?id=53934
  432. '(int) PHP_INT_MIN + 0' => [-9223372036854775808, -9223372036854775807 - 1], // ¯\_(ツ)_/¯,
  433. '(int) PHP_INT_MIN - 1' => [-9223372036854775809, -9223372036854775807 - 1], // ¯\_(ツ)_/¯,
  434. // float input types
  435. '(float) zero' => [0.0, 0],
  436. '(float) positive' => [5.5, 5],
  437. '(float) round' => [5.0, 5],
  438. '(float) negative' => [-5.5, -5],
  439. '(float) round negative' => [-5.0, -5],
  440. '(float) PHP_INT_MAX + 1' => [9223372036854775808.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  441. '(float) PHP_INT_MAX + 0' => [9223372036854775807.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  442. '(float) PHP_INT_MAX - 1' => [9223372036854775806.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  443. '(float) PHP_INT_MIN + 1' => [-9223372036854775807.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  444. '(float) PHP_INT_MIN + 0' => [-9223372036854775808.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  445. '(float) PHP_INT_MIN - 1' => [-9223372036854775809.0, -9223372036854775807 - 1], // ¯\_(ツ)_/¯
  446. '(float) 2^53 + 2' => [9007199254740994.0, 9007199254740994],
  447. '(float) 2^53 + 1' => [9007199254740993.0, 9007199254740992], // see IEEE 754
  448. '(float) 2^53 + 0' => [9007199254740992.0, 9007199254740992],
  449. '(float) 2^53 - 1' => [9007199254740991.0, 9007199254740991],
  450. '(float) 2^53 - 2' => [9007199254740990.0, 9007199254740990],
  451. '(float) -(2^53) + 2' => [-9007199254740990.0, -9007199254740990],
  452. '(float) -(2^53) + 1' => [-9007199254740991.0, -9007199254740991],
  453. '(float) -(2^53) + 0' => [-9007199254740992.0, -9007199254740992],
  454. '(float) -(2^53) - 1' => [-9007199254740993.0, -9007199254740992], // see IEEE 754
  455. '(float) -(2^53) - 2' => [-9007199254740994.0, -9007199254740994],
  456. '(float) NaN' => [acos(8), null],
  457. '(float) INF' => [INF, null],
  458. '(float) -INF' => [-INF, null],
  459. // boolean input types
  460. '(bool) true' => [true, 1],
  461. '(bool) false' => [false, 0],
  462. // other input types
  463. '(other) null' => [null, null],
  464. '(other) empty-array' => [[], null],
  465. '(other) int-array' => [[5], null],
  466. '(other) string-array' => [['5'], null],
  467. '(other) simple object' => [new stdClass(), null],
  468. ];
  469. }
  470. /**
  471. * @dataProvider toBoolProvider
  472. */
  473. public function testToBool(mixed $rawValue, ?bool $expected): void
  474. {
  475. $this->assertSame($expected, toBool($rawValue));
  476. }
  477. /**
  478. * @return array The array of test cases.
  479. */
  480. public static function toBoolProvider(): array
  481. {
  482. return [
  483. // string input types
  484. '(string) empty string' => ['', null],
  485. '(string) space' => [' ', null],
  486. '(string) some word' => ['abc', null],
  487. '(string) double 0' => ['00', null],
  488. '(string) single 0' => ['0', false],
  489. '(string) false' => ['false', null],
  490. '(string) double 1' => ['11', null],
  491. '(string) single 1' => ['1', true],
  492. '(string) true-string' => ['true', null],
  493. // int input types
  494. '(int) 0' => [0, false],
  495. '(int) 1' => [1, true],
  496. '(int) -1' => [-1, null],
  497. '(int) 55' => [55, null],
  498. '(int) negative number' => [-5, null],
  499. // float input types
  500. '(float) positive' => [5.5, null],
  501. '(float) round' => [5.0, null],
  502. '(float) 0.0' => [0.0, false],
  503. '(float) 1.0' => [1.0, true],
  504. '(float) NaN' => [acos(8), null],
  505. '(float) INF' => [INF, null],
  506. '(float) -INF' => [-INF, null],
  507. // boolean input types
  508. '(bool) true' => [true, true],
  509. '(bool) false' => [false, false],
  510. // other input types
  511. '(other) null' => [null, null],
  512. '(other) empty-array' => [[], null],
  513. '(other) int-array' => [[5], null],
  514. '(other) string-array' => [['5'], null],
  515. '(other) simple object' => [new stdClass(), null],
  516. ];
  517. }
  518. }