Completed
Push — master ( e8179e...af064b )
by Pavel
02:52
created

testProcessEnvironmentalVariableProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
namespace Paro\EnvironmentParameters\Tests;
3
4
use org\bovigo\vfs\vfsStream;
5
use org\bovigo\vfs\vfsStreamDirectory;
6
use org\bovigo\vfs\vfsStreamWrapper;
7
use Paro\EnvironmentParameters\FileHandler;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class FileHandlerTest extends \PHPUnit_Framework_TestCase
11
{
12
13
    /**
14
     * @var FileHandler
15
     */
16
    private $fileHandler;
17
18
    protected function setUp()
19
    {
20
        parent::setUp();
21
        $fs = new Filesystem();
22
        $this->fileHandler = new FileHandler($fs, array('--env=prod'));
23
    }
24
25
    /**
26
     * @dataProvider preparePathProvider
27
     * @param $path
28
     * @param $expected
29
     */
30
    public function testPreparePath($path, $expected)
31
    {
32
        $this->assertEquals($expected, $this->fileHandler->preparePath($path));
33
    }
34
35
    public function preparePathProvider()
36
    {
37
        return array(
38
            'without env parameter' => array(
39
                'path' => 'folder/parameters.yml',
40
                'expected' => 'folder/parameters.yml'
41
            ),
42
            'with env parameter' => array(
43
                'path' => '{env}/parameters.yml',
44
                'expected' => 'prod/parameters.yml'
45
            ),
46
            'with different parameter' => array(
47
                'path' => '{enev}/parameters.yml',
48
                'expected' => '{enev}/parameters.yml'
49
            )
50
        );
51
    }
52
53
    /**
54
     * @param $name
55
     * @param $expected
56
     *
57
     * @dataProvider argumentValueProvider
58
     */
59
    public function testArgumentValue($name, $expected)
60
    {
61
        $this->assertEquals($expected, $this->fileHandler->getArgumentValue($name));
62
    }
63
64
    public function argumentValueProvider()
65
    {
66
        return array(
67
            'env found' => array(
68
                'name' => 'env',
69
                'expected' => 'prod',
70
            ),
71
            '--env not found' => array(
72
                'name' => '--env',
73
                'expected' => false,
74
            )
75
        );
76
    }
77
78
79
    /**
80
     * @param $currentPath
81
     * @param $importPath
82
     * @param $expected
83
     * @dataProvider resolvePathProvider
84
     */
85
    public function testResolvePath($currentPath, $importPath, $expected)
86
    {
87
        $this->assertEquals($expected, $this->fileHandler->resolvePath($currentPath, $importPath));
88
    }
89
90
    public function resolvePathProvider()
91
    {
92
        return array(
93
            'parent path' => array(
94
                'currentPath' => '/home/user/dir/current.yml',
95
                'importPath' => '../import.yml',
96
                'expected' => '/home/user/dir/../import.yml',
97
            ),
98
            'current path' => array(
99
                'currentPath' => '/home/user/dir/current.yml',
100
                'importPath' => './import.yml',
101
                'expected' => '/home/user/dir/./import.yml',
102
            ),
103
104
            'current simple path' => array(
105
                'currentPath' => '/home/user/dir/current.yml',
106
                'importPath' => 'import.yml',
107
                'expected' => '/home/user/dir/import.yml',
108
            ),
109
            'absolute path' => array(
110
                'currentPath' => '/home/user/dir/current.yml',
111
                'importPath' => '/import.yml',
112
                'expected' => '/import.yml',
113
            )
114
        );
115
    }
116
117
    public function testInitDirectory()
118
    {
119
        vfsStreamWrapper::register();
120
        vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
121
122
        $buildDirName = 'createthis';
123
        $buildDir = vfsStream::url('root/' . $buildDirName);
124
125
        //currently doesn't exists
126
        $this->assertFalse(vfsStreamWrapper::getRoot()->hasChild($buildDirName));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method hasChild() does only exist in the following implementations of said interface: org\bovigo\vfs\DotDirectory, org\bovigo\vfs\vfsStreamDirectory.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
127
128
        //create one
129
        $this->fileHandler->initDirectory($buildDir);
130
        $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($buildDirName));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method hasChild() does only exist in the following implementations of said interface: org\bovigo\vfs\DotDirectory, org\bovigo\vfs\vfsStreamDirectory.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
131
    }
132
133
    /**
134
     * @param $expected
135
     * @param $value
136
     *
137
     * @dataProvider testProcessEnvironmentalVariableProvider
138
     */
139
    public function testProcessEnvironmentalVariable($value, $expected)
140
    {
141
        $this->assertEquals($expected, $this->fileHandler->processEnvironmentalVariable($value));
142
    }
143
144
    public function testProcessEnvironmentalVariableProvider()
145
    {
146
        return array(
147
            'string' => array(
148
                'value' => 'string',
149
                'expected' => 'string'
150
            ),
151
            'number' => array(
152
                'value' => 444,
153
                'expected' => 444
154
            ),
155
            'env variable' => array(
156
                'value' => '%env(PATH)%',
157
                'expected' => getenv('PATH')
158
            ),
159
160
            'string like env' => array(
161
                'value' => 'env(PATH)%',
162
                'expected' => 'env(PATH)%'
163
            )
164
        );
165
    }
166
}
167