FileHandlerTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 147
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testPreparePath() 0 3 1
A preparePathProvider() 0 16 1
A testArgumentValue() 0 3 1
A argumentValueProvider() 0 12 1
A testResolvePath() 0 3 1
B resolvePathProvider() 0 25 1
A testInitDirectory() 0 15 1
A testProcessEnvironmentalVariable() 0 3 1
A testProcessEnvironmentalVariableProvider() 0 21 1
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
	 * @var FileHandler
14
	 */
15
	private $fileHandler;
16
17
	protected function setUp() {
18
		parent::setUp();
19
		$fs = new Filesystem();
20
		$this->fileHandler = new FileHandler($fs, array('--env=prod'));
21
	}
22
23
	/**
24
	 * @dataProvider preparePathProvider
25
	 * @param $path
26
	 * @param $expected
27
	 */
28
	public function testPreparePath($path, $expected) {
29
		$this->assertEquals($expected, $this->fileHandler->preparePath($path));
30
	}
31
32
	public function preparePathProvider() {
33
		return array(
34
			'without env parameter' => array(
35
				'path' => 'folder/parameters.yml',
36
				'expected' => 'folder/parameters.yml'
37
			),
38
			'with env parameter' => array(
39
				'path' => '{env}/parameters.yml',
40
				'expected' => 'prod/parameters.yml'
41
			),
42
			'with different parameter' => array(
43
				'path' => '{enev}/parameters.yml',
44
				'expected' => '{enev}/parameters.yml'
45
			)
46
		);
47
	}
48
49
	/**
50
	 * @param $name
51
	 * @param $expected
52
	 *
53
	 * @dataProvider argumentValueProvider
54
	 */
55
	public function testArgumentValue($name, $expected) {
56
		$this->assertEquals($expected, $this->fileHandler->getArgumentValue($name));
57
	}
58
59
	public function argumentValueProvider() {
60
		return array(
61
			'env found' => array(
62
				'name' => 'env',
63
				'expected' => 'prod',
64
			),
65
			'--env not found' => array(
66
				'name' => '--env',
67
				'expected' => false,
68
			)
69
		);
70
	}
71
72
73
	/**
74
	 * @param $currentPath
75
	 * @param $importPath
76
	 * @param $expected
77
	 * @dataProvider resolvePathProvider
78
	 */
79
	public function testResolvePath($currentPath, $importPath, $expected) {
80
		$this->assertEquals($expected, $this->fileHandler->resolvePath($currentPath, $importPath));
81
	}
82
83
	public function resolvePathProvider() {
84
		return array(
85
			'parent path' => array(
86
				'currentPath' => '/home/user/dir/current.yml',
87
				'importPath' => '../import.yml',
88
				'expected' => '/home/user/dir/../import.yml',
89
			),
90
			'current path' => array(
91
				'currentPath' => '/home/user/dir/current.yml',
92
				'importPath' => './import.yml',
93
				'expected' => '/home/user/dir/./import.yml',
94
			),
95
96
			'current simple path' => array(
97
				'currentPath' => '/home/user/dir/current.yml',
98
				'importPath' => 'import.yml',
99
				'expected' => '/home/user/dir/import.yml',
100
			),
101
			'absolute path' => array(
102
				'currentPath' => '/home/user/dir/current.yml',
103
				'importPath' => '/import.yml',
104
				'expected' => '/import.yml',
105
			)
106
		);
107
	}
108
109
	public function testInitDirectory() {
110
		vfsStreamWrapper::register();
111
		vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
112
113
		$buildDirName = 'createthis';
114
		$buildDir = vfsStream::url('root/' . $buildDirName);
115
116
		//currently doesn't exists
117
		$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...
118
119
		//create one
120
		$this->fileHandler->initDirectory($buildDir);
121
		$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...
122
		vfsStreamWrapper::unregister();
123
	}
124
125
	/**
126
	 * @param $expected
127
	 * @param $value
128
	 *
129
	 * @dataProvider testProcessEnvironmentalVariableProvider
130
	 */
131
	public function testProcessEnvironmentalVariable($value, $expected) {
132
		$this->assertEquals($expected, $this->fileHandler->processEnvironmentalVariable($value));
133
	}
134
135
	public function testProcessEnvironmentalVariableProvider() {
136
		return array(
137
			'string' => array(
138
				'value' => 'string',
139
				'expected' => 'string'
140
			),
141
			'number' => array(
142
				'value' => 444,
143
				'expected' => 444
144
			),
145
			'env variable' => array(
146
				'value' => '%env(PATH)%',
147
				'expected' => getenv('PATH')
148
			),
149
150
			'string like env' => array(
151
				'value' => 'env(PATH)%',
152
				'expected' => 'env(PATH)%'
153
			)
154
		);
155
	}
156
}
157