-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathtest-perflab-server-timing-metric.php
127 lines (98 loc) · 4.06 KB
/
test-perflab-server-timing-metric.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
<?php
/**
* Tests for server-timing/class-perflab-server-timing-metric.php
*
* @package performance-lab
*/
/**
* @group server-timing
*/
class Test_Perflab_Server_Timing_Metric extends WP_UnitTestCase {
/** @var Perflab_Server_Timing_Metric */
private $metric;
public function set_up(): void {
parent::set_up();
$this->metric = new Perflab_Server_Timing_Metric( 'test-metric' );
}
public function test_get_slug(): void {
$this->assertSame( 'test-metric', $this->metric->get_slug() );
}
public function test_set_value_with_integer(): void {
$this->metric->set_value( 123 );
$this->assertSame( 123, $this->metric->get_value() );
}
public function test_set_value_with_float(): void {
$this->metric->set_value( 123.4567 );
$this->assertSame( 123.4567, $this->metric->get_value() );
}
public function test_set_value_with_numeric_string(): void {
$this->metric->set_value( '123.4567' );
$this->assertSame( 123.4567, $this->metric->get_value() );
}
public function test_set_value_requires_integer_or_float_or_numeric_string(): void {
$this->setExpectedIncorrectUsage( Perflab_Server_Timing_Metric::class . '::set_value' );
$this->metric->set_value( 'not-a-number' );
$this->assertNull( $this->metric->get_value() );
}
public function test_set_value_prevents_late_measurement(): void {
$this->setExpectedIncorrectUsage( Perflab_Server_Timing_Metric::class . '::set_value' );
$this->metric->set_value( 2 );
remove_all_actions( 'perflab_server_timing_send_header' );
do_action( 'perflab_server_timing_send_header' );
$this->metric->set_value( 3 );
$this->assertSame( 2, $this->metric->get_value() );
}
public function test_set_start_value_with_integer(): void {
$this->metric->set_start_value( 123 );
$this->assertSame( 123, $this->metric->get_start_value() );
}
public function test_set_start_value_with_float(): void {
$this->metric->set_start_value( 123.4567 );
$this->assertSame( 123.4567, $this->metric->get_start_value() );
}
public function test_set_start_value_with_numeric_string(): void {
$this->metric->set_start_value( '123.4567' );
$this->assertSame( 123.4567, $this->metric->get_start_value() );
}
public function test_set_start_value_requires_integer_or_float_or_numeric_string(): void {
$this->setExpectedIncorrectUsage( Perflab_Server_Timing_Metric::class . '::set_start_value' );
$this->metric->set_start_value( 'not-a-number' );
$this->assertNull( $this->metric->get_start_value() );
}
public function test_set_start_value_prevents_late_measurement(): void {
$this->setExpectedIncorrectUsage( Perflab_Server_Timing_Metric::class . '::set_start_value' );
$this->metric->set_start_value( 2 );
remove_all_actions( 'perflab_server_timing_send_header' );
do_action( 'perflab_server_timing_send_header' );
$this->metric->set_start_value( 3 );
$this->assertSame( 2, $this->metric->get_start_value() );
}
public function test_get_value(): void {
$this->metric->set_value( 86.42 );
$this->assertSame( 86.42, $this->metric->get_value() );
}
public function test_get_start_value(): void {
$t = microtime( true ) * 1000.0;
$this->metric->set_start_value( $t );
$this->assertSame( $t, $this->metric->get_start_value() );
}
public function test_measure_start_and_end_correctly(): void {
$this->metric->measure_start();
sleep( 1 );
$this->metric->measure_end();
// Loose float comparison with 100ms delta, since measurement won't be exactly 1000ms.
$this->assertEqualsWithDelta( 1000.0, $this->metric->get_value(), 100.0 );
}
public function test_measure_end_without_start(): void {
$this->setExpectedIncorrectUsage( Perflab_Server_Timing_Metric::class . '::measure_end' );
$this->metric->measure_end();
$this->assertNull( $this->metric->get_value() );
}
public function test_measure_custom_start_and_end_correctly(): void {
$this->metric->set_start_value( microtime( true ) * 1000.0 );
sleep( 1 );
$this->metric->measure_end();
// Loose float comparison with 100ms delta, since measurement won't be exactly 1000ms.
$this->assertEqualsWithDelta( 1000.0, $this->metric->get_value(), 100.0 );
}
}