-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_ordering.php
166 lines (134 loc) · 4.41 KB
/
test_ordering.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php namespace Matura\Tests;
use Matura\Test\User;
use Matura\Test\Group;
use Matura\Test\Spy;
use Matura\Runners\SuiteRunner;
use Matura\Core\ResultSet;
// Generates various test trees for us.
function gentree($spy, $max_depth, $describes, $methods = array())
{
$generate_test_block = function ($block_method, $path, $index, $spy) {
array_push($path, $block_method, $index);
$spy_method_name = implode(".", $path);
if ($block_method == 'it') {
call_user_func($block_method, $spy_method_name, array($spy, $spy_method_name));
} else {
call_user_func($block_method, array($spy, $spy_method_name));
}
};
$generate = function ($depth, $path) use (&$generate, $describes, $methods, &$generate_test_block, $spy, $max_depth) {
foreach ($methods as $block_method => $num) {
foreach (range(1, $num) as $index) {
$generate_test_block($block_method, $path, $index, $spy);
}
}
if ($depth < $max_depth) {
foreach (range(1, $describes) as $index) {
describe("describe_$index", function () use (&$generate, $depth, $path, $index) {
$generate($depth+1, array_merge($path, array("describe","$index")));
});
}
}
};
return suite('Root', function ($ctx) use ($generate) {
$generate(1, array());
});
}
describe('Ordering', function ($ctx) {
it('should invoke 1 test and its hooks in the correct order.', function ($ctx) {
$spy = new Spy();
$suite = gentree($spy, 1, 1, array(
'before' => 1,
'after' => 1,
'before_all' => 1,
'after_all' => 1,
'it' => 1
));
$suite_runner = new SuiteRunner($suite, new ResultSet());
$suite_runner->run();
expect($spy->invocations)->to->eql(array(
'before_all.1',
'before.1',
'it.1',
'after.1',
'after_all.1'
));
});
it('should invoke 2 tests and its hooks in the correct order.', function ($ctx) {
$spy = new Spy();
$suite = gentree($spy, 1, 1, array(
'before' => 1,
'after' => 1,
'before_all' => 1,
'after_all' => 1,
'it' => 2
));
$suite_runner = new SuiteRunner($suite, new ResultSet());
$suite_runner->run();
expect($spy->invocations)->to->eql(array(
'before_all.1',
'before.1',
'it.1',
'after.1',
'before.1',
'it.2',
'after.1',
'after_all.1'
));
});
it('should invoke nested describes and their hooks in the correct order.', function ($ctx) {
$spy = new Spy();
$suite = gentree($spy, 2, 2, array(
'before' => 1,
'after' => 1,
'before_all' => 1,
'after_all' => 1,
'it' => 2
));
$suite_runner = new SuiteRunner($suite, new ResultSet());
$suite_runner->run();
expect($spy->invocations)->to->eql(array(
// suite
'before_all.1',
// test
'before.1',
'it.1',
'after.1',
// test
'before.1',
'it.2',
'after.1',
// First describe
'describe.1.before_all.1',
// test
'before.1', // This might come as a surprise!
'describe.1.before.1',
'describe.1.it.1',
'describe.1.after.1',
'after.1',
// test
'before.1',
'describe.1.before.1',
'describe.1.it.2',
'describe.1.after.1',
'after.1',
'describe.1.after_all.1',
// Second describe
'describe.2.before_all.1',
// test
'before.1',
'describe.2.before.1',
'describe.2.it.1',
'describe.2.after.1',
'after.1',
// test
'before.1',
'describe.2.before.1',
'describe.2.it.2',
'describe.2.after.1',
'after.1',
'describe.2.after_all.1',
'after_all.1'
));
});
});