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\Database;
|
|
|
|
use JKingWeb\Arsse\Service;
|
2017-07-19 18:07:36 -04:00
|
|
|
use JKingWeb\Arsse\Misc\Date;
|
|
|
|
|
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;
|
|
|
|
|
2019-10-16 14:42:43 -04:00
|
|
|
public function setUp(): void {
|
2018-11-23 10:01:17 -05:00
|
|
|
self::clearData();
|
2018-11-22 19:55:54 -05:00
|
|
|
self::setConf();
|
2019-09-05 10:03:32 -04:00
|
|
|
Arsse::$db = \Phake::mock(Database::class);
|
2017-07-19 18:07:36 -04:00
|
|
|
$this->srv = new Service();
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public function testCheckIn(): void {
|
2017-07-19 18:07:36 -04:00
|
|
|
$now = time();
|
|
|
|
$this->srv->checkIn();
|
2019-09-05 10:03:32 -04:00
|
|
|
\Phake::verify(Arsse::$db)->metaSet("service_last_checkin", \Phake::capture($then), "datetime");
|
2017-07-19 18:07:36 -04:00
|
|
|
$this->assertTime($now, $then);
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public function testReportHavingCheckedIn(): void {
|
2017-07-19 18:07:36 -04:00
|
|
|
// the mock's metaGet() returns null by default
|
|
|
|
$this->assertFalse(Service::hasCheckedIn());
|
2019-01-20 22:40:49 -05:00
|
|
|
$interval = Arsse::$conf->serviceFrequency;
|
2017-07-19 18:07:36 -04:00
|
|
|
$valid = (new \DateTimeImmutable("now", new \DateTimezone("UTC")))->sub($interval);
|
|
|
|
$invalid = $valid->sub($interval)->sub($interval);
|
2019-09-05 10:03:32 -04:00
|
|
|
\Phake::when(Arsse::$db)->metaGet("service_last_checkin")->thenReturn(Date::transform($valid, "sql"))->thenReturn(Date::transform($invalid, "sql"));
|
2017-07-19 18:07:36 -04:00
|
|
|
$this->assertTrue(Service::hasCheckedIn());
|
|
|
|
$this->assertFalse(Service::hasCheckedIn());
|
|
|
|
}
|
2019-10-19 12:14:13 -04:00
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public function testPerformPreCleanup(): void {
|
2019-10-19 12:14:13 -04:00
|
|
|
$this->assertTrue(Service::cleanupPre());
|
|
|
|
\Phake::verify(Arsse::$db)->feedCleanup();
|
|
|
|
\Phake::verify(Arsse::$db)->sessionCleanup();
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public function testPerformShortPostCleanup(): void {
|
2019-10-19 12:14:13 -04:00
|
|
|
\Phake::when(Arsse::$db)->articleCleanup()->thenReturn(0);
|
|
|
|
$this->assertTrue(Service::cleanupPost());
|
|
|
|
\Phake::verify(Arsse::$db)->articleCleanup();
|
|
|
|
\Phake::verify(Arsse::$db, \Phake::times(0))->driverMaintenance();
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public function testPerformFullPostCleanup(): void {
|
2019-10-19 12:14:13 -04:00
|
|
|
\Phake::when(Arsse::$db)->articleCleanup()->thenReturn(1);
|
|
|
|
$this->assertTrue(Service::cleanupPost());
|
|
|
|
\Phake::verify(Arsse::$db)->articleCleanup();
|
|
|
|
\Phake::verify(Arsse::$db)->driverMaintenance();
|
|
|
|
}
|
2019-10-19 18:51:01 -04:00
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public function testRefreshFeeds(): void {
|
2019-10-19 18:51:01 -04:00
|
|
|
// set up mock database actions
|
|
|
|
\Phake::when(Arsse::$db)->metaSet->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->feedCleanup->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->sessionCleanup->thenReturn(true);
|
|
|
|
\Phake::when(Arsse::$db)->articleCleanup->thenReturn(0);
|
|
|
|
\Phake::when(Arsse::$db)->feedListStale->thenReturn([1,2,3]);
|
|
|
|
// perform the test
|
|
|
|
$d = \Phake::mock(\JKingWeb\Arsse\Service\Driver::class);
|
|
|
|
$s = new \JKingWeb\Arsse\Test\Service($d);
|
|
|
|
$this->assertInstanceOf(\DateTimeInterface::class, $s->watch(false));
|
|
|
|
// verify invocations
|
|
|
|
\Phake::verify($d)->queue(1, 2, 3);
|
|
|
|
\Phake::verify($d)->exec();
|
|
|
|
\Phake::verify($d)->clean();
|
|
|
|
\Phake::verify(Arsse::$db)->feedCleanup();
|
|
|
|
\Phake::verify(Arsse::$db)->sessionCleanup();
|
|
|
|
\Phake::verify(Arsse::$db)->articleCleanup();
|
|
|
|
\Phake::verify(Arsse::$db)->metaSet("service_last_checkin", $this->anything(), "datetime");
|
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|