1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SCI\Tests\Bibtex; |
4
|
|
|
|
5
|
|
|
use SCI\Bibtex\BibtexProcessor; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \SCI\Bibtex\BibtexProcessor |
9
|
|
|
* @group semantic-cite |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class BibtexProcessorTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$bibtexParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexParser' ) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMock(); |
23
|
|
|
|
24
|
|
|
$bibtexAuthorListParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexAuthorListParser' ) |
25
|
|
|
->disableOriginalConstructor() |
26
|
|
|
->getMock(); |
27
|
|
|
|
28
|
|
|
$this->assertInstanceOf( |
29
|
|
|
'\SCI\Bibtex\BibtexProcessor', |
30
|
|
|
new BibtexProcessor( $bibtexParser, $bibtexAuthorListParser ) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testDoProcess() { |
35
|
|
|
|
36
|
|
|
$bibtexParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexParser' ) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
|
40
|
|
|
$bibtexParser->expects( $this->once() ) |
41
|
|
|
->method( 'parse' ) |
42
|
|
|
->will( $this->returnValue( [ 'Foo' => 'Bar' ] ) ); |
43
|
|
|
|
44
|
|
|
$bibtexAuthorListParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexAuthorListParser' ) |
45
|
|
|
->disableOriginalConstructor() |
46
|
|
|
->getMock(); |
47
|
|
|
|
48
|
|
|
$parserParameterProcessor = $this->getMockBuilder( '\SMW\ParserParameterProcessor' ) |
49
|
|
|
->disableOriginalConstructor() |
50
|
|
|
->getMock(); |
51
|
|
|
|
52
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
53
|
|
|
->method( 'getParameterValuesFor' ) |
54
|
|
|
->with( $this->equalTo( 'bibtex' ) ) |
55
|
|
|
->will( $this->returnValue( [] ) ); |
56
|
|
|
|
57
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
58
|
|
|
->method( 'addParameter' ) |
59
|
|
|
->with( |
60
|
|
|
$this->equalTo( 'Foo' ), |
61
|
|
|
$this->equalTo( 'Bar' ) ); |
62
|
|
|
|
63
|
|
|
$instance = new BibtexProcessor( $bibtexParser, $bibtexAuthorListParser ); |
64
|
|
|
|
65
|
|
|
$instance->doProcess( |
66
|
|
|
$parserParameterProcessor |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testProcessAuthors() { |
71
|
|
|
|
72
|
|
|
$bibtexParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexParser' ) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->getMock(); |
75
|
|
|
|
76
|
|
|
$bibtexParser->expects( $this->once() ) |
77
|
|
|
->method( 'parse' ) |
78
|
|
|
->will( $this->returnValue( [ 'author' => 'Foo' ] ) ); |
79
|
|
|
|
80
|
|
|
$bibtexAuthorListParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexAuthorListParser' ) |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->getMock(); |
83
|
|
|
|
84
|
|
|
$bibtexAuthorListParser->expects( $this->once() ) |
85
|
|
|
->method( 'parse' ) |
86
|
|
|
->will( $this->returnValue( [ 'Foo' ] ) ); |
87
|
|
|
|
88
|
|
|
$parserParameterProcessor = $this->getMockBuilder( '\SMW\ParserParameterProcessor' ) |
89
|
|
|
->disableOriginalConstructor() |
90
|
|
|
->getMock(); |
91
|
|
|
|
92
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
93
|
|
|
->method( 'getParameterValuesFor' ) |
94
|
|
|
->with( $this->equalTo( 'bibtex' ) ) |
95
|
|
|
->will( $this->returnValue( [ ] ) ); |
96
|
|
|
|
97
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
98
|
|
|
->method( 'setParameter' ) |
99
|
|
|
->with( |
100
|
|
|
$this->equalTo( 'author' ), |
101
|
|
|
$this->equalTo( [ 'Foo' ] ) ); |
102
|
|
|
|
103
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
104
|
|
|
->method( 'addParameter' ) |
105
|
|
|
->with( |
106
|
|
|
$this->equalTo( 'bibtex-author' ), |
107
|
|
|
$this->equalTo( 'Foo' ) ); |
108
|
|
|
|
109
|
|
|
$instance = new BibtexProcessor( $bibtexParser, $bibtexAuthorListParser ); |
110
|
|
|
|
111
|
|
|
$instance->doProcess( |
112
|
|
|
$parserParameterProcessor |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @dataProvider doNotProcessParameterProvider |
118
|
|
|
*/ |
119
|
|
|
public function testDoNotProcess( $parameter ) { |
120
|
|
|
|
121
|
|
|
$bibtexParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexParser' ) |
122
|
|
|
->disableOriginalConstructor() |
123
|
|
|
->getMock(); |
124
|
|
|
|
125
|
|
|
$bibtexParser->expects( $this->once() ) |
126
|
|
|
->method( 'parse' ) |
127
|
|
|
->will( $this->returnValue( [ $parameter => 'Foo' ] ) ); |
128
|
|
|
|
129
|
|
|
$bibtexAuthorListParser = $this->getMockBuilder( '\SCI\Bibtex\BibtexAuthorListParser' ) |
130
|
|
|
->disableOriginalConstructor() |
131
|
|
|
->getMock(); |
132
|
|
|
|
133
|
|
|
$parserParameterProcessor = $this->getMockBuilder( '\SMW\ParserParameterProcessor' ) |
134
|
|
|
->disableOriginalConstructor() |
135
|
|
|
->getMock(); |
136
|
|
|
|
137
|
|
|
$parserParameterProcessor->expects( $this->any() ) |
138
|
|
|
->method( 'getFirstParameter' ) |
139
|
|
|
->will( $this->returnValue( '' ) ); |
140
|
|
|
|
141
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
142
|
|
|
->method( 'hasParameter' ) |
143
|
|
|
->with( $this->equalTo( $parameter ) ) |
144
|
|
|
->will( $this->returnValue( true ) ); |
145
|
|
|
|
146
|
|
|
$parserParameterProcessor->expects( $this->once() ) |
147
|
|
|
->method( 'getParameterValuesFor' ) |
148
|
|
|
->with( $this->equalTo( 'bibtex' ) ) |
149
|
|
|
->will( $this->returnValue( [ ] ) ); |
150
|
|
|
|
151
|
|
|
$parserParameterProcessor->expects( $this->never() ) |
152
|
|
|
->method( 'addParameter' ); |
153
|
|
|
|
154
|
|
|
$instance = new BibtexProcessor( $bibtexParser, $bibtexAuthorListParser ); |
155
|
|
|
|
156
|
|
|
$instance->doProcess( |
157
|
|
|
$parserParameterProcessor |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function doNotProcessParameterProvider() { |
162
|
|
|
|
163
|
|
|
$provider[] = [ |
|
|
|
|
164
|
|
|
'reference' |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
$provider[] = [ |
168
|
|
|
'type' |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
return $provider; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.