Completed
Push — master ( 24169d...e6d1d6 )
by Mike
02:29
created

tests/features/bootstrap/EnvironmentContext.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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() : void
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);
0 ignored issues
show
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
        $this->process->setWorkingDirectory($this->getWorkingDir());
64
        chdir($this->getWorkingDir());
65
    }
66
67
    /**
68
     * @AfterScenario
69
     */
70
    public function cleanup() : void
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) : void
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) : void
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
        $this->process->setWorkingDirectory($destDir);
117
        chdir($destDir);
118
        $this->workingDir = $destDir;
119
    }
120
121
122
    /**
123
     * @Given /^configuration file based on "([^"]*)"$/
124
     */
125
    public function configurationFileBasedOnIn($configFile) : void
126
    {
127
        Assert::fileExists(__DIR__ . '/../assets/config/' . $configFile);
128
        copy(
129
            __DIR__ . '/../assets/config/' . $configFile,
130
            $this->getWorkingDir(). '/phpdoc.xml'
131
        );
132
    }
133
134
    /**
135
     * @Given /^working directory is "([^"]*)"$/
136
     */
137
    public function workingDirectoryIs($dir) : void
138
    {
139
        $fullDir = $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dir;
140
        $this->process->setWorkingDirectory($fullDir);
141
        chdir($fullDir);
142
    }
143
144
    /**
145
     * @Given /^I ran "phpdoc(?: ((?:\"|[^"])*))?"$/
146
     * @When /^I run "phpdoc(?: ((?:\"|[^"])*))?"$/
147
     */
148
    public function iRun($argumentsString = '') : void
149
    {
150
        $argumentsString = strtr($argumentsString, ['\'' => '"']);
151
        if ($this->process->isStarted()) {
152
            $this->process->clearErrorOutput()->clearOutput();
153
        }
154
155
        // the app is always run in debug mode to catch debug information and collect the AST that is written to disk
156
        $this->process->setCommandLine(
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...ocess::setCommandLine() has been deprecated with message: since Symfony 4.2.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
157
            sprintf('%s %s %s', 'php', escapeshellarg($this->binaryPath), $argumentsString . ' -vvv')
158
        );
159
        $this->process->start();
160
        $this->process->wait();
161
    }
162
163
    /**
164
     * @Then /^the application must have run successfully$/
165
     * @throws \Exception when exit code of phpdoc was not 0.
166
     */
167
    public function theApplicationMustHaveRunSuccessfully() : void
168
    {
169
        if ($this->process->getExitCode() !== 0) {
170
            throw new \Exception($this->process->getErrorOutput());
171
        }
172
    }
173
174
    /**
175
     * @Then /^output contains "([^"]*)"$/
176
     * @throws \Exception
177
     */
178
    public function theOutputContains($regex) : void
179
    {
180
        if (strpos($this->process->getOutput(), $regex) === false && strpos($this->process->getErrorOutput(), $regex) === false) {
181
            throw new \Exception(
182
                sprintf('output %s doesn\'t match "%s"', $this->process->getOutput(), $regex)
183
            );
184
        }
185
    }
186
187
    /**
188
     * @Then /^output doesn't contain "([^"]*)"$/
189
     * @throws \Exception
190
     */
191
    public function theOutputContainNot($regex) : void
192
    {
193
        if (strpos($this->process->getErrorOutput(), $regex)) {
194
            throw new \Exception(
195
                sprintf('output contains "%s", which was not expected', $regex)
196
            );
197
        }
198
    }
199
200
    public function getWorkingDir() : string
201
    {
202
        return $this->workingDir;
203
    }
204
205
    public function getErrorOutput() : string
206
    {
207
        return $this->process->getErrorOutput();
208
    }
209
210
    /**
211
     * @Then /^documentation should be found in "([^"]*)"$/
212
     */
213
    public function documentationShouldBeFoundIn($expectedDir) : void
214
    {
215
        Assert::directory($this->getWorkingDir() . DIRECTORY_SEPARATOR . $expectedDir);
216
    }
217
}
218