2017-07-19 18:07:36 -04:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-07-19 18:07:36 -04:00
|
|
|
declare(strict_types=1);
|
2017-12-21 22:47:19 -05:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Service;
|
2017-08-29 10:50:31 -04:00
|
|
|
|
2017-12-21 22:47:19 -05:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Conf;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\Service;
|
2017-07-19 18:07:36 -04:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
|
|
|
use Phake;
|
|
|
|
|
2017-07-20 18:36:03 -04:00
|
|
|
/** @covers \JKingWeb\Arsse\Service */
|
2017-12-21 22:47:19 -05:00
|
|
|
class TestService extends \JKingWeb\Arsse\Test\AbstractTest {
|
2017-07-19 18:07:36 -04:00
|
|
|
protected $srv;
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function setUp() {
|
2017-07-19 18:07:36 -04:00
|
|
|
$this->clearData();
|
2018-11-22 19:55:54 -05:00
|
|
|
self::setConf();
|
2017-07-19 18:07:36 -04:00
|
|
|
Arsse::$db = Phake::mock(Database::class);
|
|
|
|
$this->srv = new Service();
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testComputeInterval() {
|
2017-07-19 18:07:36 -04:00
|
|
|
$in = [
|
|
|
|
Arsse::$conf->serviceFrequency,
|
|
|
|
"PT2M",
|
|
|
|
"PT5M",
|
|
|
|
"P2M",
|
|
|
|
"5M",
|
|
|
|
"interval",
|
|
|
|
];
|
2017-08-29 10:50:31 -04:00
|
|
|
foreach ($in as $index => $spec) {
|
|
|
|
try {
|
|
|
|
$exp = new \DateInterval($spec);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$exp = new \DateInterval("PT2M");
|
|
|
|
}
|
2017-07-19 18:07:36 -04:00
|
|
|
Arsse::$conf->serviceFrequency = $spec;
|
|
|
|
$this->assertEquals($exp, Service::interval(), "Interval #$index '$spec' was not correctly calculated");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testCheckIn() {
|
2017-07-19 18:07:36 -04:00
|
|
|
$now = time();
|
|
|
|
$this->srv->checkIn();
|
|
|
|
Phake::verify(Arsse::$db)->metaSet("service_last_checkin", Phake::capture($then), "datetime");
|
|
|
|
$this->assertTime($now, $then);
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:50:31 -04:00
|
|
|
public function testReportHavingCheckedIn() {
|
2017-07-19 18:07:36 -04:00
|
|
|
// the mock's metaGet() returns null by default
|
|
|
|
$this->assertFalse(Service::hasCheckedIn());
|
|
|
|
$interval = Service::interval();
|
|
|
|
$valid = (new \DateTimeImmutable("now", new \DateTimezone("UTC")))->sub($interval);
|
|
|
|
$invalid = $valid->sub($interval)->sub($interval);
|
|
|
|
Phake::when(Arsse::$db)->metaGet("service_last_checkin")->thenReturn(Date::transform($valid, "sql"))->thenReturn(Date::transform($invalid, "sql"));
|
|
|
|
$this->assertTrue(Service::hasCheckedIn());
|
|
|
|
$this->assertFalse(Service::hasCheckedIn());
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|