PhpErrorTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 4.4.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Error;
  17. use Cake\Error\PhpError;
  18. use Cake\TestSuite\TestCase;
  19. use PHPUnit\Framework\Attributes\DataProvider;
  20. class PhpErrorTest extends TestCase
  21. {
  22. public function testBasicGetters(): void
  23. {
  24. $error = new PhpError(E_ERROR, 'something bad');
  25. $this->assertEquals(E_ERROR, $error->getCode());
  26. $this->assertEquals('something bad', $error->getMessage());
  27. $this->assertNull($error->getFile());
  28. $this->assertNull($error->getLine());
  29. $this->assertEquals([], $error->getTrace());
  30. $this->assertEquals('', $error->getTraceAsString());
  31. }
  32. public static function errorCodeProvider(): array
  33. {
  34. // [php error code, label, log-level]
  35. return [
  36. [E_ERROR, 'error', LOG_ERR],
  37. [E_WARNING, 'warning', LOG_WARNING],
  38. [E_NOTICE, 'notice', LOG_NOTICE],
  39. [E_STRICT, 'strict', LOG_NOTICE],
  40. [E_STRICT, 'strict', LOG_NOTICE],
  41. [E_USER_DEPRECATED, 'deprecated', LOG_NOTICE],
  42. ];
  43. }
  44. #[DataProvider('errorCodeProvider')]
  45. public function testMappings($phpCode, $label, $logLevel): void
  46. {
  47. $error = new PhpError($phpCode, 'something bad');
  48. $this->assertEquals($phpCode, $error->getCode());
  49. $this->assertEquals($label, $error->getLabel());
  50. $this->assertEquals($logLevel, $error->getLogLevel());
  51. }
  52. public function testGetTraceAsString(): void
  53. {
  54. $trace = [
  55. ['file' => 'a.php', 'line' => 10, 'reference' => 'TestObject::a()'],
  56. ['file' => 'b.php', 'line' => 5, 'reference' => '[main]'],
  57. ];
  58. $error = new PhpError(E_ERROR, 'something bad', __FILE__, __LINE__, $trace);
  59. $this->assertEquals($trace, $error->getTrace());
  60. $expected = [
  61. 'TestObject::a() a.php, line 10',
  62. '[main] b.php, line 5',
  63. ];
  64. $this->assertEquals(implode("\n", $expected), $error->getTraceAsString());
  65. $this->assertEquals('error', $error->getLabel());
  66. }
  67. }