Passed
Pull Request — master (#6)
by ANTHONIUS
02:39
created

FileContext::dotfileShouldContain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Behat\Context;
15
16
use Behat\Behat\Context\Context;
17
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
18
use Behat\Gherkin\Node\PyStringNode;
19
use Dotfiles\Core\Exceptions\InvalidArgumentException;
20
use Dotfiles\Core\Util\Toolkit;
21
use Webmozart\Assert\Assert;
22
23
class FileContext implements Context
24
{
25
    /**
26
     * @var MainContext
27
     */
28
    private $mainContext;
29
30
    /**
31
     * @Given Dotfile :name should contain :contents
32
     *
33
     * @param string $name
34
     * @param string $contents
35
     */
36
    public function dotfileShouldContain(string $name, string $needle): void
37
    {
38
        $target = getenv('HOME').DIRECTORY_SEPARATOR.$name;
39
        if (!is_file($target)) {
40
            throw new InvalidArgumentException('Can not find file: '.$name);
41
        }
42
        $contents = file_get_contents($target);
43
        Assert::contains($contents, $needle);
44
    }
45
46
    /**
47
     * @param BeforeScenarioScope $scope
48
     */
49
    public function gatherContexts(BeforeScenarioScope $scope): void
50
    {
51
        $environment = $scope->getEnvironment();
52
        $this->mainContext = $environment->get(MainContext::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on Behat\Testwork\Environment\Environment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        /** @scrutinizer ignore-call */ 
53
        $this->mainContext = $environment->get(MainContext::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
    }
54
55
    /**
56
     * @Given I have backup defaults patch :path with:
57
     *
58
     * @param string       $path
59
     * @param PyStringNode $contents
60
     */
61
    public function iHaveBackupDefaultsPatch(string $path, PyStringNode $contents = null): void
62
    {
63
        $target = '/home/backup/src/defaults/patch/'.$path;
64
        $this->generateFile($target, $contents);
65
    }
66
67
    /**
68
     * @Given I have dotfile :name
69
     * @Given I have dotfile :name with:
70
     *
71
     * @param string $path     Where to create file directory
72
     * @param string $contents A file contents
73
     */
74
    public function iHaveDotfile(string $path, PyStringNode $contents = null): void
75
    {
76
        $target = getenv('HOME').DIRECTORY_SEPARATOR.$path;
77
        if (null === $contents) {
78
            touch($target);
79
        } else {
80
            $contents = $contents->getStrings();
81
            file_put_contents($target, $contents, LOCK_EX);
82
        }
83
    }
84
85
    private function generateFile($target, PyStringNode $contents = null): void
86
    {
87
        Toolkit::ensureFileDir($target);
88
        if (null === $contents) {
89
            touch($target);
90
        } else {
91
            $contents = $contents->getStrings();
92
            file_put_contents($target, $contents, LOCK_EX);
93
        }
94
    }
95
}
96