1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/tests/cases/Service/TestService.php

106 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2017-12-22 03:47:19 +00:00
namespace JKingWeb\Arsse\TestCase\Service;
2017-08-29 14:50:31 +00:00
2017-12-22 03:47:19 +00:00
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Service;
use JKingWeb\Arsse\Misc\Date;
2017-07-20 22:36:03 +00:00
/** @covers \JKingWeb\Arsse\Service */
2017-12-22 03:47:19 +00:00
class TestService extends \JKingWeb\Arsse\Test\AbstractTest {
protected $srv;
2019-10-16 18:42:43 +00:00
public function setUp(): void {
2021-02-27 20:24:02 +00:00
parent::setUp();
2018-11-23 00:55:54 +00:00
self::setConf();
2021-03-01 23:01:25 +00:00
$this->dbMock = $this->mock(Database::class);
Arsse::$db = $this->dbMock->get();
2022-06-01 03:55:04 +00:00
$this->srv = new Service;
}
2020-01-20 18:52:48 +00:00
public function testCheckIn(): void {
$now = time();
$this->srv->checkIn();
2021-03-01 23:01:25 +00:00
$this->dbMock->metaSet->calledWith("service_last_checkin", "~", "datetime");
$then = $this->dbMock->metaSet->firstCall()->argument(1);
$this->assertTime($now, $then);
}
2020-01-20 18:52:48 +00:00
public function testReportHavingCheckedIn(): void {
// the mock's metaGet() returns null by default
$this->assertFalse(Service::hasCheckedIn());
$interval = Arsse::$conf->serviceFrequency;
$valid = (new \DateTimeImmutable("now", new \DateTimezone("UTC")))->sub($interval);
$invalid = $valid->sub($interval)->sub($interval);
2021-03-01 23:01:25 +00:00
$this->dbMock->metaGet->with("service_last_checkin")->returns(Date::transform($valid, "sql"), Date::transform($invalid, "sql"));
Arsse::$db = $this->dbMock->get();
$this->assertTrue(Service::hasCheckedIn());
$this->assertFalse(Service::hasCheckedIn());
}
2019-10-19 16:14:13 +00:00
2020-01-20 18:52:48 +00:00
public function testPerformPreCleanup(): void {
2019-10-19 16:14:13 +00:00
$this->assertTrue(Service::cleanupPre());
2021-03-01 23:01:25 +00:00
$this->dbMock->feedCleanup->called();
$this->dbMock->iconCleanup->called();
$this->dbMock->sessionCleanup->called();
2019-10-19 16:14:13 +00:00
}
2020-01-20 18:52:48 +00:00
public function testPerformShortPostCleanup(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->articleCleanup->returns(0);
Arsse::$db = $this->dbMock->get();
2019-10-19 16:14:13 +00:00
$this->assertTrue(Service::cleanupPost());
2021-03-01 23:01:25 +00:00
$this->dbMock->articleCleanup->Called();
$this->dbMock->driverMaintenance->never()->called();
2019-10-19 16:14:13 +00:00
}
2020-01-20 18:52:48 +00:00
public function testPerformFullPostCleanup(): void {
2021-03-01 23:01:25 +00:00
$this->dbMock->articleCleanup->returns(1);
Arsse::$db = $this->dbMock->get();
2019-10-19 16:14:13 +00:00
$this->assertTrue(Service::cleanupPost());
2021-03-01 23:01:25 +00:00
$this->dbMock->articleCleanup->called();
$this->dbMock->driverMaintenance->called();
2019-10-19 16:14:13 +00:00
}
2019-10-19 22:51:01 +00:00
2020-01-20 18:52:48 +00:00
public function testRefreshFeeds(): void {
2019-10-19 22:51:01 +00:00
// set up mock database actions
2021-03-01 23:01:25 +00:00
$this->dbMock->metaSet->returns(true);
$this->dbMock->feedCleanup->returns(true);
$this->dbMock->sessionCleanup->returns(true);
$this->dbMock->articleCleanup->returns(0);
$this->dbMock->feedListStale->returns([1,2,3]);
2019-10-19 22:51:01 +00:00
// perform the test
2021-03-01 23:01:25 +00:00
Arsse::$db = $this->dbMock->get();
$d = $this->mock(\JKingWeb\Arsse\Service\Driver::class);
$s = new \JKingWeb\Arsse\Test\Service($d->get());
2019-10-19 22:51:01 +00:00
$this->assertInstanceOf(\DateTimeInterface::class, $s->watch(false));
// verify invocations
2021-03-01 23:01:25 +00:00
$d->queue->calledWith(1, 2, 3);
$d->exec->called();
$d->clean->called();
$this->dbMock->feedCleanup->called();
$this->dbMock->iconCleanup->called();
$this->dbMock->sessionCleanup->called();
$this->dbMock->articleCleanup->called();
$this->dbMock->metaSet->calledWith("service_last_checkin", $this->anything(), "datetime");
2019-10-19 22:51:01 +00:00
}
2021-07-06 14:07:56 +00:00
public function testReloadTheService(): void {
$u = Arsse::$user;
$l = Arsse::$lang;
$d = Arsse::$db;
$o = Arsse::$obj;
$c = Arsse::$conf;
$this->srv->reload();
$this->assertNotSame($u, Arsse::$user);
$this->assertNotSame($l, Arsse::$lang);
$this->assertNotSame($d, Arsse::$db);
$this->assertNotSame($o, Arsse::$obj);
$this->assertNotSame($c, Arsse::$conf);
}
2017-08-29 14:50:31 +00:00
}