Skip to content

Commit aa5ed03

Browse files
committed
Fix docker configurator with many mark in volumes section
1 parent a388cac commit aa5ed03

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

src/Configurator/DockerComposeConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function unconfigure(Recipe $recipe, $config, Lock $lock)
9999
}
100100

101101
foreach ($config as $key => $value) {
102-
if (0 === preg_match(sprintf('{^%s:[ \t\r\n]*([ \t]+\w)}m', $key), $contents, $matches)) {
102+
if (0 === preg_match(sprintf('{^%s:[ \t\r\n]*([ \t]+\w|#)}m', $key), $contents, $matches)) {
103103
$contents = preg_replace(sprintf('{\n?^%s:[ \t\r\n]*}sm', $key), '', $contents, -1, $count);
104104
}
105105
}

tests/Configurator/DockerComposeConfiguratorTest.php

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,6 @@ public function testConfigureFileWithExistingMarks()
234234
- db-data:/var/lib/postgresql/data:rw
235235
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
236236
# - ./docker/db/data:/var/lib/postgresql/data:rw
237-
ports:
238-
- "5432:5432"
239237
###< doctrine/doctrine-bundle ###
240238
241239
volumes:
@@ -245,9 +243,6 @@ public function testConfigureFileWithExistingMarks()
245243

246244
YAML;
247245

248-
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
249-
$recipe->method('getName')->willReturn('symfony/mercure-bundle');
250-
251246
$dockerComposeFile = FLEX_TEST_DIR.'/docker-compose.yml';
252247
file_put_contents($dockerComposeFile, $originalContent);
253248

@@ -267,8 +262,6 @@ public function testConfigureFileWithExistingMarks()
267262
' - CORS_ALLOWED_ORIGINS=*',
268263
' - PUBLISH_ALLOWED_ORIGINS=http://localhost:1337',
269264
' - DEMO=1',
270-
' ports:',
271-
' - "1337:80"',
272265
],
273266
];
274267

@@ -288,8 +281,6 @@ public function testConfigureFileWithExistingMarks()
288281
- db-data:/var/lib/postgresql/data:rw
289282
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
290283
# - ./docker/db/data:/var/lib/postgresql/data:rw
291-
ports:
292-
- "5432:5432"
293284
###< doctrine/doctrine-bundle ###
294285
295286
###> symfony/mercure-bundle ###
@@ -303,8 +294,6 @@ public function testConfigureFileWithExistingMarks()
303294
- CORS_ALLOWED_ORIGINS=*
304295
- PUBLISH_ALLOWED_ORIGINS=http://localhost:1337
305296
- DEMO=1
306-
ports:
307-
- "1337:80"
308297
###< symfony/mercure-bundle ###
309298
310299
volumes:
@@ -322,4 +311,76 @@ public function testConfigureFileWithExistingMarks()
322311
$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
323312
$this->assertEquals(self::ORIGINAL_CONTENT, file_get_contents($dockerComposeFile));
324313
}
314+
315+
public function testUnconfigureFileWithManyMarks()
316+
{
317+
$originalContent = self::ORIGINAL_CONTENT.<<<'YAML'
318+
319+
###> symfony/messenger ###
320+
rabbitmq:
321+
image: rabbitmq:management-alpine
322+
environment:
323+
# You should definitely change the password in production
324+
- RABBITMQ_DEFAULT_USER=guest
325+
- RABBITMQ_DEFAULT_PASS=guest
326+
volumes:
327+
- rabbitmq-data:/var/lib/rabbitmq
328+
###< symfony/messenger ###
329+
330+
###> doctrine/doctrine-bundle ###
331+
db:
332+
image: postgres:11-alpine
333+
environment:
334+
- POSTGRES_DB=symfony
335+
- POSTGRES_USER=symfony
336+
# You should definitely change the password in production
337+
- POSTGRES_PASSWORD=!ChangeMe!
338+
volumes:
339+
- db-data:/var/lib/postgresql/data:rw
340+
# You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data!
341+
# - ./docker/db/data:/var/lib/postgresql/data:rw
342+
###< doctrine/doctrine-bundle ###
343+
344+
volumes:
345+
###> symfony/messenger ###
346+
rabbitmq-data: {}
347+
###< symfony/messenger ###
348+
349+
###> doctrine/doctrine-bundle ###
350+
db-data: {}
351+
###< doctrine/doctrine-bundle ###
352+
353+
YAML;
354+
355+
$contentWithoutDoctrine = self::ORIGINAL_CONTENT.<<<'YAML'
356+
357+
###> symfony/messenger ###
358+
rabbitmq:
359+
image: rabbitmq:management-alpine
360+
environment:
361+
# You should definitely change the password in production
362+
- RABBITMQ_DEFAULT_USER=guest
363+
- RABBITMQ_DEFAULT_PASS=guest
364+
volumes:
365+
- rabbitmq-data:/var/lib/rabbitmq
366+
###< symfony/messenger ###
367+
368+
volumes:
369+
###> symfony/messenger ###
370+
rabbitmq-data: {}
371+
###< symfony/messenger ###
372+
373+
374+
YAML;
375+
376+
$dockerComposeFile = FLEX_TEST_DIR.'/docker-compose.yml';
377+
file_put_contents($dockerComposeFile, $originalContent);
378+
379+
/** @var Recipe|\PHPUnit\Framework\MockObject\MockObject $recipe */
380+
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
381+
$recipe->method('getName')->willReturn('symfony/messenger');
382+
383+
$this->configurator->unconfigure($this->recipeDb, self::CONFIG_DB, $this->lock);
384+
$this->assertEquals($contentWithoutDoctrine, file_get_contents($dockerComposeFile));
385+
}
325386
}

tests/Configurator/DockerfileConfiguratorTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121

2222
class DockerfileConfiguratorTest extends TestCase
2323
{
24+
public function setUp()
25+
{
26+
@mkdir(FLEX_TEST_DIR);
27+
}
28+
29+
protected function tearDown()
30+
{
31+
@unlink(FLEX_TEST_DIR.'/Dockerfile');
32+
}
33+
2434
public function testConfigure()
2535
{
2636
$originalContent = <<<'EOF'
@@ -94,7 +104,6 @@ public function testConfigure()
94104
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
95105
$recipe->method('getName')->willReturn('doctrine/doctrine-bundle');
96106

97-
@mkdir(FLEX_TEST_DIR);
98107
$config = FLEX_TEST_DIR.'/Dockerfile';
99108
file_put_contents($config, $originalContent);
100109

0 commit comments

Comments
 (0)