improve code

This commit is contained in:
Daniel Seifert 2022-11-25 15:42:33 +01:00
parent a3c75df635
commit 7a2648fe7b
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
18 changed files with 135 additions and 217 deletions

View File

@ -54,5 +54,8 @@
"psr-4": { "psr-4": {
"D3\\Totp\\": "../../../source/modules/d3/totp" "D3\\Totp\\": "../../../source/modules/d3/totp"
} }
},
"scripts": {
"totp_phpstan": "./vendor/bin/phpstan -c./vendor/d3/oxtotp/phpstan.neon"
} }
} }

View File

@ -6,3 +6,9 @@ parameters:
- src - src
level: 5 level: 5
phpVersion: 70300 phpVersion: 70300
ignoreErrors:
- '#Call to method getFieldData\(\) on an unknown class oxShop.#'
- '#Return type \(array\) of method D3\\Totp\\Application\\Controller\\d3totplogin::getBreadCrumb\(\) should be compatible with return type \(null\) of method OxidEsales\\EshopCommunity\\Application\\Controller\\FrontendController::getBreadCrumb\(\)#'
- '#Parameter \#\d+ \$value of method OxidEsales\\EshopCommunity\\Core\\Config::setConfigParam\(\) expects string, (true|false) given.#'
parallel:
processTimeout: 900.0

View File

@ -56,6 +56,7 @@ class d3totpadminlogin extends AdminController
*/ */
protected function isTotpIsNotRequired(): bool protected function isTotpIsNotRequired(): bool
{ {
/** @var d3_totp_user $user */
$user = $this->d3TotpGetUserObject(); $user = $this->d3TotpGetUserObject();
$userId = $user->d3TotpGetCurrentUser(); $userId = $user->d3TotpGetCurrentUser();
@ -131,15 +132,16 @@ class d3totpadminlogin extends AdminController
*/ */
public function d3CancelLogin(): string public function d3CancelLogin(): string
{ {
/** @var d3_totp_user $oUser */
$oUser = $this->d3TotpGetUserObject(); $oUser = $this->d3TotpGetUserObject();
$oUser->logout(); $oUser->logout();
return "login"; return "login";
} }
/** /**
* @return d3_totp_user * @return User
*/ */
public function d3TotpGetUserObject(): d3_totp_user public function d3TotpGetUserObject(): User
{ {
return oxNew(User::class); return oxNew(User::class);
} }
@ -156,7 +158,7 @@ class d3totpadminlogin extends AdminController
$userId = $user->d3TotpGetCurrentUser(); $userId = $user->d3TotpGetCurrentUser();
try { try {
$sTotp = implode('', Registry::getRequest()->getRequestEscapedParameter('d3totp', [])); $sTotp = implode('', Registry::getRequest()->getRequestEscapedParameter('d3totp') ?: []);
$totp = $this->d3TotpGetTotpObject(); $totp = $this->d3TotpGetTotpObject();
$totp->loadByUserId($userId); $totp->loadByUserId($userId);
@ -223,7 +225,7 @@ class d3totpadminlogin extends AdminController
} }
/** /**
* @return d3_totp_LoginController * @return LoginController
*/ */
public function d3GetLoginController(): LoginController public function d3GetLoginController(): LoginController
{ {

View File

@ -108,7 +108,7 @@ class d3_account_totp extends AccountController
$oTotp->save(); $oTotp->save();
$oTotpBackupCodes->save(); $oTotpBackupCodes->save();
} catch (Exception $oExcp) { } catch (Exception $oExcp) {
Registry::get(UtilsView::class)->addErrorToDisplay($oExcp); Registry::get(UtilsView::class)->addErrorToDisplay($oExcp->getMessage());
} }
} }
} }

View File

@ -100,7 +100,7 @@ class d3totplogin extends FrontendController
* *
* @return array * @return array
*/ */
public function getBreadCrumb(): array public function getBreadCrumb()
{ {
$aPaths = []; $aPaths = [];
$aPath = []; $aPath = [];

View File

@ -268,7 +268,7 @@ class d3totp extends BaseModel
} }
/** /**
* @param null $oxid * @param null|string $oxid
* @return bool * @return bool
* @throws DatabaseConnectionException * @throws DatabaseConnectionException
*/ */

View File

@ -84,7 +84,7 @@ class d3_totp_UserComponent extends d3_totp_UserComponent_parent
*/ */
public function d3TotpCheckTotpLogin() public function d3TotpCheckTotpLogin()
{ {
$sTotp = implode('', Registry::getRequest()->getRequestEscapedParameter('d3totp', [])); $sTotp = implode('', Registry::getRequest()->getRequestEscapedParameter('d3totp') ?: []);
/** @var d3_totp_user $oUser */ /** @var d3_totp_user $oUser */
$oUser = oxNew(User::class); $oUser = oxNew(User::class);
@ -99,7 +99,7 @@ class d3_totp_UserComponent extends d3_totp_UserComponent_parent
// relogin, don't extract from this try block // relogin, don't extract from this try block
$this->d3TotpGetSession()->setVariable(d3totp_conf::SESSION_AUTH, $oUser->getId()); $this->d3TotpGetSession()->setVariable(d3totp_conf::SESSION_AUTH, $oUser->getId());
$this->d3TotpGetSession()->setVariable(d3totp_conf::OXID_FRONTEND_AUTH, $oUser->getId()); $this->d3TotpGetSession()->setVariable(d3totp_conf::OXID_FRONTEND_AUTH, $oUser->getId());
$this->setUser(null); $this->setUser(oxNew(User::class));
$this->setLoginStatus(USER_LOGIN_SUCCESS); $this->setLoginStatus(USER_LOGIN_SUCCESS);
$this->_afterLogin($oUser); $this->_afterLogin($oUser);

View File

@ -21,8 +21,10 @@ use D3\Totp\Application\Model\d3totp_conf;
use D3\Totp\Modules\Application\Model\d3_totp_user; use D3\Totp\Modules\Application\Model\d3_totp_user;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
use OxidEsales\Eshop\Core\Language;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use OxidEsales\Eshop\Core\UtilsServer;
class d3_totp_LoginController extends d3_totp_LoginController_parent class d3_totp_LoginController extends d3_totp_LoginController_parent
{ {
@ -31,7 +33,7 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent
/** /**
* @return d3totp * @return d3totp
*/ */
public function d3GetTotpObject() public function d3GetTotpObject(): d3totp
{ {
return oxNew(d3totp::class); return oxNew(d3totp::class);
} }
@ -39,7 +41,7 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent
/** /**
* @return Session * @return Session
*/ */
public function d3TotpGetSession() public function d3TotpGetSession(): Session
{ {
return Registry::getSession(); return Registry::getSession();
} }
@ -111,25 +113,17 @@ class d3_totp_LoginController extends d3_totp_LoginController_parent
} }
/** /**
* @return d3_totp_user * @return UtilsServer
*/ */
protected function d3TotpGetUserObject(): d3_totp_user protected function d3TotpGetUtilsServer(): UtilsServer
{
return oxNew( User::class );
}
/**
* @return object|\OxidEsales\Eshop\Core\UtilsServer
*/
protected function d3TotpGetUtilsServer()
{ {
return Registry::getUtilsServer(); return Registry::getUtilsServer();
} }
/** /**
* @return object|\OxidEsales\Eshop\Core\Language * @return Language
*/ */
protected function d3TotpGetLangObject() protected function d3TotpGetLangObject(): Language
{ {
return Registry::getLang(); return Registry::getLang();
} }

View File

@ -91,10 +91,10 @@ class totpSystemEventHandler extends totpSystemEventHandler_parent
} }
/** /**
* @return d3_totp_user * @return User
*/ */
protected function d3TotpGetUserObject(): d3_totp_user protected function d3TotpGetUserObject(): User
{ {
return oxNew( User::class ); return oxNew(User::class);
} }
} }

View File

@ -84,7 +84,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
*/ */
public function canRenderSelectedUser() public function canRenderSelectedUser()
{ {
/** @var User|MockObject $oControllerMock */ /** @var User|MockObject $oUserMock */
$oUserMock = $this->getMockBuilder(User::class) $oUserMock = $this->getMockBuilder(User::class)
->onlyMethods([ ->onlyMethods([
'getId', 'getId',
@ -123,7 +123,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
*/ */
public function canRenderUnloadableUser() public function canRenderUnloadableUser()
{ {
/** @var User|MockObject $oControllerMock */ /** @var User|MockObject $oUserMock */
$oUserMock = $this->getMockBuilder(User::class) $oUserMock = $this->getMockBuilder(User::class)
->onlyMethods([ ->onlyMethods([
'getId', 'getId',
@ -219,13 +219,13 @@ class d3user_totpTest extends d3TotpUnitTestCase
*/ */
public function cantSaveBecauseOfNotVerifiable() public function cantSaveBecauseOfNotVerifiable()
{ {
/** @var d3backupcodelist|MockObject $oControllerMock */ /** @var d3backupcodelist|MockObject $oBackupCodeListMock */
$oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class) $oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods(['save']) ->onlyMethods(['save'])
->getMock(); ->getMock();
$oBackupCodeListMock->expects($this->never())->method('save')->willReturn(true); $oBackupCodeListMock->expects($this->never())->method('save')->willReturn(true);
/** @var d3totp|MockObject $oControllerMock */ /** @var d3totp|MockObject $oTotpMock */
$oTotpMock = $this->getMockBuilder(d3totp::class) $oTotpMock = $this->getMockBuilder(d3totp::class)
->onlyMethods([ ->onlyMethods([
'load', 'load',
@ -269,13 +269,13 @@ class d3user_totpTest extends d3TotpUnitTestCase
*/ */
public function cantSaveBecauseExistingRegistration() public function cantSaveBecauseExistingRegistration()
{ {
/** @var d3backupcodelist|MockObject $oControllerMock */ /** @var d3backupcodelist|MockObject $oBackupCodeListMock */
$oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class) $oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods(['save']) ->onlyMethods(['save'])
->getMock(); ->getMock();
$oBackupCodeListMock->expects($this->never())->method('save')->willReturn(true); $oBackupCodeListMock->expects($this->never())->method('save')->willReturn(true);
/** @var d3totp|MockObject $oControllerMock */ /** @var d3totp|MockObject $oTotpMock */
$oTotpMock = $this->getMockBuilder(d3totp::class) $oTotpMock = $this->getMockBuilder(d3totp::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->onlyMethods([ ->onlyMethods([
@ -319,7 +319,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
*/ */
public function canSave() public function canSave()
{ {
/** @var d3backupcodelist|MockObject $oControllerMock */ /** @var d3backupcodelist|MockObject $oBackupCodeListMock */
$oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class) $oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods([ ->onlyMethods([
'save', 'save',
@ -329,7 +329,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
$oBackupCodeListMock->expects($this->once())->method('save')->willReturn(true); $oBackupCodeListMock->expects($this->once())->method('save')->willReturn(true);
$oBackupCodeListMock->method('generateBackupCodes')->willReturn(true); $oBackupCodeListMock->method('generateBackupCodes')->willReturn(true);
/** @var d3totp|MockObject $oControllerMock */ /** @var d3totp|MockObject $oTotpMock */
$oTotpMock = $this->getMockBuilder(d3totp::class) $oTotpMock = $this->getMockBuilder(d3totp::class)
->onlyMethods([ ->onlyMethods([
'load', 'load',
@ -378,7 +378,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
]; ];
$_GET['editval'] = $aEditval; $_GET['editval'] = $aEditval;
/** @var d3backupcodelist|MockObject $oControllerMock */ /** @var d3backupcodelist|MockObject $oBackupCodeListMock */
$oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class) $oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods([ ->onlyMethods([
'save', 'save',
@ -388,7 +388,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
$oBackupCodeListMock->expects($this->once())->method('save')->willReturn(true); $oBackupCodeListMock->expects($this->once())->method('save')->willReturn(true);
$oBackupCodeListMock->method('generateBackupCodes')->willReturn(true); $oBackupCodeListMock->method('generateBackupCodes')->willReturn(true);
/** @var d3totp|MockObject $oControllerMock */ /** @var d3totp|MockObject $oTotpMock */
$oTotpMock = $this->getMockBuilder(d3totp::class) $oTotpMock = $this->getMockBuilder(d3totp::class)
->onlyMethods([ ->onlyMethods([
'load', 'load',
@ -514,7 +514,7 @@ class d3user_totpTest extends d3TotpUnitTestCase
*/ */
public function canGetAvailableBackupCodeCount() public function canGetAvailableBackupCodeCount()
{ {
/** @var d3backupcodelist|MockObject $oControllerMock */ /** @var d3backupcodelist|MockObject $oBackupCodeListMock */
$oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class) $oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods(['getAvailableCodeCount']) ->onlyMethods(['getAvailableCodeCount'])
->getMock(); ->getMock();

View File

@ -148,7 +148,7 @@ class d3_account_totpTest extends d3TotpUnitTestCase
*/ */
public function canGetAvailableBackupCodeCount() public function canGetAvailableBackupCodeCount()
{ {
/** @var d3backupcodelist|MockObject $oControllerMock */ /** @var d3backupcodelist|MockObject $oBackupCodeListMock */
$oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class) $oBackupCodeListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods(['getAvailableCodeCount']) ->onlyMethods(['getAvailableCodeCount'])
->getMock(); ->getMock();

View File

@ -57,7 +57,7 @@ class d3backupcodelistTest extends d3TotpUnitTestCase
*/ */
public function generateBackupCodes() public function generateBackupCodes()
{ {
/** @var FrontendController|MockObject $oConfigMock */ /** @var FrontendController|MockObject $oViewMock */
$oViewMock = $this->getMockBuilder(FrontendController::class) $oViewMock = $this->getMockBuilder(FrontendController::class)
->addMethods(['setBackupCodes']) ->addMethods(['setBackupCodes'])
->getMock(); ->getMock();

View File

@ -36,28 +36,6 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
/** @var d3_totp_UserComponent */
protected $_oController;
/**
* setup basic requirements
*/
public function setUp(): void
{
parent::setUp();
$this->_oController = oxNew(UserComponent::class);
Registry::getSession()->setVariable(d3totp_conf::SESSION_AUTH, false);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->_oController);
}
/** /**
* @test * @test
* @throws ReflectionException * @throws ReflectionException
@ -99,11 +77,9 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
$oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock); $oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock);
$this->_oController = $oControllerMock;
$this->expectException( InvalidArgumentException::class); $this->expectException( InvalidArgumentException::class);
$this->callMethod($this->_oController, '_afterLogin', [$oUser]); $this->callMethod($oControllerMock, '_afterLogin', [$oUser]);
} }
/** /**
@ -160,9 +136,7 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
$oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock); $oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock);
$this->_oController = $oControllerMock; $this->callMethod($oControllerMock, '_afterLogin', [$oUserMock]);
$this->callMethod($this->_oController, '_afterLogin', [$oUserMock]);
} }
/** /**
@ -238,9 +212,7 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
$oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock); $oControllerMock->method('d3TotpGetUtils')->willReturn($oUtilsMock);
$this->_oController = $oControllerMock; $this->callMethod($oControllerMock, '_afterLogin', [$oUserMock]);
$this->callMethod($this->_oController, '_afterLogin', [$oUserMock]);
} }
/** /**
@ -250,9 +222,12 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
*/ */
public function d3GetTotpObjectReturnsRightInstance() public function d3GetTotpObjectReturnsRightInstance()
{ {
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertInstanceOf( $this->assertInstanceOf(
d3totp::class, d3totp::class,
$this->callMethod($this->_oController, 'd3GetTotpObject') $this->callMethod($oController, 'd3GetTotpObject')
); );
} }
@ -290,11 +265,9 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock); $oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
$oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$this->_oController = $oControllerMock;
$this->assertSame( $this->assertSame(
'd3totplogin', 'd3totplogin',
$this->callMethod($this->_oController, 'd3TotpCheckTotpLogin') $this->callMethod($oControllerMock, 'd3TotpCheckTotpLogin')
); );
} }
@ -318,7 +291,7 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oSessionMock->expects($this->never())->method('setVariable'); $oSessionMock->expects($this->never())->method('setVariable');
/** @var d3totp_wrongOtpException|MockObject $oUtilsViewMock */ /** @var d3totp_wrongOtpException|MockObject $oTotpExceptionMock */
$oTotpExceptionMock = $this->getMockBuilder(d3totp_wrongOtpException::class) $oTotpExceptionMock = $this->getMockBuilder(d3totp_wrongOtpException::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -345,11 +318,9 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
$oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock); $oControllerMock->method('d3GetTotpObject')->willReturn($oTotpMock);
$oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$this->_oController = $oControllerMock;
$this->assertSame( $this->assertSame(
'd3totplogin', 'd3totplogin',
$this->callMethod($this->_oController, 'd3TotpCheckTotpLogin') $this->callMethod($oControllerMock, 'd3TotpCheckTotpLogin')
); );
} }
@ -399,10 +370,8 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
$this->identicalTo(USER_LOGIN_SUCCESS) $this->identicalTo(USER_LOGIN_SUCCESS)
); );
$this->_oController = $oControllerMock;
$this->assertFalse( $this->assertFalse(
$this->callMethod($this->_oController, 'd3TotpCheckTotpLogin') $this->callMethod($oControllerMock, 'd3TotpCheckTotpLogin')
); );
} }
@ -413,9 +382,12 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
*/ */
public function d3GetUtilsViewReturnsRightInstance() public function d3GetUtilsViewReturnsRightInstance()
{ {
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertInstanceOf( $this->assertInstanceOf(
UtilsView::class, UtilsView::class,
$this->callMethod($this->_oController, 'd3TotpGetUtilsView') $this->callMethod($oController, 'd3TotpGetUtilsView')
); );
} }
@ -426,9 +398,12 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
*/ */
public function d3GetUtilsReturnsRightInstance() public function d3GetUtilsReturnsRightInstance()
{ {
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertInstanceOf( $this->assertInstanceOf(
Utils::class, Utils::class,
$this->callMethod($this->_oController, 'd3TotpGetUtils') $this->callMethod($oController, 'd3TotpGetUtils')
); );
} }
@ -445,9 +420,7 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oControllerMock->expects($this->once())->method('d3TotpClearSessionVariables')->willReturn(false); $oControllerMock->expects($this->once())->method('d3TotpClearSessionVariables')->willReturn(false);
$this->_oController = $oControllerMock; $this->callMethod($oControllerMock, 'd3TotpCancelTotpLogin');
$this->callMethod($this->_oController, 'd3TotpCancelTotpLogin');
} }
/** /**
@ -466,8 +439,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('isActive')->willReturn(true); $oTotpMock->method('isActive')->willReturn(true);
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertTrue( $this->assertTrue(
$this->callMethod($this->_oController, 'd3TotpIsNoTotpOrNoLogin', [$oTotpMock]) $this->callMethod($oController, 'd3TotpIsNoTotpOrNoLogin', [$oTotpMock])
); );
} }
@ -487,8 +463,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('isActive')->willReturn(false); $oTotpMock->method('isActive')->willReturn(false);
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertTrue( $this->assertTrue(
$this->callMethod($this->_oController, 'd3TotpIsNoTotpOrNoLogin', [$oTotpMock]) $this->callMethod($oController, 'd3TotpIsNoTotpOrNoLogin', [$oTotpMock])
); );
} }
@ -508,8 +487,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('isActive')->willReturn(true); $oTotpMock->method('isActive')->willReturn(true);
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertFalse( $this->assertFalse(
$this->callMethod($this->_oController, 'd3TotpIsNoTotpOrNoLogin', [$oTotpMock]) $this->callMethod($oController, 'd3TotpIsNoTotpOrNoLogin', [$oTotpMock])
); );
} }
@ -529,8 +511,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('verify')->willReturn(false); $oTotpMock->method('verify')->willReturn(false);
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertTrue( $this->assertTrue(
$this->callMethod($this->_oController, 'd3TotpHasValidTotp', ['123456', $oTotpMock]) $this->callMethod($oController, 'd3TotpHasValidTotp', ['123456', $oTotpMock])
); );
} }
@ -550,8 +535,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('verify')->willReturn(true); $oTotpMock->method('verify')->willReturn(true);
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertTrue( $this->assertTrue(
$this->callMethod($this->_oController, 'd3TotpHasValidTotp', ['123456', $oTotpMock]) $this->callMethod($oController, 'd3TotpHasValidTotp', ['123456', $oTotpMock])
); );
} }
@ -571,8 +559,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('verify')->willThrowException(oxNew(d3totp_wrongOtpException::class)); $oTotpMock->method('verify')->willThrowException(oxNew(d3totp_wrongOtpException::class));
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->expectException(d3totp_wrongOtpException::class); $this->expectException(d3totp_wrongOtpException::class);
$this->callMethod($this->_oController, 'd3TotpHasValidTotp', [null, $oTotpMock]); $this->callMethod($oController, 'd3TotpHasValidTotp', [null, $oTotpMock]);
} }
/** /**
@ -591,8 +582,11 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oTotpMock->method('verify')->willReturn(false); $oTotpMock->method('verify')->willReturn(false);
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertFalse( $this->assertFalse(
$this->callMethod($this->_oController, 'd3TotpHasValidTotp', ['123456', $oTotpMock]) $this->callMethod($oController, 'd3TotpHasValidTotp', ['123456', $oTotpMock])
); );
} }
@ -615,9 +609,7 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$this->_oController = $oControllerMock; $this->callMethod($oControllerMock, 'd3TotpClearSessionVariables');
$this->callMethod($this->_oController, 'd3TotpClearSessionVariables');
} }
/** /**
@ -627,9 +619,12 @@ class d3_totp_UserComponentTest extends d3TotpUnitTestCase
*/ */
public function d3GetSessionReturnsRightInstance() public function d3GetSessionReturnsRightInstance()
{ {
/** @var d3_totp_UserComponent $oController */
$oController = oxNew(UserComponent::class);
$this->assertInstanceOf( $this->assertInstanceOf(
Session::class, Session::class,
$this->callMethod($this->_oController, 'd3TotpGetSession') $this->callMethod($oController, 'd3TotpGetSession')
); );
} }
} }

View File

@ -24,25 +24,5 @@ class d3_totp_OrderControllerTest extends d3TotpUnitTestCase
{ {
use d3_totp_getUserTestTrait; use d3_totp_getUserTestTrait;
/** @var d3_totp_OrderController|MockObject */ protected $sControllerClass = OrderController::class;
protected $_oController;
protected $sControllerClass = d3_totp_OrderController::class;
/**
* setup basic requirements
*/
public function setUp(): void
{
parent::setUp();
$this->_oController = oxNew(OrderController::class);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->_oController);
}
} }

View File

@ -23,25 +23,5 @@ class d3_totp_PaymentControllerTest extends d3TotpUnitTestCase
{ {
use d3_totp_getUserTestTrait; use d3_totp_getUserTestTrait;
/** @var d3_totp_PaymentController */ protected $sControllerClass = PaymentController::class;
protected $_oController;
protected $sControllerClass = d3_totp_PaymentController::class;
/**
* setup basic requirements
*/
public function setUp(): void
{
parent::setUp();
$this->_oController = oxNew(PaymentController::class);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->_oController);
}
} }

View File

@ -23,25 +23,5 @@ class d3_totp_UserControllerTest extends d3TotpUnitTestCase
{ {
use d3_totp_getUserTestTrait; use d3_totp_getUserTestTrait;
/** @var d3_totp_UserController */ protected $sControllerClass = UserController::class;
protected $_oController;
protected $sControllerClass = d3_totp_UserController::class;
/**
* setup basic requirements
*/
public function setUp(): void
{
parent::setUp();
$this->_oController = oxNew(UserController::class);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->_oController);
}
} }

View File

@ -18,6 +18,8 @@ namespace D3\Totp\tests\unit\Modules\Application\Controller;
use D3\TestingTools\Development\CanAccessRestricted; use D3\TestingTools\Development\CanAccessRestricted;
use D3\Totp\Application\Model\d3totp; use D3\Totp\Application\Model\d3totp;
use D3\Totp\Modules\Application\Controller\d3_totp_OrderController; use D3\Totp\Modules\Application\Controller\d3_totp_OrderController;
use D3\Totp\Modules\Application\Controller\d3_totp_PaymentController;
use D3\Totp\Modules\Application\Controller\d3_totp_UserController;
use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Session;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
@ -36,16 +38,14 @@ trait d3_totp_getUserTestTrait
*/ */
public function getUserHasNoUser() public function getUserHasNoUser()
{ {
/** @var d3_totp_orderController|MockObject $oControllerMock */ /** @var d3_totp_orderController|d3_totp_UserController|d3_totp_PaymentController|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder($this->sControllerClass) $oControllerMock = $this->getMockBuilder($this->sControllerClass)
->onlyMethods(['d3GetTotpObject']) ->onlyMethods(['d3GetTotpObject'])
->getMock(); ->getMock();
$oControllerMock->expects($this->never())->method('d3GetTotpObject'); $oControllerMock->expects($this->never())->method('d3GetTotpObject');
$this->_oController = $oControllerMock;
$this->assertFalse( $this->assertFalse(
$this->callMethod($this->_oController, 'getUser') $this->callMethod($oControllerMock, 'getUser')
); );
} }
@ -81,7 +81,7 @@ trait d3_totp_getUserTestTrait
$oTotpMock->method('isActive')->willReturn(false); $oTotpMock->method('isActive')->willReturn(false);
$oTotpMock->method('loadByUserId')->willReturn(true); $oTotpMock->method('loadByUserId')->willReturn(true);
/** @var d3_totp_orderController|MockObject $oControllerMock */ /** @var d3_totp_orderController|d3_totp_UserController|d3_totp_PaymentController|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder($this->sControllerClass) $oControllerMock = $this->getMockBuilder($this->sControllerClass)
->onlyMethods([ ->onlyMethods([
'd3GetTotpObject', 'd3GetTotpObject',
@ -93,11 +93,9 @@ trait d3_totp_getUserTestTrait
$oControllerMock->method('d3TotpGetSessionObject')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSessionObject')->willReturn($oSessionMock);
$oControllerMock->method('d3CallMockableParent')->willReturn($oUserMock); $oControllerMock->method('d3CallMockableParent')->willReturn($oUserMock);
$this->_oController = $oControllerMock;
$this->assertSame( $this->assertSame(
$oUserMock, $oUserMock,
$this->callMethod($this->_oController, 'getUser') $this->callMethod($oControllerMock, 'getUser')
); );
} }
@ -132,7 +130,7 @@ trait d3_totp_getUserTestTrait
$oTotpMock->method('isActive')->willReturn(true); $oTotpMock->method('isActive')->willReturn(true);
$oTotpMock->method('loadByUserId')->willReturn(true); $oTotpMock->method('loadByUserId')->willReturn(true);
/** @var d3_totp_orderController|MockObject $oControllerMock */ /** @var d3_totp_orderController|d3_totp_UserController|d3_totp_PaymentController|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder($this->sControllerClass) $oControllerMock = $this->getMockBuilder($this->sControllerClass)
->onlyMethods([ ->onlyMethods([
'd3GetTotpObject', 'd3GetTotpObject',
@ -144,11 +142,9 @@ trait d3_totp_getUserTestTrait
$oControllerMock->method('d3TotpGetSessionObject')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSessionObject')->willReturn($oSessionMock);
$oControllerMock->method('d3CallMockableParent')->willReturn($oUserMock); $oControllerMock->method('d3CallMockableParent')->willReturn($oUserMock);
$this->_oController = $oControllerMock;
$this->assertSame( $this->assertSame(
$oUserMock, $oUserMock,
$this->callMethod($this->_oController, 'getUser') $this->callMethod($oControllerMock, 'getUser')
); );
} }
@ -184,7 +180,7 @@ trait d3_totp_getUserTestTrait
$oTotpMock->method('isActive')->willReturn(true); $oTotpMock->method('isActive')->willReturn(true);
$oTotpMock->method('loadByUserId')->willReturn(true); $oTotpMock->method('loadByUserId')->willReturn(true);
/** @var d3_totp_orderController|MockObject $oControllerMock */ /** @var d3_totp_orderController|d3_totp_UserController|d3_totp_PaymentController|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder($this->sControllerClass) $oControllerMock = $this->getMockBuilder($this->sControllerClass)
->onlyMethods([ ->onlyMethods([
'd3GetTotpObject', 'd3GetTotpObject',
@ -196,10 +192,8 @@ trait d3_totp_getUserTestTrait
$oControllerMock->method('d3TotpGetSessionObject')->willReturn($oSessionMock); $oControllerMock->method('d3TotpGetSessionObject')->willReturn($oSessionMock);
$oControllerMock->method('d3CallMockableParent')->willReturn($oUserMock); $oControllerMock->method('d3CallMockableParent')->willReturn($oUserMock);
$this->_oController = $oControllerMock;
$this->assertFalse( $this->assertFalse(
$this->callMethod($this->_oController, 'getUser') $this->callMethod($oControllerMock, 'getUser')
); );
} }
@ -212,9 +206,12 @@ trait d3_totp_getUserTestTrait
*/ */
public function d3GetTotpObjectReturnsRightObject() public function d3GetTotpObjectReturnsRightObject()
{ {
/** @var d3_totp_UserController|d3_totp_PaymentController|d3_totp_OrderController $oController */
$oController = oxNew($this->sControllerClass);
$this->assertInstanceOf( $this->assertInstanceOf(
d3totp::class, d3totp::class,
$this->callMethod($this->_oController, 'd3GetTotpObject') $this->callMethod($oController, 'd3GetTotpObject')
); );
} }
@ -227,24 +224,15 @@ trait d3_totp_getUserTestTrait
*/ */
public function d3GetSessionObjectReturnsRightObject() public function d3GetSessionObjectReturnsRightObject()
{ {
/** @var d3_totp_UserController|d3_totp_PaymentController|d3_totp_OrderController $oController */
$oController = oxNew($this->sControllerClass);
$this->assertInstanceOf( $this->assertInstanceOf(
Session::class, Session::class,
$this->callMethod( $this->callMethod(
$this->_oController, $oController,
'd3TotpGetSessionObject' 'd3TotpGetSessionObject'
) )
); );
} }
/**
* @te__st
* @throws ReflectionException
* @covers \D3\Totp\Modules\Application\Controller\d3_totp_OrderController::d3CallMockableParent
* @covers \D3\Totp\Modules\Application\Controller\d3_totp_PaymentController::d3CallMockableParent
* @covers \D3\Totp\Modules\Application\Controller\d3_totp_UserController::d3CallMockableParent
*/
public function d3callMockableParentTest()
{
$this->callMockableParentTest($this->_oController);
}
} }

View File

@ -29,26 +29,6 @@ class d3_totp_userTest extends d3TotpUnitTestCase
{ {
use CanAccessRestricted; use CanAccessRestricted;
/** @var d3_totp_user */
protected $_oModel;
/**
* setup basic requirements
*/
public function setUp(): void
{
parent::setUp();
$this->_oModel = oxNew(User::class);
}
public function tearDown(): void
{
parent::tearDown();
unset($this->_oModel);
}
/** /**
* @test * @test
* @throws ReflectionException * @throws ReflectionException
@ -68,11 +48,11 @@ class d3_totp_userTest extends d3TotpUnitTestCase
->getMock(); ->getMock();
$oModelMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oModelMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$this->_oModel = $oModelMock; $sut = $oModelMock;
$this->assertTrue( $this->assertTrue(
$this->callMethod( $this->callMethod(
$this->_oModel, $sut,
'logout' 'logout'
) )
); );
@ -85,9 +65,14 @@ class d3_totp_userTest extends d3TotpUnitTestCase
*/ */
public function d3getTotpReturnsRightInstance() public function d3getTotpReturnsRightInstance()
{ {
$sut = oxNew(User::class);
$this->assertInstanceOf( $this->assertInstanceOf(
d3totp::class, d3totp::class,
$this->callMethod($this->_oModel, 'd3getTotp') $this->callMethod(
$sut,
'd3getTotp'
)
); );
} }
@ -98,9 +83,14 @@ class d3_totp_userTest extends d3TotpUnitTestCase
*/ */
public function d3GetSessionReturnsRightInstance() public function d3GetSessionReturnsRightInstance()
{ {
$sut = oxNew(User::class);
$this->assertInstanceOf( $this->assertInstanceOf(
Session::class, Session::class,
$this->callMethod($this->_oModel, 'd3TotpGetSession') $this->callMethod(
$sut,
'd3TotpGetSession'
)
); );
} }
@ -138,12 +128,12 @@ class d3_totp_userTest extends d3TotpUnitTestCase
$oModelMock->method('d3TotpGetSession')->willReturn($oSessionMock); $oModelMock->method('d3TotpGetSession')->willReturn($oSessionMock);
$oModelMock->method('isAdmin')->willReturn($isAdmin); $oModelMock->method('isAdmin')->willReturn($isAdmin);
$this->_oModel = $oModelMock; $sut = $oModelMock;
$this->assertSame( $this->assertSame(
$expected, $expected,
$this->callMethod( $this->callMethod(
$this->_oModel, $sut,
'd3TotpGetCurrentUser' 'd3TotpGetCurrentUser'
) )
); );