IriValueTest::testGetFragment()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\IriValue;
6
use PHPUnit\Framework\TestCase;
7
use ReflectionClass;
8
9
/**
10
 * @covers DataValues\IriValue
11
 *
12
 * @file
13
 * @since 0.1
14
 *
15
 * @ingroup DataValue
16
 *
17
 * @group DataValue
18
 * @group DataValueExtensions
19
 *
20
 * @licence GNU GPL v2+
21
 * @author Jeroen De Dauw < [email protected] >
22
 */
23
class IriValueTest extends TestCase {
24
25
26
	/**
27
	 * @see DataValueTest::getClass
28
	 *
29
	 * @since 0.1
30
	 *
31
	 * @return string
32
	 */
33
	public function getClass() {
34
		return 'DataValues\IriValue';
35
	}
36
37
	/**
38
	 * Creates and returns a new instance of the concrete class.
39
	 *
40
	 * @since 0.1
41
	 *
42
	 * @return mixed
43
	 */
44
	private function newInstance() {
45
		$reflector = new ReflectionClass( $this->getClass() );
46
		$args = func_get_args();
47
		$instance = $reflector->newInstanceArgs( $args );
48
		return $instance;
49
	}
50
51
	/**
52
	 * @since 0.1
53
	 *
54
	 * @return array [instance, constructor args]
55
	 */
56
	public function instanceProvider() {
57
		return array_map(
58
			function ( array $args ) {
59
				return [
60
					$this->newInstance( ...$args ),
61
					$args
62
				];
63
			},
64
			$this->validConstructorArgumentsProvider()
65
		);
66
	}
67
68
	public function validConstructorArgumentsProvider() {
69
		$argLists = array();
70
71
		$argLists[] = array( 'ohi', 'foo', 'bar', 'baz' );
72
		$argLists[] = array( 'http', '//www.wikidata.org/w/index.php', 'title=Special:Version', 'sv-credits-datavalues' );
73
		$argLists[] = array( 'ohi', 'foo', '', 'baz' );
74
		$argLists[] = array( 'ohi', 'foo', 'bar', '' );
75
		$argLists[] = array( 'ohi', 'foo', '', '' );
76
		$argLists[] = array( 'ohi', 'foo' );
77
78
		return $argLists;
79
	}
80
81
	public function invalidConstructorArgumentsProvider() {
82
		$argLists = array();
83
84
		$argLists[] = array();
85
86
		$argLists[] = array( 42 );
87
		$argLists[] = array( array() );
88
		$argLists[] = array( false );
89
		$argLists[] = array( true );
90
		$argLists[] = array( null );
91
		$argLists[] = array( 'foo' );
92
		$argLists[] = array( '' );
93
		$argLists[] = array( ' foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz ' );
94
95
		$argLists[] = array( '', '', '', '' );
96
		$argLists[] = array( 'foo', '', '', '' );
97
		$argLists[] = array( '', 'bar', '', '' );
98
		$argLists[] = array( '', 'foo', 'bar', 'baz' );
99
		$argLists[] = array( 'foo', '', 'bar', 'baz' );
100
		$argLists[] = array( '***', 'foo', 'bar', 'baz' );
101
		$argLists[] = array( 'abc42', 'foo', 'bar', 'baz' );
102
103
		$argLists[] = array( 'ohi', 'foo', 1 );
104
		$argLists[] = array( 'ohi', 'foo', true );
105
		$argLists[] = array( 'ohi', 'foo', 'baz', null );
106
		$argLists[] = array( 'ohi', 'foo', 'baz', array() );
107
108
		return $argLists;
109
	}
110
111
	/**
112
	 * @dataProvider instanceProvider
113
	 * @param \DataValues\IriValue $iri
114
	 * @param array $arguments
115
	 */
116
	public function testGetScheme( IriValue $iri, array $arguments ) {
117
		$this->assertEquals( $arguments[0], $iri->getScheme() );
118
	}
119
120
	/**
121
	 * @dataProvider instanceProvider
122
	 * @param \DataValues\IriValue $iri
123
	 * @param array $arguments
124
	 */
125
	public function testGetHierarchicalPart( IriValue $iri, array $arguments ) {
126
		$this->assertEquals( $arguments[1], $iri->getHierarchicalPart() );
127
	}
128
129
	/**
130
	 * @dataProvider instanceProvider
131
	 * @param \DataValues\IriValue $iri
132
	 * @param array $arguments
133
	 */
134
	public function testGetQuery( IriValue $iri, array $arguments ) {
135
		$expected = array_key_exists( 2, $arguments ) ? $arguments[2] : '';
136
		$this->assertEquals( $expected, $iri->getQuery() );
137
	}
138
139
	/**
140
	 * @dataProvider instanceProvider
141
	 * @param \DataValues\IriValue $iri
142
	 * @param array $arguments
143
	 */
144
	public function testGetFragment( IriValue $iri, array $arguments ) {
145
		$expected = array_key_exists( 3, $arguments ) ? $arguments[3] : '';
146
		$this->assertEquals( $expected, $iri->getFragment() );
147
	}
148
149
	public function serializationProvider() {
150
		$argLists = array();
151
152
		$argLists[] = array( 'ohi:there', 'ohi', 'there' );
153
		$argLists[] = array( 'http://www.wikidata.org', 'http', '//www.wikidata.org' );
154
		$argLists[] = array( 'http://www.wikidata.org/wiki/Special:Version', 'http', '//www.wikidata.org/wiki/Special:Version' );
155
		$argLists[] = array( 'http://www.wikidata.org?a=b&c=d', 'http', '//www.wikidata.org', 'a=b&c=d' );
156
		$argLists[] = array( 'http://www.wikidata.org?a=b&c=d#onoez', 'http', '//www.wikidata.org', 'a=b&c=d', 'onoez' );
157
		$argLists[] = array( 'http://www.wikidata.org#onoez', 'http', '//www.wikidata.org', '', 'onoez' );
158
		$argLists[] = array( 'http://www.wikidata.org#onoez#o_O', 'http', '//www.wikidata.org', '', 'onoez#o_O' );
159
160
		return $argLists;
161
	}
162
163
	/**
164
	 * @dataProvider serializationProvider
165
	 */
166
	public function testGetIriParts() {
167
		$expectedParts = func_get_args();
168
		$input = array_shift( $expectedParts );
169
170
		$expectedParts = array_pad( $expectedParts, 4, '' );
171
172
		$obtainedParts = IriValue::getIriParts( $input );
173
174
		$this->assertEquals( $expectedParts, $obtainedParts );
175
	}
176
177
	/**
178
	 * @dataProvider serializationProvider
179
	 */
180
	public function testGetValue() {
181
		$args = func_get_args();
182
		$expected = array_shift( $args );
183
184
		$reflector = new ReflectionClass( $this->getClass() );
185
		$instance = $reflector->newInstanceArgs( $args );
186
187
		$actual = $instance->getValue();
188
189
		$this->assertIsString( $actual );
190
		$this->assertEquals( $expected, $actual );
191
	}
192
193
}
194