Skip to content

Commit f28a12f

Browse files
authored
Merge pull request #844 from carlobeltrame/fix/remove-proxy-directory-after-database-initialization
Remove Doctrine proxy directory after fixture loading CLI command
2 parents 9a56916 + 32e84a0 commit f28a12f

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

.docker-hub/backend/docker-entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -euo pipefail
23

34
php bin/wait-for-db.php
45
vendor/bin/laminas rebuild-database

backend/module/eCampLib/src/Command/LoadDataFixturesCommand.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
use Laminas\Cli\Input\PathParam;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Filesystem\Filesystem;
1112

1213
class LoadDataFixturesCommand extends AbstractParamAwareCommand {
1314
private $entityManager;
1415
private $fixtureLoader;
16+
private $filesystem;
1517

16-
public function __construct(EntityManager $entityManager, FixtureLoader $fixtureLoader) {
18+
public function __construct(EntityManager $entityManager, FixtureLoader $fixtureLoader, Filesystem $filesystem) {
1719
parent::__construct();
1820
$this->entityManager = $entityManager;
1921
$this->fixtureLoader = $fixtureLoader;
22+
$this->filesystem = $filesystem;
2023
}
2124

2225
protected function configure() {
@@ -35,7 +38,14 @@ protected function execute(InputInterface $input, OutputInterface $output) {
3538
$this->entityManager,
3639
new \Doctrine\Common\DataFixtures\Purger\ORMPurger()
3740
);
38-
$executor->execute($this->fixtureLoader->getFixtures(), true);
41+
$fixtures = $this->fixtureLoader->getFixtures();
42+
$executor->execute($fixtures, true);
43+
44+
if (count($fixtures) > 0) {
45+
// Cleaning up the generated Doctrine proxies is necessary because the command might be run by a user other
46+
// than www-data.
47+
$this->filesystem->remove(__DIR__.'/../../../../data/DoctrineORMModule');
48+
}
3949

4050
return 0;
4151
}

backend/module/eCampLib/test/Command/LoadDataFixturesCommandTest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use eCamp\LibTest\PHPUnit\AbstractConsoleControllerTestCase;
77
use PHPUnit\Framework\Constraint\IsEqual;
88
use PHPUnit\Framework\Constraint\StringContains;
9+
use PHPUnit\Framework\Constraint\StringEndsWith;
910
use Symfony\Component\Console\Input\StringInput;
1011
use Symfony\Component\Console\Output\BufferedOutput;
1112
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Filesystem\Filesystem;
1214

1315
/**
1416
* @internal
@@ -17,12 +19,17 @@ class LoadDataFixturesCommandTest extends AbstractConsoleControllerTestCase {
1719
public function testLoadsFilesFromGivenPath() {
1820
// given
1921
$services = $this->getApplicationServiceLocator();
20-
/** @var LoadDataFixturesCommand $command */
21-
$command = $services->get(LoadDataFixturesCommand::class);
22+
2223
$input = new StringInput('load-data-fixtures --path='.__DIR__.'/../data/fixtures');
2324
$output = new BufferedOutput();
2425
$services->setService(OutputInterface::class, $output);
2526

27+
$mockFilesystem = $this->createMock(Filesystem::class);
28+
$services->setService(Filesystem::class, $mockFilesystem);
29+
30+
/** @var LoadDataFixturesCommand $command */
31+
$command = $services->get(LoadDataFixturesCommand::class);
32+
2633
// when
2734
$result = $this->runCommand($command, $input, $output);
2835

@@ -36,12 +43,20 @@ public function testLoadsFilesFromGivenPath() {
3643
public function testDoesNotCrashWhenGivenNonexistentLocation() {
3744
// given
3845
$services = $this->getApplicationServiceLocator();
39-
/** @var LoadDataFixturesCommand $command */
40-
$command = $services->get(LoadDataFixturesCommand::class);
46+
4147
$input = new StringInput('load-data-fixtures --path='.__DIR__.'/../data/some-dir-that-does-not-exist');
4248
$output = new BufferedOutput();
4349
$services->setService(OutputInterface::class, $output);
4450

51+
$mockFilesystem = $this->createMock(Filesystem::class);
52+
$services->setService(Filesystem::class, $mockFilesystem);
53+
54+
/** @var LoadDataFixturesCommand $command */
55+
$command = $services->get(LoadDataFixturesCommand::class);
56+
57+
// then
58+
$mockFilesystem->expects($this->never())->method('remove');
59+
4560
// when
4661
$result = $this->runCommand($command, $input, $output);
4762

@@ -50,4 +65,24 @@ public function testDoesNotCrashWhenGivenNonexistentLocation() {
5065
$consoleOutput = $output->fetch();
5166
$this->assertThat($consoleOutput, new IsEqual(''));
5267
}
68+
69+
public function testCleansUpDoctrineProxies() {
70+
// given
71+
$services = $this->getApplicationServiceLocator();
72+
$input = new StringInput('load-data-fixtures --path='.__DIR__.'/../data/fixtures');
73+
$output = new BufferedOutput();
74+
$services->setService(OutputInterface::class, $output);
75+
76+
$mockFilesystem = $this->createMock(Filesystem::class);
77+
$services->setService(Filesystem::class, $mockFilesystem);
78+
79+
/** @var LoadDataFixturesCommand $command */
80+
$command = $services->get(LoadDataFixturesCommand::class);
81+
82+
// then
83+
$mockFilesystem->expects($this->once())->method('remove')->with(new StringEndsWith('DoctrineORMModule'));
84+
85+
// when
86+
$this->runCommand($command, $input, $output);
87+
}
5388
}

backend/module/eCampLib/test/Command/RebuildDatabaseSchemaCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class RebuildDatabaseSchemaCommandTest extends AbstractConsoleControllerTestCase
1616
public function testRebuildsDatabaseSchema() {
1717
// given
1818
$services = $this->getApplicationServiceLocator();
19+
1920
$mockMetadata = [];
2021
$mockMetadataFactory = $this->createMock(ClassMetadataFactory::class);
2122
$mockMetadataFactory->method('getAllMetadata')->willReturn($mockMetadata);

backend/module/eCampLib/test/Command/UpdateDatabaseSchemaCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class UpdateDatabaseSchemaCommandTest extends AbstractConsoleControllerTestCase
1616
public function testUpdatesDatabaseSchema() {
1717
// given
1818
$services = $this->getApplicationServiceLocator();
19+
1920
$mockMetadata = [];
2021
$mockMetadataFactory = $this->createMock(ClassMetadataFactory::class);
2122
$mockMetadataFactory->method('getAllMetadata')->willReturn($mockMetadata);

0 commit comments

Comments
 (0)