Completed
Pull Request — master (#91)
by
unknown
03:19 queued 01:42
created

MultilingualTextValueTest::testSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DataValues\Tests;
4
5
use DataValues\IllegalValueException;
6
use DataValues\MonolingualTextValue;
7
use DataValues\MultilingualTextValue;
8
use Exception;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * @covers \DataValues\MultilingualTextValue
13
 *
14
 * @since 0.1
15
 *
16
 * @group DataValue
17
 * @group DataValueExtensions
18
 *
19
 * @license GPL-2.0+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class MultilingualTextValueTest extends TestCase {
23
24
	public function testGetters() {
25
		$monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' );
26
		$monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' );
27
		$value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] );
28
		$this->assertSame( 'multilingualtext', $value->getType() );
29
		$this->assertSame( 'enfoo', $value->getSortKey() );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MultilingualTextValue::getSortKey() has been deprecated with message: Kept for compatibility with older DataValues versions.
Do not use.

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...
30
		$this->assertSame(
31
			[ 'en' => $monolingualTextValue1, 'de' => $monolingualTextValue2 ],
32
			$value->getTexts()
33
		);
34
	}
35
36
	public function testGetters_empty() {
37
		$value = new MultilingualTextValue( [] );
38
		$this->assertSame( '', $value->getSortKey() );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MultilingualTextValue::getSortKey() has been deprecated with message: Kept for compatibility with older DataValues versions.
Do not use.

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...
39
		$this->assertSame( [], $value->getTexts() );
40
	}
41
42
	public function testArrayAndEquals() {
43
		$monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' );
44
		$monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' );
45
		$value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] );
46
		$array = $value->getArrayValue();
47
		$value2 = 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...
48
		$this->assertTrue( $value->equals( $value2 ) );
49
		$this->assertEquals( $value, $value2 );
50
	}
51
52
	public function testSerialize() {
53
		$monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' );
54
		$monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' );
55
		$value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] );
56
		$serialization = serialize( $value );
57
		$value2 = unserialize( $serialization );
58
		$this->assertEquals( $value, $value2 );
59
	}
60
61
	/**
62
	 * @dataProvider invalidConstructorArgumentsProvider
63
	 */
64
	public function testConstructorWithInvalidArguments( $monolingualValues ) {
65
		$this->expectException( Exception::class );
66
67
		$dataItem = new MultilingualTextValue( $monolingualValues );
0 ignored issues
show
Unused Code introduced by
$dataItem is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
68
	}
69
70
	public function invalidConstructorArgumentsProvider() {
71
		return [
72
			[ [ 42 ] ],
73
			[ [ false ] ],
74
			[ [ true ] ],
75
			[ [ null ] ],
76
			[ [ [] ] ],
77
			[ [ 'foo' ] ],
78
79
			[ [ 42 => 'foo' ] ],
80
			[ [ '' => 'foo' ] ],
81
			[ [ 'en' => 42 ] ],
82
			[ [ 'en' => null ] ],
83
			[ [ 'en' => true ] ],
84
			[ [ 'en' => [] ] ],
85
			[ [ 'en' => 4.2 ] ],
86
87
			[ [
88
				new MonolingualTextValue( 'en', 'foo' ),
89
				false,
90
			] ],
91
			[ [
92
				new MonolingualTextValue( 'en', 'foo' ),
93
				'foobar',
94
			] ],
95
			[ [
96
				new MonolingualTextValue( 'en', 'foo' ),
97
				new MonolingualTextValue( 'en', 'bar' ),
98
			] ],
99
		];
100
	}
101
102
	/**
103
	 * @dataProvider invalidArrayProvider
104
	 */
105
	public function testNewFromArrayWithInvalidArray( array $array ) {
106
		$this->expectException( IllegalValueException::class );
107
		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...
108
	}
109
110
	public function invalidArrayProvider() {
111
		return [
112
			[ [ null ] ],
113
			[ [ '' ] ],
114
			[ [ [] ] ],
115
			[ [ [ 'en', 'foo' ] ] ],
116
		];
117
	}
118
119
}
120