Skip to content

Commit c4bbd7a

Browse files
committed
Add more tests
1 parent a767edd commit c4bbd7a

File tree

3 files changed

+204
-10
lines changed

3 files changed

+204
-10
lines changed

src/Helpers.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ public static function getExtIniInfo(string $extension): array {
9090
/**
9191
* Return key(s) as JSON.
9292
*
93-
* This method exists mainly as a helper for tests.
94-
*
9593
* @param array<int, string>|string|null $keys
9694
*
9795
* @throws JsonException
@@ -160,10 +158,7 @@ public static function import(callable $exists, callable $store): void {
160158
/**
161159
* @param array<int, mixed> $keys
162160
*/
163-
public static function export(array $keys, string $filename, callable $value): void {
164-
header('Content-disposition: attachment; filename='.$filename.'.json');
165-
header('Content-Type: application/json');
166-
161+
public static function export(array $keys, string $filename, callable $value, bool $tests = false): ?string {
167162
$json = [];
168163

169164
foreach ($keys as $key) {
@@ -177,11 +172,18 @@ public static function export(array $keys, string $filename, callable $value): v
177172
}
178173

179174
try {
180-
echo json_encode($json, JSON_THROW_ON_ERROR);
175+
$output = json_encode($json, JSON_THROW_ON_ERROR);
181176
} catch (JsonException $e) {
182-
echo $e->getMessage();
177+
$output = $e->getMessage();
183178
}
184179

180+
if ($tests) {
181+
return $output;
182+
}
183+
184+
header('Content-disposition: attachment; filename='.$filename.'.json');
185+
header('Content-Type: application/json');
186+
echo $output;
185187
exit;
186188
}
187189

tests/ConfigTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ public function testGetter(): void {
2323
/**
2424
* @throws JsonException
2525
*/
26-
public function testGetterWithEnv(): void {
26+
public function testEnvGetter(): void {
2727
putenv('PCA_TESTENV-ARRAY='.json_encode(['item1' => 'value1', 'item2' => 'value2'], JSON_THROW_ON_ERROR));
2828
$this->assertSame('value1', Config::get('testenv-array', [])['item1']);
29+
}
2930

31+
public function testEnvInt(): void {
3032
putenv('PCA_TESTENV-INT=10');
31-
$this->assertSame(10, (int) Config::get('testenv-int', 2));
3233

34+
$this->assertSame(10, Config::get('testenv-int', 2));
35+
}
36+
37+
public function testEnvArray(): void {
3338
putenv('PCA_TESTENV-JSON={"local_cert":"path/to/redis.crt","local_pk":"path/to/redis.key","cafile":"path/to/ca.crt","verify_peer_name":false}');
3439
$this->assertEqualsCanonicalizing([
3540
'local_cert' => 'path/to/redis.crt',
@@ -38,4 +43,13 @@ public function testGetterWithEnv(): void {
3843
'verify_peer_name' => false,
3944
], Config::get('testenv-json', []));
4045
}
46+
47+
public function testEnvOverride(): void {
48+
// default in config
49+
$this->assertSame('d. m. Y H:i:s', Config::get('time-format', ''));
50+
51+
putenv('PCA_TIME-FORMAT=d. m. Y');
52+
53+
$this->assertSame('d. m. Y', Config::get('time-format', ''));
54+
}
4155
}

tests/HelpersTest.php

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/**
3+
* This file is part of the phpCacheAdmin.
4+
* Copyright (c) Róbert Kelčák (https://kelcak.com/)
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Tests;
10+
11+
use JsonException;
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\TestCase;
14+
use RobiNN\Pca\Helpers;
15+
use RobiNN\Pca\Http;
16+
use RobiNN\Pca\Template;
17+
18+
final class HelpersTest extends TestCase {
19+
private Template $template;
20+
21+
protected function setUp(): void {
22+
$this->template = new Template();
23+
}
24+
25+
public function testConvertTypesToString(): void {
26+
$input = [
27+
'string' => 'value',
28+
'bool_true' => true,
29+
'bool_false' => false,
30+
'null_value' => null,
31+
'empty_string' => '',
32+
'nested_array' => [
33+
'inner_bool' => true,
34+
'inner_null' => null,
35+
'inner_empty' => '',
36+
],
37+
'integer' => 123,
38+
];
39+
40+
$expected = [
41+
'string' => 'value',
42+
'bool_true' => 'true',
43+
'bool_false' => 'false',
44+
'null_value' => 'null',
45+
'empty_string' => 'empty',
46+
'nested_array' => [
47+
'inner_bool' => 'true',
48+
'inner_null' => 'null',
49+
'inner_empty' => 'empty',
50+
],
51+
'integer' => 123,
52+
];
53+
54+
$this->assertSame($expected, Helpers::convertTypesToString($input));
55+
}
56+
57+
public function testMixedToStringWithDifferentTypes(): void {
58+
$this->assertSame('Hello, world!', Helpers::mixedToString('Hello, world!'));
59+
$this->assertSame('123', Helpers::mixedToString(123));
60+
$this->assertSame('1', Helpers::mixedToString(true));
61+
$this->assertSame('', Helpers::mixedToString(false));
62+
$this->assertSame('', Helpers::mixedToString(null));
63+
64+
$array = ['a' => 1, 'b' => 2];
65+
$this->assertSame(serialize($array), Helpers::mixedToString($array));
66+
67+
$object = (object) ['name' => 'John', 'age' => 30];
68+
$this->assertSame(serialize($object), Helpers::mixedToString($object));
69+
70+
$resource = fopen('php://memory', 'rb');
71+
$this->assertSame((string) $resource, Helpers::mixedToString($resource));
72+
fclose($resource);
73+
}
74+
75+
/**
76+
* @throws JsonException
77+
*/
78+
public function testImport(): void {
79+
$_FILES['import'] = [
80+
'name' => 'test.json',
81+
'type' => 'application/json',
82+
'tmp_name' => __DIR__.'/test.json',
83+
'error' => 0,
84+
'size' => 1234,
85+
];
86+
87+
file_put_contents($_FILES['import']['tmp_name'], json_encode([
88+
['key' => 'testkey1', 'value' => 'value1', 'ttl' => 3600],
89+
['key' => 'testkey2', 'value' => 'value2', 'ttl' => 3600],
90+
], JSON_THROW_ON_ERROR));
91+
92+
$stored = [];
93+
94+
Http::stopRedirect();
95+
96+
Helpers::import(
97+
static fn ($key) => false,
98+
static function (string $key, string $value, int $ttl) use (&$stored) {
99+
$stored[] = ['key' => $key, 'value' => $value, 'ttl' => $ttl];
100+
}
101+
);
102+
103+
unlink($_FILES['import']['tmp_name']);
104+
105+
$this->assertEquals('testkey1', $stored[0]['key']);
106+
$this->assertEquals('value1', $stored[0]['value']);
107+
$this->assertEquals(3600, $stored[0]['ttl']);
108+
}
109+
110+
/**
111+
* @throws JsonException
112+
*/
113+
public function testExport(): void {
114+
$keys = [
115+
['key' => 'testkey1', 'items' => ['ttl' => 3600]],
116+
['key' => 'testkey2', 'items' => ['ttl' => -1]],
117+
];
118+
119+
$output = Helpers::export($keys, 'export_test', static fn ($key) => $key, true);
120+
121+
$expected = json_encode([
122+
['key' => 'testkey1', 'ttl' => 3600, 'value' => 'testkey1'],
123+
['key' => 'testkey2', 'ttl' => 0, 'value' => 'testkey2'],
124+
], JSON_THROW_ON_ERROR);
125+
126+
$this->assertJsonStringEqualsJsonString($expected, $output);
127+
}
128+
129+
/**
130+
* @param array<int, array<string, mixed>> $keys
131+
* @param array<int, array<string, mixed>> $expected
132+
*/
133+
#[DataProvider('sortKeysProvider')]
134+
public function testSortKeys(string $sortdir, string $sortcol, array $keys, array $expected): void {
135+
$_GET['sortdir'] = $sortdir;
136+
$_GET['sortcol'] = $sortcol;
137+
138+
$this->assertSame($expected, Helpers::sortKeys($this->template, $keys));
139+
}
140+
141+
/**
142+
* @return array<string, array<string, mixed>>
143+
*/
144+
public static function sortKeysProvider(): array {
145+
return [
146+
'no sorting' => [
147+
'sortdir' => 'none',
148+
'sortcol' => 'column1',
149+
'keys' => [['items' => ['column1' => 'value1']], ['items' => ['column1' => 'value2']], ['items' => ['column1' => 'value3']],],
150+
'expected' => [['items' => ['column1' => 'value1']], ['items' => ['column1' => 'value2']], ['items' => ['column1' => 'value3']],],
151+
],
152+
'ascending sort' => [
153+
'sortdir' => 'asc',
154+
'sortcol' => 'column1',
155+
'keys' => [['items' => ['column1' => 'value3']], ['items' => ['column1' => 'value1']], ['items' => ['column1' => 'value2']],],
156+
'expected' => [['items' => ['column1' => 'value1']], ['items' => ['column1' => 'value2']], ['items' => ['column1' => 'value3']],],
157+
],
158+
'descending sort' => [
159+
'sortdir' => 'desc',
160+
'sortcol' => 'column1',
161+
'keys' => [['items' => ['column1' => 'value1']], ['items' => ['column1' => 'value2']], ['items' => ['column1' => 'value3']],],
162+
'expected' => [['items' => ['column1' => 'value3']], ['items' => ['column1' => 'value2']], ['items' => ['column1' => 'value1']],],
163+
],
164+
'ascending sort with integers' => [
165+
'sortdir' => 'asc',
166+
'sortcol' => 'column1',
167+
'keys' => [['items' => ['column1' => 3]], ['items' => ['column1' => 1]], ['items' => ['column1' => 2]],],
168+
'expected' => [['items' => ['column1' => 1]], ['items' => ['column1' => 2]], ['items' => ['column1' => 3]],],
169+
],
170+
'descending sort with integers' => [
171+
'sortdir' => 'desc',
172+
'sortcol' => 'column1',
173+
'keys' => [['items' => ['column1' => 1]], ['items' => ['column1' => 2]], ['items' => ['column1' => 3]],],
174+
'expected' => [['items' => ['column1' => 3]], ['items' => ['column1' => 2]], ['items' => ['column1' => 1]],],
175+
],
176+
];
177+
}
178+
}

0 commit comments

Comments
 (0)