Completed
Push — master ( 5d9cd9...874e6e )
by mw
62:18 queued 26:13
created

DeferredCallableUpdateTest::testUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\DeferredCallableUpdate;
6
7
/**
8
 * @covers \SMW\DeferredCallableUpdate
9
 * @group semantic-mediawiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since 2.4
13
 *
14
 * @author mwjames
15
 */
16
class DeferredCallableUpdateTest extends \PHPUnit_Framework_TestCase {
17
18
	private $testEnvironment;
19
	private $spyLogger;
20
21
	protected function setUp() {
22
		parent::setUp();
23
		$this->testEnvironment = new TestEnvironment();
24
		$this->spyLogger = $this->testEnvironment->getUtilityFactory()->newSpyLogger();
25
	}
26
27
	protected function tearDown() {
28
		$this->testEnvironment->clearPendingDeferredUpdates();
29
		$this->testEnvironment->tearDown();
30
		parent::tearDown();
31
	}
32
33
	public function testCanConstruct() {
34
35
		$callback = function() {
36
			return null;
37
		};
38
39
		$this->assertInstanceOf(
40
			'\SMW\DeferredCallableUpdate',
41
			new DeferredCallableUpdate( $callback )
42
		);
43
	}
44
45
	public function testUpdate() {
46
47
		$test = $this->getMockBuilder( '\stdClass' )
48
			->disableOriginalConstructor()
49
			->setMethods( array( 'doTest' ) )
50
			->getMock();
51
52
		$test->expects( $this->once() )
53
			->method( 'doTest' );
54
55
		$callback = function() use ( $test ) {
56
			$test->doTest();
57
		};
58
59
		$instance = new DeferredCallableUpdate(
60
			$callback
61
		);
62
63
		$instance->setLogger( $this->spyLogger );
64
		$instance->pushUpdate();
65
66
		$this->testEnvironment->executePendingDeferredUpdates();
67
	}
68
69
	public function testWaitableUpdate() {
70
71
		$test = $this->getMockBuilder( '\stdClass' )
72
			->disableOriginalConstructor()
73
			->setMethods( array( 'doTest' ) )
74
			->getMock();
75
76
		$test->expects( $this->once() )
77
			->method( 'doTest' );
78
79
		$callback = function() use ( $test ) {
80
			$test->doTest();
81
		};
82
83
		$instance = new DeferredCallableUpdate(
84
			$callback
85
		);
86
87
		$instance->markAsPending( true );
88
		$instance->pushUpdate();
89
90
		$instance->releasePendingUpdates();
91
92
		$this->testEnvironment->executePendingDeferredUpdates();
93
	}
94
95
	public function testUpdateWithDisabledDeferredUpdate() {
96
97
		$test = $this->getMockBuilder( '\stdClass' )
98
			->disableOriginalConstructor()
99
			->setMethods( array( 'doTest' ) )
100
			->getMock();
101
102
		$test->expects( $this->once() )
103
			->method( 'doTest' );
104
105
		$callback = function() use ( $test ) {
106
			$test->doTest();
107
		};
108
109
		$instance = new DeferredCallableUpdate(
110
			$callback
111
		);
112
113
		$instance->enabledDeferredUpdate( false );
114
		$instance->pushUpdate();
115
	}
116
117
	public function testOrigin() {
118
119
		$callback = function() {
120
		};
121
122
		$instance = new DeferredCallableUpdate(
123
			$callback
124
		);
125
126
		$instance->setOrigin( 'Foo' );
127
128
		$this->assertEquals(
129
			'Foo',
130
			$instance->getOrigin()
131
		);
132
	}
133
134
	public function testFilterDuplicateQueueEntryByFingerprint() {
135
136
		$test = $this->getMockBuilder( '\stdClass' )
137
			->disableOriginalConstructor()
138
			->setMethods( array( 'doTest' ) )
139
			->getMock();
140
141
		$test->expects( $this->once() )
142
			->method( 'doTest' );
143
144
		$callback = function() use ( $test ) {
145
			$test->doTest();
146
		};
147
148
		$instance = new DeferredCallableUpdate(
149
			$callback
150
		);
151
152
		$instance->setFingerprint( __METHOD__ );
153
		$instance->markAsPending( true );
154
		$instance->pushUpdate();
155
156
		$instance = new DeferredCallableUpdate(
157
			$callback
158
		);
159
160
		$instance->setFingerprint( __METHOD__ );
161
		$instance->markAsPending( true );
162
		$instance->pushUpdate();
163
164
		$this->testEnvironment->executePendingDeferredUpdates();
165
	}
166
167
}
168