Passed
Push — trimNormalizer ( b2284d...0e0038 )
by no
05:06
created

MultilingualTextValueTest::testGetSortKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\IllegalValueException;
6
use DataValues\MonolingualTextValue;
7
use DataValues\MultilingualTextValue;
8
9
/**
10
 * @covers DataValues\MultilingualTextValue
11
 *
12
 * @since 0.1
13
 *
14
 * @group DataValue
15
 * @group DataValueExtensions
16
 *
17
 * @license GPL-2.0+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class MultilingualTextValueTest extends DataValueTest {
21
22
	/**
23
	 * @see DataValueTest::getClass
24
	 *
25
	 * @return string
26
	 */
27
	public function getClass() {
28
		return MultilingualTextValue::class;
29
	}
30
31
	public function validConstructorArgumentsProvider() {
32
		return [
33
			[ [] ],
34
			[ [
35
				new MonolingualTextValue( 'en', 'foo' ),
36
			] ],
37
			[ [
38
				new MonolingualTextValue( 'en', 'foo' ),
39
				new MonolingualTextValue( 'de', 'foo' ),
40
			] ],
41
			[ [
42
				new MonolingualTextValue( 'en', 'foo' ),
43
				new MonolingualTextValue( 'de', 'bar' ),
44
			] ],
45
			[ [
46
				new MonolingualTextValue( 'en', 'foo' ),
47
				new MonolingualTextValue( 'de', ' foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz ' ),
48
			] ],
49
		];
50
	}
51
52
	public function invalidConstructorArgumentsProvider() {
53
		return [
54
			[ [ 42 ] ],
55
			[ [ false ] ],
56
			[ [ true ] ],
57
			[ [ null ] ],
58
			[ [ [] ] ],
59
			[ [ 'foo' ] ],
60
61
			[ [ 42 => 'foo' ] ],
62
			[ [ '' => 'foo' ] ],
63
			[ [ 'en' => 42 ] ],
64
			[ [ 'en' => null ] ],
65
			[ [ 'en' => true ] ],
66
			[ [ 'en' => [] ] ],
67
			[ [ 'en' => 4.2 ] ],
68
69
			[ [
70
				new MonolingualTextValue( 'en', 'foo' ),
71
				false,
72
			] ],
73
			[ [
74
				new MonolingualTextValue( 'en', 'foo' ),
75
				'foobar',
76
			] ],
77
			[ [
78
				new MonolingualTextValue( 'en', 'foo' ),
79
				new MonolingualTextValue( 'en', 'bar' ),
80
			] ],
81
		];
82
	}
83
84
	public function testNewFromArray() {
85
		$array = [ [ 'text' => 'foo', 'language' => 'en' ] ];
86
		$value = MultilingualTextValue::newFromArray( $array );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MultilingualTextValue::newFromArray() has been deprecated with message: since 1.0.0. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
87
		$this->assertSame( $array, $value->getArrayValue() );
88
	}
89
90
	/**
91
	 * @dataProvider invalidArrayProvider
92
	 */
93
	public function testNewFromArrayWithInvalidArray( array $array ) {
94
		$this->setExpectedException( IllegalValueException::class );
95
		MultilingualTextValue::newFromArray( $array );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MultilingualTextValue::newFromArray() has been deprecated with message: since 1.0.0. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
	}
97
98
	public function invalidArrayProvider() {
99
		return [
100
			[ [ null ] ],
101
			[ [ '' ] ],
102
			[ [ [] ] ],
103
			[ [ [ 'en', 'foo' ] ] ],
104
		];
105
	}
106
107
	/**
108
	 * @dataProvider getSortKeyProvider
109
	 */
110
	public function testGetSortKey( array $monolingualValues, $expected ) {
111
		$value = new MultilingualTextValue( $monolingualValues );
112
		$this->assertSame( $expected, $value->getSortKey() );
113
	}
114
115
	public function getSortKeyProvider() {
116
		return [
117
			[ [], '' ],
118
			[ [
119
				new MonolingualTextValue( 'en', 'foo' ),
120
			], 'enfoo' ],
121
			[ [
122
				new MonolingualTextValue( 'en', 'foo' ),
123
				new MonolingualTextValue( 'de', 'bar' ),
124
			], 'enfoo' ],
125
		];
126
	}
127
128
	/**
129
	 * @dataProvider instanceProvider
130
	 */
131
	public function testGetTexts( MultilingualTextValue $texts, array $arguments ) {
132
		$actual = $texts->getTexts();
133
134
		$this->assertInternalType( 'array', $actual );
135
		$this->assertContainsOnlyInstancesOf( MonolingualTextValue::class, $actual );
136
		$this->assertEquals( $arguments[0], array_values( $actual ) );
137
	}
138
139
	/**
140
	 * @dataProvider instanceProvider
141
	 */
142
	public function testGetValue( MultilingualTextValue $texts, array $arguments ) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
		$this->assertInstanceOf( $this->getClass(), $texts->getValue() );
144
	}
145
146
}
147