Passed
Push — php71 ( 95a24c...2c708a )
by Jeroen De
07:19
created

DataValueTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 178
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
getClass() 0 1 ?
validConstructorArgumentsProvider() 0 1 ?
invalidConstructorArgumentsProvider() 0 1 ?
A newInstance() 0 6 1
A instanceProvider() 0 13 1
A testConstructorWithValidArguments() 0 8 1
A testConstructorWithInvalidArguments() 0 8 1
A testImplements() 0 7 1
A testGetType() 0 9 1
A testSerialization() 0 10 1
A testEquals() 0 7 2
A testGetHash() 0 7 1
A testGetCopy() 0 6 1
A testGetValueSimple() 0 4 1
A testGetArrayValueSimple() 0 4 1
A testToArray() 0 11 1
1
<?php
2
3
namespace Tests\DataValues\Geo\Values;
4
5
use Comparable;
6
use DataValues\DataValue;
7
use Exception;
8
use Hashable;
9
use Immutable;
10
use PHPUnit\Framework\TestCase;
11
use ReflectionClass;
12
use Serializable;
13
14
/**
15
 * TODO: this class is a copy of the one in DataValues Interfaces.
16
 * Since it abuses inheritance, the two derivatives in this component
17
 * should be refactored to the point where this code can be deleted.
18
 *
19
 * @license GPL-2.0+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
abstract class DataValueTest extends TestCase {
23
24
	/**
25
	 * Returns the name of the concrete class tested by this test.
26
	 *
27
	 * @since 0.1
28
	 *
29
	 * @return string
30
	 */
31
	abstract public function getClass();
32
33
	abstract public function validConstructorArgumentsProvider();
34
35
	abstract public function invalidConstructorArgumentsProvider();
36
37
	/**
38
	 * Creates and returns a new instance of the concrete class.
39
	 *
40
	 * @since 0.1
41
	 *
42
	 * @return mixed
43
	 */
44
	public function newInstance() {
45
		$reflector = new ReflectionClass( $this->getClass() );
46
		$args = func_get_args();
47
		$instance = $reflector->newInstanceArgs( $args );
48
		return $instance;
49
	}
50
51
	/**
52
	 * @since 0.1
53
	 *
54
	 * @return array [instance, constructor args]
55
	 */
56
	public function instanceProvider() {
57
		$instanceBuilder = [ $this, 'newInstance' ];
58
59
		return array_map(
60
			function ( array $args ) use ( $instanceBuilder ) {
61
				return [
62
					call_user_func_array( $instanceBuilder, $args ),
63
					$args
64
				];
65
			},
66
			$this->validConstructorArgumentsProvider()
67
		);
68
	}
69
70
	/**
71
	 * @dataProvider validConstructorArgumentsProvider
72
	 *
73
	 * @since 0.1
74
	 */
75
	public function testConstructorWithValidArguments() {
76
		$dataItem = call_user_func_array(
77
			[ $this, 'newInstance' ],
78
			func_get_args()
79
		);
80
81
		$this->assertInstanceOf( $this->getClass(), $dataItem );
82
	}
83
84
	/**
85
	 * @dataProvider invalidConstructorArgumentsProvider
86
	 *
87
	 * @since 0.1
88
	 */
89
	public function testConstructorWithInvalidArguments() {
90
		$this->expectException( Exception::class );
91
92
		call_user_func_array(
93
			[ $this, 'newInstance' ],
94
			func_get_args()
95
		);
96
	}
97
98
	/**
99
	 * @dataProvider instanceProvider
100
	 */
101
	public function testImplements( DataValue $value, 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...
102
		$this->assertInstanceOf( Immutable::class, $value );
103
		$this->assertInstanceOf( Hashable::class, $value );
104
		$this->assertInstanceOf( Comparable::class, $value );
105
		$this->assertInstanceOf( Serializable::class, $value );
106
		$this->assertInstanceOf( DataValue::class, $value );
107
	}
108
109
	/**
110
	 * @dataProvider instanceProvider
111
	 */
112
	public function testGetType( DataValue $value, 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...
113
		$valueType = $value->getType();
114
		$this->assertInternalType( 'string', $valueType );
115
		$this->assertTrue( strlen( $valueType ) > 0 );
116
117
		// Check whether using getType statically returns the same as called from an instance:
118
		$staticValueType = call_user_func( [ $this->getClass(), 'getType' ] );
119
		$this->assertEquals( $staticValueType, $valueType );
120
	}
121
122
	/**
123
	 * @dataProvider instanceProvider
124
	 */
125
	public function testSerialization( DataValue $value, 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...
126
		$serialization = serialize( $value );
127
		$this->assertInternalType( 'string', $serialization );
128
129
		$unserialized = unserialize( $serialization );
130
		$this->assertInstanceOf( DataValue::class, $unserialized );
131
132
		$this->assertTrue( $value->equals( $unserialized ) );
133
		$this->assertEquals( $value, $unserialized );
134
	}
135
136
	/**
137
	 * @dataProvider instanceProvider
138
	 */
139
	public function testEquals( DataValue $value, 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...
140
		$this->assertTrue( $value->equals( $value ) );
141
142
		foreach ( [ true, false, null, 'foo', 42, [], 4.2 ] as $otherValue ) {
143
			$this->assertFalse( $value->equals( $otherValue ) );
144
		}
145
	}
146
147
	/**
148
	 * @dataProvider instanceProvider
149
	 */
150
	public function testGetHash( DataValue $value, 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...
151
		$hash = $value->getHash();
152
153
		$this->assertInternalType( 'string', $hash );
154
		$this->assertEquals( $hash, $value->getHash() );
155
		$this->assertEquals( $hash, $value->getCopy()->getHash() );
156
	}
157
158
	/**
159
	 * @dataProvider instanceProvider
160
	 */
161
	public function testGetCopy( DataValue $value, 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...
162
		$copy = $value->getCopy();
163
164
		$this->assertInstanceOf( DataValue::class, $copy );
165
		$this->assertTrue( $value->equals( $copy ) );
166
	}
167
168
	/**
169
	 * @dataProvider instanceProvider
170
	 */
171
	public function testGetValueSimple( DataValue $value, 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...
172
		$value->getValue();
173
		$this->assertTrue( true );
174
	}
175
176
	/**
177
	 * @dataProvider instanceProvider
178
	 */
179
	public function testGetArrayValueSimple( DataValue $value, 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...
180
		$value->getArrayValue();
181
		$this->assertTrue( true );
182
	}
183
184
	/**
185
	 * @dataProvider instanceProvider
186
	 */
187
	public function testToArray( DataValue $value, 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...
188
		$array = $value->toArray();
189
190
		$this->assertInternalType( 'array', $array );
191
192
		$this->assertTrue( array_key_exists( 'type', $array ) );
193
		$this->assertTrue( array_key_exists( 'value', $array ) );
194
195
		$this->assertEquals( $value->getType(), $array['type'] );
196
		$this->assertEquals( $value->getArrayValue(), $array['value'] );
197
	}
198
199
}
200