Skip to content

Commit

Permalink
Merge pull request #173 from dschoenbauer/develop
Browse files Browse the repository at this point in the history
Throw and Exception
  • Loading branch information
dschoenbauer committed Sep 8, 2018
2 parents 7778c35 + 724b19b commit c73da1d
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/Orm/CrudModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
namespace DSchoenbauer\Orm;

use DSchoenbauer\Orm\Entity\EntityInterface;
use DSchoenbauer\Orm\Enum\EventParameters;
use DSchoenbauer\Orm\Enum\ModelEvents;
use Exception;

Expand All @@ -35,6 +37,14 @@
class CrudModel extends Model
{

private $throwExceptions = false;

public function __construct(EntityInterface $entity, $throwExceptions = false)
{
parent::__construct($entity);
$this->setThrowExceptions($throwExceptions);
}

/**
* Process the addition of a new record
* @param array $data
Expand Down Expand Up @@ -96,12 +106,27 @@ protected function processEvent($event)
try {
$this->getEventManager()->trigger($event, $this);
} catch (Exception $exc) {
$payload = [
'exception' => $exc,
'event' => $event,
];
$this->getEventManager()->trigger(ModelEvents::ERROR, $this, $payload);
if (!$this->getThrowExceptions()) {
$payload = [
EventParameters::EXCEPTION => $exc,
EventParameters::EVENT => $event,
];
$this->getEventManager()->trigger(ModelEvents::ERROR, $this, $payload);
return $this;
}
throw $exc;
}
return $this;
}

public function getThrowExceptions()
{
return $this->throwExceptions;
}

public function setThrowExceptions($throwExceptions = true)
{
$this->throwExceptions = $throwExceptions;
return $this;
}
}
36 changes: 36 additions & 0 deletions src/Orm/Enum/EventParameters.php
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";
}
69 changes: 69 additions & 0 deletions src/Orm/Events/Logger/ThrowExceptionEvent.php
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;
}
}
22 changes: 22 additions & 0 deletions tests/src/Orm/CrudModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,26 @@ public function testDeleteOnError()
$this->object->setEventManager($this->mockEventManager);
$this->object->delete(1447);
}

public function testOnException()
{
$exc = new \Exception('test');
$this->expectException(\Exception::class);
$this->expectExceptionMessage('test');
$this->mockEventManager->expects($this->exactly(1))
->method('trigger')
->withConsecutive(
[ModelEvents::FETCH, $this->object]
)->willReturnCallback(function() use ($exc) {
throw $exc;
});
$this->object->setEventManager($this->mockEventManager);
$this->object->setThrowExceptions()->fetch(1447);
}

public function testThrowException(){
$this->assertFalse($this->object->getThrowExceptions());
$this->assertTrue($this->object->setThrowExceptions()->getThrowExceptions());
$this->assertFalse($this->object->setThrowExceptions(false)->getThrowExceptions());
}
}
81 changes: 81 additions & 0 deletions tests/src/Orm/Events/Logger/ThrowExceptionEventTest.php
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());
}
}

0 comments on commit c73da1d

Please sign in to comment.