FunctionsTest.php 27 KB

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