FeatureContext   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 4
dl 0
loc 118
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createWorkDir() 0 11 1
A removeWorkDir() 0 5 1
A theSpecFileShouldContain() 0 9 2
A theSpecFileContains() 0 5 1
A saveFile() 0 9 2
A createApplicationTester() 0 8 1
A iRunPhpspecExemplifyToAddTheMethodTo() 0 5 1
A iShouldSee() 0 8 2
A iRunPhpspecExemplifyToAddTheMethodAsATo() 0 8 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 PhpSpec\Console\Application;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
/**
11
 * Defines application features from the specific context.
12
 */
13
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...
14
{
15
    /**
16
     * @var string|null
17
     */
18
    private $workDir = null;
19
20
    /**
21
     * @var ApplicationTester|null
22
     */
23
    private $applicationTester = null;
24
25
    /**
26
     * @BeforeScenario
27
     */
28
    public function createWorkDir()
29
    {
30
        $this->workDir = sprintf(
31
            '%s/%s/',
32
            sys_get_temp_dir(),
33
            uniqid('PhpSpecContext_')
34
        );
35
        $fs = new Filesystem();
36
        $fs->mkdir($this->workDir, 0777);
37
        chdir($this->workDir);
38
    }
39
40
    /**
41
     * @AfterScenario
42
     */
43
    public function removeWorkDir()
44
    {
45
        $fs = new Filesystem();
46
        $fs->remove($this->workDir);
47
    }
48
49
    /**
50
     * @Then the spec file :file should contain:
51
     */
52
    public function theSpecFileShouldContain($file, PyStringNode $string)
53
    {
54
        $actual = file_get_contents($file);
55
        $expected = $string->getRaw();
56
57
        if($actual != $expected) {
58
            throw new \RuntimeException("The generated spec file should have been:\n\n$expected\n\nbut was:\n\n$actual\n\n");
59
        }
60
    }
61
62
    /**
63
     * @Given the spec file :file contains:
64
     */
65
    public function theSpecFileContains($file, PyStringNode $string)
66
    {
67
        $this->saveFile($file, $string);
68
        require_once($file);
69
    }
70
71
    /**
72
     * @param $file
73
     * @param PyStringNode $string
74
     * @return void
75
     */
76
    private function saveFile($file, PyStringNode $string)
77
    {
78
        $dirname = dirname($file);
79
        if (!file_exists($dirname)) {
80
            mkdir($dirname, 0777, true);
81
        }
82
83
        file_put_contents($file, $string->getRaw());
84
    }
85
86
    /**
87
     * @return ApplicationTester
88
     */
89
    private function createApplicationTester()
90
    {
91
        file_put_contents('phpspec.yml', "extensions:\n    RMiller\\BehatSpec\\Extension\\ExemplifyExtension\\ExemplifyExtension: ~");
92
        $application = new Application('2.1-dev');
93
        $application->setAutoExit(false);
94
95
        return new ApplicationTester($application);
96
    }
97
98
    /**
99
     * @When I run phpspec exemplify to add the :method method to :class
100
     */
101
    public function iRunPhpspecExemplifyToAddTheMethodTo($method, $class)
102
    {
103
        $this->applicationTester = $this->createApplicationTester();
104
        $this->applicationTester->run(sprintf('exemplify --no-interaction %s %s', $class, $method));
105
    }
106
107
    /**
108
     * @Then /^(?:|I )should see "(?P<message>[^"]*)"$/
109
     */
110
    public function iShouldSee($message)
111
    {
112
        $actual = trim($this->applicationTester->getDisplay());
113
114
        if($actual != $message) {
115
            throw new \RuntimeException("The message should have been:\n\n$message\n\nbut was:\n\n$actual\n\n");
116
        }
117
    }
118
119
    /**
120
     * @When I run phpspec exemplify to add the :method method as a :type to :class
121
     */
122
    public function iRunPhpspecExemplifyToAddTheMethodAsATo($method, $type, $class)
123
    {
124
        $formattedMethodTypes = ['instance method','named constructor', 'static method'];
125
126
        $this->applicationTester = $this->createApplicationTester();
127
        $this->applicationTester->putToInputStream(sprintf("%s\n", array_search($type, $formattedMethodTypes)));
128
        $this->applicationTester->run(sprintf('exemplify %s %s', $class, $method), ['interactive' => true, 'decorated' => false]);
129
    }
130
}
131