FunctionsTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Http\Response;
  18. use Cake\TestSuite\TestCase;
  19. use PHPUnit\Framework\Error\Deprecated;
  20. use PHPUnit\Framework\Error\Warning;
  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()
  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. * @return void
  50. * @dataProvider hInputProvider
  51. */
  52. public function testH($value, $expected)
  53. {
  54. $result = h($value);
  55. $this->assertSame($expected, $result);
  56. }
  57. public function hInputProvider()
  58. {
  59. return [
  60. ['i am clean', 'i am clean'],
  61. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  62. [null, null],
  63. [1, 1],
  64. [1.1, 1.1],
  65. [new \stdClass(), '(object)stdClass'],
  66. [new Response(), ''],
  67. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  68. ];
  69. }
  70. /**
  71. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  72. */
  73. public function testDeprecationWarningEnabled()
  74. {
  75. $this->expectException(Deprecated::class);
  76. $this->expectExceptionMessageRegExp('/This is going away - (.*?)[\/\\\]FunctionsTest.php, line\: \d+/');
  77. $this->withErrorReporting(E_ALL, function () {
  78. deprecationWarning('This is going away', 2);
  79. });
  80. }
  81. /**
  82. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  83. *
  84. */
  85. public function testDeprecationWarningEnabledDefaultFrame()
  86. {
  87. $this->expectException(Deprecated::class);
  88. $this->expectExceptionMessageRegExp('/This is going away - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  89. $this->withErrorReporting(E_ALL, function () {
  90. deprecationWarning('This is going away');
  91. });
  92. }
  93. /**
  94. * Test no error when deprecated level is off.
  95. *
  96. * @return void
  97. */
  98. public function testDeprecationWarningLevelDisabled()
  99. {
  100. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function () {
  101. $this->assertNull(deprecationWarning('This is going away'));
  102. });
  103. }
  104. /**
  105. * Test error messages coming out when warning level is on.
  106. *
  107. */
  108. public function testTriggerWarningEnabled()
  109. {
  110. $this->expectException(Warning::class);
  111. $this->expectExceptionMessageRegExp('/This is going away - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  112. $this->withErrorReporting(E_ALL, function () {
  113. triggerWarning('This is going away');
  114. });
  115. }
  116. /**
  117. * Test no error when warning level is off.
  118. *
  119. * @return void
  120. */
  121. public function testTriggerWarningLevelDisabled()
  122. {
  123. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function () {
  124. $this->assertNull(triggerWarning('This is going away'));
  125. });
  126. }
  127. /**
  128. * testing getTypeName()
  129. *
  130. * @return void
  131. */
  132. public function testgetTypeName()
  133. {
  134. $this->assertSame('stdClass', getTypeName(new \stdClass()));
  135. $this->assertSame('array', getTypeName([]));
  136. $this->assertSame('string', getTypeName(''));
  137. }
  138. }