diff --git a/.gitignore b/.gitignore index 1bfef2a..3d17b4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor node_modules bin +phpunit.xml \ No newline at end of file diff --git a/README.md b/README.md index 01229e5..e44b5e4 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,12 @@ Patch Manager A php library to manage PATCH requests in a standardized (and elegant) way +### Be careful!!! + +*From version 0.3 namespace will change from PatchManager\\... to Cypress\\PatchManager\\...* + +## Install + Install with composer ``` json diff --git a/composer.json b/composer.json index 5b8f56f..b834682 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,8 @@ }, "autoload": { "psr-4": { - "PatchManager\\": "src/PatchManager/", - "PatchManager\\Tests\\": "tests/PatchManager/" + "Cypress\\PatchManager\\": "src/PatchManager/", + "Cypress\\PatchManager\\Tests\\": "tests/PatchManager/" } }, "license": "LGPL-3.0+", diff --git a/examples/data_handler.php b/examples/data_handler.php index 56ac2c8..8c2eba4 100644 --- a/examples/data_handler.php +++ b/examples/data_handler.php @@ -2,12 +2,12 @@ require_once __DIR__.'/../vendor/autoload.php'; -use PatchManager\Request\Operations; -use PatchManager\OperationMatcher; -use PatchManager\Handler\DataHandler; -use PatchManager\PatchManager; +use Cypress\PatchManager\Request\Operations; +use Cypress\PatchManager\OperationMatcher; +use Cypress\PatchManager\Handler\DataHandler; +use Cypress\PatchManager\PatchManager; -class Subject implements \PatchManager\Patchable +class Subject implements \Cypress\PatchManager\Patchable { private $a = 1; diff --git a/src/PatchManager/Bundle/DependencyInjection/Configuration.php b/src/PatchManager/Bundle/DependencyInjection/Configuration.php index 5bed766..6a1eb04 100644 --- a/src/PatchManager/Bundle/DependencyInjection/Configuration.php +++ b/src/PatchManager/Bundle/DependencyInjection/Configuration.php @@ -1,6 +1,6 @@ - + diff --git a/src/PatchManager/Bundle/Resources/config/handlers/data_doctrine.xml b/src/PatchManager/Bundle/Resources/config/handlers/data_doctrine.xml index 8c06e9e..4555068 100644 --- a/src/PatchManager/Bundle/Resources/config/handlers/data_doctrine.xml +++ b/src/PatchManager/Bundle/Resources/config/handlers/data_doctrine.xml @@ -5,6 +5,6 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + diff --git a/src/PatchManager/Bundle/Resources/config/handlers/state_machine.xml b/src/PatchManager/Bundle/Resources/config/handlers/state_machine.xml index 43b9b43..3bb96df 100644 --- a/src/PatchManager/Bundle/Resources/config/handlers/state_machine.xml +++ b/src/PatchManager/Bundle/Resources/config/handlers/state_machine.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + diff --git a/src/PatchManager/Bundle/Resources/config/services.xml b/src/PatchManager/Bundle/Resources/config/services.xml index 4ebdc80..c43aa27 100644 --- a/src/PatchManager/Bundle/Resources/config/services.xml +++ b/src/PatchManager/Bundle/Resources/config/services.xml @@ -5,16 +5,16 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + - + - + - + %patch_manager.strict_mode% diff --git a/src/PatchManager/Event/PatchManagerEvent.php b/src/PatchManager/Event/PatchManagerEvent.php index 962af56..48c16d6 100644 --- a/src/PatchManager/Event/PatchManagerEvent.php +++ b/src/PatchManager/Event/PatchManagerEvent.php @@ -1,9 +1,9 @@ setRequired(array('property', 'value')); } + + /** + * wether the handler is able to handle the given subject + * + * @param $subject + * @return bool + */ + public function canHandle($subject) + { + return true; + } } diff --git a/src/PatchManager/Handler/FiniteHandler.php b/src/PatchManager/Handler/FiniteHandler.php index 2fcb0e1..2e7e342 100644 --- a/src/PatchManager/Handler/FiniteHandler.php +++ b/src/PatchManager/Handler/FiniteHandler.php @@ -1,10 +1,10 @@ setDefined(array('check')) ->setDefaults(array('check' => false)); } + + /** + * wether the handler is able to handle the given subject + * + * @param $subject + * @return bool + */ + public function canHandle($subject) + { + return true; + } } diff --git a/src/PatchManager/MatchedPatchOperation.php b/src/PatchManager/MatchedPatchOperation.php index e040eb3..2d183bd 100644 --- a/src/PatchManager/MatchedPatchOperation.php +++ b/src/PatchManager/MatchedPatchOperation.php @@ -1,6 +1,6 @@ handlers; return $this->operations ->all() ->foldLeft( new Sequence(), - function (Sequence $matchedOperations, array $operationData) use ($handlers) { + function (Sequence $matchedOperations, array $operationData) use ($handlers, $subject) { + /** @var Option $handler */ $handler = $handlers->find(function (PatchOperationHandler $handler) use ($operationData) { return $operationData[Operations::OP_KEY_NAME] === $handler->getName(); }); if ($handler->isDefined()) { - $matchedOperations->add(MatchedPatchOperation::create($operationData, $handler->get())); + /** @var PatchOperationHandler $patchOperationHandler */ + $patchOperationHandler = $handler->get(); + if ($patchOperationHandler->canHandle($subject)) { + $matchedOperations->add(MatchedPatchOperation::create($operationData, $handler->get())); + } } return $matchedOperations; } @@ -60,13 +71,16 @@ function (Sequence $matchedOperations, array $operationData) use ($handlers) { } /** + * @param $subject + * * @return Sequence + * * @throws Exception\MissingOperationNameRequest * @throws Exception\MissingOperationRequest */ - public function getUnmatchedOperations() + public function getUnmatchedOperations($subject) { - $matchedOperations = $this->getMatchedOperations(); + $matchedOperations = $this->getMatchedOperations($subject); return $this->operations ->all() ->filter(function (array $operationData) use ($matchedOperations) { diff --git a/src/PatchManager/PatchManager.php b/src/PatchManager/PatchManager.php index 80a127b..6cfe0fd 100644 --- a/src/PatchManager/PatchManager.php +++ b/src/PatchManager/PatchManager.php @@ -1,10 +1,10 @@ strictMode && $this->operationMatcher->getMatchedOperations()->isEmpty()) { - throw new HandlerNotFoundException($this->operationMatcher->getUnmatchedOperations()); + $matchedOperations = $this->operationMatcher->getMatchedOperations($subject); + if ($this->strictMode && $matchedOperations->isEmpty()) { + throw new HandlerNotFoundException($this->operationMatcher->getUnmatchedOperations($subject)); } if (is_array($subject) || $subject instanceof \Traversable) { $this->handleMany($subject); } else { - foreach ($this->operationMatcher->getMatchedOperations() as $matchedPatchOperation) { + foreach ($matchedOperations as $matchedPatchOperation) { $this->doHandle($matchedPatchOperation, $subject); } } @@ -70,8 +71,8 @@ public function handle($subject) */ private function handleMany($subjects) { - foreach ($this->operationMatcher->getMatchedOperations() as $matchedPatchOperation) { - foreach ($subjects as $subject) { + foreach ($subjects as $subject) { + foreach ($this->operationMatcher->getMatchedOperations($subject) as $matchedPatchOperation) { $this->doHandle($matchedPatchOperation, $subject); } } diff --git a/src/PatchManager/PatchOperationHandler.php b/src/PatchManager/PatchOperationHandler.php index aac59f0..e1447c4 100644 --- a/src/PatchManager/PatchOperationHandler.php +++ b/src/PatchManager/PatchOperationHandler.php @@ -1,6 +1,6 @@ metadata->shouldReceive('getAssociationTargetClass')->andReturn('TestClass'); $this->metadata->shouldReceive('getTypeOfField')->andReturnNull()->byDefault(); $this->em->shouldReceive('getMetadataFactory->getMetadataFor') - ->with('PatchManager\Handler\DataDoctrineSubject') + ->with('Cypress\PatchManager\Handler\DataDoctrineSubject') ->andReturn($this->metadata); $this->handler = new DataDoctrineHandler($this->em); } diff --git a/tests/PatchManager/Handler/DataHandlerTest.php b/tests/PatchManager/Handler/DataHandlerTest.php index 97791ed..5016b77 100644 --- a/tests/PatchManager/Handler/DataHandlerTest.php +++ b/tests/PatchManager/Handler/DataHandlerTest.php @@ -1,11 +1,11 @@ ops = new Sequence(); $this->ops->add(array('op' => 'data')); $operations->shouldReceive('all')->andReturn($this->ops)->byDefault(); @@ -32,24 +32,24 @@ public function setUp() public function test_getMatchedOperations_without_handlers() { - $this->assertEquals(new Sequence(), $this->matcher->getMatchedOperations()); + $this->assertEquals(new Sequence(), $this->matcher->getMatchedOperations('test')); } public function test_getMatchedOperations_with_handler_not_matching() { $this->matcher->addHandler($this->mockHandler('method')); - $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations()); - $this->assertCount(0, $this->matcher->getMatchedOperations()); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations('test')); + $this->assertCount(0, $this->matcher->getMatchedOperations('test')); } public function test_getMatchedOperations_with_matching_handler() { $this->matcher->addHandler($this->mockHandler('data')); - $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations()); - $this->assertCount(1, $this->matcher->getMatchedOperations()); - $mpos = $this->matcher->getMatchedOperations(); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations('test')); + $this->assertCount(1, $this->matcher->getMatchedOperations('test')); + $mpos = $this->matcher->getMatchedOperations('test'); $this->assertInstanceOf( - 'PatchManager\MatchedPatchOperation', + 'Cypress\PatchManager\MatchedPatchOperation', $mpos->find($this->handlerNameMatcher('data'))->get() ); } @@ -58,8 +58,8 @@ public function test_getMatchedOperations_with_multiple_operations_matching() { $this->ops->add(array('op' => 'data')); $this->matcher->addHandler($this->mockHandler('data')); - $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations()); - $mpos = $this->matcher->getMatchedOperations(); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations('test')); + $mpos = $this->matcher->getMatchedOperations('test'); $this->assertCount(2, $mpos->filter($this->handlerNameMatcher('data'))); } @@ -68,11 +68,11 @@ public function test_getMatchedOperations_with_multiple_operations_matching_mult $this->ops->add(array('op' => 'method')); $this->matcher->addHandler($this->mockHandler('data')); $this->matcher->addHandler($this->mockHandler('method')); - $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations()); - $this->assertCount(2, $this->matcher->getMatchedOperations()); - $mpos = $this->matcher->getMatchedOperations(); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations('test')); + $this->assertCount(2, $this->matcher->getMatchedOperations('test')); + $mpos = $this->matcher->getMatchedOperations('test'); $this->assertInstanceOf( - 'PatchManager\MatchedPatchOperation', + 'Cypress\PatchManager\MatchedPatchOperation', $mpos->find($this->handlerNameMatcher('data'))->get() ); } @@ -80,10 +80,21 @@ public function test_getMatchedOperations_with_multiple_operations_matching_mult public function test_getUnmatchedOperations_with_handler_not_matching() { $this->matcher->addHandler($this->mockHandler('method')); - $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations()); - $this->assertCount(1, $this->matcher->getUnmatchedOperations()); - $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getUnmatchedOperations()); - $this->assertEquals(new Sequence(array('data')), $this->matcher->getUnmatchedOperations()); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations('test')); + $this->assertCount(1, $this->matcher->getUnmatchedOperations('test')); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getUnmatchedOperations('test')); + $this->assertEquals(new Sequence(array('data')), $this->matcher->getUnmatchedOperations('test')); + } + + public function test_handler_that_responds_false_to_canHandle() + { + $this->matcher->addHandler($this->mockHandler('data', false)); + $this->assertCount(0, $this->matcher->getMatchedOperations('test')); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getMatchedOperations('test')); + $this->assertEquals(new Sequence(), $this->matcher->getMatchedOperations('test')); + $this->assertCount(1, $this->matcher->getUnmatchedOperations('test')); + $this->assertInstanceOf('PhpCollection\Sequence', $this->matcher->getUnmatchedOperations('test')); + $this->assertEquals(new Sequence(array('data')), $this->matcher->getUnmatchedOperations('test')); } private function handlerNameMatcher($name) diff --git a/tests/PatchManager/PatchManagerTest.php b/tests/PatchManager/PatchManagerTest.php index b223b55..5082d11 100644 --- a/tests/PatchManager/PatchManagerTest.php +++ b/tests/PatchManager/PatchManagerTest.php @@ -1,13 +1,14 @@ operationMatcher = m::mock('PatchManager\OperationMatcher'); + $this->operationMatcher = m::mock('Cypress\PatchManager\OperationMatcher'); $this->operationMatcher->shouldReceive('getMatchedOperations') ->andReturn(new Sequence())->byDefault(); $this->operationMatcher->shouldReceive('getUnmatchedOperations') @@ -54,7 +55,7 @@ public function test_handle_without_required_keys() } /** - * @expectedException \PatchManager\Exception\HandlerNotFoundException + * @expectedException \Cypress\PatchManager\Exception\HandlerNotFoundException * @expectedExceptionMessage 'test' */ public function test_strict_mode() @@ -65,7 +66,7 @@ public function test_strict_mode() } /** - * @expectedException \PatchManager\Exception\HandlerNotFoundException + * @expectedException \Cypress\PatchManager\Exception\HandlerNotFoundException * @expectedExceptionMessage 'test, test2' */ public function test_strict_mode_multiple_ops() diff --git a/tests/PatchManager/PatchManagerTestCase.php b/tests/PatchManager/PatchManagerTestCase.php index 2372108..a96156c 100644 --- a/tests/PatchManager/PatchManagerTestCase.php +++ b/tests/PatchManager/PatchManagerTestCase.php @@ -1,24 +1,36 @@ shouldReceive('getName')->andReturn($name)->byDefault(); } $handler->shouldReceive('getRequiredKeys')->andReturn(array())->byDefault(); $handler->shouldReceive('configureOptions')->andReturn(array())->byDefault(); + $handler->shouldReceive('canHandle')->andReturn($canHandle); return $handler; } + /** + * @param null $handlerName + * @return MatchedPatchOperation + */ protected function getMatchedPatchOperation($handlerName = null) { return MatchedPatchOperation::create(array(), $this->mockHandler($handlerName)); diff --git a/tests/PatchManager/Request/OperationsTest.php b/tests/PatchManager/Request/OperationsTest.php index d6b9d8f..0212416 100644 --- a/tests/PatchManager/Request/OperationsTest.php +++ b/tests/PatchManager/Request/OperationsTest.php @@ -1,15 +1,16 @@