PageTest::testValidConstruction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 5
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Tests;
4
5
use Page;
6
use stdClass;
7
8
/**
9
 * Class PageTest
10
 *
11
 * @package Tests
12
 */
13
class PageTest extends \PHPUnit_Framework_TestCase {
14
15
    /**
16
     * Test for valid construction of the Page class
17
     *
18
     * @covers       Page::__construct
19
     * @dataProvider provideValidConstruction
20
     *
21
     * @param $title
22
     * @param null $pageid
23
     * @param bool $followRedir
24
     * @param bool $normalize
25
     * @param null $timestamp
26
     */
27
	public function testValidConstruction( $title , $pageid = null , $followRedir = true , $normalize= true, $timestamp = null ) {
28
		if( is_int( $pageid ) ) {
29
			$this->expectOutputString( "Getting page info for page ID {$pageid}..\n\n" );
30
			$expectedPageId = $pageid;
31
		} else {
32
			$this->expectOutputString( "Getting page info for {$title}..\n\n" );
33
			$expectedPageId = 1234;
34
		}
35
36
		$page = new Page( $this->getMockWiki(), $title, $pageid, $followRedir, $normalize, $timestamp );
37
38
		$this->assertEquals( $expectedPageId, $page->get_id() );
39
		$this->assertEquals( 0, $page->get_namespace() );
40
		$this->assertEquals( 'Foo', $page->get_title() );
41
		$this->assertEquals( 76, $page->get_length() );
42
		$this->assertEquals( false, $page->redirectFollowed() );
43
		$this->assertEquals( 66654, $page->get_talkID() );
44
		$this->assertEquals( '', $page->get_preload() );
45
	}
46
47
    /**
48
     * Provides data for testValidConstruction()
49
     *
50
     * @return array
51
     */
52
	public function provideValidConstruction() {
53
		return array(
54
			array( 'Foo' ),
55
			array( 'Foo', 1234 ),
56
			array( 'Foo', 1234, false ),
57
			array( 'Foo', 1234, true ),
58
			array( 'Foo', 1234, false, true ),
59
			array( 'Foo', 1234, true, false ),
60
			array( 'Foo', 1234, true, true, '20141212121212' ),
61
			array( 'Foo', 1234, true, true, 2014 ),
62
			array( 'Foo', 1234, true, true, 20141212121212 ),
63
		);
64
	}
65
66
    /**
67
     * Test for invalid construction of the Page class
68
     *
69
     * @covers       Page::__construct
70
     * @dataProvider provideInvalidConstruction
71
     *
72
     * @param       $expectedException
73
     * @param       $wiki
74
     * @param       $title
75
     * @param null $pageid
76
     * @param bool $followRedir
77
     * @param bool $normalize
78
     * @param null $timestamp
79
     */
80
	public function testInvalidConstruction( $expectedException, $wiki, $title , $pageid = null , $followRedir = true , $normalize= true, $timestamp = null ) {
81
		$this->setExpectedException( $expectedException );
82
		new Page( $wiki, $title, $pageid, $followRedir, $normalize, $timestamp );
83
	}
84
85
    /**
86
     * Data provider for testInvalidConstruction()
87
     *
88
     * @return array
89
     */
90
	public function provideInvalidConstruction() {
91
		return array(
92
			array( 'NoTitle', $this->getMockWiki(), null, null ),
93
			array( 'InvalidArgumentException', $this->getMockWiki(), new stdClass() ),
94
			array( 'InvalidArgumentException', $this->getMockWiki(), 'ImATitle', new stdClass() ),
95
			array( 'InvalidArgumentException', $this->getMockWiki(), 'ImATitle', null, null ),
96
			array( 'InvalidArgumentException', $this->getMockWiki(), 'ImATitle', null, true, null ),
97
			array( 'InvalidArgumentException', $this->getMockWiki(), 'ImATitle', null, true, true, new stdClass() ),
98
		);
99
	}
100
101
	/**
102
	 * @return \Wiki
103
	 */
104
	private function getMockWiki() {
105
		$mock = $this->getMockBuilder( 'Wiki' )
106
			->disableOriginalConstructor()
107
			->getMock();
108
		$mock->expects( $this->any() )
109
			->method( 'get_namespaces' )
110
			->will( $this->returnValue( array( 0 => '', 1 => 'Talk' ) ) );
111
		$mock->expects( $this->any() )
112
			->method( 'apiQuery' )
113
			->will(
114
				$this->returnCallback(
115
					function ( $params ) {
116
						if( $params['action'] === 'query'
117
							&& $params['prop'] === 'info'
118
							&& $params['inprop'] === 'protection|talkid|watched|watchers|notificationtimestamp|subjectid|url|readable|preload|displaytitle'
119
						) {
120
							if( array_key_exists( 'titles', $params ) ) {
121
								$title = $params['titles'];
122
								$pageid = 1234;
123
							} else {
124
								$title = 'Foo';
125
								$pageid = $params['pageids'];
126
							}
127
							return array(
128
								'query' => array(
129
									'pages' => array(
130
										$pageid => array(
131
											'pageid'                => $pageid,
132
											'ns'                    => 0,
133
											'title'                 => $title,
134
											'contentmodel'          => 'wikitext',
135
											'pagelanguage'          => 'en',
136
											'touched'               => '2014-01-26T01:13:44Z',
137
											'lastrevid'             => 999,
138
											'counter'               => '',
139
											'length'                => 76,
140
											'redirect'              => '',
141
											'protection'            => array(),
142
											'notificationtimestamp' => '',
143
											'talkid'                => '66654',
144
											'fullurl'               => 'imafullurl',
145
											'editurl'               => 'imaediturl',
146
											'readable'              => '',
147
											'preload'               => '',
148
											'displaytitle'          => 'Foo',
149
										)
150
									)
151
								)
152
							);
153
						}
154
						return array();
155
					}
156
				)
157
			);
158
		return $mock;
159
	}
160
}
161