Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 5179d42

Browse files
author
Petr Knap
committed
Changed work with exceptions
1 parent ec17d29 commit 5179d42

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

src/Enum/AbstractEnum.php

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

33
namespace PetrKnap\Php\Enum;
44

5+
use PetrKnap\Php\Enum\Exception\EnumException;
6+
use PetrKnap\Php\Enum\Exception\OutOfRangeException;
7+
58
/**
69
* Abstract enum object
710
*
@@ -142,13 +145,12 @@ private function exists($memberName)
142145
private function get($memberName)
143146
{
144147
if (!$this->exists($memberName)) {
145-
throw new EnumException(
148+
throw new OutOfRangeException(
146149
sprintf(
147150
"%s does not exist in %s",
148151
$memberName,
149152
get_called_class()
150-
),
151-
EnumException::OUT_OF_RANGE
153+
)
152154
);
153155
}
154156

@@ -167,12 +169,11 @@ public static function findByValue($value)
167169
return self::__callStatic($n, []);
168170
}
169171
}
170-
throw new EnumException(
172+
throw new OutOfRangeException(
171173
sprintf(
172174
"Value not found in %s",
173175
get_called_class()
174-
),
175-
EnumException::OUT_OF_RANGE
176+
)
176177
);
177178
}
178179
}

src/Enum/EnumException.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Enum/Exception/EnumException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PetrKnap\Php\Enum\Exception;
4+
5+
abstract class EnumException extends \Exception
6+
{
7+
// Empty class
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PetrKnap\Php\Enum\Exception;
4+
5+
class OutOfRangeException extends EnumException
6+
{
7+
// Empty class
8+
}

tests/Enum/EnumTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PetrKnap\Php\Enum\Test;
44

5-
use PetrKnap\Php\Enum\EnumException;
5+
use PetrKnap\Php\Enum\Exception\OutOfRangeException;
66
use PetrKnap\Php\Enum\Test\EnumTest\MyBoolean;
77

88
class EnumTest extends \PHPUnit_Framework_TestCase
@@ -42,11 +42,7 @@ public function testMagicConstruction_GoodKey($name, $value)
4242
*/
4343
public function testMagicConstruction_WrongKey($name)
4444
{
45-
$this->setExpectedException(
46-
get_class(new EnumException()),
47-
"",
48-
EnumException::OUT_OF_RANGE
49-
);
45+
$this->setExpectedException(OutOfRangeException::class);
5046

5147
MyBoolean::$name();
5248
}
@@ -97,7 +93,7 @@ public function dataFindByValue()
9793
return [
9894
[1, MyBoolean::MY_TRUE()],
9995
[2, MyBoolean::MY_FALSE()],
100-
[3, new EnumException()]
96+
[3, new OutOfRangeException()]
10197
];
10298
}
10399
}

0 commit comments

Comments
 (0)