Skip to content

Commit a6c7c60

Browse files
author
Codeliner
committed
Add CargoRepository
1 parent af8e5f1 commit a6c7c60

File tree

8 files changed

+177
-7
lines changed

8 files changed

+177
-7
lines changed

module/Application/config/module.config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@
9999
'orm_default' => array(
100100
//Define custom doctrine types to map the ddd value objects
101101
'types' => array(
102-
'uid' => 'Application\Infrastructure\Type\UID',
103-
'trackingid' => 'Application\Infrastructure\Type\TrackingId',
102+
'uid' => 'Application\Infrastructure\Persistence\Doctrine\Type\UID',
103+
'trackingid' => 'Application\Infrastructure\Persistence\Doctrine\Type\TrackingId',
104104
),
105105
),
106106
),

module/Application/src/Application/Domain/Model/Cargo/Cargo.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@
1010

1111
use Application\Domain\Shared\EntityInterface;
1212
use Application\Domain\Shared\UID;
13+
use Doctrine\ORM\Mapping\Entity;
14+
use Doctrine\ORM\Mapping\Table;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Id;
1317
/**
14-
* Cargo
18+
* A Cargo. This is the central class in the domain model.
19+
*
20+
* A cargo is identified by a unique tracking id.
21+
*
22+
* ---Annotations required by Doctrine---
23+
* @Entity(repositoryClass="Application\Infrastructure\Persistence\Doctrine\CargoRepositoryDoctrine")
24+
* @Table(name="cargo")
25+
* --------------------------------------
1526
*
1627
* @author Alexander Miertsch <[email protected]>
1728
*/
@@ -20,6 +31,11 @@ class Cargo implements EntityInterface
2031
/**
2132
* Unique Identifier
2233
*
34+
* ---Annotations required by Doctrine---
35+
* @Id
36+
* @Column(type="trackingid", length=13, unique=true, nullable=false)
37+
* --------------------------------------
38+
*
2339
* @var TrackingId
2440
*/
2541
protected $trackingId;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Domain\Model\Cargo;
10+
11+
/**
12+
* CargoRepository to load and store Cargos
13+
*
14+
* @author Alexander Miertsch <[email protected]>
15+
*/
16+
interface CargoRepositoryInterface
17+
{
18+
/**
19+
* Finds a cargo using given id.
20+
*
21+
* @param TrackingId $trackingId Id
22+
* @return Cargo if found, else {@code null}
23+
*/
24+
public function findCargo(TrackingId $trackingId);
25+
26+
/**
27+
* Finds all cargo.
28+
*
29+
* @return Cargo[] all Cargo
30+
*/
31+
public function findAll();
32+
33+
/**
34+
* Saves given cargo.
35+
*
36+
* @param Cargo $cargo Cargo to save
37+
*/
38+
public function store(Cargo $cargo);
39+
40+
/**
41+
* @return TrackingId A unique, generated tracking Id.
42+
*/
43+
public function getNextTrackingId();
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace Application\Infrastructure\Persistence\Doctrine;
10+
11+
use Application\Domain\Model\Cargo\CargoRepositoryInterface;
12+
use Application\Domain\Model\Cargo\Cargo;
13+
use Application\Domain\Model\Cargo\TrackingId;
14+
use Doctrine\ORM\EntityRepository;
15+
/**
16+
* CargoRepositoryDoctrine
17+
*
18+
* @author Alexander Miertsch <[email protected]>
19+
*/
20+
class CargoRepositoryDoctrine extends EntityRepository implements CargoRepositoryInterface
21+
{
22+
/**
23+
* {@inheritDoc}
24+
*/
25+
public function findCargo(TrackingId $trackingId)
26+
{
27+
return $this->find($trackingId);
28+
}
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function store(Cargo $cargo)
34+
{
35+
$this->getEntityManager()->persist($cargo);
36+
$this->getEntityManager()->flush($cargo);
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function getNextTrackingId()
43+
{
44+
return new TrackingId(uniqid());
45+
}
46+
47+
}

module/Application/src/Application/Infrastructure/Type/TrackingId.php renamed to module/Application/src/Application/Infrastructure/Persistence/Doctrine/Type/TrackingId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* For the full copyright and license information, please view the LICENSE
77
* file that was distributed with this source code.
88
*/
9-
namespace Application\Infrastrucure\Type;
9+
namespace Application\Infrastructure\Persistence\Doctrine\Type;
1010

1111
use Doctrine\DBAL\Types\TextType;
1212
use Doctrine\DBAL\Platforms\AbstractPlatform;

module/Application/src/Application/Infrastructure/Type/UID.php renamed to module/Application/src/Application/Infrastructure/Persistence/Doctrine/Type/UID.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* For the full copyright and license information, please view the LICENSE
77
* file that was distributed with this source code.
88
*/
9-
namespace Application\Infrastrucure\Type;
9+
namespace Application\Infrastructure\Persistence\Doctrine\Type;
1010

1111
use Doctrine\DBAL\Types\TextType;
1212
use Doctrine\DBAL\Platforms\AbstractPlatform;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
namespace ApplicationTest\Infrastructure\Persistence\Doctrine;
10+
11+
use ApplicationTest\TestCase;
12+
use Application\Domain\Model\Cargo;
13+
use Application\Infrastrucure\Persistence\Doctrine\CargoRepositoryDoctrine;
14+
/**
15+
* CargoRepositoryDoctrineTest
16+
*
17+
* @author Alexander Miertsch <[email protected]>
18+
*/
19+
class CargoRepositoryDoctrineTest extends TestCase
20+
{
21+
/**
22+
*
23+
* @var Cargo\CargoRepositoryInterface
24+
*/
25+
protected $cargoRepository;
26+
27+
protected function setUp()
28+
{
29+
$this->createEntitySchema('Application\Domain\Model\Cargo\Cargo');
30+
31+
$this->cargoRepository = $this->getTestEntityManager()->getRepository('Application\Domain\Model\Cargo\Cargo');
32+
}
33+
34+
public function testGetNextTrackingId()
35+
{
36+
$trackingId = $this->cargoRepository->getNextTrackingId();
37+
38+
$this->assertInstanceOf('Application\Domain\Model\Cargo\TrackingId', $trackingId);
39+
}
40+
41+
public function testStoreAndFindCargo()
42+
{
43+
$trackingId = $this->cargoRepository->getNextTrackingId();
44+
$cargo = new Cargo\Cargo($trackingId);
45+
46+
$this->cargoRepository->store($cargo);
47+
48+
$checkCargo = $this->cargoRepository->findCargo($trackingId);
49+
50+
$this->assertTrue($cargo->sameIdentityAs($checkCargo));
51+
}
52+
}

module/Application/tests/PHPUnit/ApplicationTest/TestCase.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use PHPUnit_Framework_TestCase;
1212
use Doctrine\ORM\EntityManager;
13+
use Doctrine\DBAL\Types\Type;
1314
/**
1415
* TestCase
1516
*
@@ -47,13 +48,23 @@ public function getTestEntityManager()
4748

4849
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
4950
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
50-
51+
5152
$this->entityManager = \Doctrine\ORM\EntityManager::create(array(
5253
'driver' => 'pdo_sqlite',
5354
'memory' => true
5455
), $config);
56+
57+
//Add custom DDD types to map ValueObjects correctly
58+
if (!Type::hasType('trackingid')) {
59+
Type::addType('trackingid', 'Application\Infrastructure\Persistence\Doctrine\Type\TrackingId');
60+
}
61+
62+
if (!Type::hasType('uid')) {
63+
Type::addType('uid', 'Application\Infrastructure\Persistence\Doctrine\Type\UID');
64+
}
5565
}
56-
66+
67+
5768
return $this->entityManager;
5869
}
5970

0 commit comments

Comments
 (0)