Completed
Push — master ( 16ddfb...0ea243 )
by Henry
09:18
created

tests/unit/ConfigTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Tests;
3
4
use org\bovigo\vfs\vfsStream as Stream;
5
use org\bovigo\vfs\vfsStreamFile as StreamFile;
6
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
7
8
/**
9
 * ConfigTest
10
 *
11
 * @since 2.2.0
12
 *
13
 * @package Redaxscript
14
 * @category Tests
15
 * @author Henry Ruhs
16
 *
17
 * @covers Redaxscript\Config
18
 */
19
20
class ConfigTest extends TestCaseAbstract
21
{
22
	/**
23
	 * array to restore config
24
	 *
25
	 * @var array
26
	 */
27
28
	protected $_configArray = [];
29
30
	/**
31
	 * setUp
32
	 *
33
	 * @since 3.0.0
34
	 */
35
36
	public function setUp() : void
37
	{
38
		parent::setUp();
39
		Stream::setup('root');
40
		$file = new StreamFile('config.php');
41
		StreamWrapper::getRoot()->addChild($file);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface org\bovigo\vfs\vfsStreamContent as the method addChild() 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...
42
		$this->_configArray = $this->_config->get();
43
	}
44
45
	/**
46
	 * tearDown
47
	 *
48
	 * @since 3.1.0
49
	 */
50
51
	public function tearDown() : void
52
	{
53
		$this->_config->set('dbType', $this->_configArray['dbType']);
54
		$this->_config->set('dbHost', $this->_configArray['dbHost']);
55
		$this->_config->set('dbPrefix', $this->_configArray['dbPrefix']);
56
		$this->_config->set('dbName', $this->_configArray['dbName']);
57
		$this->_config->set('dbUser', $this->_configArray['dbUser']);
58
		$this->_config->set('dbPassword', $this->_configArray['dbPassword']);
59
	}
60
61
	/**
62
	 * testInit
63
	 *
64
	 * @since 2.4.0
65
	 */
66
67
	public function testInit() : void
68
	{
69
		/* setup */
70
71
		$this->_config->init('config.php');
72
73
		/* actual */
74
75
		$actualArray = $this->_config->get();
76
77
		/* compare */
78
79
		$this->assertArrayHasKey('dbType', $actualArray);
0 ignored issues
show
It seems like $actualArray defined by $this->_config->get() on line 75 can also be of type null or string; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
80
	}
81
82
	/**
83
	 * testGetAndSet
84
	 *
85
	 * @since 2.2.0
86
	 */
87
88
	public function testGetAndSet() : void
89
	{
90
		/* setup */
91
92
		$this->_config->set('dbHost', 'localhost');
93
94
		/* actual */
95
96
		$actual = $this->_config->get('dbHost');
97
98
		/* compare */
99
100
		$this->assertEquals('localhost', $actual);
101
	}
102
103
	/**
104
	 * testGetAll
105
	 *
106
	 * @since 2.2.0
107
	 */
108
109
	public function testGetAll() : void
110
	{
111
		/* actual */
112
113
		$actualArray = $this->_config->get();
114
115
		/* compare */
116
117
		$this->assertArrayHasKey('dbHost', $actualArray);
0 ignored issues
show
It seems like $actualArray defined by $this->_config->get() on line 113 can also be of type null or string; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
118
	}
119
120
	/**
121
	 * testParse
122
	 *
123
	 * @since 3.0.0
124
	 *
125
	 * @param string $dbUrl
126
	 * @param array $configArray
127
	 *
128
	 * @dataProvider providerAutoloader
129
	 */
130
131
	public function testParse(string $dbUrl = null, array $configArray = []) : void
132
	{
133
		/* setup */
134
135
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
136
		$this->_config->parse($dbUrl);
137
138
		/* actual */
139
140
		$actual = $this->_config->get();
141
142
		/* compare */
143
144
		$this->assertEquals($configArray, $actual);
145
	}
146
147
	/**
148
	 * testWrite
149
	 *
150
	 * @since 2.4.0
151
	 */
152
153
	public function testWrite() : void
154
	{
155
		/* setup */
156
157
		$this->_config->init(Stream::url('root' . DIRECTORY_SEPARATOR . 'config.php'));
158
159
		/* actual */
160
161
		$actual = $this->_config->write();
162
163
		/* compare */
164
165
		$this->assertNotFalse($actual);
166
	}
167
}
168