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

DeferredRequestDispatchManagerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\DeferredRequestDispatchManager;
6
use SMW\DIWikiPage;
7
use SMW\Tests\TestEnvironment;
8
9
/**
10
 * @covers \SMW\DeferredRequestDispatchManager
11
 * @group semantic-mediawiki
12
 *
13
 * @license GNU GPL v2+
14
 * @since   2.3
15
 *
16
 * @author mwjames
17
 */
18
class DeferredRequestDispatchManagerTest extends \PHPUnit_Framework_TestCase {
19
20
	private $spyLogger;
21
22
	protected function setUp() {
23
		parent::setUp();
24
25
		$testEnvironment = new TestEnvironment();
26
		$this->spyLogger = $testEnvironment->getUtilityFactory()->newSpyLogger();
27
	}
28
29
	public function testCanConstruct() {
30
31
		$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' )
32
			->disableOriginalConstructor()
33
			->getMock();
34
35
		$this->assertInstanceOf(
36
			'\SMW\DeferredRequestDispatchManager',
37
			new DeferredRequestDispatchManager( $httpRequest )
38
		);
39
	}
40
41
	/**
42
	 * @dataProvider dispatchableJobProvider
43
	 */
44
	public function testDispatchJobFor( $type, $deferredJobRequestState, $parameters = array() ) {
45
46
		$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' )
47
			->disableOriginalConstructor()
48
			->getMock();
49
50
		$httpRequest->expects( $this->any() )
51
			->method( 'ping' )
52
			->will( $this->returnValue( true ) );
53
54
		$instance = new DeferredRequestDispatchManager( $httpRequest );
55
		$instance->reset();
56
57
		$instance->isEnabledHttpDeferredRequest( $deferredJobRequestState );
58
		$instance->isEnabledJobQueue( false );
59
		$instance->setLogger( $this->spyLogger );
60
61
		$this->assertTrue(
62
			$instance->dispatchJobRequestWith( $type, DIWikiPage::newFromText( __METHOD__ )->getTitle(), $parameters )
0 ignored issues
show
Bug introduced by
It seems like \SMW\DIWikiPage::newFrom...__METHOD__)->getTitle() can be null; however, dispatchJobRequestWith() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
63
		);
64
	}
65
66
	public function testDispatchParserCachePurgeJob() {
67
68
		$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' )
69
			->disableOriginalConstructor()
70
			->getMock();
71
72
		$httpRequest->expects( $this->once() )
73
			->method( 'ping' )
74
			->will( $this->returnValue( true ) );
75
76
		$instance = new DeferredRequestDispatchManager( $httpRequest );
77
		$instance->reset();
78
79
		$instance->isEnabledHttpDeferredRequest( true );
80
		$instance->isEnabledJobQueue( false );
81
82
		$parameters = array( 'idlist' => '1|2' );
83
		$title = DIWikiPage::newFromText( __METHOD__ )->getTitle();
84
85
		$this->assertTrue(
86
			$instance->dispatchParserCachePurgeJobWith( $title, $parameters )
0 ignored issues
show
Bug introduced by
It seems like $title defined by \SMW\DIWikiPage::newFrom...__METHOD__)->getTitle() on line 83 can be null; however, SMW\DeferredRequestDispa...rserCachePurgeJobWith() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
87
		);
88
	}
89
90
	public function testDispatchFulltextSearchTableUpdateJob() {
91
92
		$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' )
93
			->disableOriginalConstructor()
94
			->getMock();
95
96
		$httpRequest->expects( $this->once() )
97
			->method( 'ping' )
98
			->will( $this->returnValue( true ) );
99
100
		$instance = new DeferredRequestDispatchManager( $httpRequest );
101
		$instance->reset();
102
103
		$instance->isEnabledHttpDeferredRequest( true );
104
		$instance->isEnabledJobQueue( false );
105
106
		$parameters = array();
107
		$title = DIWikiPage::newFromText( __METHOD__ )->getTitle();
108
109
		$this->assertTrue(
110
			$instance->dispatchFulltextSearchTableUpdateJobWith( $title, $parameters )
0 ignored issues
show
Bug introduced by
It seems like $title defined by \SMW\DIWikiPage::newFrom...__METHOD__)->getTitle() on line 107 can be null; however, SMW\DeferredRequestDispa...rchTableUpdateJobWith() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
111
		);
112
	}
113
114
	/**
115
	 * @dataProvider preliminaryCheckProvider
116
	 */
117
	public function testPreliminaryCheckForType( $type, $parameters = array() ) {
118
119
		$httpRequest = $this->getMockBuilder( '\Onoi\HttpRequest\SocketRequest' )
120
			->disableOriginalConstructor()
121
			->getMock();
122
123
		$httpRequest->expects( $this->any() )
124
			->method( 'ping' )
125
			->will( $this->returnValue( true ) );
126
127
		$instance = new DeferredRequestDispatchManager( $httpRequest );
128
		$instance->reset();
129
130
		$instance->isEnabledJobQueue( false );
131
132
		$this->assertNull(
133
			$instance->dispatchJobRequestWith( $type, DIWikiPage::newFromText( __METHOD__ )->getTitle(), $parameters )
0 ignored issues
show
Bug introduced by
It seems like \SMW\DIWikiPage::newFrom...__METHOD__)->getTitle() can be null; however, dispatchJobRequestWith() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
134
		);
135
	}
136
137
	public function dispatchableJobProvider() {
138
139
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = 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...
140
			'SMW\UpdateJob',
141
			false
142
		);
143
144
		$provider[] = array(
145
			'SMW\UpdateJob',
146
			true
147
		);
148
149
		$provider[] = array(
150
			'SMW\TempChangeOpPurgeJob',
151
			true
152
		);
153
154
		$provider[] = array(
155
			'SMW\ParserCachePurgeJob',
156
			false,
157
			array( 'idlist' => '1|2' )
158
		);
159
160
		$provider[] = array(
161
			'SMW\ParserCachePurgeJob',
162
			true,
163
			array( 'idlist' => '1|2' )
164
		);
165
166
		return $provider;
167
	}
168
169
	public function preliminaryCheckProvider() {
170
171
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = 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...
172
			'UnknownJob'
173
		);
174
175
		return $provider;
176
	}
177
178
}
179