1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\Services; |
4
|
|
|
|
5
|
|
|
use SMW\Services\DataValueServiceFactory; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \SMW\Services\DataValueServiceFactory |
9
|
|
|
* @group semantic-mediawiki |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.5 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class DataValueServiceFactoryTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
private $containerBuilder; |
19
|
|
|
|
20
|
|
|
protected function setUp() { |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$this->containerBuilder = $this->getMockBuilder( '\Onoi\CallbackContainer\ContainerBuilder' ) |
24
|
|
|
->disableOriginalConstructor() |
25
|
|
|
->getMock(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testCanConstruct() { |
29
|
|
|
|
30
|
|
|
$this->assertInstanceOf( |
31
|
|
|
DataValueServiceFactory::class, |
32
|
|
|
new DataValueServiceFactory( $this->containerBuilder ) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetServiceFile() { |
37
|
|
|
|
38
|
|
|
$this->assertInternalType( |
39
|
|
|
'string', |
40
|
|
|
DataValueServiceFactory::SERVICE_FILE |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testNewDataValueByType() { |
45
|
|
|
|
46
|
|
|
$dataValue = $this->getMockBuilder( '\SMWDataValue' ) |
|
|
|
|
47
|
|
|
->disableOriginalConstructor() |
48
|
|
|
->getMockForAbstractClass(); |
49
|
|
|
|
50
|
|
|
$this->containerBuilder->expects( $this->once() ) |
51
|
|
|
->method( 'isRegistered' ) |
52
|
|
|
->with( $this->stringContains( DataValueServiceFactory::TYPE_INSTANCE . 'foo' ) ) |
53
|
|
|
->will( $this->returnValue( true ) ); |
54
|
|
|
|
55
|
|
|
$instance = new DataValueServiceFactory( |
56
|
|
|
$this->containerBuilder |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$instance->newDataValueByType( 'foo', 'bar' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGetValueParser() { |
63
|
|
|
|
64
|
|
|
$dataValue = $this->getMockBuilder( '\SMWDataValue' ) |
65
|
|
|
->disableOriginalConstructor() |
66
|
|
|
->getMockForAbstractClass(); |
67
|
|
|
|
68
|
|
|
$this->containerBuilder->expects( $this->once() ) |
69
|
|
|
->method( 'singleton' ) |
70
|
|
|
->with( $this->stringContains( DataValueServiceFactory::TYPE_PARSER ) ); |
71
|
|
|
|
72
|
|
|
$instance = new DataValueServiceFactory( |
73
|
|
|
$this->containerBuilder |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$instance->getValueParser( $dataValue ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetValueFormatterOnRegisteredFormatters() { |
80
|
|
|
|
81
|
|
|
$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' ) |
82
|
|
|
->disableOriginalConstructor() |
83
|
|
|
->getMockForAbstractClass(); |
84
|
|
|
|
85
|
|
|
$dataValue = $this->getMockBuilder( '\SMWDataValue' ) |
86
|
|
|
->disableOriginalConstructor() |
87
|
|
|
->getMockForAbstractClass(); |
88
|
|
|
|
89
|
|
|
$this->containerBuilder->expects( $this->once() ) |
90
|
|
|
->method( 'isRegistered' ) |
91
|
|
|
->will( $this->returnValue( true ) ); |
92
|
|
|
|
93
|
|
|
$this->containerBuilder->expects( $this->once() ) |
94
|
|
|
->method( 'singleton' ) |
95
|
|
|
->with( $this->stringContains( DataValueServiceFactory::TYPE_FORMATTER ) ) |
96
|
|
|
->will( $this->returnValue( $dataValueFormatter ) ); |
97
|
|
|
|
98
|
|
|
$instance = new DataValueServiceFactory( |
99
|
|
|
$this->containerBuilder |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$instance->getValueFormatter( $dataValue ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetValueFormatterOnNonRegisteredFormatters() { |
106
|
|
|
|
107
|
|
|
$dataValueFormatter = $this->getMockBuilder( '\SMW\DataValues\ValueFormatters\DataValueFormatter' ) |
108
|
|
|
->disableOriginalConstructor() |
109
|
|
|
->getMockForAbstractClass(); |
110
|
|
|
|
111
|
|
|
$dataValue = $this->getMockBuilder( '\SMWDataValue' ) |
112
|
|
|
->disableOriginalConstructor() |
113
|
|
|
->getMockForAbstractClass(); |
114
|
|
|
|
115
|
|
|
$this->containerBuilder->expects( $this->once() ) |
116
|
|
|
->method( 'isRegistered' ) |
117
|
|
|
->will( $this->returnValue( false ) ); |
118
|
|
|
|
119
|
|
|
$this->containerBuilder->expects( $this->atLeastOnce() ) |
120
|
|
|
->method( 'singleton' ) |
121
|
|
|
->with( $this->stringContains( DataValueServiceFactory::TYPE_FORMATTER ) ) |
122
|
|
|
->will( $this->returnValue( $dataValueFormatter ) ); |
123
|
|
|
|
124
|
|
|
$instance = new DataValueServiceFactory( |
125
|
|
|
$this->containerBuilder |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$instance->getValueFormatter( $dataValue ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testImportExtraneousFunctions() { |
132
|
|
|
|
133
|
|
|
$this->containerBuilder->expects( $this->atLeastOnce() ) |
134
|
|
|
->method( 'registerCallback' ) |
135
|
|
|
->with( $this->stringContains( DataValueServiceFactory::TYPE_EXT_FUNCTION . 'Foo' ) ); |
136
|
|
|
|
137
|
|
|
$instance = new DataValueServiceFactory( |
138
|
|
|
$this->containerBuilder |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$instance->importExtraneousFunctions( array( |
142
|
|
|
'Foo' => function() { return 'Foo'; } |
143
|
|
|
) ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testNewExtraneousFunctionByName() { |
147
|
|
|
|
148
|
|
|
$this->containerBuilder->expects( $this->atLeastOnce() ) |
149
|
|
|
->method( 'create' ) |
150
|
|
|
->with( $this->stringContains( DataValueServiceFactory::TYPE_EXT_FUNCTION . 'Foo' ) ); |
151
|
|
|
|
152
|
|
|
$instance = new DataValueServiceFactory( |
153
|
|
|
$this->containerBuilder |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$instance->newExtraneousFunctionByName( 'Foo' ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.