Passed
Push — main ( a1448d...532f1f )
by Peter
03:51
created

testExecuteCreatesAdditionalRandomStringsForAdditionalKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Console\Commands\Security;
6
7
use Opulence\Console\Responses\IResponse;
8
use org\bovigo\vfs\vfsStream;
9
use PHPUnit\Framework\TestCase;
10
11
class SecretGeneratorTest extends TestCase
12
{
13
    private const ROOT = 'exampleDir';
14
    private const CONFIG = 'exampleDir/config.php';
15
    private const CONFIG_EXTRA_KEY = 'MY_RANDOM_PASSWORD';
16
    private const CONFIG_CONTENT = <<<EOF
17
<?php
18
Environment::setVar("DB_PASSWORD", "mypassword");
19
Environment::setVar("ENCRYPTION_KEY", "mypassword");
20
Environment::setVar("CRYPTO_FRONTEND_SALT", "mypassword");
21
Environment::setVar("CRYPTO_ENCRYPTION_PEPPER", "mypassword");
22
Environment::setVar("OAUTH2_PRIVATE_KEY_PASSWORD", "mypassword");
23
Environment::setVar("MY_RANDOM_PASSWORD", "mypassword");
24
EOF;
25
26
    private SecretGenerator $sut;
27
28
    private string $configUrl;
29
30
    public function setUp(): void
31
    {
32
        vfsStream::setup(self::ROOT);
33
        $this->configUrl = vfsStream::url(self::CONFIG);
34
35
        $this->sut = new SecretGenerator();
36
        $this->sut->setEnvFile($this->configUrl);
37
38
        file_put_contents($this->configUrl, self::CONFIG_CONTENT);
39
    }
40
41
    public function testExecuteThrowsExceptionIfConfigIsNotFoundAndNotInDryRunMode()
42
    {
43
        $this->expectException(\RuntimeException::class);
44
45
        $this->sut->setEnvFile(null);
46
47
        $responseMock = $this->getMockBuilder(IResponse::class)
48
            ->disableOriginalConstructor()
49
            ->getMock();
50
51
        $this->sut->execute($responseMock);
52
    }
53
54
    public function testExecuteCreatesSixRandomStringsByDefault()
55
    {
56
        $responseMock = $this->getMockBuilder(IResponse::class)
57
            ->disableOriginalConstructor()
58
            ->getMock();
59
60
        $responseMock->expects($this->exactly(6))->method('writeln');
61
62
        $this->sut->execute($responseMock);
63
    }
64
65
    public function testExecuteCreatesAdditionalRandomStringsForAdditionalKeys()
66
    {
67
        $responseMock = $this->getMockBuilder(IResponse::class)
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
71
        $responseMock->expects($this->exactly(7))->method('writeln');
72
73
        $this->sut->addKey('foo', 32);
74
75
        $this->sut->execute($responseMock);
76
    }
77
78
    public function testExecuteReplacesConfigValues()
79
    {
80
        $responseMock = $this->getMockBuilder(IResponse::class)
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
84
        $this->sut->addKey(self::CONFIG_EXTRA_KEY, 32);
85
86
        $this->sut->execute($responseMock);
87
        $content = file_get_contents($this->configUrl);
88
89
        $this->assertNotSame(self::CONFIG_CONTENT, $content);
90
        $this->assertStringNotContainsString('mypassword', $content);
91
    }
92
93
    public function testExecuteCanReplaceFilesMultipleTimes()
94
    {
95
        $responseMock = $this->getMockBuilder(IResponse::class)
96
            ->disableOriginalConstructor()
97
            ->getMock();
98
99
        $this->sut->addKey(self::CONFIG_EXTRA_KEY, 32);
100
101
        $this->sut->execute($responseMock);
102
        $content1 = file_get_contents($this->configUrl);
103
104
        $this->sut->execute($responseMock);
105
        $content2 = file_get_contents($this->configUrl);
106
107
        $this->assertNotSame($content1, $content2);
108
    }
109
}
110