|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @license See the file LICENSE for copying permission. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Thorr\Persistence\Test; |
|
7
|
|
|
|
|
8
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
|
9
|
|
|
use Thorr\Persistence; |
|
10
|
|
|
use Thorr\Persistence\DataMapper; |
|
11
|
|
|
use Zend\InputFilter\InputFilter; |
|
12
|
|
|
use Zend\Mvc\Application; |
|
13
|
|
|
use Zend\ServiceManager\ServiceManager; |
|
14
|
|
|
|
|
15
|
|
|
class ModuleIntegrationTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $appConfig; |
|
21
|
|
|
|
|
22
|
|
|
protected function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->serviceManager = new ServiceManager(); |
|
|
|
|
|
|
25
|
|
|
$this->appConfig = [ |
|
26
|
|
|
'modules' => [ |
|
27
|
|
|
'Thorr\Persistence', |
|
28
|
|
|
], |
|
29
|
|
|
'module_listener_options' => [], |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testCanLoadModule() |
|
34
|
|
|
{ |
|
35
|
|
|
$app = Application::init($this->appConfig); |
|
36
|
|
|
$loadedModules = $app->getServiceManager()->get('ModuleManager')->getLoadedModules(); |
|
37
|
|
|
$this->assertArrayHasKey('Thorr\Persistence', $loadedModules); |
|
38
|
|
|
$this->assertInstanceOf(Persistence\Module::class, $loadedModules['Thorr\Persistence']); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testServicesAreRegistered() |
|
42
|
|
|
{ |
|
43
|
|
|
$app = Application::init($this->appConfig); |
|
44
|
|
|
$serviceManager = $app->getServiceManager(); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertTrue($serviceManager->has('DataMapperManager')); |
|
47
|
|
|
$this->assertTrue($serviceManager->has(DataMapper\Manager\DataMapperManager::class)); |
|
48
|
|
|
|
|
49
|
|
|
$dataMapperManager = $serviceManager->get(DataMapper\Manager\DataMapperManager::class); |
|
50
|
|
|
$this->assertInstanceOf(DataMapper\Manager\DataMapperManager::class, $dataMapperManager); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testValidatorsAreRegistered() |
|
54
|
|
|
{ |
|
55
|
|
|
$app = Application::init($this->appConfig); |
|
56
|
|
|
$validatorManager = $app->getServiceManager()->get('ValidatorManager'); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertTrue($validatorManager->has(Persistence\Validator\EntityExistsValidator::class)); |
|
59
|
|
|
$this->assertTrue($validatorManager->has(Persistence\Validator\EntityNotExistsValidator::class)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testValidatorConfigCanBeInitializedByZendInputFilterFactory() |
|
63
|
|
|
{ |
|
64
|
|
|
$fooMapper = $this->getMock(DataMapper\EntityFinderInterface::class); |
|
65
|
|
|
$fooMapper->expects($this->any())->method('getEntityClass')->willReturn(Asset\Entity::class); |
|
66
|
|
|
$dmm = new DataMapper\Manager\DataMapperManager( |
|
67
|
|
|
new DataMapper\Manager\DataMapperManagerConfig( |
|
68
|
|
|
[ |
|
69
|
|
|
'entity_data_mapper_map' => [ |
|
70
|
|
|
Asset\Entity::class => 'FooMapper', |
|
71
|
|
|
], |
|
72
|
|
|
'services' => [ |
|
73
|
|
|
'FooMapper' => $fooMapper, |
|
74
|
|
|
], |
|
75
|
|
|
] |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$app = Application::init($this->appConfig); |
|
80
|
|
|
$sm = $app->getServiceManager(); |
|
81
|
|
|
$sm->setAllowOverride(true); |
|
82
|
|
|
$sm->setService(DataMapper\Manager\DataMapperManager::class, $dmm); |
|
83
|
|
|
|
|
84
|
|
|
$inputFilter = new InputFilter(); |
|
85
|
|
|
$app->getServiceManager()->get('InputFilterManager')->populateFactory($inputFilter); |
|
86
|
|
|
|
|
87
|
|
|
$inputFilter->add([ |
|
88
|
|
|
'name' => 'test', |
|
89
|
|
|
'validators' => [ |
|
90
|
|
|
[ |
|
91
|
|
|
'name' => Persistence\Validator\EntityExistsValidator::class, |
|
92
|
|
|
'options' => [ |
|
93
|
|
|
'entity_class' => Asset\Entity::class, |
|
94
|
|
|
], |
|
95
|
|
|
], |
|
96
|
|
|
[ |
|
97
|
|
|
'name' => Persistence\Validator\EntityNotExistsValidator::class, |
|
98
|
|
|
'options' => [ |
|
99
|
|
|
'entity_class' => Asset\Entity::class, |
|
100
|
|
|
], |
|
101
|
|
|
], |
|
102
|
|
|
], |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertInstanceOf( |
|
106
|
|
|
Persistence\Validator\EntityExistsValidator::class, |
|
107
|
|
|
$inputFilter->get('test')->getValidatorChain()->getValidators()[0]['instance'] |
|
|
|
|
|
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertInstanceOf( |
|
111
|
|
|
Persistence\Validator\EntityNotExistsValidator::class, |
|
112
|
|
|
$inputFilter->get('test')->getValidatorChain()->getValidators()[1]['instance'] |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: