1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SCI\Tests\FilteredMetadata; |
4
|
|
|
|
5
|
|
|
use SCI\FilteredMetadata\NcbiPubMedResponseParser; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \SCI\FilteredMetadata\NcbiPubMedResponseParser |
9
|
|
|
* @group semantic-cite |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class NcbiPubMedResponseParserTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$ncbiPubMedFilteredHttpResponseParser = $this->getMockBuilder( '\Onoi\Remi\Ncbi\NcbiPubMedFilteredHttpResponseParser' ) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMock(); |
23
|
|
|
|
24
|
|
|
$this->assertInstanceOf( |
25
|
|
|
'\Onoi\Remi\ResponseParser', |
26
|
|
|
new NcbiPubMedResponseParser( $ncbiPubMedFilteredHttpResponseParser ) |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testInterfaceMethods() { |
31
|
|
|
|
32
|
|
|
$ncbiPubMedFilteredHttpResponseParser = $this->getMockBuilder( '\Onoi\Remi\Ncbi\NcbiPubMedFilteredHttpResponseParser' ) |
33
|
|
|
->disableOriginalConstructor() |
34
|
|
|
->getMock(); |
35
|
|
|
|
36
|
|
|
$instance = new NcbiPubMedResponseParser( $ncbiPubMedFilteredHttpResponseParser ); |
37
|
|
|
|
38
|
|
|
$this->assertNull( |
39
|
|
|
$instance->usesCache() |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->assertNull( |
43
|
|
|
$instance->getMessages() |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->assertNull( |
47
|
|
|
$instance->getFilteredRecord() |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$this->assertNull( |
51
|
|
|
$instance->getRawResponse( 42 ) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider idProvider |
57
|
|
|
*/ |
58
|
|
|
public function testDoParseForId( $id, $type, $expects ) { |
59
|
|
|
|
60
|
|
|
$record = $this->getMockBuilder( '\SCI\FilteredMetadata\BibliographicFilteredRecord' ) |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock(); |
63
|
|
|
|
64
|
|
|
$record->expects( $this->at( 0 ) ) |
65
|
|
|
->method( 'get' ) |
66
|
|
|
->with( $this->stringContains( 'ncbi-dbtype' ) ) |
67
|
|
|
->will( $this->returnValue( $type ) ); |
68
|
|
|
|
69
|
|
|
$ncbiPubMedFilteredHttpResponseParser = $this->getMockBuilder( '\Onoi\Remi\Ncbi\NcbiPubMedFilteredHttpResponseParser' ) |
70
|
|
|
->disableOriginalConstructor() |
71
|
|
|
->getMock(); |
72
|
|
|
|
73
|
|
|
$ncbiPubMedFilteredHttpResponseParser->expects( $this->any() ) |
74
|
|
|
->method( 'getFilteredRecord' ) |
75
|
|
|
->will( $this->returnValue( $record ) ); |
76
|
|
|
|
77
|
|
|
$ncbiPubMedFilteredHttpResponseParser->expects( $expects ) |
78
|
|
|
->method( 'doFilterResponseFor' ); |
79
|
|
|
|
80
|
|
|
$instance = new NcbiPubMedResponseParser( $ncbiPubMedFilteredHttpResponseParser ); |
81
|
|
|
$instance->doFilterResponseFor( $id ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function idProvider() { |
85
|
|
|
|
86
|
|
|
$provider[] = [ |
|
|
|
|
87
|
|
|
'abc', |
88
|
|
|
'pmc', |
89
|
|
|
$this->never() |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$provider[] = [ |
93
|
|
|
'abc', |
94
|
|
|
'pubmed', |
95
|
|
|
$this->never() |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$provider[] = [ |
99
|
|
|
'PMID54846467', |
100
|
|
|
'pmc', |
101
|
|
|
$this->never() |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$provider[] = [ |
105
|
|
|
'PMC54846467', |
106
|
|
|
'pmc', |
107
|
|
|
$this->once() |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
$provider[] = [ |
111
|
|
|
'PMID54846467', |
112
|
|
|
'pmid', |
113
|
|
|
$this->once() |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$provider[] = [ |
117
|
|
|
'PMID54846467', |
118
|
|
|
'pubmed', |
119
|
|
|
$this->once() |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
return $provider; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
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.