Completed
Push — develop ( 71fd61...bbac44 )
by Jaap
06:03 queued 02:27
created

EnvironmentContext::theOutputContainNot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 9.4285
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
Coding Style introduced by
Space found after opening bracket of FOREACH loop
Loading history...
Coding Style introduced by
Space found before closing bracket of FOREACH loop
Loading history...
Coding Style introduced by
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
     * @Given /^I ran "phpdoc(?: ((?:\"|[^"])*))?"$/
81
     * @When /^I run "phpdoc(?: ((?:\"|[^"])*))?"$/
82
     */
83
    public function iRun($argumentsString)
84
    {
85
        $argumentsString .= ' --template=xml';
86
        $argumentsString = strtr($argumentsString, array('\'' => '"'));
87
//      the app is always run in debug mode to catch debug information and collect the AST that is written to disk
88
        $this->process->setCommandLine(
89
            sprintf('%s %s %s', 'php', escapeshellarg($this->binaryPath), $argumentsString . ' -vvv')
90
        );
91
        $this->process->start();
92
        $this->process->wait();
93
    }
94
95
    /**
96
     * @Then /^the application must have run successfully$/
97
     * @throws \Exception when exit code of phpdoc was not 0.
98
     */
99
    public function theApplicationMustHaveRunSuccessfully()
100
    {
101
        if ($this->process->getExitCode() !== 0) {
102
            throw new \Exception($this->process->getErrorOutput());
103
        }
104
    }
105
106
    /**
107
     * @Then /^output contains "([^"]*)"$/
108
     * @throws \Exception
109
     */
110
    public function theOutputContains($regex)
111
    {
112
        if (!strpos($this->process->getOutput(), $regex)) {
113
            throw new \Exception(
114
                sprintf('output doesn\'t match "%s"', $regex));
115
        }
116
    }
117
118
    /**
119
     * @Then /^output doesn't contain "([^"]*)"$/
120
     * @throws \Exception
121
     */
122
    public function theOutputContainNot($regex)
123
    {
124
        if (strpos($this->process->getOutput(), $regex)) {
125
            throw new \Exception(
126
                sprintf('output contains "%s", which was not expected', $regex));
127
        }
128
    }
129
130
    public function getWorkingDir()
131
    {
132
        return $this->workingDir;
133
    }
134
135
    public function getErrorOutput()
136
    {
137
        return $this->process->getErrorOutput();
138
    }
139
}
140