Completed
Push — develop ( 6c5b94...329566 )
by Chuck
11s
created

EnvironmentContext   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 0
loc 147
c 1
b 0
f 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeScenario() 0 12 2
A cleanup() 0 8 3
A loadASingleFile() 0 5 1
C loadAProject() 0 25 7
A iRun() 0 11 1
A theApplicationMustHaveRunSuccessfully() 0 6 2
A theOutputContains() 0 8 2
A theOutputContainNot() 0 8 2
A getWorkingDir() 0 4 1
A getErrorOutput() 0 4 1
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 FilesystemIterator;
17
use RecursiveDirectoryIterator;
18
use RecursiveIteratorIterator;
19
use RuntimeException;
20
use Symfony\Component\Process\Process;
21
use Webmozart\Assert\Assert;
22
23
final class EnvironmentContext implements Context\Context
24
{
25
    private $workingDir;
26
27
    /** @var Process */
28
    private $process;
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
     * @beforeScenario
43
     */
44
    public function beforeScenario()
45
    {
46
        if (!is_dir($this->getWorkingDir())) {
47
            mkdir($this->getWorkingDir(), 0755, true);
48
        }
49
50
        Assert::directory($this->getWorkingDir());
51
        $this->binaryPath = __DIR__ . '/../../../bin/phpdoc';
52
        $this->process = new Process(null);
53
        $this->process->setWorkingDirectory($this->getWorkingDir());
54
        chdir($this->getWorkingDir());
55
    }
56
57
    /**
58
     * @AfterScenario
59
     */
60
    public function cleanup()
61
    {
62
        $di = new RecursiveDirectoryIterator($this->getWorkingDir(), FilesystemIterator::SKIP_DOTS);
63
        $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
64
        foreach ($ri as $file) {
65
            $file->isDir() ? rmdir($file) : unlink($file);
66
        }
67
    }
68
69
    /**
70
     * @Given /^A single file named "([^"]*)" based on "([^"]*)"$/
71
     */
72
    public function loadASingleFile($dest, $source)
73
    {
74
        Assert::fileExists(__DIR__ . '/../assets/singlefile/' . $source);
75
        copy(__DIR__ . '/../assets/singlefile/' . $source, $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dest);
76
    }
77
78
    /**
79
     * @Given /^A project named "([^"]*)" based on "([^"]*)"$/
80
     */
81
    public function loadAProject($dest, $source)
82
    {
83
        $sourceDir = __DIR__ . '/../assets/projects/' . $source;
84
        Assert::directory($sourceDir);
85
        $destDir = $this->getWorkingDir() . DIRECTORY_SEPARATOR . $dest;
86
87
        if (!mkdir($destDir, 0755) && !is_dir($destDir)) {
88
            throw new RuntimeException(sprintf('Directory "%s" was not created', $destDir));
89
        }
90
91
        foreach ($iterator = new RecursiveIteratorIterator(
92
                new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS),
93
            RecursiveIteratorIterator::SELF_FIRST
94
                ) as $item) {
95
            if ($item->isDir()) {
96
                if (!mkdir($destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()) &&
97
                    !is_dir($destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName())
98
                ) {
99
                    throw new RuntimeException(sprintf('Directory "%s" was not created', $destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName()));
100
                }
101
            } else {
102
                copy($item, $destDir . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
103
            }
104
        }
105
    }
106
107
    /**
108
     * @Given /^I ran "phpdoc(?: ((?:\"|[^"])*))?"$/
109
     * @When /^I run "phpdoc(?: ((?:\"|[^"])*))?"$/
110
     */
111
    public function iRun($argumentsString)
112
    {
113
        $argumentsString .= ' --template=xml';
114
        $argumentsString = strtr($argumentsString, ['\'' => '"']);
115
//      the app is always run in debug mode to catch debug information and collect the AST that is written to disk
116
        $this->process->setCommandLine(
117
            sprintf('%s %s %s', 'php', escapeshellarg($this->binaryPath), $argumentsString . ' -vvv')
118
        );
119
        $this->process->start();
120
        $this->process->wait();
121
    }
122
123
    /**
124
     * @Then /^the application must have run successfully$/
125
     * @throws \Exception when exit code of phpdoc was not 0.
126
     */
127
    public function theApplicationMustHaveRunSuccessfully()
128
    {
129
        if ($this->process->getExitCode() !== 0) {
130
            throw new \Exception($this->process->getErrorOutput());
131
        }
132
    }
133
134
    /**
135
     * @Then /^output contains "([^"]*)"$/
136
     * @throws \Exception
137
     */
138
    public function theOutputContains($regex)
139
    {
140
        if (!strpos($this->process->getOutput(), $regex)) {
141
            throw new \Exception(
142
                sprintf('output doesn\'t match "%s"', $regex)
143
            );
144
        }
145
    }
146
147
    /**
148
     * @Then /^output doesn't contain "([^"]*)"$/
149
     * @throws \Exception
150
     */
151
    public function theOutputContainNot($regex)
152
    {
153
        if (strpos($this->process->getOutput(), $regex)) {
154
            throw new \Exception(
155
                sprintf('output contains "%s", which was not expected', $regex)
156
            );
157
        }
158
    }
159
160
    public function getWorkingDir()
161
    {
162
        return $this->workingDir;
163
    }
164
165
    public function getErrorOutput()
166
    {
167
        return $this->process->getErrorOutput();
168
    }
169
}
170