Completed
Push — master ( 5ebf6f...02d949 )
by mw
12s
created

testAddToUpdateListOnEmpty_List()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\SQLStore\QueryDependency;
4
5
use SMW\DIWikiPage;
6
use SMW\SQLStore\QueryDependency\DependencyLinksTableUpdater;
7
use SMW\SQLStore\SQLStore;
8
use SMW\Tests\TestEnvironment;
9
10
/**
11
 * @covers \SMW\SQLStore\QueryDependency\DependencyLinksTableUpdater
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.4
16
 *
17
 * @author mwjames
18
 */
19
class DependencyLinksTableUpdaterTest extends \PHPUnit_Framework_TestCase {
20
21
	private $testEnvironment;
22
	private $store;
23
24
	protected function setUp() {
25
		parent::setUp();
26
27
		$this->testEnvironment = new TestEnvironment();
28
29
		$this->store = $this->getMockBuilder( '\SMW\Store' )
30
			->disableOriginalConstructor()
31
			->getMockForAbstractClass();
32
33
		$this->testEnvironment->registerObject( 'Store', $this->store );
34
	}
35
36
	protected function tearDown() {
37
		$this->testEnvironment->tearDown();
38
		parent::tearDown();
39
	}
40
41
	public function testCanConstruct() {
42
43
		$this->assertInstanceOf(
44
			'\SMW\SQLStore\QueryDependency\DependencyLinksTableUpdater',
45
			new DependencyLinksTableUpdater( $this->store )
46
		);
47
	}
48
49
	public function testAddToUpdateList() {
50
51
		$idTable = $this->getMockBuilder( '\stdClass' )
52
			->setMethods( array( 'getIDFor' ) )
53
			->getMock();
54
55
		$idTable->expects( $this->any() )
56
			->method( 'getIDFor' )
57
			->will( $this->onConsecutiveCalls( 1001 ) );
58
59
		$connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' )
60
			->disableOriginalConstructor()
61
			->getMock();
62
63
		$connection->expects( $this->once() )
64
			->method( 'delete' )
65
			->with(
66
				$this->equalTo( \SMWSQLStore3::QUERY_LINKS_TABLE ),
67
				$this->equalTo( array( 's_id' => 42 ) ) );
68
69
		$insert[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$insert was never initialized. Although not strictly required by PHP, it is generally a good practice to add $insert = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
70
			's_id' => 42,
71
			'o_id' => 1001
72
		);
73
74
		$connection->expects( $this->once() )
75
			->method( 'insert' )
76
			->with(
77
				$this->equalTo( \SMWSQLStore3::QUERY_LINKS_TABLE ),
78
				$this->equalTo( $insert ) );
79
80
		$connectionManager = $this->getMockBuilder( '\SMW\ConnectionManager' )
81
			->disableOriginalConstructor()
82
			->getMock();
83
84
		$connectionManager->expects( $this->any() )
85
			->method( 'getConnection' )
86
			->will( $this->returnValue( $connection ) );
87
88
		$store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' )
89
			->disableOriginalConstructor()
90
			->setMethods( array( 'getObjectIds' ) )
91
			->getMockForAbstractClass();
92
93
		$store->setConnectionManager( $connectionManager );
94
95
		$store->expects( $this->any() )
96
			->method( 'getObjectIds' )
97
			->will( $this->returnValue( $idTable ) );
98
99
		$instance = new DependencyLinksTableUpdater(
100
			$store
101
		);
102
103
		$instance->clear();
104
105
		$instance->addToUpdateList( 42, array( DIWikiPage::newFromText( 'Bar' ) ) );
106
		$instance->doUpdate();
107
	}
108
109
	public function testAddToUpdateListOnNull_List() {
110
111
		$instance = new DependencyLinksTableUpdater(
112
			$this->store
113
		);
114
115
		$this->assertNull(
116
			$instance->addToUpdateList( 42, null )
117
		);
118
	}
119
120
	public function testAddToUpdateListOnZero_Id() {
121
122
		$instance = new DependencyLinksTableUpdater(
123
			$this->store
124
		);
125
126
		$this->assertNull(
127
			$instance->addToUpdateList( 0, array() )
128
		);
129
	}
130
131
	public function testAddToUpdateListOnEmpty_List() {
132
133
		$instance = new DependencyLinksTableUpdater(
134
			$this->store
135
		);
136
137
		$this->assertNull(
138
			$instance->addToUpdateList( 42, array() )
139
		);
140
	}
141
142
	public function testAddDependenciesFromQueryResultWhereObjectIdIsYetUnknownWhichRequiresToCreateTheIdOnTheFly() {
143
144
		$idTable = $this->getMockBuilder( '\stdClass' )
145
			->setMethods( array( 'getIDFor', 'makeSMWPageID' ) )
146
			->getMock();
147
148
		$idTable->expects( $this->any() )
149
			->method( 'getIDFor' )
150
			->will( $this->returnValue( 0 ) );
151
152
		$idTable->expects( $this->any() )
153
			->method( 'makeSMWPageID' )
154
			->will( $this->returnValue( 1001 ) );
155
156
		$connection = $this->getMockBuilder( '\SMW\MediaWiki\Database' )
157
			->disableOriginalConstructor()
158
			->getMock();
159
160
		$connection->expects( $this->once() )
161
			->method( 'delete' )
162
			->with(
163
				$this->equalTo( \SMWSQLStore3::QUERY_LINKS_TABLE ),
164
				$this->equalTo( array( 's_id' => 42 ) ) );
165
166
		$insert[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$insert was never initialized. Although not strictly required by PHP, it is generally a good practice to add $insert = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
167
			's_id' => 42,
168
			'o_id' => 1001
169
		);
170
171
		$connection->expects( $this->once() )
172
			->method( 'insert' )
173
			->with(
174
				$this->equalTo( \SMWSQLStore3::QUERY_LINKS_TABLE ),
175
				$this->equalTo( $insert ) );
176
177
		$connectionManager = $this->getMockBuilder( '\SMW\ConnectionManager' )
178
			->disableOriginalConstructor()
179
			->getMock();
180
181
		$connectionManager->expects( $this->any() )
182
			->method( 'getConnection' )
183
			->will( $this->returnValue( $connection ) );
184
185
		$store = $this->getMockBuilder( '\SMW\SQLStore\SQLStore' )
186
			->disableOriginalConstructor()
187
			->setMethods( array( 'getObjectIds' ) )
188
			->getMockForAbstractClass();
189
190
		$store->setConnectionManager( $connectionManager );
191
192
		$store->expects( $this->any() )
193
			->method( 'getObjectIds' )
194
			->will( $this->returnValue( $idTable ) );
195
196
		$instance = new DependencyLinksTableUpdater(
197
			$store
198
		);
199
200
		$instance->clear();
201
202
		$instance->addToUpdateList( 42, array( DIWikiPage::newFromText( 'Bar', SMW_NS_PROPERTY ) ) );
203
		$instance->doUpdate();
204
	}
205
206
}
207