| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Core;
- use Cake\Core\Configure;
- use Cake\Http\Response;
- use Cake\TestSuite\TestCase;
- use Exception;
- use stdClass;
- /**
- * Test cases for functions in Core\functions.php
- */
- class FunctionsTest extends TestCase
- {
- /**
- * Test cases for env()
- */
- public function testEnv(): void
- {
- $_ENV['DOES_NOT_EXIST'] = null;
- $this->assertNull(env('DOES_NOT_EXIST'));
- $this->assertSame('default', env('DOES_NOT_EXIST', 'default'));
- $_ENV['DOES_EXIST'] = 'some value';
- $this->assertSame('some value', env('DOES_EXIST'));
- $this->assertSame('some value', env('DOES_EXIST', 'default'));
- $_ENV['EMPTY_VALUE'] = '';
- $this->assertSame('', env('EMPTY_VALUE'));
- $this->assertSame('', env('EMPTY_VALUE', 'default'));
- $_ENV['ZERO'] = '0';
- $this->assertSame('0', env('ZERO'));
- $this->assertSame('0', env('ZERO', '1'));
- $this->assertSame('', env('DOCUMENT_ROOT'));
- $this->assertStringContainsString('phpunit', env('PHP_SELF'));
- }
- /**
- * Test cases for h()
- *
- * @dataProvider hInputProvider
- * @param mixed $value
- * @param mixed $expected
- */
- public function testH($value, $expected): void
- {
- $result = h($value);
- $this->assertSame($expected, $result);
- }
- public function hInputProvider(): array
- {
- return [
- ['i am clean', 'i am clean'],
- ['i "need" escaping', 'i "need" escaping'],
- [null, null],
- [1, 1],
- [1.1, 1.1],
- [new stdClass(), '(object)stdClass'],
- [new Response(), ''],
- [['clean', '"clean-me'], ['clean', '"clean-me']],
- ];
- }
- /**
- * Test error messages coming out when deprecated level is on, manually setting the stack frame
- */
- public function testDeprecationWarningEnabled(): void
- {
- $this->expectDeprecation();
- $this->expectDeprecationMessageMatches('/Since 5.0.0: This is going away - (.*?)[\/\\\]FunctionsTest.php, line\: \d+/');
- $this->withErrorReporting(E_ALL, function (): void {
- deprecationWarning('5.0.0', 'This is going away', 2);
- });
- }
- /**
- * Test deprecation warnings trigger only once
- */
- public function testDeprecationWarningTriggerOnlyOnce(): void
- {
- $message = 'Test deprecation warnings trigger only once';
- try {
- $this->withErrorReporting(E_ALL, function () use ($message): void {
- deprecationWarning('5.0.0', $message);
- });
- $this->fail();
- } catch (Exception $e) {
- $this->assertStringContainsString($message, $e->getMessage());
- $this->assertStringContainsString('TestCase.php', $e->getMessage());
- }
- $this->withErrorReporting(E_ALL, function () use ($message): void {
- deprecationWarning('5.0.0', $message);
- });
- }
- /**
- * Test error messages coming out when deprecated level is on, not setting the stack frame manually
- */
- public function testDeprecationWarningEnabledDefaultFrame(): void
- {
- $this->expectDeprecation();
- $this->expectDeprecationMessageMatches('/Since 5.0.0: This is going away too - (.*?)[\/\\\]TestCase.php, line\: \d+/');
- $this->withErrorReporting(E_ALL, function (): void {
- deprecationWarning('5.0.0', 'This is going away too');
- });
- }
- /**
- * Test no error when deprecation matches ignore paths.
- */
- public function testDeprecationWarningPathDisabled(): void
- {
- $this->expectNotToPerformAssertions();
- Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
- $this->withErrorReporting(E_ALL, function (): void {
- deprecationWarning('5.0.0', 'This will be gone soon');
- });
- }
- /**
- * Test no error when deprecated level is off.
- */
- public function testDeprecationWarningLevelDisabled(): void
- {
- $this->expectNotToPerformAssertions();
- $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
- deprecationWarning('5.0.0', 'This is leaving');
- });
- }
- /**
- * Test error messages coming out when warning level is on.
- */
- public function testTriggerWarningEnabled(): void
- {
- $this->expectWarning();
- $this->expectWarningMessageMatches('/This will be gone one day - (.*?)[\/\\\]TestCase.php, line\: \d+/');
- $this->withErrorReporting(E_ALL, function (): void {
- triggerWarning('This will be gone one day');
- $this->assertTrue(true);
- });
- }
- /**
- * Test no error when warning level is off.
- */
- public function testTriggerWarningLevelDisabled(): void
- {
- $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function (): void {
- triggerWarning('This was a mistake.');
- $this->assertTrue(true);
- });
- }
- }
|