FeatureContext   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 12.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 128
rs 10
wmc 16

12 Methods

Rating   Name   Duplication   Size   Complexity  
A itShouldFail() 0 8 2
A removeWorkDir() 0 3 1
A aFileNamedWith() 0 3 1
A itShouldFailWith() 6 6 2
A itShouldPass() 0 10 2
A bootstrap() 0 3 1
A givenBehatConfiguration() 0 3 1
A iRunBehat() 0 3 1
A getFullOutput() 0 3 1
A givenBehatProject() 0 3 1
A itShouldPassWith() 6 6 2
A aContextFileNamedWith() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Noiselabs ZfTestCase Behat Extension.
4
 *
5
 * (c) Vítor Brandão <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
use Behat\Behat\Context\Context;
12
use Behat\Gherkin\Node\PyStringNode;
13
use PHPUnit\Framework\Assert;
14
15
/**
16
 * Class FeatureContext.
17
 *
18
 * Adapted from https://github.com/jakzal/BehatRestExtension/blob/master/features/bootstrap/BehatRunnerContext.php
19
 */
20
class FeatureContext implements Context
21
{
22
    /**
23
     * @var BehatRunner
24
     */
25
    private $behatRunner;
26
27
    /**
28
     * @BeforeScenario
29
     */
30
    public function bootstrap()
31
    {
32
        $this->behatRunner = new BehatRunner(sprintf('%s/%s/', sys_get_temp_dir(), uniqid('ZfTestCaseExtension')));
33
    }
34
35
    /**
36
     * @AfterScenario
37
     */
38
    public function removeWorkDir()
39
    {
40
        $this->behatRunner->removeWorkspace();
41
    }
42
43
    /**
44
     * Creates a file with specified name and context in current workdir.
45
     *
46
     * @Given /^(?:there is )?a file named "([^"]*)" with:$/
47
     *
48
     * @param   string $filename name of the file (relative path)
49
     * @param   PyStringNode $content PyString string instance
50
     */
51
    public function aFileNamedWith($filename, PyStringNode $content)
52
    {
53
        $this->behatRunner->addFile($filename, $content->getRaw());
54
    }
55
56
    /**
57
     * @Given /^a behat configuration:$/
58
     */
59
    public function givenBehatConfiguration(PyStringNode $content)
60
    {
61
        $this->behatRunner->addFile('behat.yml', $content->getRaw());
62
    }
63
64
    /**
65
     * @Given /^(?:|an?|the )(?:|context |class |feature |.*)file "(?P<fileName>[^"]*)" contains:$/
66
     */
67
    public function aContextFileNamedWith($fileName, PyStringNode $content)
68
    {
69
        $this->behatRunner->addFile($fileName, $content->getRaw());
70
    }
71
72
    /**
73
     * @When /^I run behat$/
74
     */
75
    public function iRunBehat()
76
    {
77
        $this->behatRunner->run();
78
    }
79
80
    /**
81
     * @Then /^it should pass$/
82
     */
83
    public function itShouldPass()
84
    {
85
        try {
86
            Assert::assertSame(0, $this->behatRunner->getExitCode(), 'Command terminated with an error');
87
            Assert::assertStringNotMatchesFormat('PHP Warning:', $this->behatRunner->getFullOutput());
88
            Assert::assertStringNotMatchesFormat('PHP Notice:', $this->behatRunner->getFullOutput());
89
        } catch (\Exception $e) {
90
            echo $this->behatRunner->getFullOutput();
91
92
            throw $e;
93
        }
94
    }
95
96
    /**
97
     * @Then /^it should fail$/
98
     */
99
    public function itShouldFail()
100
    {
101
        try {
102
            Assert::assertNotSame(0, $this->behatRunner->getExitCode(), 'Command succeeded');
103
        } catch (\Exception $e) {
104
            echo $this->behatRunner->getFullOutput();
105
106
            throw $e;
107
        }
108
    }
109
110
    /**
111
     * @Then /^it should pass with:$/
112
     */
113 View Code Duplication
    public function itShouldPassWith(PyStringNode $expectedOutput)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
114
    {
115
        $this->itShouldPass();
116
117
        foreach ($expectedOutput->getStrings() as $expectedLine) {
118
            Assert::assertRegExp('/' . preg_quote($expectedLine, '/') . '/sm', $this->behatRunner->getFullOutput());
119
        }
120
    }
121
122
    /**
123
     * @Then /^it should fail with:$/
124
     */
125 View Code Duplication
    public function itShouldFailWith(PyStringNode $expectedOutput)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
126
    {
127
        $this->itShouldFail();
128
129
        foreach ($expectedOutput->getStrings() as $expectedLine) {
130
            Assert::assertRegExp('/' . preg_quote($expectedLine, '/') . '/sm', $this->behatRunner->getFullOutput());
131
        }
132
    }
133
134
    /**
135
     * @param string $path
136
     */
137
    public function givenBehatProject($path)
138
    {
139
        $this->behatRunner->addDirectory($path);
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getFullOutput()
146
    {
147
        return $this->behatRunner->getFullOutput();
148
    }
149
}