Browse Source

Fix errors and failures

phake
J. King 6 months ago
parent
commit
ece19494d7
  1. 75
      tests/cases/REST/Miniflux/TestV1.php

75
tests/cases/REST/Miniflux/TestV1.php

@ -206,8 +206,8 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
Arsse::$user = \Phake::mock(User::class); Arsse::$user = \Phake::mock(User::class);
\Phake::when(Arsse::$user)->list->thenReturn(["john.doe@example.com", "jane.doe@example.com", "admin@example.com"]); \Phake::when(Arsse::$user)->list->thenReturn(["john.doe@example.com", "jane.doe@example.com", "admin@example.com"]);
\Phake::when(Arsse::$user)->propertiesGet->thenThrow($u[0]); \Phake::when(Arsse::$user)->propertiesGet->thenThrow($u[0]);
\Phake::when(Arsse::$user)->propertiesGet("john.doe@example.com")->thenReturn($u[1]); \Phake::when(Arsse::$user)->propertiesGet("john.doe@example.com", $this->anything())->thenReturn($u[1]);
\Phake::when(Arsse::$user)->propertiesGet("jane.doe@example.com")->thenReturn($u[2]); \Phake::when(Arsse::$user)->propertiesGet("jane.doe@example.com", $this->anything())->thenReturn($u[2]);
\Phake::when(Arsse::$user)->lookup->thenThrow($u[0]); \Phake::when(Arsse::$user)->lookup->thenThrow($u[0]);
\Phake::when(Arsse::$user)->lookup(1)->thenReturn("john.doe@example.com"); \Phake::when(Arsse::$user)->lookup(1)->thenReturn("john.doe@example.com");
\Phake::when(Arsse::$user)->lookup(2)->thenReturn("jane.doe@example.com"); \Phake::when(Arsse::$user)->lookup(2)->thenReturn("jane.doe@example.com");
@ -241,8 +241,8 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
Arsse::$user = \Phake::mock(User::class); Arsse::$user = \Phake::mock(User::class);
\Phake::when(Arsse::$user)->begin->thenReturn($this->transaction->get()); \Phake::when(Arsse::$user)->begin->thenReturn($this->transaction->get());
\Phake::when(Arsse::$user)->propertiesGet->thenReturn(['num' => 1, 'admin' => true]); \Phake::when(Arsse::$user)->propertiesGet->thenReturn(['num' => 1, 'admin' => true]);
\Phake::when(Arsse::$user)->propertiesGet("john.doe@example.com")->thenReturn(['num' => 2, 'admin' => $admin]); \Phake::when(Arsse::$user)->propertiesGet("john.doe@example.com", $this->anything())->thenReturn(['num' => 2, 'admin' => $admin]);
\Phake::when(Arsse::$user)->propertiesGet("ook")->thenReturn(['num' => 2, 'admin' => $admin]); \Phake::when(Arsse::$user)->propertiesGet("ook", $this->anything())->thenReturn(['num' => 2, 'admin' => $admin]);
\Phake::when(Arsse::$user)->lookup->thenThrow(new ExceptionConflict("doesNotExist")); \Phake::when(Arsse::$user)->lookup->thenThrow(new ExceptionConflict("doesNotExist"));
\Phake::when(Arsse::$user)->lookup(1)->thenReturn("jane.doe@example.com"); \Phake::when(Arsse::$user)->lookup(1)->thenReturn("jane.doe@example.com");
\Phake::when(Arsse::$user)->lookup(2)->thenReturn("john.doe@example.com"); \Phake::when(Arsse::$user)->lookup(2)->thenReturn("john.doe@example.com");
@ -307,36 +307,31 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideUserAdditions */ /** @dataProvider provideUserAdditions */
public function testAddAUser(array $body, $in1, $out1, $in2, $out2, ResponseInterface $exp): void { public function testAddAUser(array $body, $in1, $out1, $in2, $out2, ResponseInterface $exp): void {
Arsse::$user = $this->createMock(User::class); Arsse::$user = \Phake::mock(User::class);
Arsse::$user->method("begin")->willReturn($this->transaction->get()); \Phake::when(Arsse::$user)->begin->thenReturn($this->transaction->get());
Arsse::$user->method("propertiesGet")->willReturnCallback(function(string $u, bool $includeLarge) { \Phake::when(Arsse::$user)->propertiesGet->thenReturn(['num' => 2, 'admin' => false]);
if ($u === "john.doe@example.com") { \Phake::when(Arsse::$user)->propertiesGet("john.doe@example.com", $this->anything())->thenReturn(['num' => 1, 'admin' => true]);
return ['num' => 1, 'admin' => true];
} else {
return ['num' => 2, 'admin' => false];
}
});
if ($out1 instanceof \Exception) { if ($out1 instanceof \Exception) {
Arsse::$user->method("add")->willThrowException($out1); \Phake::when(Arsse::$user)->add->thenThrow($out1);
} else { } else {
Arsse::$user->method("add")->willReturn($in1[1] ?? ""); \Phake::when(Arsse::$user)->add->thenReturn($in1[1] ?? "");
} }
if ($out2 instanceof \Exception) { if ($out2 instanceof \Exception) {
Arsse::$user->method("propertiesSet")->willThrowException($out2); \Phake::when(Arsse::$user)->propertiesSet->thenThrow($out2);
} else { } else {
Arsse::$user->method("propertiesSet")->willReturn($out2 ?? []); \Phake::when(Arsse::$user)->propertiesSet->thenReturn($out2 ?? []);
} }
$this->assertMessage($exp, $this->req("POST", "/users", $body));
if ($in1 === null) { if ($in1 === null) {
Arsse::$user->expects($this->exactly(0))->method("add"); \Phake::verify(Arsse::$user, \Phake::never())->add($this->anything(), $this->anything());
} else { } else {
Arsse::$user->expects($this->exactly(1))->method("add")->with(...($in1 ?? [])); \Phake::verify(Arsse::$user)->add(...$in1);
} }
if ($in2 === null) { if ($in2 === null) {
Arsse::$user->expects($this->exactly(0))->method("propertiesSet"); \Phake::verify(Arsse::$user, \Phake::never())->propertiesSet($this->anything(), $this->anything());
} else { } else {
Arsse::$user->expects($this->exactly(1))->method("propertiesSet")->with($body['username'], $in2); \Phake::verify(Arsse::$user)->propertiesSet($body['username'], $in2);
} }
$this->assertMessage($exp, $this->req("POST", "/users", $body));
} }
public function provideUserAdditions(): iterable { public function provideUserAdditions(): iterable {
@ -357,29 +352,29 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
} }
public function testDeleteAUser(): void { public function testDeleteAUser(): void {
Arsse::$user = $this->createMock(User::class); Arsse::$user = \Phake::mock(User::class);
Arsse::$user->method("propertiesGet")->willReturn(['admin' => true]); \Phake::when(Arsse::$user)->propertiesGet->thenReturn(['admin' => true]);
Arsse::$user->method("lookup")->willReturn("john.doe@example.com"); \Phake::when(Arsse::$user)->lookup->thenReturn("john.doe@example.com");
Arsse::$user->method("remove")->willReturn(true); \Phake::when(Arsse::$user)->remove->thenReturn(true);
Arsse::$user->expects($this->exactly(1))->method("lookup")->with(2112);
Arsse::$user->expects($this->exactly(1))->method("remove")->with("john.doe@example.com");
$this->assertMessage(HTTP::respEmpty(204), $this->req("DELETE", "/users/2112")); $this->assertMessage(HTTP::respEmpty(204), $this->req("DELETE", "/users/2112"));
\Phake::verify(Arsse::$user)->lookup(2112);
\Phake::verify(Arsse::$user)->remove("john.doe@example.com");
} }
public function testDeleteAMissingUser(): void { public function testDeleteAMissingUser(): void {
Arsse::$user = $this->createMock(User::class); Arsse::$user = \Phake::mock(User::class);
Arsse::$user->method("propertiesGet")->willReturn(['admin' => true]); \Phake::when(Arsse::$user)->propertiesGet->thenReturn(['admin' => true]);
Arsse::$user->method("lookup")->willThrowException(new ExceptionConflict("doesNotExist")); \Phake::when(Arsse::$user)->lookup->thenThrow(new ExceptionConflict("doesNotExist"));
Arsse::$user->method("remove")->willReturn(true); \Phake::when(Arsse::$user)->remove->thenReturn(true);
Arsse::$user->expects($this->exactly(1))->method("lookup")->with(2112);
Arsse::$user->expects($this->exactly(0))->method("remove");
$this->assertMessage(V1::respError("404", 404), $this->req("DELETE", "/users/2112")); $this->assertMessage(V1::respError("404", 404), $this->req("DELETE", "/users/2112"));
\Phake::verify(Arsse::$user)->lookup(2112);
\Phake::verify(Arsse::$user, \Phake::never())->remove($this->anything());
} }
public function testDeleteAUserWithoutAuthority(): void { public function testDeleteAUserWithoutAuthority(): void {
Arsse::$user->expects($this->exactly(0))->method("lookup");
Arsse::$user->expects($this->exactly(0))->method("remove");
$this->assertMessage(V1::respError("403", 403), $this->req("DELETE", "/users/2112")); $this->assertMessage(V1::respError("403", 403), $this->req("DELETE", "/users/2112"));
\Phake::verify(Arsse::$user, \Phake::never())->lookup($this->anything());
\Phake::verify(Arsse::$user, \Phake::never())->remove($this->anything());
} }
public function testListCategories(): void { public function testListCategories(): void {
@ -395,8 +390,8 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
$this->assertMessage($exp, $this->req("GET", "/categories")); $this->assertMessage($exp, $this->req("GET", "/categories"));
$this->dbMock->folderList->calledWith("john.doe@example.com", null, false); $this->dbMock->folderList->calledWith("john.doe@example.com", null, false);
// run test again with a renamed root folder // run test again with a renamed root folder
Arsse::$user = $this->createMock(User::class); Arsse::$user = \Phake::mock(User::class);
Arsse::$user->method("propertiesGet")->willReturn(['num' => 47, 'admin' => false, 'root_folder_name' => "Uncategorized"]); \Phake::when(Arsse::$user)->propertiesGet->thenReturn(['num' => 47, 'admin' => false, 'root_folder_name' => "Uncategorized"]);
$exp = HTTP::respJson([ $exp = HTTP::respJson([
['id' => 1, 'title' => "Uncategorized", 'user_id' => 47], ['id' => 1, 'title' => "Uncategorized", 'user_id' => 47],
['id' => 2, 'title' => "Science", 'user_id' => 47], ['id' => 2, 'title' => "Science", 'user_id' => 47],
@ -432,15 +427,15 @@ class TestV1 extends \JKingWeb\Arsse\Test\AbstractTest {
/** @dataProvider provideCategoryUpdates */ /** @dataProvider provideCategoryUpdates */
public function testRenameACategory(int $id, $title, $out, ResponseInterface $exp): void { public function testRenameACategory(int $id, $title, $out, ResponseInterface $exp): void {
Arsse::$user->method("propertiesSet")->willReturn(['root_folder_name' => $title]); \Phake::when(Arsse::$user)->propertiesSet->thenReturn(['root_folder_name' => $title]);
if (is_string($out)) { if (is_string($out)) {
$this->dbMock->folderPropertiesSet->throws(new ExceptionInput($out)); $this->dbMock->folderPropertiesSet->throws(new ExceptionInput($out));
} else { } else {
$this->dbMock->folderPropertiesSet->returns($out); $this->dbMock->folderPropertiesSet->returns($out);
} }
$times = (int) ($id === 1 && is_string($title) && strlen(trim($title))); $times = (int) ($id === 1 && is_string($title) && strlen(trim($title)));
Arsse::$user->expects($this->exactly($times))->method("propertiesSet")->with("john.doe@example.com", ['root_folder_name' => $title]);
$this->assertMessage($exp, $this->req("PUT", "/categories/$id", ['title' => $title])); $this->assertMessage($exp, $this->req("PUT", "/categories/$id", ['title' => $title]));
\Phake::verify(Arsse::$user, \Phake::times($times))->propertiesSet("john.doe@example.com", ['root_folder_name' => $title]);
$times = (int) ($id !== 1 && is_string($title)); $times = (int) ($id !== 1 && is_string($title));
$this->dbMock->folderPropertiesSet->times($times)->calledWith("john.doe@example.com", $id - 1, ['name' => $title]); $this->dbMock->folderPropertiesSet->times($times)->calledWith("john.doe@example.com", $id - 1, ['name' => $title]);
} }

Loading…
Cancel
Save