Completed
Push — develop ( 7f7db1...e7f876 )
by Jaap
14s
created

EnvironmentContext::configurationFileBasedOnIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2018 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Behat\Contexts;
14
15
use Behat\Behat\Context;
16
use Behat\Behat\Tester\Exception\PendingException;
17
use FilesystemIterator;
18
use RecursiveDirectoryIterator;
19
use RecursiveIteratorIterator;
20
use RuntimeException;
21
use Symfony\Component\Process\Process;
22
use Webmozart\Assert\Assert;
23
24
final class EnvironmentContext implements Context\Context
25
{
26
    private $workingDir;
27
28
    /** @var Process */
29
    private $process;
30
31
    private $binaryPath;
32
33
    /**
34
     * @var null
35
     */
36
    private $pharPath;
37
38
    /**
39
     * EnvironmentContext constructor.
40
     * @param string $workingDir
41
     * @param null $pharPath
42
     */
43
    public function __construct($workingDir, $pharPath = null)
44
    {
45
        $this->workingDir = $workingDir;
46
        $this->pharPath = $pharPath;
47
    }
48
49
    /**
50
     * @beforeScenario
51
     */
52
    public function beforeScenario()
53
    {
54
        //WE no we have some deprecations in phpdocumentor. Let tests pass while we are refactoring stuff.
55
        error_reporting(error_reporting() & ~E_USER_DEPRECATED);
56
        if (!is_dir($this->getWorkingDir())) {
57
            mkdir($this->getWorkingDir(), 0755, true);
58
        }
59
60
        Assert::directory($this->getWorkingDir());
61
        $this->binaryPath = $this->pharPath ? __DIR__ . '/../../../' . $this->pharPath : __DIR__ . '/../../../bin/phpdoc';
62
        $this->process = new Process(null);
63
        $this->process->setWorkingDirectory($this->getWorkingDir());
64
        chdir($this->getWorkingDir());
65
    }
66
67
    /**
68
     * @AfterScenario
69
     */
70
    public function cleanup()
71
    {
72
        $di = new RecursiveDirectoryIterator($this->getWorkingDir(), FilesystemIterator::SKIP_DOTS);
73
        $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
74
        foreach ($ri as $file) {
75
            $file->isDir() ? rmdir($file) : unlink($file);
76
        }
77
    }
78
79
    /**
80
     * @Given /^A single file named "([^"]*)" based on "([^"]*)"$/
81
     */
82
    public function loadASingleFile($dest, $source)
83
    {
84
        Assert::fileExists(__DIR__ . '/../assets/singlefile/' . $source);
85
        copy(__DIR__ . '/../assets/singlefile/' . $source, $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dest);
86
    }
87
88
    /**
89
     * @Given /^A project named "([^"]*)" based on "([^"]*)"$/
90
     */
91
    public function loadAProject($dest, $source)
92
    {
93
        $sourceDir = __DIR__ . '/../assets/projects/' . $source;
94
        Assert::directory($sourceDir);
95
        $destDir = $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dest;
96
97
        if (!is_dir($destDir) && !mkdir($destDir, 0755)) {
98
            throw new RuntimeException(sprintf('Directory "%s" was not created', $destDir));
99
        }
100
101
        foreach ($iterator = new RecursiveIteratorIterator(
102
            new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS),
103
            RecursiveIteratorIterator::SELF_FIRST
104
        ) as $item) {
105
            if ($item->isDir()) {
106
                if (!mkdir($destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()) &&
107
                    !is_dir($destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName())
108
                ) {
109
                    throw new RuntimeException(sprintf('Directory "%s" was not created', $destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()));
110
                }
111
            } else {
112
                copy($item, $destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
113
            }
114
        }
115
    }
116
117
118
    /**
119
     * @Given /^configuration file based on "([^"]*)" in "([^"]*)"$/
120
     */
121
    public function configurationFileBasedOnIn($configFile, $destDir)
122
    {
123
        Assert::fileExists(__DIR__ . '/../assets/config/' . $configFile);
124
        copy(
125
            __DIR__ . '/../assets/config/' . $configFile,
126
            $this->getWorkingDir() . DIRECTORY_SEPARATOR . $destDir . DIRECTORY_SEPARATOR . 'phpdoc.xml'
127
        );
128
    }
129
130
    /**
131
     * @Given /^working directory is "([^"]*)"$/
132
     */
133
    public function workingDirectoryIs($dir)
134
    {
135
        $fullDir = $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dir;
136
        $this->process->setWorkingDirectory($fullDir);
137
        chdir($fullDir);
138
    }
139
140
    /**
141
     * @Given /^I ran "phpdoc(?: ((?:\"|[^"])*))?"$/
142
     * @When /^I run "phpdoc(?: ((?:\"|[^"])*))?"$/
143
     */
144
    public function iRun($argumentsString = '')
145
    {
146
        $argumentsString .= ' --template=xml';
147
        $argumentsString = strtr($argumentsString, ['\'' => '"']);
148
        if ($this->process->isStarted()) {
149
            $this->process->clearErrorOutput()->clearOutput();
150
        }
151
//      the app is always run in debug mode to catch debug information and collect the AST that is written to disk
152
        $this->process->setCommandLine(
153
            sprintf('%s %s %s', 'php', escapeshellarg($this->binaryPath), $argumentsString . ' -vvv')
154
        );
155
        $this->process->start();
156
        $this->process->wait();
157
    }
158
159
    /**
160
     * @Then /^the application must have run successfully$/
161
     * @throws \Exception when exit code of phpdoc was not 0.
162
     */
163
    public function theApplicationMustHaveRunSuccessfully()
164
    {
165
        if ($this->process->getExitCode() !== 0) {
166
            throw new \Exception($this->process->getErrorOutput());
167
        }
168
    }
169
170
    /**
171
     * @Then /^output contains "([^"]*)"$/
172
     * @throws \Exception
173
     */
174
    public function theOutputContains($regex)
175
    {
176
        if (strpos($this->process->getOutput(), $regex) === false && strpos($this->process->getErrorOutput(), $regex) === false) {
177
            throw new \Exception(
178
                sprintf('output %s doesn\'t match "%s"', $this->process->getOutput(), $regex)
179
            );
180
        }
181
    }
182
183
    /**
184
     * @Then /^output doesn't contain "([^"]*)"$/
185
     * @throws \Exception
186
     */
187
    public function theOutputContainNot($regex)
188
    {
189
        if (strpos($this->process->getErrorOutput(), $regex)) {
190
            throw new \Exception(
191
                sprintf('output contains "%s", which was not expected', $regex)
192
            );
193
        }
194
    }
195
196
    public function getWorkingDir()
197
    {
198
        return $this->workingDir;
199
    }
200
201
    public function getErrorOutput()
202
    {
203
        return $this->process->getErrorOutput();
204
    }
205
206
    /**
207
     * @Then /^documentation should be found in "([^"]*)"$/
208
     */
209
    public function documentationShouldBeFoundIn($expectedDir)
210
    {
211
        Assert::directory($this->getWorkingDir() . DIRECTORY_SEPARATOR . $expectedDir);
212
    }
213
}
214