Test Failed
Pull Request — master (#91)
by Jeroen De
16:38 queued 06:39
created

testConstructorWithInvalidArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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(
30
			[ 'en' => $monolingualTextValue1, 'de' => $monolingualTextValue2 ],
31
			$value->getTexts()
32
		);
33
	}
34
35
	public function testGetters_empty() {
36
		$value = new MultilingualTextValue( [] );
37
		$this->assertSame( [], $value->getTexts() );
38
	}
39
40
	public function testArrayAndEquals() {
41
		$monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' );
42
		$monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' );
43
		$value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] );
44
		$array = $value->getArrayValue();
45
		$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...
46
		$this->assertTrue( $value->equals( $value2 ) );
47
		$this->assertEquals( $value, $value2 );
48
	}
49
50
	public function testSerialize() {
51
		$monolingualTextValue1 = new MonolingualTextValue( 'en', 'foo' );
52
		$monolingualTextValue2 = new MonolingualTextValue( 'de', 'foo' );
53
		$value = new MultilingualTextValue( [ $monolingualTextValue1, $monolingualTextValue2 ] );
54
		$serialization = serialize( $value );
55
		$value2 = unserialize( $serialization );
56
		$this->assertEquals( $value, $value2 );
57
	}
58
59
	/**
60
	 * @dataProvider invalidConstructorArgumentsProvider
61
	 */
62
	public function testConstructorWithInvalidArguments( $monolingualValues ) {
63
		$this->expectException( Exception::class );
64
65
		$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...
66
	}
67
68
	public function invalidConstructorArgumentsProvider() {
69
		return [
70
			[ [ 42 ] ],
71
			[ [ false ] ],
72
			[ [ true ] ],
73
			[ [ null ] ],
74
			[ [ [] ] ],
75
			[ [ 'foo' ] ],
76
77
			[ [ 42 => 'foo' ] ],
78
			[ [ '' => 'foo' ] ],
79
			[ [ 'en' => 42 ] ],
80
			[ [ 'en' => null ] ],
81
			[ [ 'en' => true ] ],
82
			[ [ 'en' => [] ] ],
83
			[ [ 'en' => 4.2 ] ],
84
85
			[ [
86
				new MonolingualTextValue( 'en', 'foo' ),
87
				false,
88
			] ],
89
			[ [
90
				new MonolingualTextValue( 'en', 'foo' ),
91
				'foobar',
92
			] ],
93
			[ [
94
				new MonolingualTextValue( 'en', 'foo' ),
95
				new MonolingualTextValue( 'en', 'bar' ),
96
			] ],
97
		];
98
	}
99
100
	/**
101
	 * @dataProvider invalidArrayProvider
102
	 */
103
	public function testNewFromArrayWithInvalidArray( array $array ) {
104
		$this->expectException( IllegalValueException::class );
105
		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...
106
	}
107
108
	public function invalidArrayProvider() {
109
		return [
110
			[ [ null ] ],
111
			[ [ '' ] ],
112
			[ [ [] ] ],
113
			[ [ [ 'en', 'foo' ] ] ],
114
		];
115
	}
116
117
}
118