1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\SQLStore; |
4
|
|
|
|
5
|
|
|
use SMW\ApplicationFactory; |
6
|
|
|
use SMW\SQLStore\EntityRebuildDispatcher; |
7
|
|
|
use SMW\SQLStore\SQLStore; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \SMW\SQLStore\EntityRebuildDispatcher |
11
|
|
|
* @group semantic-mediawiki |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
* @since 2.3 |
15
|
|
|
* |
16
|
|
|
* @author mwjames |
17
|
|
|
*/ |
18
|
|
|
class EntityRebuildDispatcherTest extends \PHPUnit_Framework_TestCase { |
19
|
|
|
|
20
|
|
|
private $applicationFactory; |
21
|
|
|
|
22
|
|
|
protected function setUp() { |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
$this->applicationFactory = ApplicationFactory::getInstance(); |
26
|
|
|
|
27
|
|
|
$idTable = $this->getMockBuilder( '\stdClass' ) |
28
|
|
|
->disableOriginalConstructor() |
29
|
|
|
->setMethods( array( 'hasIDFor' ) ) |
30
|
|
|
->getMock(); |
31
|
|
|
|
32
|
|
|
$idTable->expects( $this->any() ) |
33
|
|
|
->method( 'hasIDFor' ) |
34
|
|
|
->will( $this->returnValue( 0 ) ); |
35
|
|
|
|
36
|
|
|
$store = $this->getMockBuilder( '\SMW\Store' ) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->setMethods( array( 'getObjectIds' ) ) |
39
|
|
|
->getMockForAbstractClass(); |
40
|
|
|
|
41
|
|
|
$store->expects( $this->any() ) |
42
|
|
|
->method( 'getPropertyValues' ) |
43
|
|
|
->will( $this->returnValue( array() ) ); |
44
|
|
|
|
45
|
|
|
$store->expects( $this->any() ) |
46
|
|
|
->method( 'getObjectIds' ) |
47
|
|
|
->will( $this->returnValue( $idTable ) ); |
48
|
|
|
|
49
|
|
|
$this->applicationFactory->registerObject( 'Store', $store ); |
50
|
|
|
$this->applicationFactory->getSettings()->set( 'smwgCacheType', 'hash' ); |
51
|
|
|
$this->applicationFactory->getSettings()->set( 'smwgEnableUpdateJobs', false ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function tearDown() { |
55
|
|
|
$this->applicationFactory->clear(); |
56
|
|
|
|
57
|
|
|
parent::tearDown(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testCanConstruct() { |
61
|
|
|
|
62
|
|
|
$store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' ) |
63
|
|
|
->disableOriginalConstructor() |
64
|
|
|
->getMock(); |
65
|
|
|
|
66
|
|
|
$this->assertInstanceOf( |
67
|
|
|
'\SMW\SQLStore\EntityRebuildDispatcher', |
68
|
|
|
new EntityRebuildDispatcher( $store ) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider idProvider |
74
|
|
|
*/ |
75
|
|
|
public function testDispatchRebuildForSingleIteration( $id, $expected ) { |
76
|
|
|
|
77
|
|
|
$connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' ) |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->getMock(); |
80
|
|
|
|
81
|
|
|
$connection->expects( $this->any() ) |
82
|
|
|
->method( 'select' ) |
83
|
|
|
->will( $this->returnValue( array() ) ); |
84
|
|
|
|
85
|
|
|
$connection->expects( $this->any() ) |
86
|
|
|
->method( 'selectField' ) |
87
|
|
|
->will( $this->returnValue( $expected ) ); |
88
|
|
|
|
89
|
|
|
$store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' ) |
90
|
|
|
->disableOriginalConstructor() |
91
|
|
|
->setMethods( array( 'getConnection' ) ) |
92
|
|
|
->getMock(); |
93
|
|
|
|
94
|
|
|
$store->expects( $this->any() ) |
95
|
|
|
->method( 'getConnection' ) |
96
|
|
|
->will( $this->returnValue( $connection ) ); |
97
|
|
|
|
98
|
|
|
$instance = new EntityRebuildDispatcher( $store ); |
99
|
|
|
|
100
|
|
|
$instance->setDispatchRangeLimit( 1 ); |
101
|
|
|
$instance->setUpdateJobParseMode( SMW_UJ_PM_CLASTMDATE ); |
102
|
|
|
|
103
|
|
|
$instance->useJobQueueScheduler( false ); |
104
|
|
|
$instance->startRebuildWith( $id ); |
105
|
|
|
|
106
|
|
|
$this->assertSame( |
107
|
|
|
$expected, |
108
|
|
|
$id |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->assertLessThanOrEqual( |
112
|
|
|
1, |
113
|
|
|
$instance->getEstimatedProgress() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function idProvider() { |
118
|
|
|
|
119
|
|
|
$provider[] = array( |
|
|
|
|
120
|
|
|
42, // Within the border Id |
121
|
|
|
43 |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$provider[] = array( |
125
|
|
|
51, |
126
|
|
|
-1 |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return $provider; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
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.