Completed
Push — master ( 81538e...c2bf57 )
by Jeroen De
10:06
created

BaseElementTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 69
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidConstructorProvider() 0 3 1
A newInstance() 0 6 1
getClass() 0 1 ?
A instanceProvider() 0 10 1
validConstructorProvider() 0 1 ?
A testGivenValidArguments_constructorDoesNotThrowException() 0 4 1
A testGivenInvalidArguments_constructorThrowsException() 0 4 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\Tests\Unit\LegacyModel;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\TestCase;
9
use PHPUnit4And6Compat;
10
11
/**
12
 * Base class for unit tests classes for the Maps\BaseElement deriving objects.
13
 *
14
 * @since 3.0
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
abstract class BaseElementTest extends TestCase {
20
	use PHPUnit4And6Compat;
21
22
	public function invalidConstructorProvider() {
23
		return [];
24
	}
25
26
	/**
27
	 * Creates and returns a new instance of the concrete class.
28
	 *
29
	 * @since 3.0
30
	 *
31
	 * @return mixed
32
	 */
33
	public function newInstance() {
34
		$reflector = new \ReflectionClass( $this->getClass() );
35
		$args = func_get_args();
36
		$instance = $reflector->newInstanceArgs( $args );
37
		return $instance;
38
	}
39
40
	/**
41
	 * Returns the name of the concrete class tested by this test.
42
	 *
43
	 * @since 3.0
44
	 *
45
	 * @return string
46
	 */
47
	public abstract function getClass();
48
49
	/**
50
	 * @since 3.0
51
	 *
52
	 * @return array [instance, constructor args]
53
	 */
54
	public function instanceProvider() {
55
		$phpFails = [ $this, 'newInstance' ];
56
57
		return array_map(
58
			function ( array $args ) use ( $phpFails ) {
59
				return [ call_user_func_array( $phpFails, $args ), $args ];
60
			},
61
			$this->validConstructorProvider()
62
		);
63
	}
64
65
	public abstract function validConstructorProvider();
66
67
	/**
68
	 * @dataProvider validConstructorProvider
69
	 *
70
	 * @since 3.0
71
	 */
72
	public function testGivenValidArguments_constructorDoesNotThrowException() {
73
		$instance = call_user_func_array( [ $this, 'newInstance' ], func_get_args() );
74
		$this->assertInstanceOf( $this->getClass(), $instance );
75
	}
76
77
	/**
78
	 * @dataProvider invalidConstructorProvider
79
	 *
80
	 * @since 3.0
81
	 */
82
	public function testGivenInvalidArguments_constructorThrowsException() {
83
		$this->expectException( InvalidArgumentException::class );
84
		call_user_func_array( [ $this, 'newInstance' ], func_get_args() );
85
	}
86
87
}
88