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

MonolingualTextValueTest::testGetters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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 Exception;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @covers \DataValues\MonolingualTextValue
12
 *
13
 * @since 0.1
14
 *
15
 * @group DataValue
16
 * @group DataValueExtensions
17
 *
18
 * @license GPL-2.0+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class MonolingualTextValueTest extends TestCase {
22
23
	public function testGetters() {
24
		$value = new MonolingualTextValue( 'en', 'foo' );
25
		$this->assertSame( 'monolingualtext', $value->getType() );
26
		$this->assertSame( 'foo', $value->getText() );
27
		$this->assertSame( 'en', $value->getLanguageCode() );
28
	}
29
30
	public function testArrayAndEquals() {
31
		$value = new MonolingualTextValue( 'en', 'foo' );
32
		$array = $value->getArrayValue();
33
		$value2 = MonolingualTextValue::newFromArray( $array );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MonolingualTextValue::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...
34
		$this->assertTrue( $value->equals( $value2 ) );
35
		$this->assertEquals( $value, $value2 );
36
	}
37
38
	public function testSerialize() {
39
		$value = new MonolingualTextValue( 'en', 'foo' );
40
		$serialization = serialize( $value );
41
		$value2 = unserialize( $serialization );
42
		$this->assertEquals( $value, $value2 );
43
	}
44
45
	/**
46
	 * @dataProvider invalidConstructorArgumentsProvider
47
	 */
48
	public function testConstructorWithInvalidArguments( $languageCode, $text ) {
49
		$this->expectException( Exception::class );
50
51
		$dataItem = new MonolingualTextValue( $languageCode, $text );
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...
52
	}
53
54
	public function invalidConstructorArgumentsProvider() {
55
		return [
56
			[ 42, null ],
57
			[ [], null ],
58
			[ false, null ],
59
			[ true, null ],
60
			[ null, null ],
61
			[ 'en', 42 ],
62
			[ 'en', false ],
63
			[ 'en', [] ],
64
			[ 'en', null ],
65
			[ '', 'foo' ],
66
		];
67
	}
68
69
	/**
70
	 * @dataProvider invalidArrayProvider
71
	 */
72
	public function testNewFromArrayWithInvalidArray( array $array ) {
73
		$this->expectException( IllegalValueException::class );
74
		MonolingualTextValue::newFromArray( $array );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\MonolingualTextValue::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...
75
	}
76
77
	public function invalidArrayProvider() {
78
		return [
79
			[ [] ],
80
			[ [ null ] ],
81
			[ [ '' ] ],
82
			[ [ 'en', 'foo' ] ],
83
			[ [ 'language' => 'en' ] ],
84
			[ [ 'text' => 'foo' ] ],
85
		];
86
	}
87
88
}
89