FunctionsTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\TestSuite\TestCase;
  17. /**
  18. * Test cases for functions in Core\functions.php
  19. */
  20. class FunctionsTest extends TestCase
  21. {
  22. /**
  23. * Test cases for env()
  24. */
  25. public function testEnv()
  26. {
  27. $_ENV['DOES_NOT_EXIST'] = null;
  28. $this->assertNull(env('DOES_NOT_EXIST'));
  29. $this->assertEquals('default', env('DOES_NOT_EXIST', 'default'));
  30. $_ENV['DOES_EXIST'] = 'some value';
  31. $this->assertEquals('some value', env('DOES_EXIST'));
  32. $this->assertEquals('some value', env('DOES_EXIST', 'default'));
  33. $_ENV['EMPTY_VALUE'] = '';
  34. $this->assertEquals('', env('EMPTY_VALUE'));
  35. $this->assertEquals('', env('EMPTY_VALUE', 'default'));
  36. $_ENV['ZERO'] = '0';
  37. $this->assertEquals('0', env('ZERO'));
  38. $this->assertEquals('0', env('ZERO', '1'));
  39. }
  40. /**
  41. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  42. *
  43. * @expectedException PHPUnit\Framework\Error\Deprecated
  44. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]FunctionsTest.php, line\: \d+/
  45. */
  46. public function testDeprecationWarningEnabled()
  47. {
  48. $this->withErrorReporting(E_ALL, function () {
  49. deprecationWarning('This is going away', 2);
  50. });
  51. }
  52. /**
  53. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  54. *
  55. * @expectedException PHPUnit\Framework\Error\Deprecated
  56. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]TestCase.php, line\: \d+/
  57. */
  58. public function testDeprecationWarningEnabledDefaultFrame()
  59. {
  60. $this->withErrorReporting(E_ALL, function () {
  61. deprecationWarning('This is going away');
  62. });
  63. }
  64. /**
  65. * Test no error when deprecated level is off.
  66. *
  67. * @return void
  68. */
  69. public function testDeprecationWarningLevelDisabled()
  70. {
  71. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function () {
  72. $this->assertNull(deprecationWarning('This is going away'));
  73. });
  74. }
  75. /**
  76. * Test error messages coming out when warning level is on.
  77. *
  78. * @expectedException PHPUnit\Framework\Error\Warning
  79. * @expectedExceptionMessageRegExp /This is going away - (.*?)[\/\\]TestCase.php, line\: \d+/
  80. */
  81. public function testTriggerWarningEnabled()
  82. {
  83. $this->withErrorReporting(E_ALL, function () {
  84. triggerWarning('This is going away');
  85. });
  86. }
  87. /**
  88. * Test no error when warning level is off.
  89. *
  90. * @return void
  91. */
  92. public function testTriggerWarningLevelDisabled()
  93. {
  94. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function () {
  95. $this->assertNull(triggerWarning('This is going away'));
  96. });
  97. }
  98. /**
  99. * testing getTypeName()
  100. *
  101. * @return void
  102. */
  103. public function testgetTypeName()
  104. {
  105. $this->assertEquals('stdClass', getTypeName(new \stdClass()));
  106. $this->assertEquals('array', getTypeName([]));
  107. $this->assertEquals('string', getTypeName(''));
  108. }
  109. }