FunctionsTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Core;
  16. use Cake\Http\Response;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Test cases for functions in Core\functions.php
  20. */
  21. class FunctionsTest extends TestCase
  22. {
  23. /**
  24. * Test cases for env()
  25. */
  26. public function testEnv()
  27. {
  28. $_ENV['DOES_NOT_EXIST'] = null;
  29. $this->assertNull(env('DOES_NOT_EXIST'));
  30. $this->assertEquals('default', env('DOES_NOT_EXIST', 'default'));
  31. $_ENV['DOES_EXIST'] = 'some value';
  32. $this->assertEquals('some value', env('DOES_EXIST'));
  33. $this->assertEquals('some value', env('DOES_EXIST', 'default'));
  34. $_ENV['EMPTY_VALUE'] = '';
  35. $this->assertEquals('', env('EMPTY_VALUE'));
  36. $this->assertEquals('', env('EMPTY_VALUE', 'default'));
  37. $_ENV['ZERO'] = '0';
  38. $this->assertEquals('0', env('ZERO'));
  39. $this->assertEquals('0', env('ZERO', '1'));
  40. }
  41. /**
  42. * Test cases for h()
  43. *
  44. * @return void
  45. * @dataProvider hInputProvider
  46. */
  47. public function testH($value, $expected)
  48. {
  49. $result = h($value);
  50. $this->assertSame($expected, $result);
  51. }
  52. public function hInputProvider()
  53. {
  54. return [
  55. ['i am clean', 'i am clean'],
  56. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  57. [null, null],
  58. [1, 1],
  59. [1.1, 1.1],
  60. [new \stdClass, '(object)stdClass'],
  61. [new Response(), ''],
  62. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  63. ];
  64. }
  65. /**
  66. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  67. *
  68. * @expectedException PHPUnit\Framework\Error\Deprecated
  69. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]FunctionsTest.php, line\: \d+/
  70. */
  71. public function testDeprecationWarningEnabled()
  72. {
  73. $this->withErrorReporting(E_ALL, function () {
  74. deprecationWarning('This is going away', 2);
  75. });
  76. }
  77. /**
  78. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  79. *
  80. * @expectedException PHPUnit\Framework\Error\Deprecated
  81. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]TestCase.php, line\: \d+/
  82. */
  83. public function testDeprecationWarningEnabledDefaultFrame()
  84. {
  85. $this->withErrorReporting(E_ALL, function () {
  86. deprecationWarning('This is going away');
  87. });
  88. }
  89. /**
  90. * Test no error when deprecated level is off.
  91. *
  92. * @return void
  93. */
  94. public function testDeprecationWarningLevelDisabled()
  95. {
  96. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function () {
  97. $this->assertNull(deprecationWarning('This is going away'));
  98. });
  99. }
  100. /**
  101. * Test error messages coming out when warning level is on.
  102. *
  103. * @expectedException PHPUnit\Framework\Error\Warning
  104. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]TestCase.php, line\: \d+/
  105. */
  106. public function testTriggerWarningEnabled()
  107. {
  108. $this->withErrorReporting(E_ALL, function () {
  109. triggerWarning('This is going away');
  110. });
  111. }
  112. /**
  113. * Test no error when warning level is off.
  114. *
  115. * @return void
  116. */
  117. public function testTriggerWarningLevelDisabled()
  118. {
  119. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function () {
  120. $this->assertNull(triggerWarning('This is going away'));
  121. });
  122. }
  123. /**
  124. * testing getTypeName()
  125. *
  126. * @return void
  127. */
  128. public function testgetTypeName()
  129. {
  130. $this->assertEquals('stdClass', getTypeName(new \stdClass()));
  131. $this->assertEquals('array', getTypeName([]));
  132. $this->assertEquals('string', getTypeName(''));
  133. }
  134. }