Skip to content

Commit 5824990

Browse files
committed
Provide rootPath for behat via behat.yml
1 parent 8c57573 commit 5824990

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

tests/Behat/Bootstrap/FeatureContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ public static function clean(AfterSuiteScope $scope)
8181
*/
8282
private array $lastReturnAsArray;
8383

84-
public function __construct(string $redmineVersion)
84+
public function __construct(string $redmineVersion, string $rootPath)
8585
{
8686
$version = RedmineVersion::tryFrom($redmineVersion);
8787

8888
if ($version === null) {
8989
throw new InvalidArgumentException('Redmine ' . $redmineVersion . ' is not supported.');
9090
}
9191

92-
$this->redmine = static::$tracer::getRedmineInstance($version);
92+
$this->redmine = static::$tracer::getRedmineInstance($version, $rootPath);
9393

9494
parent::__construct('BehatRedmine' . $version->asId());
9595
}

tests/Behat/behat.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ default:
66
contexts:
77
- Redmine\Tests\Behat\Bootstrap\FeatureContext:
88
redmineVersion: '6.0.2'
9+
rootPath: '%paths.base%/../../.docker'
910
redmine_50105:
1011
paths:
1112
- '%paths.base%/features'
1213
contexts:
1314
- Redmine\Tests\Behat\Bootstrap\FeatureContext:
1415
redmineVersion: '5.1.5'
16+
rootPath: '%paths.base%/../../.docker'
1517
redmine_50010:
1618
paths:
1719
- '%paths.base%/features'
1820
contexts:
1921
- Redmine\Tests\Behat\Bootstrap\FeatureContext:
2022
redmineVersion: '5.0.10'
23+
rootPath: '%paths.base%/../../.docker'

tests/RedmineExtension/BehatHookTracer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ final class BehatHookTracer implements InstanceRegistration
2222
*/
2323
private static array $instances = [];
2424

25-
public static function getRedmineInstance(RedmineVersion $redmineVersion): RedmineInstance
25+
public static function getRedmineInstance(RedmineVersion $redmineVersion, string $rootPath): RedmineInstance
2626
{
2727
if (static::$tracer === null) {
2828
throw new RuntimeException('You can only get a Redmine instance while a Behat Suite is running.');
2929
}
3030

3131
if (! array_key_exists($redmineVersion->asId(), static::$instances)) {
32-
RedmineInstance::create(static::$tracer, $redmineVersion);
32+
RedmineInstance::create(static::$tracer, $redmineVersion, $rootPath);
3333
}
3434

3535
return static::$instances[$redmineVersion->asId()];

tests/RedmineExtension/RedmineInstance.php

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ final class RedmineInstance
1414
/**
1515
* @param InstanceRegistration $tracer Required to ensure that RedmineInstance is created while Test Runner is running
1616
*/
17-
public static function create(InstanceRegistration $tracer, RedmineVersion $version): void
17+
public static function create(InstanceRegistration $tracer, RedmineVersion $version, string $rootPath): void
1818
{
19-
$tracer->registerInstance(new self($tracer, $version));
19+
$tracer->registerInstance(new self($tracer, $version, $rootPath));
2020
}
2121

2222
private InstanceRegistration $tracer;
2323

2424
private RedmineVersion $version;
2525

26-
private string $rootPath;
26+
private string $dataPath;
2727

2828
private string $workingDB;
2929

@@ -41,14 +41,19 @@ public static function create(InstanceRegistration $tracer, RedmineVersion $vers
4141

4242
private string $apiKey;
4343

44-
private function __construct(InstanceRegistration $tracer, RedmineVersion $version)
44+
private function __construct(InstanceRegistration $tracer, RedmineVersion $version, string $rootPath)
4545
{
4646
$this->tracer = $tracer;
4747
$this->version = $version;
4848

4949
$versionId = strval($version->asId());
5050

51-
$this->rootPath = dirname(__FILE__, 3) . '/.docker/redmine-' . $versionId . '_data/';
51+
// Default to .docker folder
52+
if ($rootPath === '') {
53+
$rootPath = dirname(__FILE__, 3) . '/.docker';
54+
}
55+
56+
$this->dataPath = $rootPath . '/redmine-' . $versionId . '_data/';
5257

5358
$this->workingDB = 'sqlite/redmine.db';
5459
$this->migratedDB = 'sqlite/redmine-migrated.db';
@@ -108,10 +113,10 @@ private function runHealthChecks(RedmineVersion $version): void
108113
));
109114
}
110115

111-
if (! file_exists($this->rootPath . $this->workingDB)) {
116+
if (! file_exists($this->dataPath . $this->workingDB)) {
112117
throw new InvalidArgumentException(sprintf(
113118
'Could not find database file in %s, please make sure that Redmine %s has a docker service in /docker-composer.yml and is correctly configured in /tests/Behat/behat.yml.',
114-
$this->rootPath . $this->workingDB,
119+
$this->dataPath . $this->workingDB,
115120
$version->asString(),
116121
));
117122
}
@@ -146,7 +151,7 @@ public function shutdown(InstanceRegistration $tracer): void
146151
*/
147152
public function excecuteDatabaseQuery(string $query, array $options = [], array $params = null): void
148153
{
149-
$pdo = new PDO('sqlite:' . $this->rootPath . $this->workingDB);
154+
$pdo = new PDO('sqlite:' . $this->dataPath . $this->workingDB);
150155
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
151156

152157
$stmt = $pdo->prepare($query, $options);
@@ -156,7 +161,7 @@ public function excecuteDatabaseQuery(string $query, array $options = [], array
156161
private function runDatabaseMigration()
157162
{
158163
$now = new DateTimeImmutable();
159-
$pdo = new PDO('sqlite:' . $this->rootPath . $this->workingDB);
164+
$pdo = new PDO('sqlite:' . $this->dataPath . $this->workingDB);
160165

161166
// Get admin user to check sqlite connection
162167
$stmt = $pdo->prepare('SELECT * FROM users WHERE login = :login;');
@@ -191,9 +196,9 @@ private function runDatabaseMigration()
191196
*/
192197
private function createDatabaseBackup()
193198
{
194-
$workingDB = new SQLite3($this->rootPath . $this->workingDB);
199+
$workingDB = new SQLite3($this->dataPath . $this->workingDB);
195200

196-
$backupDB = new SQLite3($this->rootPath . $this->backupDB);
201+
$backupDB = new SQLite3($this->dataPath . $this->backupDB);
197202

198203
$workingDB->backup($backupDB);
199204

@@ -206,9 +211,9 @@ private function createDatabaseBackup()
206211
*/
207212
private function saveMigratedDatabase()
208213
{
209-
$workingDB = new SQLite3($this->rootPath . $this->workingDB);
214+
$workingDB = new SQLite3($this->dataPath . $this->workingDB);
210215

211-
$migratedDB = new SQLite3($this->rootPath . $this->migratedDB);
216+
$migratedDB = new SQLite3($this->dataPath . $this->migratedDB);
212217

213218
$workingDB->backup($migratedDB);
214219

@@ -218,9 +223,9 @@ private function saveMigratedDatabase()
218223

219224
private function restoreFromMigratedDatabase(): void
220225
{
221-
$workingDB = new SQLite3($this->rootPath . $this->workingDB);
226+
$workingDB = new SQLite3($this->dataPath . $this->workingDB);
222227

223-
$migratedDB = new SQLite3($this->rootPath . $this->migratedDB);
228+
$migratedDB = new SQLite3($this->dataPath . $this->migratedDB);
224229

225230
$migratedDB->backup($workingDB);
226231

@@ -230,9 +235,9 @@ private function restoreFromMigratedDatabase(): void
230235

231236
private function restoreDatabaseFromBackup(): void
232237
{
233-
$workingDB = new SQLite3($this->rootPath . $this->workingDB);
238+
$workingDB = new SQLite3($this->dataPath . $this->workingDB);
234239

235-
$backupDB = new SQLite3($this->rootPath . $this->backupDB);
240+
$backupDB = new SQLite3($this->dataPath . $this->backupDB);
236241

237242
$backupDB->backup($workingDB);
238243

@@ -242,66 +247,66 @@ private function restoreDatabaseFromBackup(): void
242247

243248
private function removeDatabaseBackups(): void
244249
{
245-
unlink($this->rootPath . $this->migratedDB);
246-
unlink($this->rootPath . $this->backupDB);
250+
unlink($this->dataPath . $this->migratedDB);
251+
unlink($this->dataPath . $this->backupDB);
247252
}
248253

249254
private function createFilesBackup()
250255
{
251256
// Add an empty file to avoid warnings about copying and removing content from an empty folder
252-
touch($this->rootPath . $this->workingFiles . 'empty');
257+
touch($this->dataPath . $this->workingFiles . 'empty');
253258
exec(sprintf(
254259
'cp -r %s %s',
255-
$this->rootPath . $this->workingFiles,
256-
$this->rootPath . rtrim($this->backupFiles, '/'),
260+
$this->dataPath . $this->workingFiles,
261+
$this->dataPath . rtrim($this->backupFiles, '/'),
257262
));
258263
}
259264

260265
private function saveMigratedFiles()
261266
{
262267
exec(sprintf(
263268
'cp -r %s %s',
264-
$this->rootPath . $this->workingFiles,
265-
$this->rootPath . rtrim($this->migratedFiles, '/'),
269+
$this->dataPath . $this->workingFiles,
270+
$this->dataPath . rtrim($this->migratedFiles, '/'),
266271
));
267272
}
268273

269274
private function restoreFromMigratedFiles(): void
270275
{
271276
exec(sprintf(
272277
'rm -r %s',
273-
$this->rootPath . $this->workingFiles . '*',
278+
$this->dataPath . $this->workingFiles . '*',
274279
));
275280

276281
exec(sprintf(
277282
'cp -r %s %s',
278-
$this->rootPath . $this->migratedFiles . '*',
279-
$this->rootPath . rtrim($this->workingFiles, '/'),
283+
$this->dataPath . $this->migratedFiles . '*',
284+
$this->dataPath . rtrim($this->workingFiles, '/'),
280285
));
281286
}
282287

283288
private function restoreFilesFromBackup(): void
284289
{
285290
exec(sprintf(
286291
'rm -r %s',
287-
$this->rootPath . $this->workingFiles . '*',
292+
$this->dataPath . $this->workingFiles . '*',
288293
));
289294

290295
exec(sprintf(
291296
'cp -r %s %s',
292-
$this->rootPath . $this->backupFiles . '*',
293-
$this->rootPath . rtrim($this->workingFiles, '/'),
297+
$this->dataPath . $this->backupFiles . '*',
298+
$this->dataPath . rtrim($this->workingFiles, '/'),
294299
));
295300
}
296301

297302
private function removeFilesBackups(): void
298303
{
299304
exec(sprintf(
300305
'rm -r %s %s',
301-
$this->rootPath . $this->migratedFiles,
302-
$this->rootPath . $this->backupFiles,
306+
$this->dataPath . $this->migratedFiles,
307+
$this->dataPath . $this->backupFiles,
303308
));
304309

305-
unlink($this->rootPath . $this->workingFiles . 'empty');
310+
unlink($this->dataPath . $this->workingFiles . 'empty');
306311
}
307312
}

0 commit comments

Comments
 (0)