From c882a7d6518301266f6baac5306e4fb71b8e8035 Mon Sep 17 00:00:00 2001 From: Alexis Thinardon Date: Fri, 8 Jun 2018 11:05:45 +0200 Subject: [PATCH] Add OptimusException --- CHANGELOG.md | 1 + src/AbstractMappingTransformer.php | 9 +++++---- src/Exception/OptimusException.php | 7 +++++++ tests/AbstractMappingTransformerTest.php | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/Exception/OptimusException.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e16e726..b6384b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +- Add an OptimusException ## [0.1.1] - 2018-03-05 ### Fixed diff --git a/src/AbstractMappingTransformer.php b/src/AbstractMappingTransformer.php index 5ec6c49..669335e 100644 --- a/src/AbstractMappingTransformer.php +++ b/src/AbstractMappingTransformer.php @@ -2,6 +2,7 @@ namespace Biig\Optimus; +use Biig\Optimus\Exception\OptimusException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessor; @@ -34,7 +35,7 @@ abstract public function transform(array $data); * @param array $mapping The mapping to use to transform input array * @param array $data The input array * @return array The output array - * @throws \Exception + * @throws OptimusException */ protected function transformFromMapping(array $mapping, array $data) { @@ -46,9 +47,9 @@ protected function transformFromMapping(array $mapping, array $data) // Get our node value $nodeValue = $this->getNodeValue($node, $data); - // If we didn't get value but field was required, throw Exception + // If we didn't get value but field was required, throw OptimusException if (isset($node['required']) && true === $node['required'] && null === $nodeValue) { - throw new \Exception('Field ' . $node['from'] . ' required.'); + throw new OptimusException('Field ' . $node['from'] . ' required.'); } if (isset($node['dependencies'])) { @@ -61,7 +62,7 @@ protected function transformFromMapping(array $mapping, array $data) } if ($dependenciesExist && null === $nodeValue) { - throw new \Exception('Field ' . $node['from'] . ' required if dependencies true.'); + throw new OptimusException('Field ' . $node['from'] . ' required if dependencies true.'); } } diff --git a/src/Exception/OptimusException.php b/src/Exception/OptimusException.php new file mode 100644 index 0000000..8d5bcc0 --- /dev/null +++ b/src/Exception/OptimusException.php @@ -0,0 +1,7 @@ +assertEquals($expected, $result); } + + /** + * @expectedException \Biig\Optimus\Exception\OptimusException + * @expectedExceptionMessage Field foo required. + */ + public function testItThrowAnException() + { + $mapping = [ + 'node' => [ + 'from' => 'foo', + 'to' => 'bar', + 'required' => true, + ], + ]; + + $transformer = new ProxyDummyTransformer(); + $transformer->transformFromMapping($mapping, []); + } } class ProxyDummyTransformer extends AbstractMappingTransformer