diff --git a/src/BitWeb/ErrorReporting/Configuration.php b/src/BitWeb/ErrorReporting/Configuration.php index 5751c78..d45011e 100644 --- a/src/BitWeb/ErrorReporting/Configuration.php +++ b/src/BitWeb/ErrorReporting/Configuration.php @@ -6,32 +6,36 @@ class Configuration extends AbstractConfiguration { protected $errorReportingLevel = E_ALL; - protected $emails = array(); + protected $emails = []; protected $subject = 'Errors'; protected $fromAddress = ''; - protected $botList = array(); + protected $botList = []; protected $ignore404 = false; protected $ignoreBot404 = false; - protected $ignorableExceptions = array('ErrorException'); - protected $ignorablePaths = array(); + protected $ignorableExceptions = ['ErrorException']; + protected $ignorablePaths = []; /** * The level of error reporting. + * * @var integer $errorReportingLevel */ - public function setErrorReportingLevel($errorReportingLevel) { + public function setErrorReportingLevel($errorReportingLevel) + { $this->errorReportingLevel = $errorReportingLevel; } /** * @return integer */ - public function getErrorReportingLevel() { + public function getErrorReportingLevel() + { return $this->errorReportingLevel; } /** * Defines bots. + * * @var array $botList */ public function setBotList(array $botList) @@ -40,7 +44,6 @@ public function setBotList(array $botList) } /** - * * @return array */ public function getBotList() @@ -50,6 +53,7 @@ public function getBotList() /** * An array of emails the error report is sent to. + * * @var array $emails */ public function setEmails(array $emails) @@ -67,6 +71,7 @@ public function getEmails() /** * Address where the message is sent from. + * * @var String $fromAddress */ public function setFromAddress($fromAddress) @@ -84,6 +89,7 @@ public function getFromAddress() /** * Exceptions to ignore. + * * @var array $ignorableExceptions */ public function setIgnorableExceptions(array $ignorableExceptions) @@ -101,6 +107,7 @@ public function getIgnorableExceptions() /** * Are 404 errors ignored? + * * @var boolean $ignore404 */ public function setIgnore404($ignore404) @@ -118,6 +125,7 @@ public function getIgnore404() /** * Are bot 404 errors ignored? + * * @var boolean $ignoreBot404 */ public function setIgnoreBot404($ignoreBot404) @@ -135,6 +143,7 @@ public function getIgnoreBot404() /** * The subject of the message being sent. + * * @var string $subject */ public function setSubject($subject) @@ -152,17 +161,19 @@ public function getSubject() /** * Paths to ignore. + * * @var array $ignorablePaths */ - public function setIgnorablePaths(array $ignorablePaths) { + public function setIgnorablePaths(array $ignorablePaths) + { $this->ignorablePaths = $ignorablePaths; } /** * @return array */ - public function getIgnorablePaths() { + public function getIgnorablePaths() + { return $this->ignorablePaths; } - -} \ No newline at end of file +} diff --git a/src/BitWeb/ErrorReporting/Error.php b/src/BitWeb/ErrorReporting/Error.php index c39498f..15d7e86 100644 --- a/src/BitWeb/ErrorReporting/Error.php +++ b/src/BitWeb/ErrorReporting/Error.php @@ -7,21 +7,21 @@ class Error /** * @var ErrorInfo[] */ - protected $errors = array(); + protected $errors = []; /** * @var ErrorMeta */ protected $meta; - function __construct($errors, $meta) + public function __construct($errors, $meta) { $this->errors = $errors; $this->meta = $meta; } /** - * @param ErrorInfo[] $errors + * @param ErrorInfo[] $errors * @return self */ public function setErrors(array $errors) @@ -31,8 +31,8 @@ public function setErrors(array $errors) } /** - * @param ErrorInfo $errorInfo - * @return $this + * @param ErrorInfo $errorInfo + * @return self */ public function addError(ErrorInfo $errorInfo) { @@ -49,7 +49,7 @@ public function getErrors() } /** - * @param ErrorMeta $meta + * @param ErrorMeta $meta * @return self */ public function setMeta(ErrorMeta $meta) @@ -65,4 +65,4 @@ public function getMeta() { return $this->meta; } -} \ No newline at end of file +} diff --git a/src/BitWeb/ErrorReporting/ErrorEventManager.php b/src/BitWeb/ErrorReporting/ErrorEventManager.php index 779f792..a06115e 100644 --- a/src/BitWeb/ErrorReporting/ErrorEventManager.php +++ b/src/BitWeb/ErrorReporting/ErrorEventManager.php @@ -1,17 +1,10 @@ class = $class; $this->severity = $severity; @@ -34,7 +33,7 @@ function __construct($class, $title, $tracking, $severity = null) } /** - * @param string $class + * @param string $class * @return self */ public function setClass($class) @@ -52,7 +51,7 @@ public function getClass() } /** - * @param string $severity + * @param string $severity * @return self */ public function setSeverity($severity) @@ -70,7 +69,7 @@ public function getSeverity() } /** - * @param string $title + * @param string $title * @return self */ public function setTitle($title) @@ -88,7 +87,7 @@ public function getTitle() } /** - * @param string $tracking + * @param string $tracking * @return self */ public function setTracking($tracking) @@ -104,4 +103,4 @@ public function getTracking() { return $this->tracking; } -} \ No newline at end of file +} diff --git a/src/BitWeb/ErrorReporting/ErrorMeta.php b/src/BitWeb/ErrorReporting/ErrorMeta.php index d108233..f0c71e6 100644 --- a/src/BitWeb/ErrorReporting/ErrorMeta.php +++ b/src/BitWeb/ErrorReporting/ErrorMeta.php @@ -56,7 +56,7 @@ class ErrorMeta protected $requestDuration; /** - * @param array $getData + * @param array $getData * @return self */ public function setGetData(array $getData) @@ -74,7 +74,7 @@ public function getGetData() } /** - * @param string $ip + * @param string $ip * @return self */ public function setIp($ip) @@ -92,7 +92,7 @@ public function getIp() } /** - * @param array $postData + * @param array $postData * @return self */ public function setPostData(array $postData) @@ -110,7 +110,7 @@ public function getPostData() } /** - * @param string $referrer + * @param string $referrer * @return self */ public function setReferrer($referrer) @@ -128,7 +128,7 @@ public function getReferrer() } /** - * @param float $requestDuration + * @param float $requestDuration * @return self */ public function setRequestDuration($requestDuration) @@ -146,7 +146,7 @@ public function getRequestDuration() } /** - * @param \DateTime $requestTime + * @param \DateTime $requestTime * @return self */ public function setRequestTime(\DateTime $requestTime) @@ -164,7 +164,7 @@ public function getRequestTime() } /** - * @param array $serverData + * @param array $serverData * @return self */ public function setServerData(array $serverData) @@ -182,7 +182,7 @@ public function getServerData() } /** - * @param array $sessionData + * @param array $sessionData * @return self */ public function setSessionData(array $sessionData = null) @@ -200,7 +200,7 @@ public function getSessionData() } /** - * @param string $url + * @param string $url * @return self */ public function setUrl($url) @@ -218,7 +218,7 @@ public function getUrl() } /** - * @param string $userAgent + * @param string $userAgent * @return self */ public function setUserAgent($userAgent) @@ -234,4 +234,4 @@ public function getUserAgent() { return $this->userAgent; } -} \ No newline at end of file +} diff --git a/src/BitWeb/ErrorReporting/Service/ErrorService.php b/src/BitWeb/ErrorReporting/Service/ErrorService.php index 5704603..24dec90 100644 --- a/src/BitWeb/ErrorReporting/Service/ErrorService.php +++ b/src/BitWeb/ErrorReporting/Service/ErrorService.php @@ -1,10 +1,5 @@ setConfig($configuration); + } + /** * @param \BitWeb\ErrorReporting\ErrorEventManager $eventManager */ @@ -48,17 +52,6 @@ public function setEvent($event) $this->event = $event; } - - public $errors = []; - protected $startTime = null; - - protected static $errorException; - - public function __construct(Configuration $configuration) - { - $this->setConfig($configuration); - } - public function setConfig(Configuration $configuration) { $this->configuration = $configuration; @@ -141,6 +134,7 @@ public function isBotRequest() return true; } } + return false; } @@ -149,14 +143,21 @@ protected function hasOnlyIgnorableExceptions() if ($this->configuration->getIgnorableExceptions() == null) { return false; } + $ignorableExceptions = $this->configuration->getIgnorableExceptions(); foreach ($this->errors as $error) { + $exceptionIgnored = false; foreach ($ignorableExceptions as $ignorable) { - if (!($error instanceof $ignorable)) { - return false; + if ($error instanceof $ignorable) { + $exceptionIgnored = true; } } + + if (!$exceptionIgnored) { + return false; + } } + return true; } @@ -165,14 +166,14 @@ public function isIgnorablePath() if ($this->configuration->getIgnorablePaths() == null || !isset($_SERVER['REQUEST_URI'])) { return false; } - $ignorablePaths = array_map(function($path) { + $ignorablePaths = array_map(function ($path) { return '(' . str_replace('/', '\/', $path) . ')'; }, $this->configuration->getIgnorablePaths()); $pattern = '/(' . implode('|', $ignorablePaths) . ')+/i'; $path = $_SERVER['REQUEST_URI']; - return (boolean) preg_match($pattern, $path); + return (boolean)preg_match($pattern, $path); } public function restoreDefaultErrorHandling() @@ -220,27 +221,27 @@ protected function composeAndSendErrorMail() ])); $renderedView = $renderer->render($viewModel); - $to = array(); - $cc = array(); + $to = []; + $cc = []; - foreach($this->configuration->getEmails() as $index => $mail){ - if($index == 0){ - $to = array('email' => $mail, 'name' => ''); + foreach ($this->configuration->getEmails() as $index => $mail) { + if ($index == 0) { + $to = ['email' => $mail, 'name' => '']; } else { - $cc[] = array('email' => $mail, 'name' => ''); + $cc[] = ['email' => $mail, 'name' => '']; } } - $params = array( + $params = [ 'to' => $to, 'cc' => $cc, - 'from' => array( + 'from' => [ 'name' => '', 'email' => $this->configuration->getFromAddress(), - ), + ], 'subject' => $this->configuration->getSubject(), 'body' => $renderedView, - ); + ]; $this->eventManager->trigger($this->event, null, $params); } diff --git a/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php b/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php index 106e5de..cf289d5 100644 --- a/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php +++ b/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php @@ -6,6 +6,8 @@ use BitWeb\ErrorReporting\Configuration; use BitWeb\ErrorReporting\Error; use BitWeb\ErrorReporting\Service\ErrorService; +use BitWebTest\ErrorReporting\TestAsset\AnotherTestException; +use BitWebTest\ErrorReporting\TestAsset\TestException; class ErrorServiceTest extends \PHPUnit_Framework_TestCase { @@ -84,7 +86,7 @@ public function testGetErrorReportMetaData() public function testEndErrorHandlingWithFatal() { -// $serviceMock = $this->getMock(ErrorService::class, array(), array($this->configuration)); +// $serviceMock = $this->getMock(ErrorService::class, [], [$this->configuration]); // $serviceMock->expects($this->any())->method('endErrorHandling')->will($this->returnValue(true)); // $serviceMock->startErrorHandling(); // trigger_error("Cannot divide by zero", E_ALL); @@ -95,12 +97,12 @@ public function testEndErrorHandlingWithFatal() public function testEndErrorHandling() { -// $this->configuration->setEmails(array('kristjan.andresson@bitweb.ee')); +// $this->configuration->setEmails(['kristjan.andresson@bitweb.ee']); // $service = new ErrorService($this->configuration); // $service->startErrorHandling(); // trigger_error("Cannot divide by zero", E_USER_ERROR); // $service->endErrorHandling(); -// $this->assertEquals(array(), $service->errors); +// $this->assertEquals([], $service->errors); // $this->assertNull(\PHPUnit_Framework_Assert::readAttribute($service, 'startTime')); } @@ -122,17 +124,43 @@ public function testIsBotRequest() $this->assertTrue($service->isBotRequest()); } - public function testHasOnlyIgnorableExceptions() + public function testHasOnlyIgnorableExceptionsWithEmptyConfig() { $service = new ErrorService($this->configuration); $method = self::getMethod(ErrorService::class, 'hasOnlyIgnorableExceptions'); + $this->assertTrue($method->invokeArgs($service, [])); - $this->configuration->setIgnorableExceptions(array('TestException')); + } + + public function testHasOnlyIgnorableExceptionsReturnsTrue() + { + $this->configuration->setIgnorableExceptions([TestException::class, AnotherTestException::class]); + $service = new ErrorService($this->configuration); + $method = self::getMethod(ErrorService::class, 'hasOnlyIgnorableExceptions'); + $service->startErrorHandling(); - trigger_error("Fatal error", E_USER_NOTICE); - $this->assertFalse($method->invokeArgs($service, [])); + $service->errors = [ + new AnotherTestException() + ]; + $this->assertTrue($method->invokeArgs($service, [])); + } + + public function testHasOnlyIgnorableExceptionsReturnsFalse() + { + $this->configuration->setIgnorableExceptions([TestException::class, AnotherTestException::class]); + + $service = new ErrorService($this->configuration); + $method = self::getMethod(ErrorService::class, 'hasOnlyIgnorableExceptions'); + + $service->startErrorHandling(); + $service->errors = [ + new AnotherTestException(), + new \InvalidArgumentException() + ]; + + $this->assertFalse($method->invokeArgs($service, [])); } public function testIsIgnorablePath() @@ -152,7 +180,5 @@ public function testRestoreDefaultErrorHandling() { $service = new ErrorService($this->configuration); $this->assertTrue($service->restoreDefaultErrorHandling()); - } - -} \ No newline at end of file +} diff --git a/test/BitWebTest/ErrorReporting/TestAsset/AnotherTestException.php b/test/BitWebTest/ErrorReporting/TestAsset/AnotherTestException.php new file mode 100644 index 0000000..131b39f --- /dev/null +++ b/test/BitWebTest/ErrorReporting/TestAsset/AnotherTestException.php @@ -0,0 +1,8 @@ +