/
HelperTest.php
142 lines (129 loc) · 4.25 KB
/
HelperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
# MantisBT - a php based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Mantis Unit Tests
* @package Tests
* MantisBT Core Unit Tests
* @subpackage Helper
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
# Includes
require_once 'MantisCoreBase.php';
/**
* Helper API tests
* @package Tests
* @subpackage String
*/
class HelperTest extends MantisCoreBase {
/**
* Tests helper_array_transpose() with good values.
*
* @param mixed $p_in Input array.
* @param mixed $p_out Output array.
* @return void
*
* @dataProvider providerArrayTransposeValid
*/
public function testArrayTransposeValid( $p_in, $p_out ) {
$this->assertEquals( $p_out, helper_array_transpose( $p_in ) );
}
/**
* Tests helper_array_transpose() with invalid values.
*
* @param mixed $p_in Input value.
* @return void
*
* @dataProvider providerArrayTransposeInvalid
*/
public function testArrayTransposeInvalid( $p_in ) {
$this->expectException(PHPUnit\Framework\Error\Error::class);
$this->expectExceptionCode(E_USER_ERROR);
$this->expectExceptionMessage((string)ERROR_GENERIC);
helper_array_transpose( $p_in );
}
/**
* Provides a series of "Good" test cases.
*
* Test case structure:
* <case> => array( <test matrix>, <expected transposition> )
*
* helper_array_transpose() should successfully transpose <test matrix>
* into <expected transposition>.
*
* @return array List of test cases
*/
public function providerArrayTransposeValid() {
return array(
'Bidimensional simple array' => array(
array( array( 'a' ), array( 'b' ) ),
array( array ( 'a', 'b', ) )
),
'Bidimensional array with numeric indices' => array(
array( 10 => array( 100 => 'a' ), 20 => array( 100 => 'b' ) ),
array( 100 => array ( 10 => 'a', 20 => 'b', ) )
),
'Bidimensional array with numeric indices and missing keys' => array(
# | 0 | 1 | 2 | | 0 | 1 |
# ---+-----+-----+-----+ ---+-----+-----+
# 0 | 111 | 222 | - | 0 | 111 | 333 |
# ---+-----+-----+-----+ ==> ---+-----+-----+
# 1 | 333 | - | 444 | 1 | 222 | - |
# ---+-----+-----+-----+ ---+-----+-----+
# 2 | - | 444 |
# ---+-----+-----+
array( array( 111, 222 ), array( 333, 2 => 444 ) ),
array( array ( 111, 333 ), array( 222 ), array( 1 => 444 ) )
),
'Bidimensional associative array' => array(
array( 'a' => array( 'k1' => 1, 'k2' => 2 ),'b' => array( 'k1' => 3,'k2' => 4) ),
array( 'k1' => array( 'a' => 1, 'b' => 3 ), 'k2' => array( 'a' => 2, 'b' => 4) )
),
'Bidimensional array with arrays as data' => array(
array(
'a' => array( 'k1' => array( 1, 2, 3 ), 'k2' => 2),
'b' => array( 'k1' => array( 4, 5, 6 ), 'k2' => 4)
),
array(
'k1' => array( 'a' => array( 1, 2, 3 ), 'b' => array( 4, 5, 6 ) ),
'k2' => array( 'a' => 2, 'b' => 4 )
)
),
);
}
/**
* Provides a series of test cases that should fail transposition.
*
* Test case structure:
* <case> => array( <test matrix> )
*
* helper_array_transpose() is expected to fail for each case .
* Note: we don't need to test non-array types as these would throw a
* TypeError exception or an E_RECOVERABLE_ERROR (depending on PHP version).
*
* @return array List of test cases
*/
public function providerArrayTransposeInvalid() {
return array(
'Simple array' => array(
array( 1, 2 )
),
# 1st element array, 2nd scalar
'Mixed, "non-square" array' => array(
array( 'a' => array( 'k1' => 1, 'k2' => 2 ), 'b' => 123 )
),
);
}
}