FileContext::dotfileShouldNotContain()   A
last analyzed

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 CommandContext
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
     * @Given Dotfile :name should not contain :contents
48
     *
49
     * @param string $name
50
     * @param string $contents
51
     */
52
    public function dotfileShouldNotContain(string $name, string $needle): void
53
    {
54
        $target = getenv('HOME').DIRECTORY_SEPARATOR.$name;
55
        if (!is_file($target)) {
56
            throw new InvalidArgumentException('Can not find file: '.$name);
57
        }
58
        $contents = file_get_contents($target);
59
        Assert::notContains($contents, $needle);
60
    }
61
62
    /**
63
     * @param BeforeScenarioScope $scope
64
     */
65
    public function gatherContexts(BeforeScenarioScope $scope): void
66
    {
67
        $environment = $scope->getEnvironment();
68
        $this->mainContext = $environment->get(CommandContext::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

68
        /** @scrutinizer ignore-call */ 
69
        $this->mainContext = $environment->get(CommandContext::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...
69
    }
70
71
    /**
72
     * @Given I have backup :section patch :path with:
73
     *
74
     * @param string       $path
75
     * @param PyStringNode $contents
76
     */
77
    public function iHaveBackupDefaultsPatch(string $section, string $path, PyStringNode $contents = null): void
78
    {
79
        $target = '/home/backup/src/'.$section.'/patch/'.$path;
80
        $this->generateFile($target, $contents);
81
    }
82
83
    /**
84
     * @Given I have dotfile :name
85
     * @Given I have dotfile :name with:
86
     *
87
     * @param string $path     Where to create file directory
88
     * @param string $contents A file contents
89
     */
90
    public function iHaveDotfile(string $path, PyStringNode $contents = null): void
91
    {
92
        $target = getenv('HOME').DIRECTORY_SEPARATOR.$path;
93
        if (null === $contents) {
94
            touch($target);
95
        } else {
96
            $contents = $contents->getStrings();
97
            file_put_contents($target, $contents, LOCK_EX);
98
        }
99
    }
100
101
    private function generateFile($target, PyStringNode $contents = null): void
102
    {
103
        Toolkit::ensureFileDir($target);
104
        if (null === $contents) {
105
            touch($target);
106
        } else {
107
            $contents = $contents->getStrings();
108
            file_put_contents($target, $contents, LOCK_EX);
109
        }
110
    }
111
}
112