LGV_TZ_Lookup 1.4.2
Time zone lookup for PHP applications and HTTP clients
Loading...
Searching...
No Matches
server.php
Go to the documentation of this file.
1<?php
2/**
3 © Copyright 2023-2026, Little Green Viper Software Development LLC.
4 MIT License; see LICENSE.
5 \file
6 \brief Verify the built-in server test report and performance summaries without a database server.
7*/
8declare(strict_types=1);
9require dirname(__DIR__).'/src/LGV_TZ_Lookup_Test.php';
10require dirname(__DIR__).'/src/TestLocations.php';
12foreach ($test_locations_param_array as $case) {
13 $expectedLookups[$case['params']['lng'].','.$case['params']['lat']] = $case['result'];
14}
15$forceFailure = in_array('--failure', $argv, true);
18
19/** \brief Exercise the report with known lookup results, including one optional failure. */
20function call_server(string $query, bool $cli): string {
22 parse_str($query, $arguments);
23 if (($arguments['secret'] ?? '') !== 'fixture-secret') { throw new RuntimeException('Test requests lost their configured secret.'); }
24 ++$calls;
25 return $forceFailure && $calls === 1 ? 'Wrong/Zone' : $expectedLookups[$arguments['ll']];
26}
27
28/** \brief Assert one report property. */
29function reportCheck(bool $value, string $label): void {
30 global $checks;
31 ++$checks;
32 if (!$value) { throw new RuntimeException($label); }
33}
34
35$summary = test_server_performance(range(1000000, 20000000, 1000000), 1000000000, 8 * 1048576);
36foreach (['Lookups measured: 20', 'Total test time: 1.000 s', 'Average lookup: 10.500 ms',
37 'Median lookup: 10.500 ms', '95th-percentile lookup: 19.000 ms', 'Slowest lookup: 20.000 ms',
38 'PHP request peak memory: 8.00 MiB'] as $text) {
39 reportCheck(str_contains($summary, $text), 'Incorrect performance statistic: '.$text);
40}
41reportCheck(test_server_performance([], 0, 0) === '', 'Empty report has no statistics.');
42reportCheck(str_contains(test_server_performance([3000000, 1000000, 2000000], 0, 0), 'Median lookup: 2.000 ms'), 'Odd sample median.');
43$config = tempnam(sys_get_temp_dir(), 'lgv-tz-report-');
44file_put_contents($config, '<?php $g_server_secret = "fixture-secret";');
45define('__CONFIG_FILE_', $config);
46try {
47 $html = test_server();
48 reportCheck($calls === 200, 'All locations are measured.');
49 reportCheck(str_contains($html, $forceFailure ? '1 Test Failures!' : 'All Tests (200) Passed!'), 'Location results are retained.');
50 reportCheck(substr_count($html, 'Lookup time: ') === 200, 'Each location reports elapsed time.');
51 reportCheck(str_contains($html, 'Lookups measured: 200'), 'Summary measures every result, including failures.');
52 reportCheck(preg_match('/Total test time: [0-9]+\.[0-9]{3} s/', $html) === 1, 'Total time is reported in seconds.');
53 reportCheck(str_contains($html, 'PHP request peak memory:'), 'Memory is reported.');
54 reportCheck(!str_contains($html, 'fixture-secret'), 'Report does not disclose the secret.');
55 echo 'Passed '.$checks.' server report checks'.($forceFailure ? ' with a failing location' : '').".\n";
56} finally { unlink($config); }
test_server()
test_server_performance(array $nanoseconds, int|float $elapsed, int $peakMemory)
Summarize request lookup timings and the PHP request's peak memory.
$test_locations_param_array
This file has a list of long/lat pairs, along with the expected results, when presented to the server...
if(!is_file($autoload)) $checks
Definition composer.php:11
$calls
Definition server.php:16
$config
Definition server.php:43
$expectedLookups
Definition server.php:11
reportCheck(bool $value, string $label)
Assert one report property.
Definition server.php:29
$summary
Definition server.php:35
foreach($test_locations_param_array as $case) $forceFailure
Definition server.php:15
call_server( $inQuery, $inIsCLI)
Definition index.php:71