|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the prooph/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 CodelinerTest\CargoBackend; |
|
10
|
|
|
|
|
11
|
|
|
use Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type\LegsDoctrineType; |
|
12
|
|
|
use Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type\TrackingIdDoctrineType; |
|
13
|
|
|
use PHPUnit_Framework_TestCase; |
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
|
15
|
|
|
use Doctrine\DBAL\Types\Type; |
|
16
|
|
|
/** |
|
17
|
|
|
* TestCase |
|
18
|
|
|
* |
|
19
|
|
|
* @author Alexander Miertsch <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class TestCase extends PHPUnit_Framework_TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
protected $entityManager; |
|
24
|
|
|
protected $schemaTool; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @return EntityManager |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getTestEntityManager() |
|
31
|
|
|
{ |
|
32
|
|
|
if (null === $this->entityManager) { |
|
33
|
|
|
$conn = \Doctrine\DBAL\DriverManager::getConnection(array( |
|
|
|
|
|
|
34
|
|
|
'driver' => 'pdo_sqlite', |
|
35
|
|
|
'dbname' => ':memory:' |
|
36
|
|
|
)); |
|
37
|
|
|
|
|
38
|
|
|
$config = new \Doctrine\ORM\Configuration(); |
|
39
|
|
|
|
|
40
|
|
|
$config->setAutoGenerateProxyClasses(true); |
|
41
|
|
|
$config->setProxyDir(\sys_get_temp_dir()); |
|
42
|
|
|
$config->setProxyNamespace(get_class($this) . '\Entities'); |
|
43
|
|
|
$config->setMetadataDriverImpl( |
|
44
|
|
|
new \Doctrine\ORM\Mapping\Driver\XmlDriver( |
|
45
|
|
|
array( |
|
46
|
|
|
__DIR__ . '/../src/Infrastructure/Persistence/Doctrine/ORM' |
|
47
|
|
|
) |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$config->setNamingStrategy(new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy()); |
|
52
|
|
|
|
|
53
|
|
|
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); |
|
54
|
|
|
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); |
|
55
|
|
|
|
|
56
|
|
|
$this->entityManager = \Doctrine\ORM\EntityManager::create(array( |
|
57
|
|
|
'driver' => 'pdo_sqlite', |
|
58
|
|
|
'memory' => true |
|
59
|
|
|
), $config); |
|
60
|
|
|
|
|
61
|
|
|
//Add custom DDD types to map ValueObjects correctly |
|
62
|
|
|
if (!Type::hasType('cargo_itinerary_legs')) { |
|
63
|
|
|
Type::addType('cargo_itinerary_legs', LegsDoctrineType::class); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!Type::hasType('cargo_tracking_id')) { |
|
67
|
|
|
Type::addType('cargo_tracking_id', TrackingIdDoctrineType::class); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $this->entityManager; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function createEntitySchema($entityNameOrNamespace, $pathToEntityDir = null) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!is_null($pathToEntityDir)) { |
|
78
|
|
|
$dir = opendir($pathToEntityDir); |
|
79
|
|
|
|
|
80
|
|
|
$entityNameOrNamespace = trim($entityNameOrNamespace, '\\'); |
|
81
|
|
|
|
|
82
|
|
|
while($file = readdir($dir)) { |
|
83
|
|
|
if (0 !== strpos($file, '.')) { |
|
84
|
|
|
$entityClass = $entityNameOrNamespace . '\\' . str_replace('.php', '', $file); |
|
85
|
|
|
$this->createEntitySchema($entityClass); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (null === $this->schemaTool) { |
|
93
|
|
|
$this->schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->getTestEntityManager()); |
|
94
|
|
|
} |
|
95
|
|
|
$schema = $this->getTestEntityManager()->getClassMetadata($entityNameOrNamespace); |
|
96
|
|
|
$this->schemaTool->dropSchema(array($schema)); |
|
97
|
|
|
$this->schemaTool->createSchema(array($schema)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.