testExecuteThrowsExceptionIfConfigIsNotFoundAndNotInDryRunMode()   A
last analyzed

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
    protected const ROOT = 'exampleDir';
14
    protected const CONFIG = 'exampleDir/config.php';
15
    protected const CONFIG_EXTRA_KEY = 'MY_RANDOM_PASSWORD';
16
    protected 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
    /** @var SecretGenerator - System Under Test */
27
    protected SecretGenerator $sut;
28
29
    protected string $configUrl;
30
31
    public function setUp(): void
32
    {
33
        vfsStream::setup(self::ROOT);
34
        $this->configUrl = vfsStream::url(self::CONFIG);
35
36
        $this->sut = new SecretGenerator();
37
        $this->sut->setEnvFile($this->configUrl);
38
39
        file_put_contents($this->configUrl, self::CONFIG_CONTENT);
40
    }
41
42
    public function testExecuteThrowsExceptionIfConfigIsNotFoundAndNotInDryRunMode(): void
43
    {
44
        $this->expectException(\RuntimeException::class);
45
46
        $this->sut->setEnvFile(null);
47
48
        $responseMock = $this->getMockBuilder(IResponse::class)
49
            ->disableOriginalConstructor()
50
            ->getMock();
51
52
        $this->sut->execute($responseMock);
53
    }
54
55
    public function testExecuteCreatesSixRandomStringsByDefault(): void
56
    {
57
        $responseMock = $this->getMockBuilder(IResponse::class)
58
            ->disableOriginalConstructor()
59
            ->getMock();
60
61
        $responseMock->expects($this->exactly(6))->method('writeln');
62
63
        $this->sut->execute($responseMock);
64
    }
65
66
    public function testExecuteCreatesAdditionalRandomStringsForAdditionalKeys(): void
67
    {
68
        $responseMock = $this->getMockBuilder(IResponse::class)
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
72
        $responseMock->expects($this->exactly(7))->method('writeln');
73
74
        $this->sut->addKey('foo', 32);
75
76
        $this->sut->execute($responseMock);
77
    }
78
79
    public function testExecuteReplacesConfigValues(): void
80
    {
81
        $responseMock = $this->getMockBuilder(IResponse::class)
82
            ->disableOriginalConstructor()
83
            ->getMock();
84
85
        $this->sut->addKey(self::CONFIG_EXTRA_KEY, 32);
86
87
        $this->sut->execute($responseMock);
88
        $content = file_get_contents($this->configUrl);
89
90
        $this->assertNotSame(self::CONFIG_CONTENT, $content);
91
        $this->assertStringNotContainsString('mypassword', $content);
92
    }
93
94
    public function testExecuteCanReplaceFilesMultipleTimes(): void
95
    {
96
        $responseMock = $this->getMockBuilder(IResponse::class)
97
            ->disableOriginalConstructor()
98
            ->getMock();
99
100
        $this->sut->addKey(self::CONFIG_EXTRA_KEY, 32);
101
102
        $this->sut->execute($responseMock);
103
        $content1 = file_get_contents($this->configUrl);
104
105
        $this->sut->execute($responseMock);
106
        $content2 = file_get_contents($this->configUrl);
107
108
        $this->assertNotSame($content1, $content2);
109
    }
110
}
111