Skip to content

Commit

Permalink
Merge pull request #2 from athos7933/optimus-exception
Browse files Browse the repository at this point in the history
Add OptimusException
  • Loading branch information
Awkan authored Jun 8, 2018
2 parents de5d3dd + c882a7d commit 653c79e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions src/AbstractMappingTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Biig\Optimus;

use Biig\Optimus\Exception\OptimusException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

Expand Down Expand Up @@ -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)
{
Expand All @@ -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'])) {
Expand All @@ -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.');
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Exception/OptimusException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Biig\Optimus\Exception;

class OptimusException extends \Exception
{
}
19 changes: 19 additions & 0 deletions tests/AbstractMappingTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Biig\Optimus\Tests;

use Biig\Optimus\Exception\OptimusException;
use PHPUnit\Framework\TestCase;
use Biig\Optimus\AbstractMappingTransformer;

Expand Down Expand Up @@ -118,6 +119,24 @@ public function testTransformFromToDefaultFunction()

$this->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
Expand Down

0 comments on commit 653c79e

Please sign in to comment.