Completed
Push — master ( 8d1de0...f3fa0d )
by Loïc
02:42 queued 11s
created

MakefileContext::useDefaultMakeFileCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 :firstData
44
     * @When I override makefile :commandName command with :firstdata and :secondData
45
     */
46
    public function overrideCommandWithData(string $commandName, ...$data)
47
    {
48
        $commandData = implode("\n\t", $data);
49
50
        $this->ensureMakeFileDirExist();
51
        $this->writeMakefileData(<<<EOM
52
include Makefile.dist
53
54
$commandName:
55
\t$commandData
56
57
EOM
58
        );
59
    }
60
61
    /**
62
     * @Then the command make :commandName should exist
63
     */
64
    public function commandExist(string $commandName): void
65
    {
66
        $this->dryRunCommand($commandName);
67
        Assert::true($this->process->isSuccessful());
68
    }
69
70
    /**
71
     * @Then it should execute :commandName
72
     */
73
    public function iShouldExecute(string $commandName)
74
    {
75
        Assert::contains($this->lastOutput, $commandName);
76
    }
77
78
    /**
79
     * @Then it should not execute :commandName
80
     */
81
    public function iShouldNotSee(string $commandName)
82
    {
83
        $this->dryRunCommand($commandName);
84
        Assert::notContains($this->lastOutput, $commandName);
85
    }
86
87
    private function dryRunCommand(string $command): void
88
    {
89
        $this->process = new Process([
90
            'make',
91
            '-n',
92
            $command,
93
            sprintf('--file=%s', $this->getMakeFilePathname()),
94
        ]);
95
        $this->process->run();
96
        $this->lastOutput = $this->process->getOutput();
97
    }
98
99
    private function getMakeFileDir(): string
100
    {
101
        return __DIR__.'/../../../../var';
102
    }
103
104
    private function getMakeFilePathname(): string
105
    {
106
        return sprintf('%s/Makefile', $this->getMakeFileDir());
107
    }
108
109
    private function ensureMakeFileDirExist(): void
110
    {
111
        $fileSystem = new Filesystem();
112
        $fileSystem->mkdir($this->getMakeFileDir());
113
    }
114
115
    private function writeMakefileData(string $data)
116
    {
117
        $this->ensureMakeFileDirExist();
118
        file_put_contents($this->getMakeFilePathname(), $data);
119
    }
120
}
121