Skip to content

Commit

Permalink
Convert remaining usage of Phony to Phake
Browse files Browse the repository at this point in the history
  • Loading branch information
JKingweb committed Dec 24, 2024
1 parent 1cd3f29 commit f1d3055
Show file tree
Hide file tree
Showing 27 changed files with 705 additions and 913 deletions.
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
xdebug_set_filter(\XDEBUG_FILTER_CODE_COVERAGE, XDEBUG_PATH_WHITELIST, [BASE."lib/"]);
}
}

\Phake::setClient(\Phake::CLIENT_PHPUNIT9);
3 changes: 1 addition & 2 deletions tests/cases/CLI/TestCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace JKingWeb\Arsse\TestCase\CLI;

use Eloquent\Phony\Phpunit\Phony;
use GuzzleHttp\Exception\ClientException;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Conf;
Expand Down Expand Up @@ -210,7 +209,7 @@ public function testAuthenticateAUser(string $cmd, int $exitStatus, string $outp
\Phake::when(Arsse::$user)->auth->thenReturn(false);
\Phake::when(Arsse::$user)->auth("[email protected]", "secret")->thenReturn(true);
\Phake::when(Arsse::$user)->auth("[email protected]", "superman")->thenReturn(true);
$fever = $this->mock(FeverUser::class);
$fever = \Phake::mock(FeverUser::class);
\Phake::when($fever)->authenticate->thenReturn(false);
\Phake::when($fever)->authenticate("[email protected]", "ashalla")->thenReturn(true);
\Phake::when($fever)->authenticate("[email protected]", "thx1138")->thenReturn(true);
Expand Down
28 changes: 12 additions & 16 deletions tests/cases/Database/SeriesSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,10 @@ protected function tearDownSeriesSubscription(): void {
public function testAddASubscriptionToAnExistingFeed(): void {
$url = "http://example.com/feed1";
$subID = $this->nextID("arsse_subscriptions");
$db = $this->partialMock(Database::class, static::$drv);
$db->feedUpdate->returns(true);
Arsse::$db = $db->get();
Arsse::$db = \Phake::partialMock(Database::class, static::$drv);
\Phake::when($db)->feedUpdate->thenReturn(true);
$this->assertSame($subID, Arsse::$db->subscriptionAdd($this->user, $url));
$db->feedUpdate->never()->called();
\Phake::verify(Arsse::$db, \Phake::never())->feedUpdate(\Phake::anyParameters());
$state = $this->primeExpectations($this->data, [
'arsse_feeds' => ['id','url','username','password'],
'arsse_subscriptions' => ['id','owner','feed'],
Expand All @@ -232,11 +231,10 @@ public function testAddASubscriptionToANewFeed(): void {
$url = "http://example.org/feed1";
$feedID = $this->nextID("arsse_feeds");
$subID = $this->nextID("arsse_subscriptions");
$db = $this->partialMock(Database::class, static::$drv);
$db->feedUpdate->returns(true);
Arsse::$db = $db->get();
Arsse::$db = \Phake::partialMock(Database::class, static::$drv);
\Phake::when(Arsse::$db)->feedUpdate->thenReturn(true);
$this->assertSame($subID, Arsse::$db->subscriptionAdd($this->user, $url, "", "", false));
$db->feedUpdate->calledWith($feedID, true, false);
\Phake::verify($db)->feedUpdate($feedID, true, false);
$state = $this->primeExpectations($this->data, [
'arsse_feeds' => ['id','url','username','password'],
'arsse_subscriptions' => ['id','owner','feed'],
Expand All @@ -255,11 +253,10 @@ public function testAddASubscriptionToANewFeedViaDiscovery(): void {
$discovered = "http://localhost:8000/Feed/Discovery/Feed";
$feedID = $this->nextID("arsse_feeds");
$subID = $this->nextID("arsse_subscriptions");
$db = $this->partialMock(Database::class, static::$drv);
$db->feedUpdate->returns(true);
Arsse::$db = $db->get();
Arsse::$db = \Phake::partialMock(Database::class, static::$drv);
\Phake::when(Arsse::$db)->feedUpdate->thenReturn(true);
$this->assertSame($subID, Arsse::$db->subscriptionAdd($this->user, $url, "", "", true));
$db->feedUpdate->calledWith($feedID, true, false);
\Phake::verify(Arsse::$db)->feedUpdate($feedID, true, false);
$state = $this->primeExpectations($this->data, [
'arsse_feeds' => ['id','url','username','password'],
'arsse_subscriptions' => ['id','owner','feed'],
Expand All @@ -276,14 +273,13 @@ public function testAddASubscriptionToANewFeedViaDiscovery(): void {
public function testAddASubscriptionToAnInvalidFeed(): void {
$url = "http://example.org/feed1";
$feedID = $this->nextID("arsse_feeds");
$db = $this->partialMock(Database::class, static::$drv);
$db->feedUpdate->throws(new FeedException("", ['url' => $url], $this->mockGuzzleException(ClientException::class, "", 404)));
Arsse::$db = $db->get();
Arsse::$db = \Phake::partialMock(Database::class, static::$drv);
\Phake::when(Arsse::$db)->feedUpdate->thenThrow(new FeedException("", ['url' => $url], $this->mockGuzzleException(ClientException::class, "", 404)));
$this->assertException("invalidUrl", "Feed");
try {
Arsse::$db->subscriptionAdd($this->user, $url, "", "", false);
} finally {
$db->feedUpdate->calledWith($feedID, true, false);
\Phake::verify(Arsse::$db)->feedUpdate($feedID, true, false);
$state = $this->primeExpectations($this->data, [
'arsse_feeds' => ['id','url','username','password'],
'arsse_subscriptions' => ['id','owner','feed'],
Expand Down
30 changes: 15 additions & 15 deletions tests/cases/Db/TestTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,49 @@ class TestTransaction extends \JKingWeb\Arsse\Test\AbstractTest {

public function setUp(): void {
parent::setUp();
$drv = $this->mock(\JKingWeb\Arsse\Db\SQLite3\Driver::class);
$drv->savepointRelease->returns(true);
$drv->savepointUndo->returns(true);
$drv->savepointCreate->returns(1, 2);
$drv = \Phake::mock(\JKingWeb\Arsse\Db\SQLite3\Driver::class);
\Phake::when($drv)->savepointRelease->thenReturn(true);
\Phake::when($drv)->savepointUndo->thenReturn(true);
\Phake::when($drv)->savepointCreate->thenReturn(1, 2);
$this->drv = $drv;
}

public function testManipulateTransactions(): void {
$drv = $this->drv->get();
$drv = $this->drv;
$tr1 = new Transaction($drv);
$tr2 = new Transaction($drv);
$this->drv->savepointCreate->twice()->called();
\Phake::verify($this->drv, \Phake::times(2))->savepointCreate();
$this->assertSame(1, $tr1->getIndex());
$this->assertSame(2, $tr2->getIndex());
unset($tr1);
$this->drv->savepointUndo->calledWith(1);
\Phake::verify($this->drv)->savepointUndo(1);
unset($tr2);
$this->drv->savepointUndo->calledWith(2);
\Phake::verify($this->drv)->savepointUndo(2);
}

public function testCloseTransactions(): void {
$drv = $this->drv->get();
$drv = $this->drv;
$tr1 = new Transaction($drv);
$tr2 = new Transaction($drv);
$this->assertTrue($tr1->isPending());
$this->assertTrue($tr2->isPending());
$tr1->commit();
$this->assertFalse($tr1->isPending());
$this->assertTrue($tr2->isPending());
$this->drv->savepointRelease->calledWith(1);
\Phake::verify($this->drv)->savepointRelease(1);
$tr2->rollback();
$this->assertFalse($tr1->isPending());
$this->assertFalse($tr2->isPending());
$this->drv->savepointUndo->calledWith(2);
\Phake::verify($this->drv)->savepointUndo(2);
}

public function testIgnoreRollbackErrors(): void {
$this->drv->savepointUndo->throws(new Exception("savepointStale"));
$drv = $this->drv->get();
\Phake::when($this->drv)->savepointUndo->thenThrow(new Exception("savepointStale"));
$drv = $this->drv;
$tr1 = new Transaction($drv);
$tr2 = new Transaction($drv);
unset($tr1, $tr2); // no exception should bubble up
$this->drv->savepointUndo->calledWith(1);
$this->drv->savepointUndo->calledWith(2);
\Phake::verify($this->drv)->savepointUndo(1);
\Phake::verify($this->drv)->savepointUndo(2);
}
}
26 changes: 11 additions & 15 deletions tests/cases/Feed/TestFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use JKingWeb\Arsse\Database;
use JKingWeb\Arsse\Misc\Date;
use JKingWeb\Arsse\Test\Result;
use Eloquent\Phony\Phpunit\Phony;

/**
* @covers \JKingWeb\Arsse\Feed
Expand Down Expand Up @@ -96,13 +95,12 @@ public function setUp(): void {
$this->base = self::$host."Feed/";
parent::setUp();
self::setConf();
$this->dbMock = $this->mock(Database::class);
$this->dbMock->feedMatchLatest->with(Phony::wildcard())->returns(new Result([]));
$this->dbMock->feedMatchLatest->with(1, Phony::any())->returns(new Result($this->latest));
$this->dbMock->feedMatchIds->with(Phony::wildcard())->returns(new Result([]));
$this->dbMock->feedMatchIds->with(1, Phony::wildcard())->returns(new Result($this->others));
$this->dbMock->feedRulesGet->returns([]);
Arsse::$db = $this->dbMock->get();
Arsse::$db = \Phake::mock(Database::class);
\Phake::when(Arsse::$db)->feedMatchLatest->thenReturn(new Result([]));
\Phake::when(Arsse::$db)->feedMatchLatest(1, $this->anything())->thenReturn(new Result($this->latest));
\Phake::when(Arsse::$db)->feedMatchIds->thenReturn(new Result([]));
\Phake::when(Arsse::$db)->feedMatchIds(1, \Phake::ignoreRemaining())->thenReturn(new Result($this->others));
\Phake::when(Arsse::$db)->feedRulesGet->thenReturn([]);
}

public function testParseAFeed(): void {
Expand Down Expand Up @@ -343,10 +341,9 @@ public function testComputeNextFetchFrom200(): void {
}

public function testMatchLatestArticles(): void {
$this->dbMock = $this->mock(Database::class);
$this->dbMock->feedMatchLatest->with(Phony::wildcard())->returns(new Result([]));
$this->dbMock->feedMatchLatest->with(1, Phony::any())->returns(new Result($this->latest));
Arsse::$db = $this->dbMock->get();
Arsse::$db = \Phake::mock(Database::class);
\Phake::when(Arsse::$db)->feedMatchLatest(\Phake::anyParameters())->thenReturn(new Result([]));
\Phake::when(Arsse::$db)->feedMatchLatest(1, $this->anything())->thenReturn(new Result($this->latest));
$f = new Feed(1, $this->base."Matching/1");
$this->assertCount(0, $f->newItems);
$this->assertCount(0, $f->changedItems);
Expand Down Expand Up @@ -393,19 +390,18 @@ public function testApplyFilterRules(): void {
'jack' => ['new' => [false, true, true, false, true], 'changed' => [7 => true, 47 => true, 2112 => false, 1 => true, 42 => false]],
'sam' => ['new' => [false, true, false, false, false], 'changed' => [7 => false, 47 => true, 2112 => false, 1 => false, 42 => false]],
];
$this->dbMock->feedMatchIds->returns(new Result([
\Phake::when(Arsse::$db)->feedMatchIds->thenReturn(new Result([
// these are the sixth through tenth entries in the feed; the title hashes have been omitted for brevity
['id' => 7, 'guid' => '0f2a218c311e3d8105f1b075142a5d26dabf056ffc61abe77e96c8f071bbf4a7', 'edited' => null, 'url_title_hash' => "", 'url_content_hash' => '', 'title_content_hash' => ''],
['id' => 47, 'guid' => '1c19e3b9018bc246b7414ae919ddebc88d0c575129e8c4a57b84b826c00f6db5', 'edited' => null, 'url_title_hash' => "", 'url_content_hash' => '', 'title_content_hash' => ''],
['id' => 2112, 'guid' => '964db0b9292ad0c7a6c225f2e0966f3bda53486fae65db0310c97409974e65b8', 'edited' => null, 'url_title_hash' => "", 'url_content_hash' => '', 'title_content_hash' => ''],
['id' => 1, 'guid' => '436070cda5713a0d9a8fdc8652c7ab142f0550697acfd5206a16c18aee355039', 'edited' => null, 'url_title_hash' => "", 'url_content_hash' => '', 'title_content_hash' => ''],
['id' => 42, 'guid' => '1a731433a1904220ef26e731ada7262e1d5bcecae53e7b5df9e1f5713af6e5d3', 'edited' => null, 'url_title_hash' => "", 'url_content_hash' => '', 'title_content_hash' => ''],
]));
$this->dbMock->feedRulesGet->returns([
\Phake::when(Arsse::$db)->feedRulesGet->thenReturn([
'jack' => ['keep' => "", 'block' => '`A|W|J|S`u'],
'sam' => ['keep' => "`B|T|X`u", 'block' => '`C`u'],
]);
Arsse::$db = $this->dbMock->get();
$f = new Feed(5, $this->base."Filtering/1");
$this->assertSame($exp, $f->filteredItems);
}
Expand Down
18 changes: 9 additions & 9 deletions tests/cases/ImportExport/TestFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class TestFile extends \JKingWeb\Arsse\Test\AbstractTest {
public function setUp(): void {
parent::setUp();
// create a mock Import/Export processor with stubbed underlying import/export routines
$this->proc = $this->partialMock(AbstractImportExport::class);
$this->proc->export->returns("EXPORT_FILE");
$this->proc->import->returns(true);
$this->proc = \Phake::partialMock(AbstractImportExport::class);
\Phake::when($this->proc)->export->thenReturn("EXPORT_FILE");
\Phake::when($this->proc)->import->thenReturn(true);
$this->vfs = vfsStream::setup("root", null, [
'exportGoodFile' => "",
'exportGoodDir' => [],
Expand Down Expand Up @@ -51,13 +51,13 @@ public function testExportToAFile(string $file, string $user, bool $flat, $exp):
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
$this->proc->get()->exportFile($path, $user, $flat);
$this->proc->exportFile($path, $user, $flat);
} else {
$this->assertSame($exp, $this->proc->get()->exportFile($path, $user, $flat));
$this->assertSame($exp, $this->proc->exportFile($path, $user, $flat));
$this->assertSame("EXPORT_FILE", $this->vfs->getChild($file)->getContent());
}
} finally {
$this->proc->export->calledWith($user, $flat);
\Phake::verify($this->proc)->export($user, $flat);
}
}

Expand Down Expand Up @@ -90,12 +90,12 @@ public function testImportFromAFile(string $file, string $user, bool $flat, bool
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
$this->proc->get()->importFile($path, $user, $flat, $replace);
$this->proc->importFile($path, $user, $flat, $replace);
} else {
$this->assertSame($exp, $this->proc->get()->importFile($path, $user, $flat, $replace));
$this->assertSame($exp, $this->proc->importFile($path, $user, $flat, $replace));
}
} finally {
$this->proc->import->times((int) ($exp === true))->calledWith($user, "GOOD_FILE", $flat, $replace);
\Phake::verify($this->proc, \Phake::times((int) ($exp === true)))->import($user, "GOOD_FILE", $flat, $replace);
}
}

Expand Down
Loading

0 comments on commit f1d3055

Please sign in to comment.