1 | <?php |
||
33 | class EntityRetrievingClosestReferencedEntityIdLookupTest extends PHPUnit_Framework_TestCase { |
||
34 | |||
35 | /** |
||
36 | * @param EntityLookup $entityLookup |
||
37 | * @param int|null $expectedNumberOfGetEntityCalls |
||
38 | * @return EntityLookup |
||
39 | */ |
||
40 | private function restrictEntityLookup( EntityLookup $entityLookup, $expectedNumberOfGetEntityCalls = null ) { |
||
41 | $entityLookupMock = $this->getMock( EntityLookup::class ); |
||
42 | |||
43 | $entityLookupMock->expects( |
||
44 | $expectedNumberOfGetEntityCalls === null ? $this->any() : $this->exactly( $expectedNumberOfGetEntityCalls ) |
||
45 | ) |
||
46 | ->method( 'getEntity' ) |
||
47 | ->willReturnCallback( function ( EntityId $entityId ) use ( $entityLookup ) { |
||
48 | return $entityLookup->getEntity( $entityId ); |
||
49 | } ); |
||
50 | |||
51 | return $entityLookupMock; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param int $expectedPrefetches |
||
56 | * @return EntityPrefetcher |
||
57 | */ |
||
58 | private function newEntityPrefetcher( $expectedPrefetches ) { |
||
59 | $entityPrefetcher = $this->getMock( EntityPrefetcher::class ); |
||
60 | $entityPrefetcher->expects( $this->exactly( $expectedPrefetches ) ) |
||
61 | ->method( 'prefetch' ) |
||
62 | ->with( $this->isType( 'array' ) ); |
||
63 | |||
64 | return $entityPrefetcher; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param PropertyId $via |
||
69 | * @param EntityId[] $to |
||
70 | * |
||
71 | * @return StatementList |
||
72 | */ |
||
73 | private function newReferencingStatementList( PropertyId $via, array $to ) { |
||
74 | $statementList = new StatementList(); |
||
75 | |||
76 | foreach ( $to as $toId ) { |
||
77 | $value = new EntityIdValue( $toId ); |
||
78 | $mainSnak = new PropertyValueSnak( $via, $value ); |
||
79 | $statementList->addStatement( new Statement( $mainSnak ) ); |
||
80 | } |
||
81 | |||
82 | return $statementList; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return EntityLookup |
||
87 | */ |
||
88 | private function newReferencingEntityStructure() { |
||
89 | // This returns the following entity structure (all entities linked by P599) |
||
90 | // Q1 -> Q5 -> Q599 -> Q1234 |
||
91 | // \ \ |
||
92 | // \ -- Q12 -> Q404 |
||
93 | // --- Q90 -> Q3 |
||
94 | // Note: Q404 doesn't exist |
||
95 | |||
96 | $pSubclassOf = new PropertyId( 'P599' ); |
||
97 | $q1 = new ItemId( 'Q1' ); |
||
98 | $q5 = new ItemId( 'Q5' ); |
||
99 | $q599 = new ItemId( 'Q599' ); |
||
100 | $q12 = new ItemId( 'Q12' ); |
||
101 | $q404 = new ItemId( 'Q404' ); |
||
102 | $q1234 = new ItemId( 'Q1234' ); |
||
103 | $q90 = new ItemId( 'Q90' ); |
||
104 | $q3 = new ItemId( 'Q3' ); |
||
105 | |||
106 | $lookup = new InMemoryEntityLookup(); |
||
107 | |||
108 | $lookup->addEntity( |
||
109 | new Item( $q1, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q5, $q90 ] ) ) |
||
110 | ); |
||
111 | $lookup->addEntity( |
||
112 | new Item( $q5, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q599 ] ) ) |
||
113 | ); |
||
114 | $lookup->addEntity( |
||
115 | new Item( $q599, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q12, $q1234 ] ) ) |
||
116 | ); |
||
117 | $lookup->addEntity( |
||
118 | new Item( $q12, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q404 ] ) ) |
||
119 | ); |
||
120 | $lookup->addEntity( |
||
121 | new Item( $q90, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q3 ] ) ) |
||
122 | ); |
||
123 | $lookup->addEntity( new Item( $q1234, null, null, null ) ); |
||
124 | $lookup->addEntity( new Item( $q3, null, null, null ) ); |
||
125 | |||
126 | return $lookup; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return EntityLookup |
||
131 | */ |
||
132 | private function newCircularReferencingEntityStructure() { |
||
133 | // This returns the following entity structure (all entities linked by P599) |
||
134 | // Q1 -> Q5 -> Q1 -> Q5 -> … |
||
135 | // \ \ |
||
136 | // --- Q90 --- Q90 |
||
137 | |||
138 | $pSubclassOf = new PropertyId( 'P599' ); |
||
139 | $q1 = new ItemId( 'Q1' ); |
||
140 | $q5 = new ItemId( 'Q5' ); |
||
141 | $q90 = new ItemId( 'Q90' ); |
||
142 | |||
143 | $lookup = new InMemoryEntityLookup(); |
||
144 | |||
145 | $lookup->addEntity( |
||
146 | new Item( $q1, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q5, $q90 ] ) ) |
||
147 | ); |
||
148 | $lookup->addEntity( |
||
149 | new Item( $q5, null, null, $this->newReferencingStatementList( $pSubclassOf, [ $q1 ] ) ) |
||
150 | ); |
||
151 | $lookup->addEntity( |
||
152 | new Item( $q90, null, null, null ) |
||
153 | ); |
||
154 | |||
155 | return $lookup; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
160 | */ |
||
161 | public function provideGetReferencedEntityIdNoError() { |
||
274 | |||
275 | /** |
||
276 | * @dataProvider provideGetReferencedEntityIdNoError |
||
277 | */ |
||
278 | public function testGetReferencedEntityIdNoError( |
||
306 | |||
307 | public function provideGetReferencedEntityIdMaxDepthExceeded() { |
||
324 | |||
325 | /** |
||
326 | * @dataProvider provideGetReferencedEntityIdMaxDepthExceeded |
||
327 | */ |
||
328 | public function testGetReferencedEntityIdMaxDepthExceeded( |
||
352 | |||
353 | public function provideGetReferencedEntityIdMaxEntityVisitsExceeded() { |
||
370 | |||
371 | /** |
||
372 | * @dataProvider provideGetReferencedEntityIdMaxEntityVisitsExceeded |
||
373 | */ |
||
374 | public function testGetReferencedEntityIdMaxEntityVisitsExceeded( |
||
398 | |||
399 | public function provideGetReferencedEntityIdTestInvalidSnak() { |
||
431 | |||
432 | /** |
||
433 | * @dataProvider provideGetReferencedEntityIdTestInvalidSnak |
||
434 | */ |
||
435 | public function testGetReferencedEntityIdTestInvalidSnak( |
||
452 | |||
453 | public function testGetReferencedEntityIdEntityLookupException() { |
||
476 | |||
477 | } |
||
478 |