1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2025-01-21 18:40:33 +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-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;
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 {
protected $srv;
2019-10-16 14:42:43 -04:00
public function setUp(): void {
2021-02-27 15:24:02 -05:00
parent::setUp();
2018-11-22 19:55:54 -05:00
self::setConf();
2021-03-01 18:01:25 -05:00
$this->dbMock = $this->mock(Database::class);
Arsse::$db = $this->dbMock->get();
$this->srv = new Service();
}
2020-01-20 13:52:48 -05:00
public function testCheckIn(): void {
$now = time();
$this->srv->checkIn();
2021-03-01 18:01:25 -05:00
$this->dbMock->metaSet->calledWith("service_last_checkin", "~", "datetime");
$then = $this->dbMock->metaSet->firstCall()->argument(1);
$this->assertTime($now, $then);
}
2020-01-20 13:52:48 -05: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 18:01:25 -05: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 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());
2021-03-01 18:01:25 -05:00
$this->dbMock->feedCleanup->called();
$this->dbMock->iconCleanup->called();
$this->dbMock->sessionCleanup->called();
2019-10-19 12:14:13 -04:00
}
2020-01-20 13:52:48 -05:00
public function testPerformShortPostCleanup(): void {
2021-03-01 18:01:25 -05:00
$this->dbMock->articleCleanup->returns(0);
Arsse::$db = $this->dbMock->get();
2019-10-19 12:14:13 -04:00
$this->assertTrue(Service::cleanupPost());
2021-03-01 18:01:25 -05:00
$this->dbMock->articleCleanup->Called();
$this->dbMock->driverMaintenance->never()->called();
2019-10-19 12:14:13 -04:00
}
2020-01-20 13:52:48 -05:00
public function testPerformFullPostCleanup(): void {
2021-03-01 18:01:25 -05:00
$this->dbMock->articleCleanup->returns(1);
Arsse::$db = $this->dbMock->get();
2019-10-19 12:14:13 -04:00
$this->assertTrue(Service::cleanupPost());
2021-03-01 18:01:25 -05:00
$this->dbMock->articleCleanup->called();
$this->dbMock->driverMaintenance->called();
2019-10-19 12:14:13 -04:00
}
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
2021-03-01 18:01:25 -05: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 18:51:01 -04:00
// perform the test
2021-03-01 18:01:25 -05: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 18:51:01 -04:00
$this->assertInstanceOf(\DateTimeInterface::class, $s->watch(false));
// verify invocations
2021-03-01 18:01:25 -05: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 18:51:01 -04:00
}
2021-07-06 10:07:56 -04: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 10:50:31 -04:00
}