Completed
Pull Request — master (#124)
by Jon
13:19
created

iHaveNotConfiguredBehatToUseSharedKernel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Behat\Behat\Context\SnippetAcceptingContext;
4
use Behat\Gherkin\Node\PyStringNode;
5
use Symfony\Component\Process\PhpExecutableFinder;
6
use Symfony\Component\Process\Process;
7
8
/**
9
 * Behat context class.
10
 */
11
class FeatureContext implements SnippetAcceptingContext
0 ignored issues
show
Deprecated Code introduced by
The interface Behat\Behat\Context\SnippetAcceptingContext has been deprecated with message: will be removed in 4.0. Use --snippets-for CLI option instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
12
{
13
    /**
14
     * @var string
15
     */
16
    private $phpBin;
17
    /**
18
     * @var Process
19
     */
20
    private $process;
21
22
    /**
23
     * Prepares test folders in the temporary directory.
24
     *
25
     * @BeforeScenario
26
     */
27
    public function prepareProcess()
28
    {
29
        $phpFinder = new PhpExecutableFinder();
30
        if (false === $php = $phpFinder->find()) {
31
            throw new \RuntimeException('Unable to find the PHP executable.');
32
        }
33
        $this->phpBin = $php;
34
        $this->process = new Process(null);
35
    }
36
37
38
    /**
39
     * @Given I have not configured behat to use shared kernel
40
     */
41
    public function iHaveNotConfiguredBehatToUseSharedKernel()
42
    {
43
    }
44
45
    /**
46
     * @Given I have configured behat to use shared kernel
47
     */
48
    public function iHaveConfiguredBehatToUseSharedKernel()
49
    {
50
    }
51
52
    /**
53
     * Runs behat command with provided parameters
54
     *
55
     * @When /^I run "behat(?: ((?:\"|[^"])*))?"$/
56
     *
57
     * @param   string $argumentsString
58
     */
59
    public function iRunBehat($argumentsString = '')
60
    {
61
        $argumentsString = strtr($argumentsString, array('\'' => '"'));
62
63
        $this->process->setWorkingDirectory(__DIR__ . '/../../testapp');
64
        $this->process->setCommandLine(
65
            sprintf(
66
                '%s %s %s %s',
67
                $this->phpBin,
68
                escapeshellarg(BEHAT_BIN_PATH),
69
                $argumentsString,
70
                strtr('--format-settings=\'{"timer": false}\'', array('\'' => '"', '"' => '\"'))
71
            )
72
        );
73
        $this->process->start();
74
        $this->process->wait();
75
    }
76
77
    /**
78
     * Checks whether previously runned command passes|failes with provided output.
79
     *
80
     * @Then /^it should (fail|pass) with:$/
81
     *
82
     * @param   string       $success "fail" or "pass"
83
     * @param   PyStringNode $text    PyString text instance
84
     */
85
    public function itShouldPassWith($success, PyStringNode $text)
86
    {
87
        $this->itShouldFail($success);
88
        $this->theOutputShouldContain($text);
89
    }
90
91
    /**
92
     * Checks whether last command output contains provided string.
93
     *
94
     * @Then the output should contain:
95
     *
96
     * @param   PyStringNode $text PyString text instance
97
     */
98
    public function theOutputShouldContain(PyStringNode $text)
99
    {
100
        PHPUnit_Framework_Assert::assertContains($this->getExpectedOutput($text), $this->getOutput());
101
    }
102
103
    private function getExpectedOutput(PyStringNode $expectedText)
104
    {
105
        $text = strtr($expectedText, array('\'\'\'' => '"""'));
106
107
        // windows path fix
108
        if ('/' !== DIRECTORY_SEPARATOR) {
109
            $text = preg_replace_callback(
110
                '/ features\/[^\n ]+/', function ($matches) {
111
                    return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]);
112
                }, $text
113
            );
114
            $text = preg_replace_callback(
115
                '/\<span class\="path"\>features\/[^\<]+/', function ($matches) {
116
                    return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]);
117
                }, $text
118
            );
119
            $text = preg_replace_callback(
120
                '/\+[fd] [^ ]+/', function ($matches) {
121
                    return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]);
122
                }, $text
123
            );
124
        }
125
126
        return $text;
127
    }
128
129
    /**
130
     * Checks whether previously runned command failed|passed.
131
     *
132
     * @Then /^it should (fail|pass)$/
133
     *
134
     * @param   string $success "fail" or "pass"
135
     */
136
    public function itShouldFail($success)
137
    {
138
        if ('fail' === $success) {
139 View Code Duplication
            if (0 === $this->getExitCode()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
                echo 'Actual output:' . PHP_EOL . PHP_EOL . $this->getOutput();
141
            }
142
143
            PHPUnit_Framework_Assert::assertNotEquals(0, $this->getExitCode());
144
        } else {
145 View Code Duplication
            if (0 !== $this->getExitCode()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
                echo 'Actual output:' . PHP_EOL . PHP_EOL . $this->getOutput();
147
            }
148
149
            PHPUnit_Framework_Assert::assertEquals(0, $this->getExitCode());
150
        }
151
    }
152
153
    private function getExitCode()
154
    {
155
        return $this->process->getExitCode();
156
    }
157
158
    private function getOutput()
159
    {
160
        $output = $this->process->getErrorOutput() . $this->process->getOutput();
161
162
        // Normalize the line endings in the output
163
        if ("\n" !== PHP_EOL) {
164
            $output = str_replace(PHP_EOL, "\n", $output);
165
        }
166
167
        return trim(preg_replace("/ +$/m", '', $output));
168
    }
169
}
170