forked from liip/LiipTestFixturesBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigTest.php
More file actions
254 lines (193 loc) · 7.54 KB
/
ConfigTest.php
File metadata and controls
254 lines (193 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
declare(strict_types=1);
/*
* This file is part of the Liip/TestFixturesBundle
*
* (c) Lukas Kahwe Smith <smith@pooteeweet.org>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Liip\Acme\Tests\Test;
use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
use Doctrine\Persistence\ObjectRepository;
use Liip\Acme\Tests\App\Entity\User;
use Liip\Acme\Tests\AppConfig\AppConfigKernel;
use Liip\Acme\Tests\Traits\ContainerProvider;
use Liip\TestFixturesBundle\Services\DatabaseBackup\SqliteDatabaseBackup;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use Liip\TestFixturesBundle\Services\DatabaseTools\AbstractDatabaseTool;
use Liip\TestFixturesBundle\Services\DatabaseTools\ORMSqliteDatabaseTool;
/**
* Tests that configuration has been loaded and users can be logged in.
*
* Use Tests/AppConfig/AppConfigKernel.php instead of
* Tests/App/AppKernel.php.
* So it must be loaded in a separate process.
*
* @runTestsInSeparateProcesses
*
* @preserveGlobalState disabled
*
* Avoid conflict with PHPUnit annotation when reading QueryCount
* annotation:
*
* @IgnoreAnnotation("expectedException")
*
* @internal
*/
class ConfigTest extends FixturesTestCase
{
use ContainerProvider;
/** @var AbstractDatabaseTool */
protected $databaseTool;
/** @var ObjectRepository */
private $userRepository;
/** @var SqliteDatabaseBackup */
private $sqliteDatabaseBackup;
protected function setUp(): void
{
parent::setUp();
self::bootKernel();
$this->userRepository = $this->getTestContainer()->get('doctrine')
->getRepository(User::class)
;
$this->databaseTool = $this->getTestContainer()->get(DatabaseToolCollection::class)->get();
$this->assertInstanceOf(ORMSqliteDatabaseTool::class, $this->databaseTool);
$this->sqliteDatabaseBackup = $this->getTestContainer()->get(SqliteDatabaseBackup::class);
$this->assertInstanceOf(SqliteDatabaseBackup::class, $this->sqliteDatabaseBackup);
}
/**
* Load Data Fixtures with custom loader defined in configuration.
*/
public function testLoadFixturesFilesWithCustomProvider(): void
{
// Load default Data Fixtures.
$fixtures = $this->databaseTool->loadAliceFixture([
'@AcmeBundle/DataFixtures/ORM/user.yml',
]);
$this->assertIsArray($fixtures);
// 10 users are loaded
$this->assertCount(
10,
$fixtures
);
$fixtureId = 'user_id_1';
$this->assertArrayHasKey($fixtureId, $fixtures);
/** @var User $user1 */
$user1 = $fixtures[$fixtureId];
// The custom provider has not been used successfully.
$this->assertStringStartsNotWith(
'foo',
$user1->getName()
);
// Load Data Fixtures with custom loader defined in configuration.
$fixtures = $this->databaseTool->loadAliceFixture([
'@AcmeBundle/DataFixtures/ORM/user_with_custom_provider.yml',
]);
$fixtureId = 'custom_user_id_11';
$this->assertArrayHasKey($fixtureId, $fixtures);
/** @var User $user11 */
$user11 = $fixtures[$fixtureId];
// The custom provider "foo" has been loaded and used successfully.
$this->assertSame(
'fooa string',
$user11->getName()
);
}
public function testCacheCanBeDisabled(): void
{
$fixtures = [
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadDependentUserData',
];
$this->databaseTool->setDatabaseCacheEnabled(false);
$this->databaseTool->loadFixtures($fixtures);
// Load data from database
/** @var User $user1 */
$user1 = $this->userRepository->findOneBy(['id' => 1]);
// Store random data, in order to check it after reloading fixtures.
$user1Salt = $user1->getSalt();
sleep(2);
$this->getTestContainer()->get('doctrine')->getManager()->clear();
// Reload the fixtures.
$this->databaseTool->loadFixtures($fixtures);
/** @var User $user1 */
$user1 = $this->userRepository->findOneBy(['id' => 1]);
// The salt are not the same because cache were not used
$this->assertNotSame($user1Salt, $user1->getSalt());
// Enable the cache again
$this->databaseTool->setDatabaseCacheEnabled(true);
}
/**
* Update a fixture file and check that the cache will be refreshed.
*/
public function testBackupIsRefreshed(): void
{
$fixtures = [
'Liip\Acme\Tests\App\DataFixtures\ORM\LoadDependentUserData',
];
$this->databaseTool->loadFixtures($fixtures);
// Load data from database
/** @var User $user1 */
$user1 = $this->userRepository->findOneBy(['id' => 1]);
// Store random data, in order to check it after reloading fixtures.
$user1Salt = $user1->getSalt();
$dependentFixtureFilePath = static::$kernel->locateResource(
'@AcmeBundle/DataFixtures/ORM/LoadUserData.php'
);
$dependentFixtureFilemtime = filemtime($dependentFixtureFilePath);
// The backup service provide the path of the backup file
$databaseFilePath = $this->sqliteDatabaseBackup->getBackupFilePath();
if (!is_file($databaseFilePath)) {
$this->fail($databaseFilePath.' is not a file.');
}
$databaseFilemtime = filemtime($databaseFilePath);
sleep(2);
// Reload the fixtures.
$this->databaseTool->loadFixtures($fixtures);
// The mtime of the file has not changed.
$this->assertSame(
$dependentFixtureFilemtime,
filemtime($dependentFixtureFilePath),
'File modification time of the fixture has been updated.'
);
// The backup has not been updated.
$this->assertSame(
$databaseFilemtime,
filemtime($databaseFilePath),
'File modification time of the backup has been updated.'
);
$user1 = $this->userRepository->findOneBy(['id' => 1]);
// Check that random data has not been changed, to ensure that backup was created and loaded successfully.
$this->assertSame($user1Salt, $user1->getSalt());
sleep(2);
// Update the filemtime of the fixture file used as a dependency.
touch($dependentFixtureFilePath);
$this->getTestContainer()->get('doctrine')->getManager()->clear();
$this->databaseTool->loadFixtures($fixtures);
// The mtime of the fixture file has been updated.
$this->assertGreaterThan(
$dependentFixtureFilemtime,
filemtime($dependentFixtureFilePath),
'File modification time of the fixture has not been updated.'
);
// The backup has been refreshed: mtime is greater.
$this->assertGreaterThan(
$databaseFilemtime,
filemtime($databaseFilePath),
'File modification time of the backup has not been updated.'
);
$user1 = $this->userRepository->findOneBy(['id' => 1]);
// Check that random data has been changed, to ensure that backup was not used.
$this->assertNotSame($user1Salt, $user1->getSalt());
}
protected static function getKernelClass(): string
{
return AppConfigKernel::class;
}
protected function tearDown(): void
{
parent::tearDown();
unset($this->userRepository, $this->databaseTool);
}
}