FunctionsTest.php 27 KB

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