Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

tests/unit/ConfigTest.php (1 issue)

possible assignment of non-compatible types.

Bug Documentation Minor

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);
42
		$this->_configArray = $this->_config->get();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_config->get() can also be of type string or null. However, the property $_configArray is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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);
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);
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