Completed
Pull Request — master (#6)
by ANTHONIUS
02:44
created

FileContext::iHaveDotfile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

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 6
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
     * @param BeforeScenarioScope $scope
32
     */
33
    public function gatherContexts(BeforeScenarioScope $scope)
34
    {
35
        $environment = $scope->getEnvironment();
36
        $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

36
        /** @scrutinizer ignore-call */ 
37
        $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...
37
    }
38
39
    /**
40
     * @Given I have dotfile :name
41
     * @Given I have dotfile :name with:
42
     * @param string $path Where to create file directory
43
     * @param string $contents A file contents
44
     */
45
    public function iHaveDotfile(string $path, PyStringNode $contents = null)
46
    {
47
        $target = getenv('HOME').DIRECTORY_SEPARATOR.$path;
48
        if(is_null($contents)){
49
            touch($target);
50
        }else{
51
            $contents = $contents->getStrings();
52
            file_put_contents($target,$contents, LOCK_EX);
53
        }
54
    }
55
56
    /**
57
     * @Given Dotfile :name should contain :contents
58
     * @param string    $name
59
     * @param string    $contents
60
     */
61
    public function dotfileShouldContain(string $name,string $needle)
62
    {
63
        $target = getenv('HOME').DIRECTORY_SEPARATOR.$name;
64
        if(!is_file($target)){
65
            throw new InvalidArgumentException('Can not find file: '.$name);
66
        }
67
        $contents = file_get_contents($target);
68
        Assert::contains($contents,$needle);
69
    }
70
71
    /**
72
     *
73
     * @Given I have backup defaults patch :path with:
74
     * @param string $path
75
     * @param PyStringNode $contents
76
     */
77
    public function iHaveBackupDefaultsPatch(string $path, PyStringNode $contents = null)
78
    {
79
        $target = '/home/backup/src/defaults/patch/'.$path;
80
        $this->generateFile($target,$contents);
81
    }
82
83
    private function generateFile($target, PyStringNode $contents = null)
84
    {
85
        Toolkit::ensureFileDir($target);
86
        if(is_null($contents)){
87
            touch($target);
88
        }else{
89
            $contents = $contents->getStrings();
90
            file_put_contents($target,$contents, LOCK_EX);
91
        }
92
    }
93
94
}
95