1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\MongoDB; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\Item; |
6
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers Wikibase\EntityStore\MongoDB\MongoDBEntityDatabase |
10
|
|
|
* |
11
|
|
|
* @licence GPLv2+ |
12
|
|
|
* @author Thomas Pellissier Tanon |
13
|
|
|
*/ |
14
|
|
|
class MongoDBEntityDatabaseTest extends \PHPUnit_Framework_TestCase { |
15
|
|
|
|
16
|
|
|
public function testGetEntityDocumentForId() { |
17
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
18
|
|
|
|
19
|
|
|
$collectionMock = $this->getMockBuilder( 'Doctrine\MongoDB\Collection' ) |
20
|
|
|
->disableOriginalConstructor() |
21
|
|
|
->getMock(); |
22
|
|
|
$collectionMock->expects( $this->once() ) |
23
|
|
|
->method( 'findOne' ) |
24
|
|
|
->with( $this->equalTo( [ '_id' => 'Q1' ] ) ) |
25
|
|
|
->willReturn( [ 'id' => 'Q1' ] ); |
26
|
|
|
|
27
|
|
|
$databaseMock = $this->getMockBuilder( 'Doctrine\MongoDB\Database' ) |
28
|
|
|
->disableOriginalConstructor() |
29
|
|
|
->getMock(); |
30
|
|
|
$databaseMock->expects( $this->once() ) |
31
|
|
|
->method( 'selectCollection' ) |
32
|
|
|
->with( $this->equalTo( 'item' ) ) |
33
|
|
|
->willReturn( $collectionMock ); |
34
|
|
|
|
35
|
|
|
$documentBuilderMock = $this->getMockBuilder( 'Wikibase\EntityStore\MongoDB\MongoDBDocumentBuilder' ) |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
$documentBuilderMock->expects( $this->once() ) |
39
|
|
|
->method( 'buildEntityForDocument' ) |
40
|
|
|
->with( $this->equalTo( [ 'id' => 'Q1' ] ) ) |
41
|
|
|
->willReturn( $item ); |
42
|
|
|
|
43
|
|
|
$entityStore = new MongoDBEntityDatabase( $databaseMock, $documentBuilderMock ); |
44
|
|
|
|
45
|
|
|
$this->assertEquals( |
46
|
|
|
$item, |
47
|
|
|
$entityStore->getEntityDocumentForId( new ItemId( 'Q1' ) ) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testGetEntityDocumentForIdWithoutDocument() { |
52
|
|
|
$collectionMock = $this->getMockBuilder( 'Doctrine\MongoDB\Collection' ) |
53
|
|
|
->disableOriginalConstructor() |
54
|
|
|
->getMock(); |
55
|
|
|
$collectionMock->expects( $this->once() ) |
56
|
|
|
->method( 'findOne' ) |
57
|
|
|
->with( $this->equalTo( [ '_id' => 'Q1' ] ) ) |
58
|
|
|
->willReturn( null ); |
59
|
|
|
|
60
|
|
|
$databaseMock = $this->getMockBuilder( 'Doctrine\MongoDB\Database' ) |
61
|
|
|
->disableOriginalConstructor() |
62
|
|
|
->getMock(); |
63
|
|
|
$databaseMock->expects( $this->once() ) |
64
|
|
|
->method( 'selectCollection' ) |
65
|
|
|
->with( $this->equalTo( 'item' ) ) |
66
|
|
|
->willReturn( $collectionMock ); |
67
|
|
|
|
68
|
|
|
$documentBuilderMock = $this->getMockBuilder( 'Wikibase\EntityStore\MongoDB\MongoDBDocumentBuilder' ) |
69
|
|
|
->disableOriginalConstructor() |
70
|
|
|
->getMock(); |
71
|
|
|
|
72
|
|
|
$entityStore = new MongoDBEntityDatabase( $databaseMock, $documentBuilderMock ); |
73
|
|
|
|
74
|
|
|
$this->assertNull( $entityStore->getEntityDocumentForId( new ItemId( 'Q1' ) ) ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGetEntityDocumentsForIds() { |
78
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
79
|
|
|
|
80
|
|
|
$collectionMock = $this->getMockBuilder( 'Doctrine\MongoDB\Collection' ) |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->getMock(); |
83
|
|
|
$collectionMock->expects( $this->once() ) |
84
|
|
|
->method( 'find' ) |
85
|
|
|
->with( $this->equalTo( [ '_id' => [ '$in' => [ 'Q1', 'Q2' ] ] ] ) ) |
86
|
|
|
->willReturn( [ |
87
|
|
|
[ 'id' => 'Q1' ] |
88
|
|
|
] ); |
89
|
|
|
|
90
|
|
|
$databaseMock = $this->getMockBuilder( 'Doctrine\MongoDB\Database' ) |
91
|
|
|
->disableOriginalConstructor() |
92
|
|
|
->getMock(); |
93
|
|
|
$databaseMock->expects( $this->once() ) |
94
|
|
|
->method( 'selectCollection' ) |
95
|
|
|
->with( $this->equalTo( 'item' ) ) |
96
|
|
|
->willReturn( $collectionMock ); |
97
|
|
|
|
98
|
|
|
$documentBuilderMock = $this->getMockBuilder( 'Wikibase\EntityStore\MongoDB\MongoDBDocumentBuilder' ) |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->getMock(); |
101
|
|
|
$documentBuilderMock->expects( $this->once() ) |
102
|
|
|
->method( 'buildEntityForDocument' ) |
103
|
|
|
->with( $this->equalTo( [ 'id' => 'Q1' ] ) ) |
104
|
|
|
->willReturn( $item ); |
105
|
|
|
|
106
|
|
|
$entityStore = new MongoDBEntityDatabase( $databaseMock, $documentBuilderMock ); |
107
|
|
|
|
108
|
|
|
$this->assertEquals( |
109
|
|
|
[ $item ], |
110
|
|
|
$entityStore->getEntityDocumentsForIds( [ new ItemId( 'Q1' ), new ItemId( 'Q2' ) ] ) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testSaveEntityDocument() { |
115
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
116
|
|
|
|
117
|
|
|
$collectionMock = $this->getMockBuilder( 'Doctrine\MongoDB\Collection' ) |
118
|
|
|
->disableOriginalConstructor() |
119
|
|
|
->getMock(); |
120
|
|
|
$collectionMock->expects( $this->once() ) |
121
|
|
|
->method( 'upsert' ) |
122
|
|
|
->with( |
123
|
|
|
$this->equalTo( [ '_id' => 'Q1' ] ), |
124
|
|
|
$this->equalTo( [ 'id' => 'Q1' ] ) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$databaseMock = $this->getMockBuilder( 'Doctrine\MongoDB\Database' ) |
128
|
|
|
->disableOriginalConstructor() |
129
|
|
|
->getMock(); |
130
|
|
|
$databaseMock->expects( $this->once() ) |
131
|
|
|
->method( 'selectCollection' ) |
132
|
|
|
->with( $this->equalTo( 'item' ) ) |
133
|
|
|
->willReturn( $collectionMock ); |
134
|
|
|
|
135
|
|
|
$documentBuilderMock = $this->getMockBuilder( 'Wikibase\EntityStore\MongoDB\MongoDBDocumentBuilder' ) |
136
|
|
|
->disableOriginalConstructor() |
137
|
|
|
->getMock(); |
138
|
|
|
$documentBuilderMock->expects( $this->once() ) |
139
|
|
|
->method( 'buildDocumentForEntity' ) |
140
|
|
|
->with( $this->equalTo( $item ) ) |
141
|
|
|
->willReturn( [ 'id' => 'Q1' ] ); |
142
|
|
|
|
143
|
|
|
$entityStore = new MongoDBEntityDatabase( $databaseMock, $documentBuilderMock ); |
144
|
|
|
|
145
|
|
|
$entityStore->saveEntityDocument( $item ); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|