FunctionsTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /**
  20. * Test cases for functions in Core\functions.php
  21. */
  22. class FunctionsTest extends TestCase
  23. {
  24. /**
  25. * Test cases for env()
  26. */
  27. public function testEnv()
  28. {
  29. $_ENV['DOES_NOT_EXIST'] = null;
  30. $this->assertNull(env('DOES_NOT_EXIST'));
  31. $this->assertEquals('default', env('DOES_NOT_EXIST', 'default'));
  32. $_ENV['DOES_EXIST'] = 'some value';
  33. $this->assertEquals('some value', env('DOES_EXIST'));
  34. $this->assertEquals('some value', env('DOES_EXIST', 'default'));
  35. $_ENV['EMPTY_VALUE'] = '';
  36. $this->assertEquals('', env('EMPTY_VALUE'));
  37. $this->assertEquals('', env('EMPTY_VALUE', 'default'));
  38. $_ENV['ZERO'] = '0';
  39. $this->assertEquals('0', env('ZERO'));
  40. $this->assertEquals('0', env('ZERO', '1'));
  41. }
  42. /**
  43. * Test cases for h()
  44. *
  45. * @return void
  46. * @dataProvider hInputProvider
  47. */
  48. public function testH($value, $expected)
  49. {
  50. $result = h($value);
  51. $this->assertSame($expected, $result);
  52. }
  53. public function hInputProvider()
  54. {
  55. return [
  56. ['i am clean', 'i am clean'],
  57. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  58. [null, null],
  59. [1, 1],
  60. [1.1, 1.1],
  61. [new \stdClass, '(object)stdClass'],
  62. [new Response(), ''],
  63. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  64. ];
  65. }
  66. /**
  67. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  68. *
  69. * @expectedException PHPUnit\Framework\Error\Deprecated
  70. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]FunctionsTest.php, line\: \d+/
  71. */
  72. public function testDeprecationWarningEnabled()
  73. {
  74. $this->withErrorReporting(E_ALL, function () {
  75. deprecationWarning('This is going away', 2);
  76. });
  77. }
  78. /**
  79. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  80. *
  81. * @expectedException PHPUnit\Framework\Error\Deprecated
  82. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]TestCase.php, line\: \d+/
  83. */
  84. public function testDeprecationWarningEnabledDefaultFrame()
  85. {
  86. $this->withErrorReporting(E_ALL, function () {
  87. deprecationWarning('This is going away');
  88. });
  89. }
  90. /**
  91. * Test no error when deprecated level is off.
  92. *
  93. * @return void
  94. */
  95. public function testDeprecationWarningLevelDisabled()
  96. {
  97. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function () {
  98. $this->assertNull(deprecationWarning('This is going away'));
  99. });
  100. }
  101. /**
  102. * Test error messages coming out when warning level is on.
  103. *
  104. * @expectedException PHPUnit\Framework\Error\Warning
  105. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]TestCase.php, line\: \d+/
  106. */
  107. public function testTriggerWarningEnabled()
  108. {
  109. $this->withErrorReporting(E_ALL, function () {
  110. triggerWarning('This is going away');
  111. });
  112. }
  113. /**
  114. * Test no error when warning level is off.
  115. *
  116. * @return void
  117. */
  118. public function testTriggerWarningLevelDisabled()
  119. {
  120. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function () {
  121. $this->assertNull(triggerWarning('This is going away'));
  122. });
  123. }
  124. /**
  125. * testing getTypeName()
  126. *
  127. * @return void
  128. */
  129. public function testgetTypeName()
  130. {
  131. $this->assertEquals('stdClass', getTypeName(new \stdClass()));
  132. $this->assertEquals('array', getTypeName([]));
  133. $this->assertEquals('string', getTypeName(''));
  134. }
  135. }