Completed
Pull Request — master (#194)
by Loïc
03:46
created

MakefileContext::writeMakefileData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of monofony.
5
 *
6
 * (c) Mobizel
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Behat\Context\Cli;
15
16
use Behat\Behat\Context\Context;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Process\Process;
19
use Webmozart\Assert\Assert;
20
21
class MakefileContext implements Context
22
{
23
    /** @var Process */
24
    private $process;
25
26
    /** @var string */
27
    private $lastOutput;
28
29
    /**
30
     * @When I use default makefile commands
31
     */
32
    public function useDefaultMakeFileCommands()
33
    {
34
        $this->ensureMakeFileDirExist();
35
        $this->writeMakefileData(<<<EOM
36
include Makefile.dist
37
38
EOM
39
);
40
    }
41
42
    /**
43
     * @When I override makefile :commandName command with :data
44
     */
45
    public function overrideCommandWithData(string $commandName, string $data)
46
    {
47
        $this->ensureMakeFileDirExist();
48
        $this->writeMakefileData(<<<EOM
49
include Makefile.dist
50
51
$commandName:
52
\t$data
53
54
EOM
55
        );
56
    }
57
58
    /**
59
     * @Then the command make :commandName should exist
60
     */
61
    public function commandExist(string $commandName): void
62
    {
63
        $this->dryRunCommand($commandName);
64
        Assert::true($this->process->isSuccessful());
65
    }
66
67
    /**
68
     * @Then it should execute :commandName
69
     */
70
    public function iShouldExecute(string $commandName)
71
    {
72
        Assert::contains($this->lastOutput, $commandName);
73
    }
74
75
    /**
76
     * @Then it should not execute :commandName
77
     */
78
    public function iShouldNotSee(string $commandName)
79
    {
80
        $this->dryRunCommand($commandName);
81
        Assert::notContains($this->lastOutput, $commandName);
82
    }
83
84
    private function dryRunCommand(string $command): void
85
    {
86
        $this->process = new Process([
87
            'make',
88
            '-n',
89
            $command,
90
            sprintf('--file=%s', $this->getMakeFilePathname()),
91
        ]);
92
        $this->process->run();
93
        $this->lastOutput = $this->process->getOutput();
94
    }
95
96
    private function getMakeFileDir(): string
97
    {
98
        return __DIR__.'/../../../../var';
99
    }
100
101
    private function getMakeFilePathname(): string
102
    {
103
        return sprintf('%s/Makefile', $this->getMakeFileDir());
104
    }
105
106
    private function ensureMakeFileDirExist(): void
107
    {
108
        $fileSystem = new Filesystem();
109
        $fileSystem->mkdir($this->getMakeFileDir());
110
    }
111
112
    private function writeMakefileData(string $data)
113
    {
114
        $this->ensureMakeFileDirExist();
115
        file_put_contents($this->getMakeFilePathname(), $data);
116
    }
117
}
118