-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemoryLimitTest.php
182 lines (160 loc) · 3.81 KB
/
MemoryLimitTest.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace PhpMemory;
use PhpMemory\Unit\Binary\Gibibyte;
use PhpMemory\Unit\Binary\Gigabyte;
use PhpMemory\Unit\Binary\Kibibyte;
use PhpMemory\Unit\Binary\Kilobyte;
use PhpMemory\Unit\Binary\Mebibyte;
use PhpMemory\Unit\Binary\Megabyte;
use PhpMemory\Unit\Byte;
use PHPUnit\Framework\TestCase;
final class MemoryLimitTest extends TestCase
{
/**
* @dataProvider get_with_strings_provider
*/
public function test_get_with_strings(string $limit, Size $expected)
{
ini_set(MemoryLimit::INI_OPTION, $limit);
$actual = MemoryLimit::get();
$this->assertNotNull($actual);
$this->assertEquals($expected->getValue(), $actual->getValue());
$this->assertEquals($expected->getUnit()->bytes(), $actual->getUnit()->bytes());
$this->assertEquals($expected->getBytes(), $actual->getBytes());
}
public function get_with_strings_provider()
{
return [
'integer string bytes' => [
'100',
Size::create(100, new Byte()),
],
'1K' => [
'1K',
Size::create(1, new Kilobyte()),
],
'1M' => [
'1M',
Size::create(1, new Megabyte()),
],
'1G' => [
'1G',
Size::create(1, new Gigabyte()),
],
];
}
public function test_get_no_limit()
{
ini_set(MemoryLimit::INI_OPTION, -1);
$actual = MemoryLimit::get();
$this->assertNull($actual);
}
public function test_get_positive_integer()
{
$expected = Size::create(100, new Byte());
ini_set(MemoryLimit::INI_OPTION, 100);
$actual = MemoryLimit::get();
$this->assertNotNull($actual);
$this->assertEquals($expected->getValue(), $actual->getValue());
$this->assertEquals($expected->getUnit()->bytes(), $actual->getUnit()->bytes());
$this->assertEquals($expected->getBytes(), $actual->getBytes());
}
public function test_get_negative_integer_throws()
{
ini_set(MemoryLimit::INI_OPTION, -10);
$this->expectException(InvalidArgumentException::class);
MemoryLimit::get();
}
public function test_get_invalid_unit_type_throws()
{
ini_set(MemoryLimit::INI_OPTION, '1MB');
$this->expectException(InvalidArgumentException::class);
MemoryLimit::get();
}
/**
* @dataProvider set_as_bytes_provider
*/
public function test_set_as_bytes(Size $expected)
{
MemoryLimit::set($expected);
$actual = MemoryLimit::get();
$this->assertNotNull($actual);
$this->assertEquals($expected->getBytes(), $actual->getBytes());
}
public function set_as_bytes_provider()
{
return [
'100 bytes' => [
Size::create(100, new Byte()),
],
'1 kibibyte' => [
Size::create(1, new Kibibyte()),
],
'1 kilobyte' => [
Size::create(1, new Kilobyte()),
],
'1 mebibyte' => [
Size::create(1, new Mebibyte()),
],
'1 megabyte' => [
Size::create(1, new Megabyte()),
],
'1 gibibyte' => [
Size::create(1, new Gibibyte()),
],
'1 gigabyte' => [
Size::create(1, new Gigabyte()),
],
];
}
/**
* @dataProvider set_as_string_provider
*/
public function test_set_as_string(Size $size, string $expected)
{
MemoryLimit::set($size, true);
$actual = ini_get(MemoryLimit::INI_OPTION);
$this->assertNotNull($actual);
$this->assertNotFalse($actual);
$this->assertEquals($expected, $actual);
}
public function set_as_string_provider()
{
return [
'100 bytes' => [
Size::create(100, new Byte()),
'100',
],
'1 kibibyte' => [
Size::create(1, new Kibibyte()),
'1K',
],
'1 kilobyte' => [
Size::create(1, new Kilobyte()),
'1K',
],
'1 mebibyte' => [
Size::create(1, new Mebibyte()),
'1M',
],
'1 megabyte' => [
Size::create(1, new Megabyte()),
'1M',
],
'1 gibibyte' => [
Size::create(1, new Gibibyte()),
'1G',
],
'1 gigabyte' => [
Size::create(1, new Gigabyte()),
'1G',
],
];
}
public function test_set_no_limit()
{
MemoryLimit::set(null);
$actual = MemoryLimit::get();
$this->assertNull($actual);
}
}