2017-08-16 21:29:05 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2021-03-10 00:36:56 +08:00
|
|
|
use App\Repositories\KvpRepository;
|
2020-02-12 01:32:41 +08:00
|
|
|
use App\Support\ICAO;
|
2022-03-14 23:45:18 +08:00
|
|
|
use App\Support\Units\Distance;
|
2020-02-12 01:32:41 +08:00
|
|
|
use App\Support\Units\Time;
|
|
|
|
use App\Support\Utils;
|
|
|
|
use Carbon\Carbon;
|
2017-08-16 21:29:05 +08:00
|
|
|
|
|
|
|
class UtilsTest extends TestCase
|
|
|
|
{
|
2018-03-18 11:20:08 +08:00
|
|
|
public function testDates()
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
$carbon = new Carbon('2018-04-28T12:55:40Z');
|
2018-05-02 09:58:05 +08:00
|
|
|
$this->assertNotNull($carbon);
|
2018-03-18 11:20:08 +08:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
/**
|
|
|
|
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue
|
|
|
|
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName
|
|
|
|
*/
|
|
|
|
public function testUnitRounding()
|
|
|
|
{
|
|
|
|
$this->updateSetting('units.distance', 'km');
|
|
|
|
|
|
|
|
$alt = new Distance(1065.3456, 'nmi');
|
|
|
|
|
|
|
|
$km = $alt->toUnit('km');
|
|
|
|
$this->assertEquals(1973.0200512, $km);
|
|
|
|
|
|
|
|
$km = $alt->toUnit('km', 2);
|
|
|
|
$this->assertEquals(1973.02, $km);
|
|
|
|
|
|
|
|
$km = $alt->toUnit('km', 0);
|
|
|
|
$this->assertEquals(1973, $km);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test local conversions
|
|
|
|
*/
|
|
|
|
|
|
|
|
$km = $alt->local();
|
|
|
|
$this->assertEquals(1973.0200512, $km);
|
|
|
|
|
|
|
|
$km = $alt->local(0);
|
|
|
|
$this->assertEquals(1973, $km);
|
|
|
|
|
|
|
|
$km = $alt->local(2);
|
|
|
|
$this->assertEquals(1973.02, $km);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal units, shouldn't do a conversion
|
|
|
|
*/
|
|
|
|
$int = $alt->internal();
|
|
|
|
$this->assertEquals(1065.3456, $int);
|
|
|
|
|
|
|
|
$int = $alt->internal(2);
|
|
|
|
$this->assertEquals(1065.35, $int);
|
|
|
|
|
|
|
|
$int = $alt->internal(0);
|
|
|
|
$this->assertEquals(1065, $int);
|
|
|
|
}
|
|
|
|
|
2021-03-10 00:36:56 +08:00
|
|
|
/**
|
|
|
|
* Simple test for KVP
|
|
|
|
*/
|
|
|
|
public function testKvp()
|
|
|
|
{
|
|
|
|
/** @var KvpRepository $kvpRepo */
|
|
|
|
$kvpRepo = app(KvpRepository::class);
|
|
|
|
$kvpRepo->save('testkey', 'some value');
|
|
|
|
$this->assertEquals('some value', $kvpRepo->get('testkey'));
|
|
|
|
|
|
|
|
// test that default value is working
|
|
|
|
$this->assertEquals('default value', $kvpRepo->get('unknownkey', 'default value'));
|
|
|
|
|
|
|
|
// try saving an integer
|
|
|
|
$kvpRepo->save('intval', 1);
|
|
|
|
$this->assertEquals(1, $kvpRepo->get('intval'));
|
|
|
|
}
|
|
|
|
|
2018-08-27 02:50:08 +08:00
|
|
|
/**
|
2020-05-23 23:43:29 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 02:50:08 +08:00
|
|
|
*/
|
2017-12-04 04:34:32 +08:00
|
|
|
public function testSecondsToTimeParts()
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeParts(3600);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals(['h' => 1, 'm' => 0, 's' => 0], $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeParts(3720);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals(['h' => 1, 'm' => 2, 's' => 0], $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeParts(3722);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals(['h' => 1, 'm' => 2, 's' => 2], $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeParts(60);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals(['h' => 0, 'm' => 1, 's' => 0], $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeParts(62);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals(['h' => 0, 'm' => 1, 's' => 2], $t);
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2017-12-24 01:17:16 +08:00
|
|
|
public function testSecondsToTime()
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeString(3600);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals('1h 0m', $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeString(3720);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals('1h 2m', $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeString(3722);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals('1h 2m', $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::secondsToTimeString(3722, true);
|
2017-12-04 04:34:32 +08:00
|
|
|
$this->assertEquals('1h 2m 2s', $t);
|
2017-08-16 21:29:05 +08:00
|
|
|
}
|
2017-12-24 01:17:16 +08:00
|
|
|
|
|
|
|
public function testMinutesToTime()
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::minutesToTimeParts(65);
|
2017-12-24 01:17:16 +08:00
|
|
|
$this->assertEquals(['h' => 1, 'm' => 5], $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::minutesToTimeString(65);
|
2017-12-24 01:17:16 +08:00
|
|
|
$this->assertEquals('1h 5m', $t);
|
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$t = Time::minutesToTimeString(43200);
|
2017-12-24 01:17:16 +08:00
|
|
|
$this->assertEquals('720h 0m', $t);
|
|
|
|
}
|
2017-12-31 03:41:34 +08:00
|
|
|
|
|
|
|
public function testApiKey()
|
|
|
|
{
|
|
|
|
$api_key = Utils::generateApiKey();
|
|
|
|
$this->assertNotNull($api_key);
|
|
|
|
}
|
2018-02-13 10:58:23 +08:00
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2018-02-13 10:58:23 +08:00
|
|
|
public function testHexCode()
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
$hex_code = ICAO::createHexCode();
|
2018-02-13 10:58:23 +08:00
|
|
|
$this->assertNotNull($hex_code);
|
|
|
|
}
|
2020-01-29 01:42:46 +08:00
|
|
|
|
|
|
|
public function testGetDomain()
|
|
|
|
{
|
|
|
|
$tests = [
|
|
|
|
'http://phpvms.net',
|
|
|
|
'https://phpvms.net',
|
2020-08-16 01:50:58 +08:00
|
|
|
'https://phpvms.net/',
|
2020-01-29 01:42:46 +08:00
|
|
|
'phpvms.net',
|
|
|
|
'https://phpvms.net/index.php',
|
|
|
|
'https://demo.phpvms.net',
|
|
|
|
'https://demo.phpvms.net/file/index.php',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($tests as $case) {
|
2020-02-12 01:32:41 +08:00
|
|
|
$this->assertEquals('phpvms.net', Utils::getRootDomain($case));
|
2020-01-29 01:42:46 +08:00
|
|
|
}
|
|
|
|
|
2020-03-29 07:07:46 +08:00
|
|
|
$this->assertEquals('phpvms.co.uk', Utils::getRootDomain('http://phpvms.co.uk'));
|
|
|
|
$this->assertEquals('phpvms.co.uk', Utils::getRootDomain('http://www.phpvms.co.uk'));
|
2020-08-16 01:54:01 +08:00
|
|
|
$this->assertEquals('127.0.0.1', Utils::getRootDomain('http://127.0.0.1'));
|
2021-02-14 04:59:19 +08:00
|
|
|
$this->assertEquals('localhost', Utils::getRootDomain('http://localhost'));
|
2020-01-29 01:42:46 +08:00
|
|
|
}
|
2017-08-16 21:29:05 +08:00
|
|
|
}
|