Skip to content

Commit

Permalink
fix the tests for PHPUnit 11 (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuch committed Jan 27, 2025
1 parent 6d4896b commit b098f56
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 122 deletions.
31 changes: 15 additions & 16 deletions tests/classes/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public function testCorrectLogin(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'user' ])
->onlyMethods([ 'config', 'user' ])
->getMock();
$core->expects($this->exactly(3))
->method('config')
->with($this->logicalOr(
$this->equalTo('admin/password'),
$this->equalTo('users/user_management')
))
->will($this->returnCallback(function ($param) {
->willReturnCallback(function ($param) {
return $param === 'admin/password' ? 'some' : false;
}));
});
$core->expects($this->once())
->method('user')
->with($this->callback(function ($param) {
Expand All @@ -52,15 +52,14 @@ public function testLoginWithIncorrectUsername(): void
{
$mp = $this->getMockBuilder(UserMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ 'exists' ])
->getMockForAbstractClass();
->getMock();
$mp->expects($this->once())
->method('exists')
->willReturn(false);

$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->onlyMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
Expand All @@ -69,7 +68,7 @@ public function testLoginWithIncorrectUsername(): void

$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'database', 'config' ])
->onlyMethods([ 'database', 'config' ])
->getMock();
$core->expects($this->once())
->method('database')
Expand All @@ -80,9 +79,9 @@ public function testLoginWithIncorrectUsername(): void
$this->equalTo('admin/password'),
$this->equalTo('users/user_management')
))
->will($this->returnCallback(function ($param) {
->willReturnCallback(function ($param) {
return $param === 'admin/password' ? 'some' : true;
}));
});

$this->expectException(AuthException::class);
(new Auth($core))->login('fake_user', 'password');
Expand All @@ -92,7 +91,7 @@ public function testLogout(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'destroySession' ])
->onlyMethods([ 'destroySession' ])
->getMock();
$core->expects($this->once())
->method('destroySession');
Expand All @@ -107,7 +106,7 @@ public function testIsAllowedWhenAuthDisabled(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'user' ])
->onlyMethods([ 'config', 'user' ])
->getMock();
$core->expects($this->once())
->method('config')
Expand All @@ -122,7 +121,7 @@ public function testIsAllowedWithActiveSession(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'user' ])
->onlyMethods([ 'config', 'user' ])
->getMock();
$core->expects($this->once())
->method('config')
Expand All @@ -138,17 +137,17 @@ public function testIsAllowedWithoutSession(): void
{
$core = $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'config', 'user' ])
->onlyMethods([ 'config', 'user' ])
->getMock();
$core->expects($this->exactly(3))
->method('config')
->with($this->logicalOr(
$this->equalTo('admin/password'),
$this->equalTo('users/user_management')
))
->will($this->returnCallback(function ($param) {
->willReturnCallback(function ($param) {
return $param === 'admin/password' ? 'some' : false;
}));
});
$core->expects($this->once())
->method('user')
->willReturn(null);
Expand All @@ -160,7 +159,7 @@ public function testIsAllowedWithoutSession(): void

private function coreWithConfigValue(string $key, $value)
{
$core = $this->getMockBuilder(Core::class)->disableOriginalConstructor()->setMethods([ 'config' ])->getMock();
$core = $this->getMockBuilder(Core::class)->disableOriginalConstructor()->onlyMethods([ 'config' ])->getMock();
$core->expects($this->once())->method('config')->with($this->equalTo($key))->willReturn($value);
return $core;
}
Expand Down
8 changes: 6 additions & 2 deletions tests/classes/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Liuch\DmarcSrg\Users\AdminUser;
use Liuch\DmarcSrg\Exception\SoftException;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;

class CoreTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -23,10 +24,11 @@ public function testSelfInstance(): void
/**
* @runInSeparateProcess
*/
#[RunInSeparateProcess]
public function testRequestMethod(): void
{
$_SERVER['REQUEST_METHOD'] = 'some_method';
$this->assertSame('some_method', $this->core->method());
$this->assertSame('some_method', $this->core->requestMethod());
}

public function testAdminUser(): void
Expand All @@ -41,6 +43,7 @@ public function testAdminUser(): void
/**
* @runInSeparateProcess
*/
#[RunInSeparateProcess]
public function testSendHtml(): void
{
foreach ([
Expand Down Expand Up @@ -69,6 +72,7 @@ public function testSendHtml(): void
/**
* @runInSeparateProcess
*/
#[RunInSeparateProcess]
public function testSendJson(): void
{
$data = [ 'key1' => 'value1', [ 'key2' => 'value2' ] ];
Expand Down Expand Up @@ -133,7 +137,7 @@ private function getConfig(string $param, $defval, $value)
{
$config = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->setMethods([ 'get' ])
->onlyMethods([ 'get' ])
->getMock();
$config->expects($this->once())
->method('get')
Expand Down
4 changes: 2 additions & 2 deletions tests/classes/Database/DatabaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private function getCoreWithSettings($data): object

private function getConnector(string $method, $callback): object
{
$con = $this->getMockBuilder(\StdClass::class)
->setMethods([ $method ])
$con = $this->getMockBuilder(Database\DatabaseConnector::class)
->disableOriginalConstructor()
->getMock();
$con->expects($this->once())
->method($this->equalTo($method))
Expand Down
10 changes: 4 additions & 6 deletions tests/classes/Domains/DomainListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Liuch\DmarcSrg\Users\UserList;
use Liuch\DmarcSrg\Domains\Domain;
use Liuch\DmarcSrg\Domains\DomainList;
use Liuch\DmarcSrg\Database\DomainMapperInterface;

class DomainListTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -38,18 +37,17 @@ public function testGettingNames(): void

private function getDbMapperOnce(string $method, $value): object
{
$mapper = $this->getMockBuilder(DomainMapperInterface::class)
$mapper = $this->getMockBuilder(Database\DomainMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
->getMock();
$mapper->expects($this->once())
->method($method)
->willReturnCallback(function () use ($value) {
return $value;
});

$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
$db = $this->getMockBuilder(Database\DatabaseConnector::class)
->disableOriginalConstructor()
->getMock();
$db->method('getMapper')
->with('domain')
Expand Down
7 changes: 3 additions & 4 deletions tests/classes/Domains/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->onlyMethods([ 'getMapper' ])
->getMock();
$db->expects($this->never())
->method('getMapper');
Expand All @@ -153,8 +153,7 @@ private function getDbMapperOnce(string $method, string $key, $value): object
{
$mapper = $this->getMockBuilder(DomainMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
->getMock();
$mr = $mapper->expects($this->once())->method($method);
if (empty($key)) {
if (!is_null($value)) {
Expand All @@ -168,7 +167,7 @@ private function getDbMapperOnce(string $method, string $key, $value): object

$db = $this->getMockBuilder(DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->onlyMethods([ 'getMapper' ])
->getMock();
$db->method('getMapper')
->with('domain')
Expand Down
14 changes: 6 additions & 8 deletions tests/classes/ReportLog/ReportLogItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Liuch\DmarcSrg\Exception\SoftException;
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
use Liuch\DmarcSrg\ReportLog\ReportLogItem;
use Liuch\DmarcSrg\Database\ReportLogMapperInterface;

class ReportLogItemTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -137,16 +136,15 @@ public function testSaving(): void

private function getDbMapperOnce(string $method, $callback): object
{
$mapper = $this->getMockBuilder(ReportLogMapperInterface::class)
$mapper = $this->getMockBuilder(Database\ReportLogMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
->getMock();
$mapper->expects($this->once())
->method($method)
->willReturnCallback($callback);

$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
$db = $this->getMockBuilder(Database\DatabaseConnector::class)
->disableOriginalConstructor()
->getMock();
$db->method('getMapper')
->with('report-log')
Expand All @@ -157,8 +155,8 @@ private function getDbMapperOnce(string $method, $callback): object

private function getDbMapperNever(): object
{
$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
$db = $this->getMockBuilder(Database\DatabaseConnector::class)
->disableOriginalConstructor()
->getMock();
$db->expects($this->never())
->method('getMapper');
Expand Down
11 changes: 4 additions & 7 deletions tests/classes/ReportLog/ReportLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Liuch\DmarcSrg\Sources\Source;
use Liuch\DmarcSrg\ReportLog\ReportLog;
use Liuch\DmarcSrg\Database\ReportLogMapperInterface;

class ReportLogTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -186,16 +185,14 @@ public function testDeleting(): void

private function getDbMapperOnce(string $method, $callback): object
{
$mapper = $this->getMockBuilder(ReportLogMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ $method ])
->getMockForAbstractClass();
$mapper = $this->getMockBuilder(Database\ReportLogMapperInterface::class)
->getMock();
$mapper->expects($this->once())
->method($method)
->willReturnCallback($callback);

$db = $this->getMockBuilder(\StdClass::class)
->setMethods([ 'getMapper' ])
$db = $this->getMockBuilder(Database\DatabaseConnector::class)
->disableOriginalConstructor()
->getMock();
$db->method('getMapper')
->with('report-log')
Expand Down
20 changes: 9 additions & 11 deletions tests/classes/Settings/SettingDefaultValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Liuch\DmarcSrg\Settings\SettingString;
use Liuch\DmarcSrg\Settings\SettingStringSelect;
use Liuch\DmarcSrg\Settings\SettingsList;
use Liuch\DmarcSrg\Database\DatabaseController;
use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;

class SettingDefaultValueTest extends \PHPUnit\Framework\TestCase
Expand Down Expand Up @@ -75,7 +74,7 @@ private function getCore(): object
{
return $this->getMockBuilder(Core::class)
->disableOriginalConstructor()
->setMethods([ 'user', 'database' ])
->onlyMethods([ 'user', 'database' ])
->getMock();
}

Expand All @@ -89,18 +88,17 @@ private function getCoreWithDatabaseNever($user): object

private function getCoreWithDatabaseMapperOnce($key, $user, $value): object
{
$mapper = $this->getMockBuilder(StdClass::class)
->disableOriginalConstructor()
->setMethods([ 'value' ])
$mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
->onlyMethods([ 'value', 'list', 'save' ])
->getMock();
$mapper->expects($this->once())
->method('value')
->with($key, $user->id())
->willReturn($value);

$db = $this->getMockBuilder(DatabaseController::class)
$db = $this->getMockBuilder(Database\DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->onlyMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
Expand All @@ -114,18 +112,18 @@ private function getCoreWithDatabaseMapperOnce($key, $user, $value): object

private function getCoreWithDatabaseMapperNotFound($parameter, $user): object
{
$mapper = $this->getMockBuilder(StdClass::class)
$mapper = $this->getMockBuilder(Database\SettingMapperInterface::class)
->disableOriginalConstructor()
->setMethods([ 'value' ])
->onlyMethods([ 'value', 'list', 'save' ])
->getMock();
$mapper->expects($this->once())
->method('value')
->with($this->equalTo($parameter))
->willThrowException(new DatabaseNotFoundException());

$db = $this->getMockBuilder(DatabaseController::class)
$db = $this->getMockBuilder(Database\DatabaseController::class)
->disableOriginalConstructor()
->setMethods([ 'getMapper' ])
->onlyMethods([ 'getMapper' ])
->getMock();
$db->expects($this->once())
->method('getMapper')
Expand Down
Loading

0 comments on commit b098f56

Please sign in to comment.