-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from dschoenbauer/develop
Throw and Exception
- Loading branch information
Showing
5 changed files
with
238 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2018 David Schoenbauer. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
namespace DSchoenbauer\Orm\Enum; | ||
|
||
/** | ||
* A list of possible keys for event parameters | ||
* | ||
* @author David Schoenbauer | ||
*/ | ||
class EventParameters | ||
{ | ||
const EXCEPTION = "exception"; | ||
const EVENT = "event"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2018 David Schoenbauer. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
namespace DSchoenbauer\Orm\Events\Logger; | ||
|
||
use DSchoenbauer\Orm\Enum\EventParameters; | ||
use DSchoenbauer\Orm\Enum\EventPriorities; | ||
use DSchoenbauer\Orm\Events\AbstractEvent; | ||
use Exception; | ||
use Zend\EventManager\EventInterface; | ||
|
||
/** | ||
* Description of ThrowExceptionEvent | ||
* | ||
* @author David Schoenbauer | ||
*/ | ||
class ThrowExceptionEvent extends AbstractEvent | ||
{ | ||
|
||
const NO_EXCEPTION_MESSAGE = "Exception expected to be thrown but no exception provided"; | ||
|
||
private $alwaysThrowException = false; | ||
|
||
public function __construct(array $events = [], $alwaysThrowException = false, $priority = EventPriorities::ON_TIME) | ||
{ | ||
$this->setAlwaysThrowException($alwaysThrowException); | ||
parent::__construct($events, $priority); | ||
} | ||
|
||
public function onExecute(EventInterface $event) | ||
{ | ||
if (!$event->getParam(EventParameters::EXCEPTION) instanceof Exception) { | ||
throw new Exception(static::NO_EXCEPTION_MESSAGE); | ||
} | ||
throw $event->getParam(EventParameters::EXCEPTION); | ||
} | ||
|
||
public function getAlwaysThrowException() | ||
{ | ||
return $this->alwaysThrowException; | ||
} | ||
|
||
public function setAlwaysThrowException($alwaysThrowException = true) | ||
{ | ||
$this->alwaysThrowException = $alwaysThrowException; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2018 David Schoenbauer. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
namespace DSchoenbauer\Orm\Events\Logger; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Zend\EventManager\EventInterface; | ||
|
||
/** | ||
* Description of ThrowExceptionEventTest | ||
* | ||
* @author David Schoenbauer | ||
*/ | ||
class ThrowExceptionEventTest extends TestCase | ||
{ | ||
private $object; | ||
|
||
protected function setUp() | ||
{ | ||
$this->object = new ThrowExceptionEvent(); | ||
} | ||
|
||
function testOnExecuteGoodParameter(){ | ||
$event = $this->getMockBuilder(EventInterface::class)->getMock(); | ||
$exception = new Exception('Test'); | ||
$event->expects($this->any())->method('getParam')->willReturn($exception); | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('Test'); | ||
$this->object->onExecute($event); | ||
} | ||
|
||
function testOnExecuteBadParameter(){ | ||
$event = $this->getMockBuilder(EventInterface::class)->getMock(); | ||
$exception = new Exception('Test'); | ||
$this->expectException(Exception::class); | ||
$event->expects($this->any())->method('getParam')->willReturn(new \stdClass()); | ||
$this->expectExceptionMessage(ThrowExceptionEvent::NO_EXCEPTION_MESSAGE); | ||
$this->object->onExecute($event); | ||
} | ||
|
||
function testOnExecuteNoParameter(){ | ||
$event = $this->getMockBuilder(EventInterface::class)->getMock(); | ||
$exception = new Exception('Test'); | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage(ThrowExceptionEvent::NO_EXCEPTION_MESSAGE); | ||
$this->object->onExecute($event); | ||
} | ||
|
||
function testAlwaysThrowException(){ | ||
$this->assertFalse($this->object->getAlwaysThrowException()); | ||
$this->assertTrue($this->object->setAlwaysThrowException()->getAlwaysThrowException()); | ||
$this->assertFalse($this->object->setAlwaysThrowException(false)->getAlwaysThrowException()); | ||
} | ||
|
||
function testAlwaysThrowExceptionContructor(){ | ||
$this->object = new ThrowExceptionEvent([], true); | ||
$this->assertTrue($this->object->getAlwaysThrowException()); | ||
} | ||
} |