Completed
Push — master ( 81f3e7...e6d8eb )
by
unknown
03:27 queued 37s
created

DiffOpFactoryTest::invalidArrayFromArrayProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Diff\Tests;
4
5
use Diff\DiffOp\Diff\Diff;
6
use Diff\DiffOp\DiffOp;
7
use Diff\DiffOp\DiffOpAdd;
8
use Diff\DiffOp\DiffOpChange;
9
use Diff\DiffOp\DiffOpRemove;
10
use Diff\DiffOpFactory;
11
12
/**
13
 * @covers Diff\DiffOpFactory
14
 *
15
 * @group Diff
16
 * @group DiffOpFactory
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 * @author Daniel Kinzler
21
 */
22
class DiffOpFactoryTest extends DiffTestCase {
23
24
	public function diffOpProvider() {
25
		$diffOps = array();
26
27
		$diffOps[] = new DiffOpAdd( 42 );
28
		$diffOps['foo bar'] = new DiffOpAdd( '42' );
29
		$diffOps[9001] = new DiffOpAdd( 4.2 );
30
		$diffOps['42'] = new DiffOpAdd( array( 42, array( 9001 ) ) );
31
		$diffOps[] = new DiffOpRemove( 42 );
32
		$diffOps[] = new DiffOpAdd( new DiffOpChange( 'spam', 'moar spam' ) );
33
34
		$atomicDiffOps = $diffOps;
35
36
		foreach ( array( true, false, null ) as $isAssoc ) {
37
			$diffOps[] = new Diff( $atomicDiffOps, $isAssoc );
38
		}
39
40
		$diffOps[] = new DiffOpChange( 42, '9001' );
41
42
		$diffOps[] = new Diff( $diffOps );
43
44
		return $this->arrayWrap( $diffOps );
45
	}
46
47
	/**
48
	 * @dataProvider diffOpProvider
49
	 *
50
	 * @param DiffOp $diffOp
51
	 */
52
	public function testNewFromArray( DiffOp $diffOp ) {
53
		$factory = new DiffOpFactory();
54
55
		// try without conversion callback
56
		$array = $diffOp->toArray();
57
		$newInstance = $factory->newFromArray( $array );
58
59
		// If an equality method is implemented in DiffOp, it should be used here
60
		$this->assertEquals( $diffOp, $newInstance );
61
		$this->assertEquals( $diffOp->getType(), $newInstance->getType() );
62
	}
63
64
	/**
65
	 * @dataProvider diffOpProvider
66
	 *
67
	 * @param DiffOp $diffOp
68
	 */
69
	public function testNewFromArrayWithConversion( DiffOp $diffOp ) {
70
		$unserializationFunction = function( $array ) {
71
			if ( is_array( $array ) && isset( $array['type'] ) && $array['type'] === 'Change' ) {
72
				return new DiffOpChange( $array['teh_old'], $array['teh_new'] );
73
			}
74
75
			return $array;
76
		};
77
78
		$factory = new DiffOpFactory( $unserializationFunction );
79
80
		$serializationFunction = function( $obj ) {
81
			if ( $obj instanceof DiffOpChange ) {
82
				return array(
83
					'type' => 'Change',
84
					'teh_old' => $obj->getOldValue(),
85
					'teh_new' => $obj->getNewValue(),
86
				);
87
			}
88
89
			return $obj;
90
		};
91
92
		// try with conversion callback
93
		$array = $diffOp->toArray( $serializationFunction );
94
95
		$newInstance = $factory->newFromArray( $array );
96
97
		// If an equality method is implemented in DiffOp, it should be used here
98
		$this->assertEquals( $diffOp, $newInstance );
99
		$this->assertEquals( $diffOp->getType(), $newInstance->getType() );
100
	}
101
102
	public function invalidArrayFromArrayProvider() {
103
		return array(
104
			array( array() ),
105
			array( array( '~=[,,_,,]:3' ) ),
106
			array( array( '~=[,,_,,]:3' => '~=[,,_,,]:3' ) ),
107
			array( array( 'type' => '~=[,,_,,]:3' ) ),
108
			array( array( 'type' => 'add', 'oldvalue' => 'foo' ) ),
109
			array( array( 'type' => 'remove', 'newvalue' => 'foo' ) ),
110
			array( array( 'type' => 'change', 'newvalue' => 'foo' ) ),
111
			array( array( 'diff' => 'remove', 'newvalue' => 'foo' ) ),
112
			array( array( 'diff' => 'remove', 'operations' => array() ) ),
113
			array( array( 'diff' => 'remove', 'isassoc' => true ) ),
114
		);
115
	}
116
117
	/**
118
	 * @dataProvider invalidArrayFromArrayProvider
119
	 *
120
	 * @param array $array
121
	 */
122
	public function testNewFromArrayInvalid( array $array ) {
123
		$this->setExpectedException( 'InvalidArgumentException' );
124
125
		$factory = new DiffOpFactory();
126
		$factory->newFromArray( $array );
127
	}
128
129
}
130