FunctionsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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->expectExceptionMessageMatches('/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. public function testDeprecationWarningEnabledDefaultFrame()
  85. {
  86. $this->expectException(Deprecated::class);
  87. $this->expectExceptionMessageMatches('/This is going away - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  88. $this->withErrorReporting(E_ALL, function () {
  89. deprecationWarning('This is going away');
  90. });
  91. }
  92. /**
  93. * Test no error when deprecated level is off.
  94. *
  95. * @return void
  96. */
  97. public function testDeprecationWarningLevelDisabled()
  98. {
  99. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function () {
  100. $this->assertNull(deprecationWarning('This is going away'));
  101. });
  102. }
  103. /**
  104. * Test error messages coming out when warning level is on.
  105. */
  106. public function testTriggerWarningEnabled()
  107. {
  108. $this->expectException(Warning::class);
  109. $this->expectExceptionMessageMatches('/This is going away - (.*?)[\/\\\]TestCase.php, line\: \d+/');
  110. $this->withErrorReporting(E_ALL, function () {
  111. triggerWarning('This is going away');
  112. });
  113. }
  114. /**
  115. * Test no error when warning level is off.
  116. *
  117. * @return void
  118. */
  119. public function testTriggerWarningLevelDisabled()
  120. {
  121. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function () {
  122. $this->assertNull(triggerWarning('This is going away'));
  123. });
  124. }
  125. /**
  126. * testing getTypeName()
  127. *
  128. * @return void
  129. */
  130. public function testgetTypeName()
  131. {
  132. $this->assertSame('stdClass', getTypeName(new \stdClass()));
  133. $this->assertSame('array', getTypeName([]));
  134. $this->assertSame('string', getTypeName(''));
  135. }
  136. }