Passed
Push — master ( c32af1...923c61 )
by ANTHONIUS
04:29
created

FilesystemContext::thereIsNoFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\PhpSpec\CodeCoverage\Context;
15
16
use Behat\Behat\Context\Context;
17
use Behat\Gherkin\Node\PyStringNode;
18
use PHPUnit\Framework\Assert;
19
use Symfony\Component\Filesystem\Exception\IOException;
20
use Symfony\Component\Filesystem\Filesystem;
21
22
/**
23
 * Defines application features from the specific context.
24
 */
25
class FilesystemContext implements Context
26
{
27
    /**
28
     * @var string
29
     */
30
    private $workingDirectory;
31
32
    /**
33
     * @var Filesystem
34
     */
35
    private $filesystem;
36
37
    public function __construct()
38
    {
39
        $this->filesystem = new Filesystem();
40
    }
41
42
    /**
43
     * @beforeScenario
44
     */
45
    public function prepWorkingDirectory()
46
    {
47
        $this->workingDirectory = tempnam(sys_get_temp_dir(), 'phpspec-behat');
48
        $this->filesystem->remove($this->workingDirectory);
49
        $this->filesystem->mkdir($this->workingDirectory);
50
        chdir($this->workingDirectory);
51
52
        $fakeHomeDirectory = sprintf('%s/fake-home/', $this->workingDirectory);
53
        $this->filesystem->mkdir($fakeHomeDirectory.'.phpspec');
54
55
        if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) {
56
            $_SERVER['HOMEPATH'] = substr($fakeHomeDirectory, 2);
57
        } else {
58
            putenv(sprintf('HOME=%s', $fakeHomeDirectory));
59
        }
60
61
        $this->filesystem->mkdir($this->workingDirectory.'/vendor');
62
        $this->filesystem->copy(
63
            __DIR__.'/../Resources/autoloader/autoload.php',
64
            $this->workingDirectory.'/vendor/autoload.php'
65
        );
66
    }
67
68
    /**
69
     * @afterScenario
70
     */
71
    public function removeWorkingDirectory()
72
    {
73
        try {
74
            $this->filesystem->remove($this->workingDirectory);
75
        } catch (IOException $e) {
76
            //ignoring exception
77
        }
78
    }
79
80
    /**
81
     * @Given I have a custom :template template that contains:
82
     *
83
     * @param mixed $template
84
     */
85
    public function iHaveACustomTemplateThatContains($template, PyStringNode $contents)
86
    {
87
        $this->filesystem->dumpFile(sprintf('fake-home/.phpspec/%s.tpl', $template), $contents);
88
    }
89
90
    /**
91
     * @Given the bootstrap file :file contains:
92
     *
93
     * @param mixed $file
94
     */
95
    public function theFileContains($file, PyStringNode $contents)
96
    {
97
        $this->filesystem->dumpFile($file, (string) $contents);
98
    }
99
100
    /**
101
     * @Given the class file :file contains:
102
     * @Given the trait file :file contains:
103
     *
104
     * @param mixed $file
105
     */
106
    public function theClassOrTraitFileContains($file, PyStringNode $contents)
107
    {
108
        $this->theFileContains($file, $contents);
109
        require_once $file;
110
    }
111
112
    /**
113
     * @Given the spec file :file contains:
114
     *
115
     * @param mixed $file
116
     */
117
    public function theSpecFileContains($file, PyStringNode $contents)
118
    {
119
        $this->theFileContains($file, $contents);
120
    }
121
122
    /**
123
     * @Given the config file contains:
124
     */
125
    public function theConfigFileContains(PyStringNode $contents)
126
    {
127
        $this->theFileContains('phpspec.yml', $contents);
128
    }
129
130
    /**
131
     * @Given there is no file :file
132
     *
133
     * @param mixed $file
134
     */
135
    public function thereIsNoFile($file)
136
    {
137
        if (file_exists($file)) {
138
            throw new \Exception(sprintf(
139
                "File unexpectedly exists at path '%s'",
140
                $file
141
            ));
142
        }
143
    }
144
145
    /**
146
     * @Then the class in :file should contain:
147
     * @Then a new class/spec should be generated in the :file:
148
     *
149
     * @param mixed $file
150
     */
151
    public function theFileShouldContain($file, PyStringNode $contents)
152
    {
153
        if (!file_exists($file)) {
154
            throw new \Exception(sprintf(
155
                "File did not exist at path '%s'",
156
                $file
157
            ));
158
        }
159
160
        $expectedContents = (string) $contents;
161
        if ($expectedContents !== file_get_contents($file)) {
162
            throw new \Exception(sprintf(
163
                "File at '%s' did not contain expected contents.\nExpected: '%s'\nActual: '%s'",
164
                $file,
165
                $expectedContents,
166
                file_get_contents($file)
167
            ));
168
        }
169
    }
170
171
    /**
172
     * @Given the config file located in :folder contains:
173
     *
174
     * @param mixed $folder
175
     */
176
    public function theConfigFileInFolderContains($folder, PyStringNode $contents)
177
    {
178
        $this->theFileContains($folder.\DIRECTORY_SEPARATOR.'phpspec.yml', $contents);
179
    }
180
181
    /**
182
     * @Given I have not configured an autoloader
183
     */
184
    public function iHaveNotConfiguredAnAutoloader()
185
    {
186
        $this->filesystem->remove($this->workingDirectory.'/vendor/autoload.php');
187
    }
188
189
    /**
190
     * @Given there should be no file :path
191
     *
192
     * @param mixed $path
193
     */
194
    public function thereShouldBeNoFile($path)
195
    {
196
        Assert::assertFileNotExists($path);
197
    }
198
}
199