Completed
Push — master ( 95e4ad...3dc5e6 )
by Jeroen De
01:44
created

DataTypeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 8
c 5
b 0
f 0
lcom 1
cbo 2
dl 0
loc 67
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A tearDown() 0 5 1
A testConstructorThrowsException() 0 4 1
A invalidConstructorArgumentsProvider() 0 12 1
A testGetId() 0 4 1
A testGetDataValueType() 0 4 1
A testGetLabel() 0 4 1
A testToArray() 0 4 1
1
<?php
2
3
namespace DataTypes\Tests\Phpunit;
4
5
use DataTypes\DataType;
6
use DataTypes\Message;
7
use PHPUnit_Framework_TestCase;
8
use ReflectionClass;
9
10
/**
11
 * @covers DataTypes\DataType
12
 *
13
 * @group DataTypes
14
 *
15
 * @license GPL-2.0+
16
 * @author Jeroen De Dauw < [email protected] >
17
 * @author Thiemo Mättig
18
 */
19
class DataTypeTest extends PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @var callable|null
23
	 */
24
	private $textFunction;
25
26
	protected function setUp() {
27
		parent::setUp();
28
29
		$class = new ReflectionClass( 'DataTypes\Message' );
30
		$properties = $class->getStaticProperties();
31
		$this->textFunction = $properties['textFunction'];
32
33
		Message::registerTextFunction( function( $key, $languageCode ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key 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...
Unused Code introduced by
The parameter $languageCode 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...
34
			return implode( '|', func_get_args() );
35
		} );
36
	}
37
38
	protected function tearDown() {
39
		Message::registerTextFunction( $this->textFunction );
0 ignored issues
show
Bug introduced by
It seems like $this->textFunction can also be of type null; however, DataTypes\Message::registerTextFunction() does only seem to accept callable, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
41
		parent::tearDown();
42
	}
43
44
	/**
45
	 * @dataProvider invalidConstructorArgumentsProvider
46
	 */
47
	public function testConstructorThrowsException( $propertyType, $valueType ) {
48
		$this->setExpectedException( 'InvalidArgumentException' );
49
		new DataType( $propertyType, $valueType );
50
	}
51
52
	public function invalidConstructorArgumentsProvider() {
53
		return array(
54
			array( 'propertyType', '' ),
55
			array( 'propertyType', null ),
56
			array( 'propertyType', false ),
57
			array( 'propertyType', 1 ),
58
			array( '', 'valueType' ),
59
			array( null, 'valueType' ),
60
			array( false, 'valueType' ),
61
			array( 0, 'valueType' ),
62
		);
63
	}
64
65
	public function testGetId() {
66
		$type = new DataType( 'propertyType', 'valueType' );
67
		$this->assertSame( 'propertyType', $type->getId() );
68
	}
69
70
	public function testGetDataValueType() {
71
		$type = new DataType( 'propertyType', 'valueType' );
72
		$this->assertSame( 'valueType', $type->getDataValueType() );
73
	}
74
75
	public function testGetLabel() {
76
		$type = new DataType( 'propertyType', 'valueType' );
77
		$this->assertSame( 'datatypes-type-propertyType|en', $type->getLabel( 'en' ) );
78
	}
79
80
	public function testToArray() {
81
		$type = new DataType( 'propertyType', 'valueType' );
82
		$this->assertSame( array( 'dataValueType' => 'valueType' ), $type->toArray() );
83
	}
84
85
}
86