1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Full coverage! Fixes #66

This commit is contained in:
J. King 2019-10-19 18:51:01 -04:00
parent 728eecfbb5
commit 71c7cd8fb1
10 changed files with 149 additions and 12 deletions

View file

@ -12,18 +12,14 @@ class Service {
const DRIVER_NAMES = [
'serial' => \JKingWeb\Arsse\Service\Serial\Driver::class,
'subprocess' => \JKingWeb\Arsse\Service\Subprocess\Driver::class,
'curl' => \JKingWeb\Arsse\Service\Curl\Driver::class,
];
/** @var Service\Driver */
protected $drv;
/** @var \DateInterval */
protected $interval;
public function __construct() {
$driver = Arsse::$conf->serviceDriver;
$this->drv = new $driver();
$this->interval = Arsse::$conf->serviceFrequency;
}
public function watch(bool $loop = true): \DateTimeInterface {
@ -34,12 +30,12 @@ class Service {
$list = Arsse::$db->feedListStale();
if ($list) {
$this->drv->queue(...$list);
unset($list);
$this->drv->exec();
$this->drv->clean();
unset($list);
}
static::cleanupPost();
$t->add($this->interval);
$t->add(Arsse::$conf->serviceFrequency);
// @codeCoverageIgnoreStart
if ($loop) {
do {

View file

@ -11,5 +11,5 @@ interface Driver {
public static function requirementsMet(): bool;
public function queue(int ...$feeds): int;
public function exec(): int;
public function clean(): bool;
public function clean(): int;
}

View file

@ -36,8 +36,9 @@ class Driver implements \JKingWeb\Arsse\Service\Driver {
return Arsse::$conf->serviceQueueWidth - sizeof($this->queue);
}
public function clean(): bool {
public function clean(): int {
$out = sizeof($this->queue);
$this->queue = [];
return true;
return $out;
}
}

View file

@ -33,7 +33,7 @@ class Driver implements \JKingWeb\Arsse\Service\Driver {
$id = (int) array_shift($this->queue);
$php = escapeshellarg(\PHP_BINARY);
$arsse = escapeshellarg($_SERVER['argv'][0]);
array_push($pp, popen("$php $arsse feed refresh $id", "r"));
array_push($pp, $this->execCmd("$php $arsse feed refresh $id"));
}
while ($pp) {
$p = array_pop($pp);
@ -43,8 +43,14 @@ class Driver implements \JKingWeb\Arsse\Service\Driver {
return Arsse::$conf->serviceQueueWidth - sizeof($this->queue);
}
public function clean(): bool {
/** @codeCoverageIgnore */
protected function execCmd(string $cmd) {
return popen($cmd, "r");
}
public function clean(): int {
$out = sizeof($this->queue);
$this->queue = [];
return true;
return $out;
}
}

View file

@ -0,0 +1,47 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Service;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Service\Driver as DriverInterface;
use JKingWeb\Arsse\Service\Serial\Driver;
/** @covers \JKingWeb\Arsse\Service\Serial\Driver */
class TestSerial extends \JKingWeb\Arsse\Test\AbstractTest {
public function setUp() {
self::clearData();
self::setConf();
Arsse::$db = \Phake::mock(Database::class);
}
public function testConstruct() {
$this->assertTrue(Driver::requirementsMet());
$this->assertInstanceOf(DriverInterface::class, new Driver);
}
public function testFetchDriverName() {
$this->assertTrue(strlen(Driver::driverName()) > 0);
}
public function testEnqueueFeeds() {
$d = new Driver;
$this->assertSame(3, $d->queue(1, 2, 3));
$this->assertSame(5, $d->queue(4, 5));
$this->assertSame(5, $d->clean());
$this->assertSame(1, $d->queue(5));
}
public function testRefreshFeeds() {
$d = new Driver;
$d->queue(1, 4, 3);
$this->assertSame(Arsse::$conf->serviceQueueWidth, $d->exec());
\Phake::verify(Arsse::$db)->feedUpdate(1);
\Phake::verify(Arsse::$db)->feedUpdate(4);
\Phake::verify(Arsse::$db)->feedUpdate(3);
}
}

View file

@ -59,4 +59,25 @@ class TestService extends \JKingWeb\Arsse\Test\AbstractTest {
\Phake::verify(Arsse::$db)->articleCleanup();
\Phake::verify(Arsse::$db)->driverMaintenance();
}
public function testRefreshFeeds() {
// 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");
}
}

View file

@ -0,0 +1,48 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\Service;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Service\Driver as DriverInterface;
use JKingWeb\Arsse\Service\Subprocess\Driver;
/** @covers \JKingWeb\Arsse\Service\Subprocess\Driver */
class TestSubprocess extends \JKingWeb\Arsse\Test\AbstractTest {
public function setUp() {
self::clearData();
self::setConf();
}
public function testConstruct() {
$this->assertTrue(Driver::requirementsMet());
$this->assertInstanceOf(DriverInterface::class, new Driver);
}
public function testFetchDriverName() {
$this->assertTrue(strlen(Driver::driverName()) > 0);
}
public function testEnqueueFeeds() {
$d = new Driver;
$this->assertSame(3, $d->queue(1, 2, 3));
$this->assertSame(5, $d->queue(4, 5));
$this->assertSame(5, $d->clean());
$this->assertSame(1, $d->queue(5));
}
public function testRefreshFeeds() {
$d = \Phake::partialMock(Driver::class);
\Phake::when($d)->execCmd->thenReturnCallback(function(string $cmd) {
// FIXME: Does this work in Windows?
return popen("echo ".escapeshellarg($cmd), "r");
});
$this->assertSame(3, $d->queue(1, 4, 3));
$this->assertSame(Arsse::$conf->serviceQueueWidth, $d->exec());
\Phake::verify($d, \Phake::times(3))->execCmd;
}
}

View file

@ -37,6 +37,9 @@ class TestArsse extends \JKingWeb\Arsse\Test\AbstractTest {
}
public function testLoadNewData() {
if (!\JKingWeb\Arsse\Db\SQLite3\Driver::requirementsMet() && !\JKingWeb\Arsse\Db\SQLite3\PDODriver::requirementsMet()) {
$this->markTestSkipped("A functional SQLite interface is required for this test");
}
$conf = (new Conf)->import(['dbSQLite3File' => ":memory:"]);
Arsse::load($conf);
$this->assertInstanceOf(Conf::class, Arsse::$conf);

13
tests/lib/Service.php Normal file
View file

@ -0,0 +1,13 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Test;
class Service extends \JKingWeb\Arsse\Service {
public function __construct(\JKingWeb\Arsse\Service\Driver $drv) {
$this->drv = $drv;
}
}

View file

@ -128,6 +128,8 @@
</testsuite>
<testsuite name="Admin tools">
<file>cases/Service/TestService.php</file>
<file>cases/Service/TestSerial.php</file>
<file>cases/Service/TestSubprocess.php</file>
<file>cases/CLI/TestCLI.php</file>
<file>cases/TestArsse.php</file>
</testsuite>