Completed
Push — master ( 5838a7...145d26 )
by Hannes
04:37
created

FeatureContext::theExitCodeIs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
use Behat\Behat\Tester\Exception\PendingException;
4
use Behat\Behat\Context\Context;
5
use Behat\Behat\Context\SnippetAcceptingContext;
6
use Behat\Gherkin\Node\PyStringNode;
7
use Behat\Gherkin\Node\TableNode;
8
9
/**
10
 * Defines application features from the specific context.
11
 */
12
class FeatureContext implements Context, 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...
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...
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