-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathOptionsTest.php
More file actions
274 lines (256 loc) · 9.51 KB
/
OptionsTest.php
File metadata and controls
274 lines (256 loc) · 9.51 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
// load functions
require_once "src/card.php";
final class OptionsTest extends TestCase
{
private $defaultTheme = [
"background" => "#FFFEFE",
"border" => "#E4E2E2",
"stroke" => "#E4E2E2",
"ring" => "#FB8C00",
"fire" => "#FB8C00",
"currStreakNum" => "#151515",
"sideNums" => "#151515",
"currStreakLabel" => "#FB8C00",
"sideLabels" => "#151515",
"dates" => "#464646",
"excludeDaysLabel" => "#464646",
];
/**
* Test theme request parameters return colors for theme
*/
public function testThemes(): void
{
// check that getRequestedTheme returns correct colors for each theme
$themes = include "src/themes.php";
foreach ($themes as $theme => $colors) {
$actualColors = getRequestedTheme(["theme" => $theme]);
$expectedColors = $colors;
if (strpos($colors["background"], ",") !== false) {
$expectedColors["background"] = "url(#gradient)";
// check that the background gradient is correct
$this->assertStringContainsString("<linearGradient", $actualColors["backgroundGradient"]);
}
unset($expectedColors["backgroundGradient"]);
unset($actualColors["backgroundGradient"]);
$this->assertEquals($expectedColors, $actualColors);
}
}
/**
* Test that all themes appear in the documentation (docs/themes.md)
*/
public function testThemesInDocumentation(): void
{
$themes = include "src/themes.php";
$docContent = file_get_contents("docs/themes.md");
foreach (array_keys($themes) as $theme) {
$this->assertStringContainsString(
"`$theme`",
$docContent,
"The theme '$theme' is missing from the documentation (docs/themes.md).",
);
}
}
/**
* Test fallback to default theme
*/
public function testFallbackToDefaultTheme(): void
{
// check that getRequestedTheme returns default for invalid theme
// request parameters
$params = ["theme" => "not a theme name"];
// test that invalid theme name gives default values
$actual = getRequestedTheme($params);
$expected = $this->defaultTheme;
$expected["backgroundGradient"] = "";
$this->assertEquals($expected, $actual);
}
/**
* Check that all themes have valid values for all parameters
*/
public function testThemesHaveValidParameters(): void
{
// check that all themes contain all parameters and have valid values
$themes = include "src/themes.php";
$hexPartialRegex = "(?:[A-F0-9]{3}|[A-F0-9]{4}|[A-F0-9]{6}|[A-F0-9]{8})";
$hexRegex = "/^#{$hexPartialRegex}$/";
$backgroundRegex = "/^#{$hexPartialRegex}|-?\d+(?:,{$hexPartialRegex})+$/";
foreach ($themes as $theme => $colors) {
// check that there are no extra keys in the theme
$this->assertEquals(
array_diff_key($colors, $this->defaultTheme),
[],
"The theme '$theme' contains invalid parameters.",
);
# check that no parameters are missing and all values are valid
foreach (array_keys($this->defaultTheme) as $param) {
// check that the key exists
$this->assertArrayHasKey($param, $colors, "The theme '$theme' is missing the key '$param'.");
if ($param === "background") {
// check that the key is a valid background value
$this->assertMatchesRegularExpression(
$backgroundRegex,
$colors[$param],
"The parameter '$param' of '$theme' is not a valid background value.",
);
continue;
}
// check that the key is a valid hex color
$this->assertMatchesRegularExpression(
$hexRegex,
strtoupper($colors[$param]),
"The parameter '$param' of '$theme' is not a valid hex color.",
);
// check that the key is a valid hex color in uppercase
$this->assertMatchesRegularExpression(
$hexRegex,
$colors[$param],
"The parameter '$param' of '$theme' should not contain lowercase letters.",
);
}
}
}
/**
* Test parameters to override specific color
*/
public function testColorOverrideParameters(): void
{
// clear request parameters
$params = [];
// set default expected value
$expected = $this->defaultTheme;
foreach (array_keys($this->defaultTheme) as $param) {
// set request parameter
$params[$param] = "f00";
// update parameter in expected result
$expected = array_merge($expected, [$param => "#f00"]);
// test color change
$actual = getRequestedTheme($params);
$expected["backgroundGradient"] = "";
$this->assertEquals($expected, $actual);
}
}
/**
* Test color override parameters - all valid color inputs
*/
public function testValidColorInputs(): void
{
// valid color inputs and what the output color will be
$validInputTypes = [
"f00" => "#f00",
"f00f" => "#f00f",
"ff0000" => "#ff0000",
"FF0000" => "#ff0000",
"ff0000ff" => "#ff0000ff",
"red" => "red",
];
// set default expected value
$expected = $this->defaultTheme;
foreach ($validInputTypes as $input => $output) {
// set request parameter
$params = ["background" => $input];
// update parameter in expected result
$expected = array_merge($expected, ["background" => $output]);
// test color change
$actual = getRequestedTheme($params);
$expected["backgroundGradient"] = "";
$this->assertEquals($expected, $actual);
}
}
/**
* Test color override parameters - invalid color inputs
*/
public function testInvalidColorInputs(): void
{
// invalid color inputs
$invalidInputTypes = [
"g00", # not 0-9, A-F
"f00f0", # invalid number of characters
"fakecolor", # invalid color name
];
foreach ($invalidInputTypes as $input) {
// set request parameter
$params = ["background" => $input];
// test that theme is still default
$actual = getRequestedTheme($params);
$expected = $this->defaultTheme;
$expected["backgroundGradient"] = "";
$this->assertEquals($expected, $actual);
}
}
/**
* Test hide_border parameter
*/
public function testHideBorder(): void
{
// check that getRequestedTheme returns transparent border when hide_border is true
$params = ["hide_border" => "true"];
$theme = getRequestedTheme($params);
$this->assertEquals("#0000", $theme["border"]);
// check that getRequestedTheme returns solid border when hide_border is not true
$params = ["hide_border" => "false"];
$theme = getRequestedTheme($params);
$this->assertEquals($this->defaultTheme["border"], $theme["border"]);
}
/**
* Test date formatter for same year
*/
public function testDateFormatSameYear(): void
{
$year = date("Y");
$formatted = formatDate("$year-04-12", "M j[, Y]", "en");
$this->assertEquals("Apr 12", $formatted);
}
/**
* Test date formatter for different year
*/
public function testDateFormatDifferentYear(): void
{
$formatted = formatDate("2000-04-12", "M j[, Y]", "en");
$this->assertEquals("Apr 12, 2000", $formatted);
}
/**
* Test date formatter no brackets different year
*/
public function testDateFormatNoBracketsDiffYear(): void
{
$formatted = formatDate("2000-04-12", "Y/m/d", "en");
$this->assertEquals("2000/04/12", $formatted);
}
/**
* Test date formatter no brackets same year
*/
public function testDateFormatNoBracketsSameYear(): void
{
$year = date("Y");
$formatted = formatDate("$year-04-12", "Y/m/d", "en");
$this->assertEquals("$year/04/12", $formatted);
}
/**
* Test normalizing theme name
*/
public function testNormalizeThemeName(): void
{
$this->assertEquals("mytheme", normalizeThemeName("myTheme"));
$this->assertEquals("my-theme", normalizeThemeName("My_Theme"));
$this->assertEquals("my-theme", normalizeThemeName("my_theme"));
$this->assertEquals("my-theme", normalizeThemeName("my-theme"));
}
/**
* Test all theme names are normalized
*/
public function testAllThemeNamesNormalized(): void
{
$themes = include "src/themes.php";
foreach (array_keys($themes) as $theme) {
$normalized = normalizeThemeName($theme);
$this->assertEquals(
$theme,
$normalized,
"Theme name '$theme' is not normalized. It should contain only lowercase letters, numbers, and dashes. Consider renaming it to '$normalized'.",
);
}
}
}