Skip to content

Commit 15758c0

Browse files
authored
Fix to broken constructors not using defaults (#8)
Fix to completely missed constructor removal
1 parent 3e1990c commit 15758c0

File tree

7 files changed

+37
-5
lines changed

7 files changed

+37
-5
lines changed

Exceptions/Collection/CollectionException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Exceptions\Collection;
44

5+
use Exceptions\Helpers\DefaultConstructorTrait;
56
use Exceptions\Helpers\DefaultsInterface;
67
use Exceptions\Helpers\FromException;
78

@@ -13,7 +14,7 @@
1314
*/
1415
abstract class CollectionException extends \RuntimeException implements CollectionExceptionInterface, DefaultsInterface
1516
{
16-
use FromException;
17+
use FromException, DefaultConstructorTrait;
1718

1819
/**
1920
* {@inheritdoc}

Exceptions/Data/DataException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Exceptions\Data;
44

5+
use Exceptions\Helpers\DefaultConstructorTrait;
56
use Exceptions\Helpers\DefaultsInterface;
67
use Exceptions\Helpers\FromException;
78

@@ -13,7 +14,7 @@
1314
*/
1415
abstract class DataException extends \RuntimeException implements DataExceptionInterface, DefaultsInterface
1516
{
16-
use FromException;
17+
use FromException, DefaultConstructorTrait;
1718

1819
/**
1920
* {@inheritdoc}

Exceptions/Http/HttpException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Exceptions\Http;
44

5+
use Exceptions\Helpers\DefaultConstructorTrait;
56
use Exceptions\Helpers\DefaultsInterface;
67
use Exceptions\Helpers\FromException;
78

@@ -16,7 +17,7 @@
1617
*/
1718
abstract class HttpException extends \RuntimeException implements HttpExceptionInterface, DefaultsInterface
1819
{
19-
use FromException;
20+
use FromException, DefaultConstructorTrait;
2021

2122
/**
2223
* {@inheritdoc}

Exceptions/IO/IOException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Exceptions\IO;
44

5+
use Exceptions\Helpers\DefaultConstructorTrait;
56
use Exceptions\Helpers\DefaultsInterface;
67
use Exceptions\Helpers\FromException;
78

@@ -13,7 +14,7 @@
1314
*/
1415
abstract class IOException extends \RuntimeException implements IOExceptionInterface, DefaultsInterface
1516
{
16-
use FromException;
17+
use FromException, DefaultConstructorTrait;
1718

1819
/**
1920
* {@inheritdoc}

Exceptions/Operation/OperationException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Exceptions\Operation;
44

5+
use Exceptions\Helpers\DefaultConstructorTrait;
56
use Exceptions\Helpers\DefaultsInterface;
67
use Exceptions\Helpers\FromException;
78

@@ -13,7 +14,7 @@
1314
*/
1415
abstract class OperationException extends \RuntimeException implements OperationExceptionInterface, DefaultsInterface
1516
{
16-
use FromException;
17+
use FromException, DefaultConstructorTrait;
1718

1819
/**
1920
* {@inheritdoc}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Exceptions\Helpers;
3+
4+
/**
5+
* Defines an easy to reuse constructor that builds the exception using the defaults interface definition
6+
*
7+
* @package Exceptions
8+
*/
9+
trait DefaultConstructorTrait
10+
{
11+
public function __construct(string $message = "", int $code = 0, \Throwable $previous = null)
12+
{
13+
parent::__construct($message ?: $this->getDefaultMessage(), $code ?: $this->getDefaultCode(), $previous);
14+
}
15+
}

Tests/ExceptionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ public function testDefaults($className)
8282
$this->assertNotNull($className::getDefaultCode());
8383
}
8484

85+
/**
86+
* @dataProvider providesConstructorTestClasses
87+
*
88+
* @param string $className Class to test that defaults are defined
89+
*/
90+
public function testDefaultConstructor($className)
91+
{
92+
$exception = new $className();
93+
$this->assertEquals($exception->getMessage(), $className::getDefaultMessage());
94+
$this->assertEquals($exception->getCode(), $className::getDefaultCode());
95+
}
96+
8597
/**
8698
* @dataProvider providesConstructorTestClasses
8799
*

0 commit comments

Comments
 (0)