|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Tests\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
7
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
8
|
|
|
use Nelmio\Alice\Fixtures; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
11
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class VictoireWebTestCase. |
|
17
|
|
|
* |
|
18
|
|
|
* This class can be used for functional tests |
|
19
|
|
|
* It provides : |
|
20
|
|
|
* - a property to access the container |
|
21
|
|
|
* - a property to access Doctrine ManagerRegistry |
|
22
|
|
|
* - a property to access the Entity Manager |
|
23
|
|
|
* - a method to login into Victoire |
|
24
|
|
|
* - a method to reset database schema |
|
25
|
|
|
* - a method to load seeds and other fixtures |
|
26
|
|
|
* - a method to reset ViewReferences |
|
27
|
|
|
*/ |
|
28
|
|
|
class VictoireWebTestCase extends WebTestCase |
|
29
|
|
|
{ |
|
30
|
|
|
const VICTOIRE_FIREWALL = 'main'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ContainerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $container; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ManagerRegistry |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $doctrine; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ObjectManager |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $entityManager; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Start tests by set up Kernel, Doctrine and Entity Manager. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function setUp() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->setUpSymfonyKernel(); |
|
53
|
|
|
$this->setUpDoctrine(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Drop and create database schema. |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Doctrine\ORM\Tools\ToolsException |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function resetSchema() |
|
62
|
|
|
{ |
|
63
|
|
|
if ($metadata = $this->getMetadata()) { |
|
64
|
|
|
$schemaTool = new SchemaTool($this->entityManager); |
|
|
|
|
|
|
65
|
|
|
$schemaTool->dropSchema($metadata); |
|
66
|
|
|
$schemaTool->createSchema($metadata); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Victoire login. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Client $client |
|
74
|
|
|
* @param array $roles |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function logIn(Client $client, array $roles) |
|
77
|
|
|
{ |
|
78
|
|
|
$session = $client->getContainer()->get('session'); |
|
79
|
|
|
|
|
80
|
|
|
$token = new UsernamePasswordToken('test', null, self::VICTOIRE_FIREWALL, $roles); |
|
81
|
|
|
$session->set('_security_'.self::VICTOIRE_FIREWALL, serialize($token)); |
|
82
|
|
|
$session->save(); |
|
83
|
|
|
|
|
84
|
|
|
$cookie = new Cookie($session->getName(), $session->getId()); |
|
85
|
|
|
$client->getCookieJar()->set($cookie); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Load seeds fixtures and given fixtures. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $fixtures |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function loadFixtures(array $fixtures = []) |
|
94
|
|
|
{ |
|
95
|
|
|
$fixtures = array_merge($fixtures, $this->getSeeds()); |
|
96
|
|
|
$faker = new \Faker\Generator(); |
|
97
|
|
|
Fixtures::load( |
|
98
|
|
|
$fixtures, |
|
99
|
|
|
$this->entityManager, |
|
100
|
|
|
[ |
|
101
|
|
|
'providers' => [ |
|
102
|
|
|
$this, |
|
103
|
|
|
new \Faker\Provider\Base($faker), |
|
104
|
|
|
], |
|
105
|
|
|
] |
|
106
|
|
|
); |
|
107
|
|
|
$this->resetViewsReference(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Reset Victoire ViewReferences. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function resetViewsReference() |
|
114
|
|
|
{ |
|
115
|
|
|
$viewsReferences = $this->container->get('victoire_core.view_helper')->buildViewsReferences(); |
|
116
|
|
|
$this->container->get('victoire_view_reference.manager')->saveReferences($viewsReferences); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set up Symfony Kernel and provide container. |
|
121
|
|
|
*/ |
|
122
|
|
|
private function setUpSymfonyKernel() |
|
123
|
|
|
{ |
|
124
|
|
|
static::$kernel = $this->createKernel(); |
|
125
|
|
|
static::$kernel->boot(); |
|
126
|
|
|
|
|
127
|
|
|
$this->container = static::$kernel->getContainer(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Provide doctrine and entity manager. |
|
132
|
|
|
*/ |
|
133
|
|
|
private function setUpDoctrine() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->doctrine = $this->createDoctrineRegistry(); |
|
136
|
|
|
$this->entityManager = $this->doctrine->getManager(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get seeds files for Users, Pages and Templates. |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
private function getSeeds() |
|
145
|
|
|
{ |
|
146
|
|
|
return [ |
|
147
|
|
|
__DIR__.'/../App/src/Acme/AppBundle/DataFixtures/Seeds/ORM/User/user.yml', |
|
148
|
|
|
__DIR__.'/../App/src/Acme/AppBundle/DataFixtures/Seeds/ORM/View/template.yml', |
|
149
|
|
|
__DIR__.'/../App/src/Acme/AppBundle/DataFixtures/Seeds/ORM/View/page.yml', |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns all metadata by default. |
|
155
|
|
|
* |
|
156
|
|
|
* Override to only build selected metadata. |
|
157
|
|
|
* Return an empty array to prevent building the schema. |
|
158
|
|
|
* |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
private function getMetadata() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->entityManager->getMetadataFactory()->getAllMetadata(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Override to build doctrine registry yourself. |
|
168
|
|
|
* |
|
169
|
|
|
* By default a Symfony container is used to create it. It requires the SymfonyKernel trait. |
|
170
|
|
|
* |
|
171
|
|
|
* @return ManagerRegistry |
|
172
|
|
|
*/ |
|
173
|
|
|
private function createDoctrineRegistry() |
|
174
|
|
|
{ |
|
175
|
|
|
if (isset(static::$kernel)) { |
|
176
|
|
|
return static::$kernel->getContainer()->get('doctrine'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
throw new \RuntimeException(sprintf('Override %s to create a ManagerRegistry or use the SymfonyKernel trait.', __METHOD__)); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.