1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\MediaWiki\Jobs; |
4
|
|
|
|
5
|
|
|
use SMW\Tests\TestEnvironment; |
6
|
|
|
use SMW\DIWikiPage; |
7
|
|
|
use SMW\MediaWiki\Jobs\EntityIdDisposerJob; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \SMW\MediaWiki\Jobs\EntityIdDisposerJob |
11
|
|
|
* @group semantic-mediawiki |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
* @since 2.5 |
15
|
|
|
* |
16
|
|
|
* @author mwjames |
17
|
|
|
*/ |
18
|
|
|
class EntityIdDisposerJobTest extends \PHPUnit_Framework_TestCase { |
19
|
|
|
|
20
|
|
|
private $testEnvironment; |
21
|
|
|
|
22
|
|
|
protected function setUp() { |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
$this->testEnvironment = new TestEnvironment(); |
26
|
|
|
|
27
|
|
|
$connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' ) |
28
|
|
|
->disableOriginalConstructor() |
29
|
|
|
->getMock(); |
30
|
|
|
|
31
|
|
|
$connection->expects( $this->any() ) |
32
|
|
|
->method( 'select' ) |
33
|
|
|
->will( $this->returnValue( array( 'Foo' ) ) ); |
34
|
|
|
|
35
|
|
|
$store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' ) |
36
|
|
|
->getMockForAbstractClass(); |
37
|
|
|
|
38
|
|
|
$connectionManager = $this->getMockBuilder( '\SMW\ConnectionManager' ) |
39
|
|
|
->disableOriginalConstructor() |
40
|
|
|
->getMock(); |
41
|
|
|
|
42
|
|
|
$connectionManager->expects( $this->any() ) |
43
|
|
|
->method( 'getConnection' ) |
44
|
|
|
->will( $this->returnValue( $connection ) ); |
45
|
|
|
|
46
|
|
|
$store->setConnectionManager( $connectionManager ); |
47
|
|
|
|
48
|
|
|
$this->testEnvironment->registerObject( 'Store', $store ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function tearDown() { |
52
|
|
|
$this->testEnvironment->tearDown(); |
53
|
|
|
parent::tearDown(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCanConstruct() { |
57
|
|
|
|
58
|
|
|
$title = $this->getMockBuilder( 'Title' ) |
59
|
|
|
->disableOriginalConstructor() |
60
|
|
|
->getMock(); |
61
|
|
|
|
62
|
|
|
$this->assertInstanceOf( |
63
|
|
|
'SMW\MediaWiki\Jobs\EntityIdDisposerJob', |
64
|
|
|
new EntityIdDisposerJob( $title ) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @dataProvider parametersProvider |
70
|
|
|
*/ |
71
|
|
|
public function testJobRun( $parameters ) { |
72
|
|
|
|
73
|
|
|
$subject = DIWikiPage::newFromText( __METHOD__ ); |
74
|
|
|
|
75
|
|
|
$instance = new EntityIdDisposerJob( |
76
|
|
|
$subject->getTitle(), |
77
|
|
|
$parameters |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$this->assertTrue( |
81
|
|
|
$instance->run() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function parametersProvider() { |
86
|
|
|
|
87
|
|
|
$provider[] = array( |
|
|
|
|
88
|
|
|
array() |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$provider[] = array( |
92
|
|
|
array( 'id' => 42 ) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $provider; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
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.