Completed
Push — develop ( 04dbcf...051570 )
by Jaap
09:44
created

tests/features/bootstrap/EnvironmentContext.php (3 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-2017 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 FilesystemIterator;
17
use RecursiveDirectoryIterator;
18
use RecursiveIteratorIterator;
19
use Symfony\Component\Process\Process;
20
use Webmozart\Assert\Assert;
21
22
final class EnvironmentContext implements Context\Context
23
{
24
    private $workingDir;
25
26
    /** @var  Process */
27
    private $process;
28
29
30
    private $binaryPath;
31
32
    /**
33
     * EnvironmentContext constructor.
34
     * @param string $workingDir
35
     */
36
    public function __construct($workingDir)
37
    {
38
        $this->workingDir = $workingDir;
39
    }
40
41
42
    /**
43
     * @beforeScenario
44
     */
45
    public function beforeScenario()
46
    {
47
        if (!is_dir($this->getWorkingDir())) {
48
            mkdir($this->getWorkingDir(), 0755, true);
49
        }
50
51
        Assert::directory($this->getWorkingDir());
52
        $this->binaryPath = __DIR__ . '/../../../bin/phpdoc';
53
        $this->process = new Process(null);
54
        $this->process->setWorkingDirectory($this->getWorkingDir());
55
        chdir($this->getWorkingDir());
56
    }
57
58
    /**
59
     * @AfterScenario
60
     */
61
    public function cleanup()
62
    {
63
        $di = new RecursiveDirectoryIterator($this->getWorkingDir(), FilesystemIterator::SKIP_DOTS);
64
        $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
65
        foreach ( $ri as $file ) {
0 ignored issues
show
Space found after opening bracket of FOREACH loop
Loading history...
Space found before closing bracket of FOREACH loop
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
66
            $file->isDir() ?  rmdir($file) : unlink($file);
67
        }
68
    }
69
70
    /**
71
     * @Given /^A single file named "([^"]*)" based on "([^"]*)"$/
72
     */
73
    public function loadASingleFile($dest, $source)
74
    {
75
        Assert::fileExists(__DIR__ . '/../assets/singlefile/'. $source);
76
        copy(__DIR__ . '/../assets/singlefile/'. $source, $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dest);
77
    }
78
79
    /**
80
     * @When /^I run "phpdoc(?: ((?:\"|[^"])*))?"$/
81
     */
82
    public function iRun($argumentsString)
83
    {
84
        $argumentsString .= ' --template=xml';
85
        $argumentsString = strtr($argumentsString, array('\'' => '"'));
86
//      the app is always run in debug mode to catch debug information and collect the AST that is written to disk
87
        $this->process->setCommandLine(
88
            sprintf('%s %s %s', 'php', escapeshellarg($this->binaryPath), $argumentsString . ' -vvv')
89
        );
90
        $this->process->start();
91
        $this->process->wait();
92
    }
93
94
    /**
95
     * @Then /^the application must have run successfully$/
96
     * @throws \Exception when exit code of phpdoc was not 0.
97
     */
98
    public function theApplicationMustHaveRunSuccessfully()
99
    {
100
        if ($this->process->getExitCode() !== 0) {
101
            throw new \Exception($this->process->getErrorOutput());
102
        }
103
    }
104
105
    public function getWorkingDir()
106
    {
107
        return $this->workingDir;
108
    }
109
110
    public function getErrorOutput()
111
    {
112
        return $this->process->getErrorOutput();
113
    }
114
}
115