Completed
Pull Request — master (#4187)
by Craig
04:48
created

LocalDotEnvHelperTest::getFileContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CoreBundle\Tests\Helper;
15
16
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Filesystem\Filesystem;
18
use Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper;
19
20
class LocalDotEnvHelperTest extends TestCase
21
{
22
    private $projectDir;
23
24
    public function testWriteLocalEnvVars()
25
    {
26
        $this->projectDir = dirname(__DIR__, 5) . '/var/cache/test/dotenvtest';
27
        $fileSystem = new Filesystem();
28
        $fileSystem->copy(__DIR__ . '/Fixture/.env.local', $this->projectDir . '/.env.local', true);
29
        $originalFileContents = trim($this->getFileContents());
30
        $helper = new LocalDotEnvHelper($this->projectDir);
31
32
        $helper->writeLocalEnvVars([]);
33
        $this->assertEquals($originalFileContents, $this->getFileContents());
34
35
        $helper->writeLocalEnvVars(['MY_NEW_VAR' => 'foo']);
36
        $expected = $originalFileContents . "\nMY_NEW_VAR=foo";
37
        $this->assertEquals($expected, $this->getFileContents());
38
39
        $helper->writeLocalEnvVars(['MY_NEW_VAR' => 'f#oo']);
40
        $expected = $originalFileContents . "\nMY_NEW_VAR=f%23oo";
41
        $this->assertEquals($expected, $this->getFileContents());
42
43
        $helper->writeLocalEnvVars(['MY_NEW_VAR' => '\'bar\'']);
44
        $expected = $originalFileContents . "\nMY_NEW_VAR='bar'";
45
        $this->assertEquals($expected, $this->getFileContents());
46
47
        $helper->writeLocalEnvVars(['MY_NEW_VAR' => 'foo', 'BAR' => 123], true);
48
        $expected = "MY_NEW_VAR=foo\nBAR=123";
49
        $this->assertEquals($expected, $this->getFileContents());
50
51
        $helper->writeLocalEnvVars(['MY_NEW_VAR' => '!f@oo', 'BAR' => '!1(2)3'], true);
52
        $expected = "MY_NEW_VAR=f@oo\nBAR=1(2)3";
53
        $this->assertEquals($expected, $this->getFileContents());
54
55
        $helper->writeLocalEnvVars(['MY_NEW_VAR' => '!\'f@oo\''], true);
56
        $expected = 'MY_NEW_VAR=\'f@oo\'';
57
        $this->assertEquals($expected, $this->getFileContents());
58
    }
59
60
    private function getFileContents(): string
61
    {
62
        return file_get_contents($this->projectDir . '/.env.local');
63
    }
64
}
65