FunctionsTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\TestSuite\TestCase;
  20. use stdClass;
  21. /**
  22. * Test cases for functions in Core\functions.php
  23. */
  24. class FunctionsTest extends TestCase
  25. {
  26. /**
  27. * Test cases for env()
  28. */
  29. public function testEnv(): void
  30. {
  31. $_ENV['DOES_NOT_EXIST'] = null;
  32. $this->assertNull(env('DOES_NOT_EXIST'));
  33. $this->assertSame('default', env('DOES_NOT_EXIST', 'default'));
  34. $_ENV['DOES_EXIST'] = 'some value';
  35. $this->assertSame('some value', env('DOES_EXIST'));
  36. $this->assertSame('some value', env('DOES_EXIST', 'default'));
  37. $_ENV['EMPTY_VALUE'] = '';
  38. $this->assertSame('', env('EMPTY_VALUE'));
  39. $this->assertSame('', env('EMPTY_VALUE', 'default'));
  40. $_ENV['ZERO'] = '0';
  41. $this->assertSame('0', env('ZERO'));
  42. $this->assertSame('0', env('ZERO', '1'));
  43. $this->assertSame('', env('DOCUMENT_ROOT'));
  44. $this->assertStringContainsString('phpunit', env('PHP_SELF'));
  45. }
  46. /**
  47. * Test cases for h()
  48. *
  49. * @dataProvider hInputProvider
  50. * @param mixed $value
  51. * @param mixed $expected
  52. */
  53. public function testH($value, $expected): void
  54. {
  55. $result = h($value);
  56. $this->assertSame($expected, $result);
  57. }
  58. public function hInputProvider(): array
  59. {
  60. return [
  61. ['i am clean', 'i am clean'],
  62. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  63. [null, null],
  64. [1, 1],
  65. [1.1, 1.1],
  66. [new stdClass(), '(object)stdClass'],
  67. [new Response(), ''],
  68. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  69. ];
  70. }
  71. /**
  72. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  73. */
  74. public function testDeprecationWarningEnabled(): void
  75. {
  76. $this->expectDeprecation();
  77. $this->expectDeprecationMessageMatches('/This is going away\n(.*?)[\/\\\]FunctionsTest.php, line\: \d+/');
  78. $this->withErrorReporting(E_ALL, function (): void {
  79. deprecationWarning('This is going away', 2);
  80. });
  81. }
  82. /**
  83. * Test deprecation warnings trigger only once
  84. */
  85. public function testDeprecationWarningTriggerOnlyOnce(): void
  86. {
  87. $message = 'Test deprecation warnings trigger only once';
  88. try {
  89. $this->withErrorReporting(E_ALL, function () use ($message): void {
  90. deprecationWarning($message);
  91. });
  92. $this->fail();
  93. } catch (\Exception $e) {
  94. $this->assertStringContainsString($message, $e->getMessage());
  95. $this->assertStringContainsString('TestCase.php', $e->getMessage());
  96. }
  97. $this->withErrorReporting(E_ALL, function () use ($message): void {
  98. deprecationWarning($message);
  99. });
  100. }
  101. /**
  102. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  103. */
  104. public function testDeprecationWarningEnabledDefaultFrame(): void
  105. {
  106. $this->expectDeprecation();
  107. $this->expectDeprecationMessageMatches('/This is going away too\n(.*?)[\/\\\]TestCase.php, line\: \d+/');
  108. $this->withErrorReporting(E_ALL, function (): void {
  109. deprecationWarning('This is going away too');
  110. });
  111. }
  112. /**
  113. * Test no error when deprecation matches ignore paths.
  114. */
  115. public function testDeprecationWarningPathDisabled(): void
  116. {
  117. $this->expectNotToPerformAssertions();
  118. Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
  119. $this->withErrorReporting(E_ALL, function (): void {
  120. deprecationWarning('This will be gone soon');
  121. });
  122. }
  123. /**
  124. * Test no error when deprecated level is off.
  125. */
  126. public function testDeprecationWarningLevelDisabled(): void
  127. {
  128. $this->expectNotToPerformAssertions();
  129. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
  130. deprecationWarning('This is leaving');
  131. });
  132. }
  133. /**
  134. * Test error messages coming out when warning level is on.
  135. */
  136. public function testTriggerWarningEnabled(): void
  137. {
  138. $this->expectWarning();
  139. $this->expectWarningMessageMatches('/This will be gone one day - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  140. $this->withErrorReporting(E_ALL, function (): void {
  141. triggerWarning('This will be gone one day');
  142. $this->assertTrue(true);
  143. });
  144. }
  145. /**
  146. * Test no error when warning level is off.
  147. */
  148. public function testTriggerWarningLevelDisabled(): void
  149. {
  150. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function (): void {
  151. triggerWarning('This was a mistake.');
  152. $this->assertTrue(true);
  153. });
  154. }
  155. /**
  156. * testing getTypeName()
  157. */
  158. public function testgetTypeName(): void
  159. {
  160. $this->assertSame('stdClass', getTypeName(new \stdClass()));
  161. $this->assertSame('array', getTypeName([]));
  162. $this->assertSame('string', getTypeName(''));
  163. }
  164. }