From 75d4a253fdebd89bca7e57f7fcfa2f15fe4559a7 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Wed, 13 Aug 2014 21:57:02 +0200 Subject: [PATCH 1/3] unit test --- test/Storage/Adapter/AbstractAdapterTest.php | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/test/Storage/Adapter/AbstractAdapterTest.php b/test/Storage/Adapter/AbstractAdapterTest.php index 96c40e665..7b0e9c3f7 100644 --- a/test/Storage/Adapter/AbstractAdapterTest.php +++ b/test/Storage/Adapter/AbstractAdapterTest.php @@ -382,6 +382,84 @@ public function testGetItemReturnsNullIfFailed() $this->assertFalse($success, '$success should be false if the item cannot be retrieved'); } + public function simpleEventHandlingMethodDefinitions() + { + return array( + array( + 'name' => 'getItem', + 'args' => array('key'), + 'internalName' => 'internalGetItem', + 'internalArgs' => array('key'), + 'retVal' => 'value', + ) + ); + } + + /** + * @dataProvider simpleEventHandlingMethodDefinitions + */ + public function testEventHandlingSimple($methodName, $methodArgs, $internalMethodName, $internalMethodArgs, $retVal) + { + $this->_storage = $this->getMockForAbstractAdapter(array($internalMethodName)); + + $eventList = array(); + $eventHandler = function ($event) use (&$eventList) { + $eventList[] = $event->getName(); + }; + $this->_storage->getEventManager()->attach($methodName . '.pre', $eventHandler); + $this->_storage->getEventManager()->attach($methodName . '.post', $eventHandler); + $this->_storage->getEventManager()->attach($methodName . '.exception', $eventHandler); + + $mock = $this->_storage + ->expects($this->once()) + ->method($internalMethodName); + $mock = call_user_func_array(array($mock, 'with'), array_map(array($this, 'equalTo'), $internalMethodArgs)); + $mock->will($this->returnValue($retVal)); + + call_user_func_array(array($this->_storage, $methodName), $methodArgs); + + $expectedEventList = array( + $methodName . '.pre', + $methodName . '.post' + ); + $this->assertSame($expectedEventList, $eventList); + } + + /** + * @dataProvider simpleEventHandlingMethodDefinitions + */ + public function testEventHandlingStopInPre($methodName, $methodArgs, $internalMethodName, $internalMethodArgs, $retVal) + { + $this->_storage = $this->getMockForAbstractAdapter(array($internalMethodName)); + + $eventList = array(); + $eventHandler = function ($event) use (&$eventList) { + $eventList[] = $event->getName(); + }; + $this->_storage->getEventManager()->attach($methodName . '.pre', $eventHandler); + $this->_storage->getEventManager()->attach($methodName . '.post', $eventHandler); + $this->_storage->getEventManager()->attach($methodName . '.exception', $eventHandler); + + $this->_storage->getEventManager()->attach($methodName . '.pre', function ($event) use ($retVal) { + $event->stopPropagation(); + return $retVal; + }); + + // the internal method should never be called + $this->_storage->expects($this->never())->method($internalMethodName); + + // the return vaue should be available by pre-event + $result = call_user_func_array(array($this->_storage, $methodName), $methodArgs); + $this->assertSame($retVal, $result); + + // after the triggered pre-event the post-event should be triggered as well + $expectedEventList = array( + $methodName . '.pre', + $methodName . '.post', + ); + $this->assertSame($expectedEventList, $eventList); + } + /* public function testGetMetadatas() { From 305fa3d148459a1e3c54443fbffe2f4fd2934af2 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Wed, 13 Aug 2014 22:14:18 +0200 Subject: [PATCH 2/3] unit test --- test/Storage/Adapter/AbstractAdapterTest.php | 45 +++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/test/Storage/Adapter/AbstractAdapterTest.php b/test/Storage/Adapter/AbstractAdapterTest.php index 7b0e9c3f7..49029a25d 100644 --- a/test/Storage/Adapter/AbstractAdapterTest.php +++ b/test/Storage/Adapter/AbstractAdapterTest.php @@ -385,20 +385,45 @@ public function testGetItemReturnsNullIfFailed() public function simpleEventHandlingMethodDefinitions() { return array( - array( - 'name' => 'getItem', - 'args' => array('key'), - 'internalName' => 'internalGetItem', - 'internalArgs' => array('key'), - 'retVal' => 'value', - ) + // name, internalName, args, internalName, returnValue + array('hasItem', 'internalGetItem', array('k'), 'v'), + array('hasItems', 'internalHasItems', array(array('k1', 'k2')), array('v1', 'v2')), + + array('getItem', 'internalGetItem', array('k'), 'v'), + array('getItems', 'internalGetItems', array(array('k1', 'k2')), array('k1' => 'v1', 'k2' => 'v2')), + + array('getMetadata', 'internalGetMetadata', array('k'), array()), + array('getMetadatas', 'internalGetMetadatas', array(array('k1', 'k2')), array('k1' => array(), 'k2' => array())), + + array('setItem', 'internalSetItem', array('k', 'v'), true), + array('setItems', 'internalSetItems', array(array('k1' => 'v1', 'k2' => 'v2')), array()), + + array('replaceItem', 'internalReplaceItem', array('k', 'v'), true), + array('replaceItems', 'internalReplaceItems', array(array('k1' => 'v1', 'k2' => 'v2')), array()), + + array('addItem', 'internalAddItem', array('k', 'v'), true), + array('addItems', 'internalAddItems', array(array('k1' => 'v1', 'k2' => 'v2')), array()), + + array('checkAndSetItem', 'internalCheckAndSetItem', array(123, 'k', 'v'), true), + + array('touchItem', 'internalTouchItem', array('k'), true), + array('touchItems', 'internalTouchItems', array(array('k1', 'k2')), array()), + + array('removeItem', 'internalRemoveItem', array('k'), true), + array('removeItems', 'internalRemoveItems', array(array('k1', 'k2')), array()), + + array('incrementItem', 'internalIncrementItem', array('k', 1), true), + array('incrementItems', 'internalIncrementItems', array(array('k1' => 1, 'k2' => 2)), array()), + + array('decrementItem', 'internalDecrementItem', array('k', 1), true), + array('decrementItems', 'internalDecrementItems', array(array('k1' => 1, 'k2' => 2)), array()), ); } /** * @dataProvider simpleEventHandlingMethodDefinitions */ - public function testEventHandlingSimple($methodName, $methodArgs, $internalMethodName, $internalMethodArgs, $retVal) + public function testEventHandlingSimple($methodName, $internalMethodName, $methodArgs, $retVal) { $this->_storage = $this->getMockForAbstractAdapter(array($internalMethodName)); @@ -413,7 +438,7 @@ public function testEventHandlingSimple($methodName, $methodArgs, $internalMetho $mock = $this->_storage ->expects($this->once()) ->method($internalMethodName); - $mock = call_user_func_array(array($mock, 'with'), array_map(array($this, 'equalTo'), $internalMethodArgs)); + $mock = call_user_func_array(array($mock, 'with'), array_map(array($this, 'equalTo'), $methodArgs)); $mock->will($this->returnValue($retVal)); call_user_func_array(array($this->_storage, $methodName), $methodArgs); @@ -428,7 +453,7 @@ public function testEventHandlingSimple($methodName, $methodArgs, $internalMetho /** * @dataProvider simpleEventHandlingMethodDefinitions */ - public function testEventHandlingStopInPre($methodName, $methodArgs, $internalMethodName, $internalMethodArgs, $retVal) + public function testEventHandlingStopInPre($methodName, $internalMethodName, $methodArgs, $retVal) { $this->_storage = $this->getMockForAbstractAdapter(array($internalMethodName)); From 2a8ac17e729e9601e76e22f10a4f4afd90e7f14f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 14 Nov 2014 01:48:10 +0100 Subject: [PATCH 3/3] zendframework/zf2#6231 - cleaning up some complex conditionals with simpler ternaries where possible --- src/Storage/Adapter/AbstractAdapter.php | 227 ++++++++++++------------ 1 file changed, 113 insertions(+), 114 deletions(-) diff --git a/src/Storage/Adapter/AbstractAdapter.php b/src/Storage/Adapter/AbstractAdapter.php index 90e5a4f06..3eaf26f6c 100644 --- a/src/Storage/Adapter/AbstractAdapter.php +++ b/src/Storage/Adapter/AbstractAdapter.php @@ -217,11 +217,10 @@ protected function triggerPost($eventName, ArrayObject $args, & $result) { $postEvent = new PostEvent($eventName . '.post', $this, $args, $result); $eventRs = $this->getEventManager()->trigger($postEvent); - if ($eventRs->stopped()) { - return $eventRs->last(); - } - return $postEvent->getResult(); + return $eventRs->stopped() + ? $eventRs->last() + : $postEvent->getResult(); } /** @@ -246,11 +245,9 @@ protected function triggerException($eventName, ArrayObject $args, & $result, \E throw $exceptionEvent->getException(); } - if ($eventRs->stopped()) { - return $eventRs->last(); - } - - return $exceptionEvent->getResult(); + return $eventRs->stopped() + ? $eventRs->last() + : $exceptionEvent->getResult(); } /** @@ -357,6 +354,7 @@ public function getItem($key, & $success = null, & $casToken = null) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); + if ($eventRs->stopped()) { $result = $eventRs->last(); } elseif ($args->offsetExists('success') && $args->offsetExists('casToken')) { @@ -366,6 +364,7 @@ public function getItem($key, & $success = null, & $casToken = null) } else { $result = $this->internalGetItem($args['key']); } + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = null; @@ -409,11 +408,11 @@ public function getItems(array $keys) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalGetItems($args['keys']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalGetItems($args['keys']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array(); @@ -466,11 +465,11 @@ public function hasItem($key) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalHasItem($args['key']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalHasItem($args['key']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -516,11 +515,11 @@ public function hasItems(array $keys) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalHasItems($args['keys']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalHasItems($args['keys']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array(); @@ -570,11 +569,11 @@ public function getMetadata($key) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalGetMetadata($args['key']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalGetMetadata($args['key']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -622,11 +621,11 @@ public function getMetadatas(array $keys) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalGetMetadatas($args['keys']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalGetMetadatas($args['keys']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array(); @@ -681,11 +680,11 @@ public function setItem($key, $value) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalSetItem($args['key'], $args['value']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalSetItem($args['key'], $args['value']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -727,11 +726,11 @@ public function setItems(array $keyValuePairs) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalSetItems($args['keyValuePairs']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalSetItems($args['keyValuePairs']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array_keys($keyValuePairs); @@ -783,11 +782,11 @@ public function addItem($key, $value) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalAddItem($args['key'], $args['value']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalAddItem($args['key'], $args['value']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -835,11 +834,11 @@ public function addItems(array $keyValuePairs) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalAddItems($args['keyValuePairs']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalAddItems($args['keyValuePairs']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array_keys($keyValuePairs); @@ -891,11 +890,11 @@ public function replaceItem($key, $value) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalReplaceItem($args['key'], $args['value']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalReplaceItem($args['key'], $args['value']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -944,11 +943,11 @@ public function replaceItems(array $keyValuePairs) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalReplaceItems($args['keyValuePairs']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalReplaceItems($args['keyValuePairs']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array_keys($keyValuePairs); @@ -1003,11 +1002,11 @@ public function checkAndSetItem($token, $key, $value) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalCheckAndSetItem($args['token'], $args['key'], $args['value']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalCheckAndSetItem($args['token'], $args['key'], $args['value']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -1060,11 +1059,11 @@ public function touchItem($key) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalTouchItem($args['key']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalTouchItem($args['key']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -1114,11 +1113,11 @@ public function touchItems(array $keys) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalTouchItems($args['keys']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalTouchItems($args['keys']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { return $this->triggerException(__FUNCTION__, $args, $keys, $e); @@ -1167,11 +1166,11 @@ public function removeItem($key) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalRemoveItem($args['key']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalRemoveItem($args['key']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -1212,11 +1211,11 @@ public function removeItems(array $keys) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalRemoveItems($args['keys']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalRemoveItems($args['keys']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { return $this->triggerException(__FUNCTION__, $args, $keys, $e); @@ -1267,11 +1266,11 @@ public function incrementItem($key, $value) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalIncrementItem($args['key'], $args['value']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalIncrementItem($args['key'], $args['value']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -1327,11 +1326,11 @@ public function incrementItems(array $keyValuePairs) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalIncrementItems($args['keyValuePairs']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalIncrementItems($args['keyValuePairs']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array(); @@ -1384,11 +1383,11 @@ public function decrementItem($key, $value) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalDecrementItem($args['key'], $args['value']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalDecrementItem($args['key'], $args['value']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false; @@ -1444,11 +1443,11 @@ public function decrementItems(array $keyValuePairs) try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalDecrementItems($args['keyValuePairs']); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalDecrementItems($args['keyValuePairs']); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = array(); @@ -1491,11 +1490,11 @@ public function getCapabilities() try { $eventRs = $this->triggerPre(__FUNCTION__, $args); - if ($eventRs->stopped()) { - $result = $eventRs->last(); - } else { - $result = $this->internalGetCapabilities(); - } + + $result = $eventRs->stopped() + ? $eventRs->last() + : $this->internalGetCapabilities(); + return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { $result = false;