Passed
Push — master ( e3acee...71d19a )
by Jeroen De
02:21
created

DiffOpTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
getClass() 0 1 ?
constructorProvider() 0 1 ?
A newInstance() 0 4 1
A instanceProvider() 0 16 2
A testConstructor() 0 11 3
A testIsAtomic() 0 3 1
A testGetType() 0 3 1
A testSerialization() 0 6 1
A testCount() 0 17 3
A testToArray() 0 8 1
A testToArrayWithConversion() 0 7 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Diff\Tests\DiffOp;
6
7
use Diff\DiffOp\DiffOp;
8
use Diff\Tests\DiffTestCase;
9
use ReflectionClass;
10
11
/**
12
 * Base test class for the Diff\DiffOp\DiffOp deriving classes.
13
 *
14
 * @group Diff
15
 * @group DiffOp
16
 *
17
 * @license BSD-3-Clause
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Daniel Kinzler
20
 */
21
abstract class DiffOpTest extends DiffTestCase {
22
23
	/**
24
	 * Returns the name of the concrete class tested by this test.
25
	 *
26
	 * @since 0.1
27
	 *
28
	 * @return string
29
	 */
30
	public abstract function getClass();
31
32
	/**
33
	 * First element can be a boolean indication if the successive values are valid,
34
	 * or a string indicating the type of exception that should be thrown (ie not valid either).
35
	 *
36
	 * @since 0.1
37
	 */
38
	public abstract function constructorProvider();
39
40
	/**
41
	 * Creates and returns a new instance of the concrete class.
42
	 *
43
	 * @since 0.1
44
	 *
45
	 * @return mixed
46
	 */
47
	public function newInstance() {
48
		$reflector = new ReflectionClass( $this->getClass() );
49
		return $reflector->newInstanceArgs( func_get_args() );
50
	}
51
52
	/**
53
	 * @since 0.1
54
	 *
55
	 * @return array[] An array of arrays, each containing an instance and an array of constructor
56
	 * arguments used to construct the instance.
57
	 */
58
	public function instanceProvider() {
59
		$self = $this;
60
61
		return array_filter( array_map(
62
			function( array $args ) use ( $self ) {
63
				$isValid = array_shift( $args ) === true;
64
65
				if ( !$isValid ) {
66
					return false;
67
				}
68
69
				return array( call_user_func_array( array( $self, 'newInstance' ), $args ), $args );
70
			},
71
			$this->constructorProvider()
72
		), 'is_array' );
73
	}
74
75
	/**
76
	 * @dataProvider constructorProvider
77
	 *
78
	 * @since 0.1
79
	 */
80
	public function testConstructor() {
81
		$args = func_get_args();
82
		$valid = array_shift( $args );
83
84
		if ( $valid !== true ) {
85
			$this->expectException( $valid ?: 'InvalidArgumentException' );
86
		}
87
88
		$dataItem = call_user_func_array( array( $this, 'newInstance' ), $args );
89
		$this->assertInstanceOf( $this->getClass(), $dataItem );
90
	}
91
92
	/**
93
	 * @dataProvider instanceProvider
94
	 */
95
	public function testIsAtomic( DiffOp $diffOp ) {
96
		$this->assertInternalType( 'boolean', $diffOp->isAtomic() );
97
	}
98
99
	/**
100
	 * @dataProvider instanceProvider
101
	 */
102
	public function testGetType( DiffOp $diffOp ) {
103
		$this->assertInternalType( 'string', $diffOp->getType() );
104
	}
105
106
	/**
107
	 * @dataProvider instanceProvider
108
	 */
109
	public function testSerialization( DiffOp $diffOp ) {
110
		$serialization = serialize( $diffOp );
111
		$unserialization = unserialize( $serialization );
112
		$this->assertEquals( $diffOp, $unserialization );
113
		$this->assertEquals( serialize( $diffOp ), serialize( $unserialization ) );
114
	}
115
116
	/**
117
	 * @dataProvider instanceProvider
118
	 */
119
	public function testCount( DiffOp $diffOp ) {
120
		if ( $diffOp->isAtomic() ) {
121
			$this->assertSame( 1, $diffOp->count() );
122
		}
123
		else {
124
			$count = 0;
125
126
			/**
127
			 * @var DiffOp $childOp
128
			 */
129
			foreach ( $diffOp as $childOp ) {
0 ignored issues
show
Bug introduced by
The expression $diffOp of type object<Diff\DiffOp\DiffOp> is not traversable.
Loading history...
130
				$count += $childOp->count();
131
			}
132
133
			$this->assertSame( $count, $diffOp->count() );
134
		}
135
	}
136
137
	/**
138
	 * @dataProvider instanceProvider
139
	 */
140
	public function testToArray( DiffOp $diffOp ) {
141
		$array = $diffOp->toArray();
142
143
		$this->assertInternalType( 'array', $array );
144
		$this->assertArrayHasKey( 'type', $array );
145
		$this->assertInternalType( 'string', $array['type'] );
146
		$this->assertEquals( $diffOp->getType(), $array['type'] );
147
	}
148
149
	/**
150
	 * @dataProvider instanceProvider
151
	 */
152
	public function testToArrayWithConversion( DiffOp $diffOp ) {
153
		$array = $diffOp->toArray( function() {
154
			return array( 'Nyan!' );
155
		} );
156
157
		$this->assertInternalType( 'array', $array );
158
	}
159
160
}
161