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

EnvironmentContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 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
A iRun() 0 11 1
A theApplicationMustHaveRunSuccessfully() 0 6 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-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
     * @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