Test Failed
Push — master ( 59f97c...82a449 )
by Hannes
02:54
created

FeatureContext::failuresAreFound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
use Behat\Behat\Tester\Exception\PendingException;
0 ignored issues
show
Bug introduced by
The type Behat\Behat\Tester\Exception\PendingException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use Behat\Behat\Context\Context;
0 ignored issues
show
Bug introduced by
The type Behat\Behat\Context\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Behat\Behat\Context\SnippetAcceptingContext;
0 ignored issues
show
Bug introduced by
The type Behat\Behat\Context\SnippetAcceptingContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Behat\Gherkin\Node\PyStringNode;
0 ignored issues
show
Bug introduced by
The type Behat\Gherkin\Node\PyStringNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Behat\Gherkin\Node\TableNode;
0 ignored issues
show
Bug introduced by
The type Behat\Gherkin\Node\TableNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * Defines application features from the specific context.
11
 */
12
class FeatureContext implements Context, SnippetAcceptingContext
13
{
14
    /**
15
     * @var string Name of directory where test files are keept
16
     */
17
    private $sourceDir;
18
19
    /**
20
     * @var string[] Arguments used when invoking readme-tester
21
     */
22
    private $args = [];
23
24
    /**
25
     * @var integer The number of processed files
26
     */
27
    private $fileCount = 0;
28
29
    /**
30
     * @var integer The number of assertions
31
     */
32
    private $assertionCount = 0;
33
34
    /**
35
     * @var integer The number of failed assertions
36
     */
37
    private $failureCount = 0;
38
39
    /**
40
     * @var integer Return value of the last readme-tester execution
41
     */
42
    private $returnValue = 0;
43
44
    public function __construct()
45
    {
46
        $this->sourceDir = sys_get_temp_dir() . '/readmetester-behat-' . rand() . '/';
47
        mkdir($this->sourceDir);
48
    }
49
50
    public function __destruct()
51
    {
52
        exec("rm -rf {$this->sourceDir}");
53
    }
54
55
    /**
56
     * @Given a markdown file:
57
     */
58
    public function aMarkdownFile(PyStringNode $string)
59
    {
60
        file_put_contents($this->sourceDir . rand() . '.md', (string)$string);
61
    }
62
63
    /**
64
     * @Given a source file :filename:
65
     */
66
    public function aSourceFile($filename, PyStringNode $string)
67
    {
68
        file_put_contents($this->sourceDir . $filename, (string)$string);
69
    }
70
71
72
   /**
73
     * @Given the command line argument :argument
74
     */
75
    public function theCommandLineArgument($argument)
76
    {
77
        $this->args[] = $argument;
78
    }
79
80
81
    /**
82
     * @When I run readme tester
83
     */
84
    public function iRunReadmeTester()
85
    {
86
        $command = realpath('bin/readme-tester') . " test {$this->sourceDir} " . implode(' ', $this->args);
87
88
        $cwd = getcwd();
89
        chdir($this->sourceDir);
90
        $lastLine = exec($command, $output, $this->returnValue);
91
        chdir($cwd);
92
93
        $success = preg_match('/^(\d+) files? tested, (\d+) assertions?, (\d+) failures?.$/', $lastLine, $matches);
94
95
        if (!$success) {
96
            throw new \Exception("Last line of output did not contain test report: ($lastLine)");
97
        }
98
99
        $this->fileCount = (int)$matches[1];
100
        $this->assertionCount = (int)$matches[2];
101
        $this->failureCount = (int)$matches[3];
102
    }
103
104
    /**
105
     * @Then :number tests are executed
106
     */
107
    public function testsAreExecuted(int $number)
108
    {
109
        if ($this->assertionCount != $number) {
110
            throw new \Exception("{$this->assertionCount} assertions tested, expected $number");
111
        }
112
    }
113
114
    /**
115
     * @Then :number failures are found
116
     */
117
    public function failuresAreFound($number)
118
    {
119
        if ($this->failureCount != $number) {
120
            throw new \Exception("{$this->failureCount} failures found, expected $number");
121
        }
122
    }
123
124
    /**
125
     * @Then the exit code is :code
126
     */
127
    public function theExitCodeIs(int $code)
128
    {
129
        if ($this->returnValue !== $code) {
130
            throw new \Exception("Readme tester exited with {$this->returnValue}, expected $code");
131
        }
132
    }
133
}
134