FeatureContext::iRunALocalScript()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
use Behat\Behat\Context\Context;
4
use Behat\Gherkin\Node\PyStringNode;
5
6
/**
7
 * Defines application features from the specific context.
8
 */
9
class FeatureContext implements Context
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private $testDir;
12
    private $zBinary;
13
    private $zBinaryPath;
14
    private $packageBinary;
15
    private $response;
16
17
18
    /**
19
     * Initializes context.
20
     *
21
     * Every scenario gets its own context instance.
22
     * You can also pass arbitrary arguments to the
23
     * context constructor through behat.yml.
24
     */
25
    public function __construct()
26
    {
27
        $this->testDir = __DIR__ . '/tmp';
28
        $this->zBinary = escapeshellarg(__DIR__ . '/../../bin/z');
29
        $this->zBinaryPath = realpath(__DIR__ . '/../../bin/z');
30
        $this->packageBinary = escapeshellarg(__DIR__ . '/../../bin/package.php');
31
32
        putenv('ZPATH=.');
33
        putenv('ZFILE=z.yml');
34
    }
35
36
37
    /**
38
     * @Given /^I am in (?P<type>a|the) test directory$/
39
     */
40
    public function iAmInATestDirectory($type)
41
    {
42
        if ($type == "a") {
43
            if (!is_dir($this->testDir)) {
44
                mkdir($this->testDir);
45
            }
46
            chdir($this->testDir);
47
            shell_exec('rm -r *');
48
        } else {
49
            chdir($this->testDir);
50
        }
51
    }
52
53
54
    /**
55
     * @Given /^there is (?:a )?file "(?P<file>[^"]+)"$/
56
     */
57
    public function thereIsFile($file, PyStringNode $string)
58
    {
59
        $dir = dirname($file);
60
        if ($dir && !is_dir($dir)) {
61
            mkdir($dir);
62
        }
63
64
        file_put_contents($file, $string->getRaw());
65
    }
66
67
68
    /**
69
     * @Given /^there is an executable file "([^"]*)" with a shebang pointing to Z$/
70
     */
71
    public function thereIsAFileWithAShebangPointingToZ($file, PyStringNode $string)
72
    {
73
        $this->thereIsFile($file, $string);
74
        // this is necessary when the binary is inside a path that contains spaces.
75
        // A shebang path cannot contain spaces... whoa.
76
        if (file_exists("/tmp/z-interpreter")) {
77
            unlink("/tmp/z-interpreter");
78
        }
79
        symlink($this->zBinaryPath, "/tmp/z-interpreter");
80
        file_put_contents($file, "#!/tmp/z-interpreter -\n\n" . file_get_contents($file));
81
        chmod($file, 0755);
82
    }
83
84
85
    /**
86
     * @When /^I run "z ([^"]*)"$/
87
     */
88
    public function iRunZ($cmd)
89
    {
90
        $this->response = shell_exec($this->zBinary . ' ' . $cmd . ' 2>&1');
91
    }
92
93
    /**
94
     * @When /^I run "package ([^"]*)"$/
95
     */
96
    public function iRunPackage($cmd)
97
    {
98
        $this->response = shell_exec('php ' . $this->packageBinary . ' ' . $cmd . ' 2>&1');
99
    }
100
101
    /**
102
     * @When /^I run ".\/([^"]*)"$/
103
     */
104
    public function iRunALocalScript($cmd)
105
    {
106
        $this->response = shell_exec('./' . $cmd . ' 2>&1');
107
    }
108
109
    /**
110
     * @Then /^I should (?P<negate>not )?see text matching "(?P<pattern>[^"]*)"$/
111
     */
112
    public function iShouldSeeTextMatching($pattern, $negate = false)
113
    {
114
        $res = preg_match($pattern, $this->response);
115
        if ((bool)$res == (bool)$negate) {
116
            throw new UnexpectedValueException(
117
                "Pattern {$pattern} was" . ($negate ? "" : " not") . " found in program response:\n{$this->response}"
118
            );
119
        }
120
    }
121
}
122