1
|
|
|
<?php |
2
|
|
|
namespace FwlibTest\Config; |
3
|
|
|
|
4
|
|
|
use Fwlib\Config\StringOptions; |
5
|
|
|
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
6
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @copyright Copyright 2015 Fwolf |
10
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
11
|
|
|
*/ |
12
|
|
|
class StringOptionsTest extends PHPUnitTestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string[] $methods |
16
|
|
|
* @return MockObject|StringOptions |
17
|
|
|
*/ |
18
|
|
|
protected function buildMock(array $methods = null) |
19
|
|
|
{ |
20
|
|
|
$mock = $this->getMock( |
21
|
|
|
StringOptions::class, |
22
|
|
|
$methods |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
return $mock; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function testExport() |
30
|
|
|
{ |
31
|
|
|
$options = $this->buildMock(); |
32
|
|
|
|
33
|
|
|
$options->set('foo1', 42) |
|
|
|
|
34
|
|
|
->set('foo2', true) |
35
|
|
|
->set('foo3', false); |
36
|
|
|
$this->assertEquals( |
37
|
|
|
'foo1: 42, foo2: true, foo3: false', |
38
|
|
|
$options->export(', ', ': ') |
|
|
|
|
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
public function testGet() |
44
|
|
|
{ |
45
|
|
|
$options = $this->buildMock(); |
46
|
|
|
|
47
|
|
|
$this->assertNotNull($options->get('notExist')); |
|
|
|
|
48
|
|
|
$this->assertTrue(false === $options->get('notExist')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function testImport() |
53
|
|
|
{ |
54
|
|
|
$options = $this->buildMock(); |
55
|
|
|
|
56
|
|
|
$options->import(',,foo1 = 4 2 , foo2=true , foo3 = false', ',', '='); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->assertEquals('4 2', $options->get('foo1')); |
|
|
|
|
59
|
|
|
$this->assertTrue($options->get('foo2')); |
60
|
|
|
$this->assertFalse($options->get('foo3')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @expectedException \Fwlib\Base\Exception\InvalidFormatException |
66
|
|
|
*/ |
67
|
|
|
public function testImportError() |
68
|
|
|
{ |
69
|
|
|
$options = $this->buildMock(); |
70
|
|
|
|
71
|
|
|
$options->import('foo == 42', ',', '='); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
public function testOtherSeparatorAndKvSplitter() |
76
|
|
|
{ |
77
|
|
|
/** @var MockObject|StringOptions $options */ |
78
|
|
|
$options = $this->getMock( |
79
|
|
|
StringOptions::class, |
80
|
|
|
null, |
81
|
|
|
['foo:: 42; bar', ';', '::'] |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$this->assertEquals(42, $options->get('foo')); |
|
|
|
|
85
|
|
|
$this->assertTrue($options->get('bar')); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: