FunctionsTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 - (.*?)[\/\\\]FunctionsTest.php, line\: \d+/');
  78. $this->withErrorReporting(E_ALL, function (): void {
  79. deprecationWarning('This is going away', 2);
  80. });
  81. }
  82. /**
  83. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  84. */
  85. public function testDeprecationWarningEnabledDefaultFrame(): void
  86. {
  87. $this->expectDeprecation();
  88. $this->expectDeprecationMessageMatches('/This is going away too - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  89. $this->withErrorReporting(E_ALL, function (): void {
  90. deprecationWarning('This is going away too');
  91. });
  92. }
  93. /**
  94. * Test no error when deprecation matches ignore paths.
  95. */
  96. public function testDeprecationWarningPathDisabled(): void
  97. {
  98. $this->expectNotToPerformAssertions();
  99. Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
  100. $this->withErrorReporting(E_ALL, function (): void {
  101. deprecationWarning('This will be gone soon');
  102. });
  103. }
  104. /**
  105. * Test no error when deprecated level is off.
  106. */
  107. public function testDeprecationWarningLevelDisabled(): void
  108. {
  109. $this->expectNotToPerformAssertions();
  110. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
  111. deprecationWarning('This is leaving');
  112. });
  113. }
  114. /**
  115. * Test error messages coming out when warning level is on.
  116. */
  117. public function testTriggerWarningEnabled(): void
  118. {
  119. $this->expectWarning();
  120. $this->expectWarningMessageMatches('/This will be gone one day - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  121. $this->withErrorReporting(E_ALL, function (): void {
  122. triggerWarning('This will be gone one day');
  123. $this->assertTrue(true);
  124. });
  125. }
  126. /**
  127. * Test no error when warning level is off.
  128. */
  129. public function testTriggerWarningLevelDisabled(): void
  130. {
  131. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function (): void {
  132. triggerWarning('This was a mistake.');
  133. $this->assertTrue(true);
  134. });
  135. }
  136. /**
  137. * testing getTypeName()
  138. */
  139. public function testgetTypeName(): void
  140. {
  141. $this->assertSame('stdClass', getTypeName(new \stdClass()));
  142. $this->assertSame('array', getTypeName([]));
  143. $this->assertSame('string', getTypeName(''));
  144. }
  145. }