Completed
Push — master ( 4e392b...2d72fd )
by Richard
10s
created

FeatureContext   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 3
dl 0
loc 110
rs 10
c 0
b 0
f 0

9 Methods

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