Asserting the Output of Complex Arrays in PHPUnit/PHPStorm

In unit testing, have you ever had assert a really complex array that you didn’t really want to generate the whole expected array yourself? Such as:

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
143
144
array (
  0 => 
  array (
    0 => 'var 0-0',
    1 => 'var 0-1',
    2 => 'var 0-2',
    3 => 'var 0-3',
    4 => 'var 0-4',
    5 => 'var 0-5',
    6 => 'var 0-6',
    7 => 'var 0-7',
    8 => 'var 0-8',
    9 => 'var 0-9',
  ),
  1 => 
  array (
    0 => 'var 1-0',
    1 => 'var 1-1',
    2 => 'var 1-2',
    3 => 'var 1-3',
    4 => 'var 1-4',
    5 => 'var 1-5',
    6 => 'var 1-6',
    7 => 'var 1-7',
    8 => 'var 1-8',
    9 => 'var 1-9',
  ),
  2 => 
  array (
    0 => 'var 2-0',
    1 => 'var 2-1',
    2 => 'var 2-2',
    3 => 'var 2-3',
    4 => 'var 2-4',
    5 => 'var 2-5',
    6 => 'var 2-6',
    7 => 'var 2-7',
    8 => 'var 2-8',
    9 => 'var 2-9',
  ),
  3 => 
  array (
    0 => 'var 3-0',
    1 => 'var 3-1',
    2 => 'var 3-2',
    3 => 'var 3-3',
    4 => 'var 3-4',
    5 => 'var 3-5',
    6 => 'var 3-6',
    7 => 'var 3-7',
    8 => 'var 3-8',
    9 => 'var 3-9',
  ),
  4 => 
  array (
    0 => 'var 4-0',
    1 => 'var 4-1',
    2 => 'var 4-2',
    3 => 'var 4-3',
    4 => 'var 4-4',
    5 => 'var 4-5',
    6 => 'var 4-6',
    7 => 'var 4-7',
    8 => 'var 4-8',
    9 => 'var 4-9',
  ),
  5 => 
  array (
    0 => 'var 5-0',
    1 => 'var 5-1',
    2 => 'var 5-2',
    3 => 'var 5-3',
    4 => 'var 5-4',
    5 => 'var 5-5',
    6 => 'var 5-6',
    7 => 'var 5-7',
    8 => 'var 5-8',
    9 => 'var 5-9',
  ),
  6 => 
  array (
    0 => 'var 6-0',
    1 => 'var 6-1',
    2 => 'var 6-2',
    3 => 'var 6-3',
    4 => 'var 6-4',
    5 => 'var 6-5',
    6 => 'var 6-6',
    7 => 'var 6-7',
    8 => 'var 6-8',
    9 => 'var 6-9',
  ),
  7 => 
  array (
    0 => 'var 7-0',
    1 => 'var 7-1',
    2 => 'var 7-2',
    3 => 'var 7-3',
    4 => 'var 7-4',
    5 => 'var 7-5',
    6 => 'var 7-6',
    7 => 'var 7-7',
    8 => 'var 7-8',
    9 => 'var 7-9',
  ),
  8 => 
  array (
    0 => 'var 8-0',
    1 => 'var 8-1',
    2 => 'var 8-2',
    3 => 'var 8-3',
    4 => 'var 8-4',
    5 => 'var 8-5',
    6 => 'var 8-6',
    7 => 'var 8-7',
    8 => 'var 8-8',
    9 => 'var 8-9',
  ),
  9 => 
  array (
    0 => 'var 9-0',
    1 => 'var 9-1',
    2 => 'var 9-2',
    3 => 'var 9-3',
    4 => 'var 9-4',
    5 => 'var 9-5',
    6 => 'var 9-6',
    7 => 'var 9-7',
    8 => 'var 9-8',
    9 => 'var 9-9',
  ),
  10 => 
  array (
    0 => 'var 10-0',
    1 => 'var 10-1',
    2 => 'var 10-2',
    3 => 'var 10-3',
    4 => 'var 10-4',
    5 => 'var 10-5',
    6 => 'var 10-6',
    7 => 'var 10-7',
    8 => 'var 10-8',
    9 => 'var 10-9',
  )

(I don’t want to type that up by hand!)

Well, guess what? You can be lazy about it… Simply use var_export and php will generate all the code for you to straight copy and paste.

1
var_export($complexObj);

**DISCLAIMER WARNING** This is generally a VERY bad practice. You are always suppose to create the assertions independently from what the output generates. It will be very easy to just copy and paste a mistake in the output that is not correct. Be sure if you do this that you look very carefully at the output and make sure it is exactly what it is supposed to be.

Leave a Reply

Your email address will not be published. Required fields are marked *