Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1.31.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
brettminnie committed Dec 10, 2014
2 parents 4205a76 + ce2a8ef commit 85bfdfa
Show file tree
Hide file tree
Showing 5 changed files with 461 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/Opg/Common/Model/Entity/FactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Opg\Common\Model\Entity;

use JMS\Serializer\Serializer;
use Opg\Core\Model\Entity\LegalEntity\LegalEntity;

/**
* Interface FactoryInterface
* @package Opg\Common\Model\Entity
*/
interface FactoryInterface
{
/**
* @param array $data
* @param Serializer $serializer
* @return LegalEntity
* @throws \Exception
*/
public static function create(array $data, Serializer $serializer);
}
15 changes: 11 additions & 4 deletions src/Opg/Core/Model/Entity/CaseActor/PersonFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
namespace Opg\Core\Model\Entity\CaseActor;

use JMS\Serializer\Serializer;
use Opg\Common\Model\Entity\FactoryInterface;
use Opg\Core\Model\Entity\LegalEntity\LegalEntity;

/**
* Class PersonFactory
* @package Opg\Core\Model\Entity\CaseActor
* @codeCoverageIgnore
* serializer to be mocked out
*/
class PersonFactory
class PersonFactory implements FactoryInterface
{
/**
* @param array $data
* @param Serializer $serializer
* @return LegalEntity
* @throws \Exception
*/
public static function create(array $data, Serializer $serializer)
{
$personType = null;
Expand Down Expand Up @@ -68,14 +75,14 @@ public static function create(array $data, Serializer $serializer)

// Try-Catch added due to https://github.com/schmittjoh/serializer/issues/216
try {
/** @var Person $person */
$person = $serializer->deserialize(
json_encode($data),
$personType,
'json'
);
} catch (\Exception $e) {
//@todo add logging for this or return the actual exception
$person = null;
throw $e;
}

return $person;
Expand Down
55 changes: 55 additions & 0 deletions src/Opg/Core/Model/Entity/CaseItem/CaseItemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Opg\Core\Model\Entity\CaseItem;

use JMS\Serializer\Serializer;
use Opg\Common\Model\Entity\FactoryInterface;
use Opg\Core\Model\Entity\LegalEntity\LegalEntity;

/**
* Class CaseItemFactory
* @package Opg\Core\Model\Entity\CaseItem
*/
class CaseItemFactory implements FactoryInterface
{
/**
* @param array $data
* @param Serializer $serializer
* @return LegalEntity
* @throws \Exception
*/
public static function create(array $data, Serializer $serializer)
{
$caseType = null;
$data['caseType'] = (isset($data['caseType'])) ? $data['caseType'] : 'Lpa';

if (!empty($data['caseType'])) {
switch ($data['caseType']) {
case "Epa" :
$caseType = "Opg\\Core\\Model\\Entity\\CaseItem\\PowerOfAttorney\\Epa";
break;
case "Order" :
$caseType = "Opg\\Core\\Model\\Entity\\CaseItem\\Deputyship\\Order";
break;
default:
$caseType = "Opg\\Core\\Model\\Entity\\CaseItem\\PowerOfAttorney\\Lpa";
break;
}
} else {
throw new \Exception('Cannot build unknown case type.');
}

try {
/** @var CaseItem $case */
$case = $serializer->deserialize(
json_encode($data),
$caseType,
'json'
);
} catch (\Exception $e) {
throw $e;
}

return $case;
}
}
267 changes: 267 additions & 0 deletions tests/OpgTest/Core/Model/Entity/CaseActor/PersonFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<?php

namespace OpgTest\Core\Model\Entity\CaseActor;


use JMS\Serializer\Serializer;
use Opg\Core\Model\Entity\CaseActor\Attorney;
use Opg\Core\Model\Entity\CaseActor\CertificateProvider;
use Opg\Core\Model\Entity\CaseActor\Client;
use Opg\Core\Model\Entity\CaseActor\Correspondent;
use Opg\Core\Model\Entity\CaseActor\Deputy;
use Opg\Core\Model\Entity\CaseActor\Donor;
use Opg\Core\Model\Entity\CaseActor\FeePayer;
use Opg\Core\Model\Entity\CaseActor\NonCaseContact;
use Opg\Core\Model\Entity\CaseActor\NotifiedAttorney;
use Opg\Core\Model\Entity\CaseActor\NotifiedPerson;
use Opg\Core\Model\Entity\CaseActor\NotifiedRelative;
use Opg\Core\Model\Entity\CaseActor\PersonFactory;
use Opg\Core\Model\Entity\CaseActor\PersonNotifyDonor;
use Opg\Core\Model\Entity\CaseActor\ReplacementAttorney;
use Opg\Core\Model\Entity\CaseActor\TrustCorporation;

class PersonFactoryTest extends \PHPUnit_Framework_TestCase
{
/** @var \Mockery\MockInterface */
protected $serializer;

public function setUp()
{
$this->serializer = \Mockery::mock("JMS\Serializer\Serializer");
}

/**
* @expectedException \Exception
*/
public function testEmptyTypeThrowsException()
{
$data = array('personType' => '', 'id' => 1);
PersonFactory::create($data, $this->serializer);
}

/**
* @expectedException \Exception
*/
public function testUnknownIdReturnsNull()
{
$data = array('personType' => 'Donor', 'id' => -1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andThrow(new \Exception());

PersonFactory::create($data, $this->serializer);
}

public function testUnknownTypeReturnsNonCaseContact()
{
$data = array('personType' => 'Doctor', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new NonCaseContact())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof NonCaseContact);
$this->assertEquals($data['id'], $person->getId());
}

public function testNoPersonTypeReturnsDonor()
{
$data = array('id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new Donor())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof Donor);
$this->assertEquals($data['id'], $person->getId());
}

public function testAttorney()
{
$data = array('personType' => 'Attorney', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new Attorney())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof Attorney);
$this->assertEquals($data['id'], $person->getId());
}

public function testReplacementAttorney()
{
$data = array('personType' => 'ReplacementAttorney', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new ReplacementAttorney())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof ReplacementAttorney);
$this->assertEquals($data['id'], $person->getId());
}

public function testTrustCorporation()
{
$data = array('personType' => 'TrustCorporation', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new TrustCorporation())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof TrustCorporation);
$this->assertEquals($data['id'], $person->getId());
}

public function testCertificateProvider()
{
$data = array('personType' => 'CertificateProvider', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new CertificateProvider())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof CertificateProvider);
$this->assertEquals($data['id'], $person->getId());
}

public function testNotifiedPerson()
{
$data = array('personType' => 'NotifiedPerson', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new NotifiedPerson())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof NotifiedPerson);
$this->assertEquals($data['id'], $person->getId());
}

public function testCorrespondent()
{
$data = array('personType' => 'Correspondent', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new Correspondent())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof Correspondent);
$this->assertEquals($data['id'], $person->getId());
}

public function testNotifiedRelative()
{
$data = array('personType' => 'NotifiedRelative', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new NotifiedRelative())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof NotifiedRelative);
$this->assertEquals($data['id'], $person->getId());
}

public function testNotifiedAttorney()
{
$data = array('personType' => 'NotifiedAttorney', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new NotifiedAttorney())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof NotifiedAttorney);
$this->assertEquals($data['id'], $person->getId());
}

public function testPersonNotifyDonor()
{
$data = array('personType' => 'PersonNotifyDonor', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new PersonNotifyDonor())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof PersonNotifyDonor);
$this->assertEquals($data['id'], $person->getId());
}

public function testClient()
{
$data = array('personType' => 'Client', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new Client())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof Client);
$this->assertEquals($data['id'], $person->getId());
}

public function testDeputy()
{
$data = array('personType' => 'Deputy', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new Deputy())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof Deputy);
$this->assertEquals($data['id'], $person->getId());
}

public function testFeePayer()
{
$data = array('personType' => 'FeePayer', 'id' => 1);

$this->serializer
->shouldReceive('deserialize')
->withAnyArgs()
->andReturn((new FeePayer())->setId($data['id']));

$person = PersonFactory::create($data, $this->serializer);

$this->assertTrue($person instanceof FeePayer);
$this->assertEquals($data['id'], $person->getId());
}

}

0 comments on commit 85bfdfa

Please sign in to comment.