Skip to content

Commit 9e8923a

Browse files
committed
Add custom exception to ensure unified error messages.
1 parent fe58a2c commit 9e8923a

5 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* CROSS PHPunit Utils
4+
*
5+
* @filesource
6+
* @copyright 2019 Cross Solution <https://www.cross-solution.de>
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Cross\TestUtils\Exception;
13+
14+
/**
15+
* Marker interface for package exceptions.
16+
*
17+
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
18+
*/
19+
interface ExceptionInterface
20+
{
21+
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* CROSS PHPunit Utils
4+
*
5+
* @filesource
6+
* @copyright 2019 Cross Solution <https://www.cross-solution.de>
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Cross\TestUtils\Exception;
13+
14+
/**
15+
* Exception thrown if a helper trait is used the wrong way
16+
*
17+
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
18+
* @todo write tests
19+
*/
20+
class InvalidUsageException extends \PHPUnit_Framework_Exception implements ExceptionInterface
21+
{
22+
use TemplatedMessageExceptionTrait;
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* CROSS PHPunit Utils
4+
*
5+
* @filesource
6+
* @copyright 2019 Cross Solution <https://www.cross-solution.de>
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Cross\TestUtils\Exception;
13+
14+
/**
15+
* Boilerplate for an exception with templated message.
16+
*
17+
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
18+
* @todo write tests
19+
*/
20+
trait TemplatedMessageExceptionTrait
21+
{
22+
public static function create(...$args): \Exception
23+
{
24+
$ex = end($args) instanceof \Throwable ? array_pop($args) : null;
25+
$message = sprintf(...$args);
26+
27+
return new static($message, 0, $ex);
28+
}
29+
30+
public static function fromTrait(string $trait, string $class, string $message, ...$args): \Exception
31+
{
32+
if (count($args)) {
33+
$message = sprintf($message, ...$args);
34+
}
35+
36+
return static::create('%s: %s: %s', $trait, $class, $message);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* CROSS PHPunit Utils
4+
*
5+
* @filesource
6+
* @copyright 2019 Cross Solution <https://www.cross-solution.de>
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Cross\TestUtilsTest\Exception;
13+
14+
use Cross\TestUtils\Exception\ExceptionInterface;
15+
use Cross\TestUtils\Exception\InvalidUsageException;
16+
use Cross\TestUtils\Exception\TemplatedMessageExceptionTrait;
17+
18+
use Cross\TestUtils\TestCase\TestUsesTraitsTrait;
19+
use Cross\TestUtils\TestCase\TestInheritanceTrait;
20+
21+
/**
22+
* Tests for \Cross\TestUtils\Exception\InvalidUsageException
23+
*
24+
* @covers \Cross\TestUtils\Exception\InvalidUsageException
25+
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
26+
*
27+
* @group Cross.TestUtils
28+
* @group Cross.TestUtils.Exception
29+
* @group Cross.TestUtils.Exception.InvalidUsageException
30+
*/
31+
class InvalidUsageExceptionTest extends \PHPUnit_Framework_TestCase
32+
{
33+
use TestInheritanceTrait, TestUsesTraitsTrait;
34+
35+
private $target = InvalidUsageException::class;
36+
private $inheritance = [ \PHPUnit_Framework_Exception::class, ExceptionInterface::class ];
37+
private $usesTraits = [ TemplatedMessageExceptionTrait::class ];
38+
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* CROSS PHPunit Utils
4+
*
5+
* @filesource
6+
* @copyright 2019 Cross Solution <https://www.cross-solution.de>
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Cross\TestUtilsTest\Exception;
13+
14+
use Cross\TestUtils\Exception\InvalidUsageException;
15+
16+
/**
17+
* Tests for \Cross\TestUtils\Exception\TemplatedMessageExceptionTrait
18+
*
19+
* @covers \Cross\TestUtils\Exception\TemplatedMessageExceptionTrait
20+
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
21+
*
22+
* @group Cross.TestUtils
23+
* @group Cross.TestUtils.Exception
24+
* @group Cross.TestUtils.Exception.TemplatedMessageExceptionTrait
25+
*/
26+
class TemplatedMessageExceptionTraitTest extends \PHPUnit_Framework_TestCase
27+
{
28+
public function testCreateWithoutPrevious()
29+
{
30+
$target = InvalidUsageException::create('%s %s', 'value1', 'value2');
31+
32+
static::assertEquals('value1 value2', $target->getMessage());
33+
}
34+
35+
public function testCreateWithPrevious()
36+
{
37+
$ex = new \Exception;
38+
$target = InvalidUsageException::create('%s', 'value1', $ex);
39+
40+
static::assertEquals('value1', $target->getMessage());
41+
static::assertSame($ex, $target->getPrevious());
42+
}
43+
44+
public function testFromTrait()
45+
{
46+
$target = InvalidUsageException::fromTrait('TRAIT', 'CLASS', '%s', 'value1');
47+
48+
static::assertEquals('TRAIT: CLASS: value1', $target->getMessage());
49+
}
50+
}

0 commit comments

Comments
 (0)