Completed
Push — master ( fcf711...00bb57 )
by
unknown
02:50
created

tests/phpunit/DiffOp/DiffOpTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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