FunctionsTest.php 5.5 KB

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