BasicsTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * BasicsTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 1.2.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase;
  19. use Cake\Collection\Collection;
  20. use Cake\Error\Debugger;
  21. use Cake\Event\EventManager;
  22. use Cake\Http\Response;
  23. use Cake\TestSuite\TestCase;
  24. require_once CAKE . 'basics.php';
  25. /**
  26. * BasicsTest class
  27. */
  28. class BasicsTest extends TestCase
  29. {
  30. /**
  31. * test the array_diff_key compatibility function.
  32. *
  33. * @return void
  34. */
  35. public function testArrayDiffKey()
  36. {
  37. $one = ['one' => 1, 'two' => 2, 'three' => 3];
  38. $two = ['one' => 'one', 'two' => 'two'];
  39. $result = array_diff_key($one, $two);
  40. $expected = ['three' => 3];
  41. $this->assertSame($expected, $result);
  42. $one = ['one' => ['value', 'value-two'], 'two' => 2, 'three' => 3];
  43. $two = ['two' => 'two'];
  44. $result = array_diff_key($one, $two);
  45. $expected = ['one' => ['value', 'value-two'], 'three' => 3];
  46. $this->assertSame($expected, $result);
  47. $one = ['one' => null, 'two' => 2, 'three' => '', 'four' => 0];
  48. $two = ['two' => 'two'];
  49. $result = array_diff_key($one, $two);
  50. $expected = ['one' => null, 'three' => '', 'four' => 0];
  51. $this->assertSame($expected, $result);
  52. $one = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  53. $two = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  54. $result = array_diff_key($one, $two);
  55. $this->assertSame([], $result);
  56. $one = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  57. $two = [];
  58. $result = array_diff_key($one, $two);
  59. $this->assertSame($one, $result);
  60. }
  61. /**
  62. * testHttpBase method
  63. *
  64. * @return void
  65. */
  66. public function testEnv()
  67. {
  68. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  69. $server = $_SERVER;
  70. $env = $_ENV;
  71. $_SERVER = $_ENV = [];
  72. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  73. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  74. $_SERVER = $_ENV = [];
  75. $_ENV['CGI_MODE'] = 'BINARY';
  76. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  77. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  78. $_SERVER = $_ENV = [];
  79. $this->assertFalse(env('HTTPS'));
  80. $_SERVER['HTTPS'] = 'on';
  81. $this->assertTrue(env('HTTPS'));
  82. $_SERVER['HTTPS'] = '1';
  83. $this->assertTrue(env('HTTPS'));
  84. $_SERVER['HTTPS'] = 'I am not empty';
  85. $this->assertTrue(env('HTTPS'));
  86. $_SERVER['HTTPS'] = 1;
  87. $this->assertTrue(env('HTTPS'));
  88. $_SERVER['HTTPS'] = 'off';
  89. $this->assertFalse(env('HTTPS'));
  90. $_SERVER['HTTPS'] = false;
  91. $this->assertFalse(env('HTTPS'));
  92. $_SERVER['HTTPS'] = '';
  93. $this->assertFalse(env('HTTPS'));
  94. $_SERVER = [];
  95. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  96. $this->assertTrue(env('HTTPS'));
  97. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  98. $this->assertFalse(env('HTTPS'));
  99. $_SERVER = $_ENV = [];
  100. $this->assertNull(env('TEST_ME'));
  101. $_ENV['TEST_ME'] = 'a';
  102. $this->assertSame(env('TEST_ME'), 'a');
  103. $_SERVER['TEST_ME'] = 'b';
  104. $this->assertSame(env('TEST_ME'), 'b');
  105. unset($_ENV['TEST_ME']);
  106. $this->assertSame(env('TEST_ME'), 'b');
  107. $_SERVER = $server;
  108. $_ENV = $env;
  109. }
  110. /**
  111. * Test h()
  112. *
  113. * @return void
  114. */
  115. public function testH()
  116. {
  117. $string = '<foo>';
  118. $result = h($string);
  119. $this->assertSame('&lt;foo&gt;', $result);
  120. $in = ['this & that', '<p>Which one</p>'];
  121. $result = h($in);
  122. $expected = ['this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;'];
  123. $this->assertSame($expected, $result);
  124. $string = '<foo> & &nbsp;';
  125. $result = h($string);
  126. $this->assertSame('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  127. $string = '<foo> & &nbsp;';
  128. $result = h($string, false);
  129. $this->assertSame('&lt;foo&gt; &amp; &nbsp;', $result);
  130. $string = "An invalid\x80string";
  131. $result = h($string);
  132. $this->assertStringContainsString('string', $result);
  133. $arr = ['<foo>', '&nbsp;'];
  134. $result = h($arr);
  135. $expected = [
  136. '&lt;foo&gt;',
  137. '&amp;nbsp;',
  138. ];
  139. $this->assertSame($expected, $result);
  140. $arr = ['<foo>', '&nbsp;'];
  141. $result = h($arr, false);
  142. $expected = [
  143. '&lt;foo&gt;',
  144. '&nbsp;',
  145. ];
  146. $this->assertSame($expected, $result);
  147. $arr = ['f' => '<foo>', 'n' => '&nbsp;'];
  148. $result = h($arr, false);
  149. $expected = [
  150. 'f' => '&lt;foo&gt;',
  151. 'n' => '&nbsp;',
  152. ];
  153. $this->assertSame($expected, $result);
  154. $arr = ['invalid' => "\x99An invalid\x80string", 'good' => 'Good string'];
  155. $result = h($arr);
  156. $this->assertStringContainsString('An invalid', $result['invalid']);
  157. $this->assertSame('Good string', $result['good']);
  158. // Test that boolean values are not converted to strings
  159. $result = h(false);
  160. $this->assertFalse($result);
  161. $arr = ['foo' => false, 'bar' => true];
  162. $result = h($arr);
  163. $this->assertFalse($result['foo']);
  164. $this->assertTrue($result['bar']);
  165. $obj = new \stdClass();
  166. $result = h($obj);
  167. $this->assertSame('(object)stdClass', $result);
  168. $obj = new Response(['body' => 'Body content']);
  169. $result = h($obj);
  170. $this->assertSame('Body content', $result);
  171. }
  172. /**
  173. * test debug()
  174. *
  175. * @return void
  176. */
  177. public function testDebug()
  178. {
  179. ob_start();
  180. $this->assertSame('this-is-a-test', debug('this-is-a-test', false));
  181. $result = ob_get_clean();
  182. $expectedText = <<<EXPECTED
  183. %s (line %d)
  184. ########## DEBUG ##########
  185. 'this-is-a-test'
  186. ###########################
  187. EXPECTED;
  188. $expected = sprintf($expectedText, Debugger::trimPath(__FILE__), __LINE__ - 9);
  189. $this->assertSame($expected, $result);
  190. ob_start();
  191. $value = '<div>this-is-a-test</div>';
  192. $this->assertSame($value, debug($value, true));
  193. $result = ob_get_clean();
  194. $this->assertStringContainsString('<div class="cake-debug-output', $result);
  195. $this->assertStringContainsString('this-is-a-test', $result);
  196. ob_start();
  197. debug('<div>this-is-a-test</div>', true, true);
  198. $result = ob_get_clean();
  199. $expected = <<<EXPECTED
  200. <div class="cake-debug-output cake-debug" style="direction:ltr">
  201. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  202. EXPECTED;
  203. $expected = sprintf($expected, Debugger::trimPath(__FILE__), __LINE__ - 6);
  204. $this->assertStringContainsString($expected, $result);
  205. ob_start();
  206. debug('<div>this-is-a-test</div>', true, false);
  207. $result = ob_get_clean();
  208. $this->assertStringNotContainsString('(line', $result);
  209. }
  210. /**
  211. * test pr()
  212. *
  213. * @return void
  214. */
  215. public function testPr()
  216. {
  217. ob_start();
  218. $this->assertTrue(pr(true));
  219. $result = ob_get_clean();
  220. $expected = "\n1\n\n";
  221. $this->assertSame($expected, $result);
  222. ob_start();
  223. $this->assertFalse(pr(false));
  224. $result = ob_get_clean();
  225. $expected = "\n\n\n";
  226. $this->assertSame($expected, $result);
  227. ob_start();
  228. $this->assertNull(pr(null));
  229. $result = ob_get_clean();
  230. $expected = "\n\n\n";
  231. $this->assertSame($expected, $result);
  232. ob_start();
  233. $this->assertSame(123, pr(123));
  234. $result = ob_get_clean();
  235. $expected = "\n123\n\n";
  236. $this->assertSame($expected, $result);
  237. ob_start();
  238. pr('123');
  239. $result = ob_get_clean();
  240. $expected = "\n123\n\n";
  241. $this->assertSame($expected, $result);
  242. ob_start();
  243. pr('this is a test');
  244. $result = ob_get_clean();
  245. $expected = "\nthis is a test\n\n";
  246. $this->assertSame($expected, $result);
  247. ob_start();
  248. pr(['this' => 'is', 'a' => 'test', 123 => 456]);
  249. $result = ob_get_clean();
  250. $expected = "\nArray\n(\n [this] => is\n [a] => test\n [123] => 456\n)\n\n";
  251. $this->assertSame($expected, $result);
  252. }
  253. /**
  254. * test pj()
  255. *
  256. * @return void
  257. */
  258. public function testPj()
  259. {
  260. ob_start();
  261. $this->assertTrue(pj(true));
  262. $result = ob_get_clean();
  263. $expected = "\ntrue\n\n";
  264. $this->assertSame($expected, $result);
  265. ob_start();
  266. $this->assertFalse(pj(false));
  267. $result = ob_get_clean();
  268. $expected = "\nfalse\n\n";
  269. $this->assertSame($expected, $result);
  270. ob_start();
  271. $this->assertNull(pj(null));
  272. $result = ob_get_clean();
  273. $expected = "\nnull\n\n";
  274. $this->assertSame($expected, $result);
  275. ob_start();
  276. $this->assertSame(123, pj(123));
  277. $result = ob_get_clean();
  278. $expected = "\n123\n\n";
  279. $this->assertSame($expected, $result);
  280. ob_start();
  281. pj('123');
  282. $result = ob_get_clean();
  283. $expected = "\n\"123\"\n\n";
  284. $this->assertSame($expected, $result);
  285. ob_start();
  286. pj('this is a test');
  287. $result = ob_get_clean();
  288. $expected = "\n\"this is a test\"\n\n";
  289. $this->assertSame($expected, $result);
  290. ob_start();
  291. $value = ['this' => 'is', 'a' => 'test', 123 => 456];
  292. $this->assertSame($value, pj($value));
  293. $result = ob_get_clean();
  294. $expected = "\n{\n \"this\": \"is\",\n \"a\": \"test\",\n \"123\": 456\n}\n\n";
  295. $this->assertSame($expected, $result);
  296. }
  297. /**
  298. * Test splitting plugin names.
  299. *
  300. * @return void
  301. */
  302. public function testPluginSplit()
  303. {
  304. $result = pluginSplit('Something.else');
  305. $this->assertSame(['Something', 'else'], $result);
  306. $result = pluginSplit('Something.else.more.dots');
  307. $this->assertSame(['Something', 'else.more.dots'], $result);
  308. $result = pluginSplit('Somethingelse');
  309. $this->assertSame([null, 'Somethingelse'], $result);
  310. $result = pluginSplit('Something.else', true);
  311. $this->assertSame(['Something.', 'else'], $result);
  312. $result = pluginSplit('Something.else.more.dots', true);
  313. $this->assertSame(['Something.', 'else.more.dots'], $result);
  314. $result = pluginSplit('Post', false, 'Blog');
  315. $this->assertSame(['Blog', 'Post'], $result);
  316. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  317. $this->assertSame(['Blog', 'Post'], $result);
  318. }
  319. /**
  320. * test namespaceSplit
  321. *
  322. * @return void
  323. */
  324. public function testNamespaceSplit()
  325. {
  326. $result = namespaceSplit('Something');
  327. $this->assertSame(['', 'Something'], $result);
  328. $result = namespaceSplit('\Something');
  329. $this->assertSame(['', 'Something'], $result);
  330. $result = namespaceSplit('Cake\Something');
  331. $this->assertSame(['Cake', 'Something'], $result);
  332. $result = namespaceSplit('Cake\Test\Something');
  333. $this->assertSame(['Cake\Test', 'Something'], $result);
  334. }
  335. /**
  336. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  337. *
  338. * @return void
  339. */
  340. public function testStackTrace()
  341. {
  342. ob_start();
  343. [$r, $expected] = [stackTrace(), \Cake\Error\Debugger::trace()];
  344. $result = ob_get_clean();
  345. $this->assertSame($expected, $result);
  346. $opts = ['args' => true];
  347. ob_start();
  348. [$r, $expected] = [stackTrace($opts), \Cake\Error\Debugger::trace($opts)];
  349. $result = ob_get_clean();
  350. $this->assertSame($expected, $result);
  351. }
  352. /**
  353. * Tests that the collection() method is a shortcut for new Collection
  354. *
  355. * @return void
  356. */
  357. public function testCollection()
  358. {
  359. $items = [1, 2, 3];
  360. $collection = collection($items);
  361. $this->assertInstanceOf(Collection::class, $collection);
  362. $this->assertSame($items, $collection->toArray());
  363. }
  364. /**
  365. * Test that works in tandem with testEventManagerReset2 to
  366. * test the EventManager reset.
  367. *
  368. * The return value is passed to testEventManagerReset2 as
  369. * an arguments.
  370. *
  371. * @return \Cake\Event\EventManager
  372. */
  373. public function testEventManagerReset1()
  374. {
  375. $eventManager = EventManager::instance();
  376. $this->assertInstanceOf(EventManager::class, $eventManager);
  377. return $eventManager;
  378. }
  379. /**
  380. * Test if the EventManager is reset between tests.
  381. *
  382. * @depends testEventManagerReset1
  383. * @return void
  384. */
  385. public function testEventManagerReset2($prevEventManager)
  386. {
  387. $this->assertInstanceOf(EventManager::class, $prevEventManager);
  388. $this->assertNotSame($prevEventManager, EventManager::instance());
  389. }
  390. }