Browse Source

Add tests for PhpError and file headers.

Mark Story 4 years ago
parent
commit
2e0682f865
3 changed files with 158 additions and 5 deletions
  1. 13 0
      src/Error/ErrorRendererInterface.php
  2. 71 5
      src/Error/PhpError.php
  3. 74 0
      tests/TestCase/Error/PhpErrorTest.php

+ 13 - 0
src/Error/ErrorRendererInterface.php

@@ -1,6 +1,19 @@
 <?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         4.4.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
 namespace Cake\Error;
 
 /**

+ 71 - 5
src/Error/PhpError.php

@@ -1,6 +1,19 @@
 <?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         4.4.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
 namespace Cake\Error;
 
 /**
@@ -36,22 +49,53 @@ class PhpError
     private $trace;
 
     /**
+     * @var array<int, string>
+     */
+    private $levelMap = [
+        E_PARSE => 'error',
+        E_ERROR => 'error',
+        E_CORE_ERROR => 'error',
+        E_COMPILE_ERROR => 'error',
+        E_USER_ERROR => 'error',
+        E_WARNING => 'warning',
+        E_USER_WARNING => 'warning',
+        E_COMPILE_WARNING => 'warning',
+        E_RECOVERABLE_ERROR => 'warning',
+        E_NOTICE => 'notice',
+        E_USER_NOTICE => 'notice',
+        E_STRICT => 'strict',
+        E_DEPRECATED => 'deprecated',
+        E_USER_DEPRECATED => 'deprecated',
+    ];
+
+    /**
+     * @var array<string, int>
+     */
+    private $logMap = [
+        'error' => LOG_ERR,
+        'warning' => LOG_WARNING,
+        'notice' => LOG_NOTICE,
+        'strict' => LOG_NOTICE,
+        'deprecated' => LOG_NOTICE,
+    ];
+
+    /**
      * Constructor
      *
-     * @param int $level The PHP error constant
+     * @param int $code The PHP error code constant
      * @param string $message The error message.
      * @param string|null $file The filename of the error.
      * @param int|null $line The line number for the error.
      * @param array $trace The backtrace for the error.
      */
     public function __construct(
-        int $level,
+        int $code,
         string $message,
         ?string $file = null,
         ?int $line = null,
         array $trace = [],
     ) {
-        $this->level = $level;
+        $this->code = $code;
         $this->message = $message;
         $this->file = $file;
         $this->line = $line;
@@ -63,9 +107,31 @@ class PhpError
      *
      * @return string
      */
-    public function getLevel(): int
+    public function getCode(): int
+    {
+        return $this->code;
+    }
+
+    /**
+     * Get the mapped LOG_ constant.
+     *
+     * @return int
+     */
+    public function getLogLevel(): int
+    {
+        $label = $this->getLabel();
+
+        return $this->logMap[$label] ?? 'error';
+    }
+
+    /**
+     * Get the error code label
+     *
+     * @return string
+     */
+    public function getLabel(): string
     {
-        return $this->level;
+        return $this->levelMap[$this->code] ?? 'error';
     }
 
     /**

+ 74 - 0
tests/TestCase/Error/PhpErrorTest.php

@@ -0,0 +1,74 @@
+<?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         4.4.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Error;
+
+use Cake\Error\PhpError;
+use Cake\TestSuite\TestCase;
+
+class PhpErrorTest extends TestCase
+{
+    public function testBasicGetters()
+    {
+        $error = new PhpError(E_ERROR, 'something bad');
+        $this->assertEquals(E_ERROR, $error->getCode());
+        $this->assertEquals('something bad', $error->getMessage());
+        $this->assertNull($error->getFile());
+        $this->assertNull($error->getLine());
+        $this->assertEquals([], $error->getTrace());
+        $this->assertEquals('', $error->getTraceAsString());
+    }
+
+    public static function errorCodeProvider(): array
+    {
+        // [php error code, label, log-level]
+        return [
+            [E_ERROR, 'error', LOG_ERR],
+            [E_WARNING, 'warning', LOG_WARNING],
+            [E_NOTICE, 'notice', LOG_NOTICE],
+            [E_STRICT, 'strict', LOG_NOTICE],
+            [E_STRICT, 'strict', LOG_NOTICE],
+            [E_USER_DEPRECATED, 'deprecated', LOG_NOTICE],
+        ];
+    }
+
+    /**
+     * @dataProvider errorCodeProvider
+     */
+    public function testMappings($phpCode, $label, $logLevel)
+    {
+        $error = new PhpError($phpCode, 'something bad');
+        $this->assertEquals($phpCode, $error->getCode());
+        $this->assertEquals($label, $error->getLabel());
+        $this->assertEquals($logLevel, $error->getLogLevel());
+    }
+
+    public function testGetTraceAsString()
+    {
+        $trace = [
+            ['file' => 'a.php', 'line' => 10, 'reference' => 'TestObject::a()'],
+            ['file' => 'b.php', 'line' => 5, 'reference' => '[main]'],
+        ];
+        $error = new PhpError(E_ERROR, 'something bad', __FILE__, __LINE__, $trace);
+        $this->assertEquals($trace, $error->getTrace());
+        $expected = [
+            'TestObject::a() a.php, line 10',
+            '[main] b.php, line 5',
+        ];
+        $this->assertEquals(implode("\n", $expected), $error->getTraceAsString());
+        $this->assertEquals('error', $error->getLabel());
+    }
+}