-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathMachineTest.php
113 lines (85 loc) · 3 KB
/
MachineTest.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
<?php
use Scientist\Result;
use Scientist\Machine;
class MachineTest extends \PHPUnit\Framework\TestCase
{
public function test_that_machine_can_be_created()
{
$m = new Machine(function () {});
$this->assertInstanceOf(Machine::class, $m);
}
public function test_that_machine_can_receive_parameters()
{
$m = new Machine(function () {}, [1, 2, 3]);
$this->assertInstanceOf(Machine::class, $m);
}
public function test_that_machine_can_receive_mutable_state()
{
$m = new Machine(function () {}, [1, 2, 3], true);
$this->assertInstanceOf(Machine::class, $m);
}
public function test_that_machine_can_produce_a_result()
{
$m = new Machine(function () {});
$this->assertInstanceOf(Result::class, $m->execute());
}
public function test_that_machine_determines_callback_result_value()
{
$m = new Machine(function () { return 'foo'; });
$this->assertEquals('foo', $m->execute()->getValue());
}
public function test_that_machine_sets_context_result()
{
$context = ['foo' => 'bar'];
$m = new Machine(function () { return 'foo'; }, [], true, $context);
$this->assertSame($context, $m->execute()->getContext());
}
public function test_that_machine_executes_callback_with_parameters()
{
$m = new Machine(function ($one, $two, $three) {
return $one + $two + $three;
}, [1, 2, 3], true);
$this->assertEquals(6, $m->execute()->getValue());
}
public function test_that_exceptions_can_be_thrown_by_machine_callback_execution()
{
$m = new Machine(function () { throw new Exception('foo'); });
$this->expectException(Exception::class);
$m->execute();
}
public static function getErrorData()
{
return [
[new Exception()],
[new Error()],
];
}
/**
* @dataProvider getErrorData
*/
public function test_that_machine_can_mute_exceptions_from_callback($exception)
{
$m = new Machine(function () use ($exception) { throw $exception; }, [], true);
$this->assertEquals(null, $m->execute()->getValue());
}
public function test_that_machine_can_determine_start_and_end_times_for_callbacks()
{
$m = new Machine(function () {});
$s = microtime(true) - 60;
$r = $m->execute();
$e = microtime(true) + 60;
$this->assertIsFloat($r->getStartTime());
$this->assertIsFloat($r->getEndTime());
$this->assertGreaterThan($s, $r->getStartTime());
$this->assertGreaterThan($s, $r->getEndTime());
$this->assertLessThan($e, $r->getStartTime());
$this->assertLessThan($e, $r->getEndTime());
}
public function test_that_machine_can_determine_memory_usage_changes()
{
$m = new Machine(function () {});
$r = $m->execute();
$this->assertIsInt($r->getStartMemory());
$this->assertIsInt($r->getEndMemory());
}
}