diff --git a/composer.json b/composer.json index e526e60..e3fd33a 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ "nyholm/psr7": "^1.5.1", "nyholm/psr7-server": "^1.0.2", "ext-json": "*", - "d3/testingtools": "^1.1" + "d3/testingtools": "^1.1", + "d3/oxid-dic-handler": "^1.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.19", diff --git a/src/Application/Controller/Admin/d3user_webauthn.php b/src/Application/Controller/Admin/d3user_webauthn.php index b9679ab..73ecd5e 100755 --- a/src/Application/Controller/Admin/d3user_webauthn.php +++ b/src/Application/Controller/Admin/d3user_webauthn.php @@ -33,6 +33,7 @@ use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; use Throwable; class d3user_webauthn extends AdminDetailsController @@ -48,7 +49,9 @@ class d3user_webauthn extends AdminDetailsController */ public function render(): string { - $this->addTplParam('readonly', !$this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable()); + /** @var Webauthn $webauthn */ + $webauthn = d3GetOxidDIC()->get(Webauthn::class); + $this->addTplParam('readonly', !$webauthn->isAvailable()); $this->d3CallMockableFunction([AdminDetailsController::class, 'render']); @@ -56,7 +59,7 @@ class d3user_webauthn extends AdminDetailsController if ($soxId != "-1") { /** @var d3_User_Webauthn $oUser */ - $oUser = $this->d3GetMockableOxNewObject(User::class); + $oUser = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); if ($oUser->load($soxId)) { $this->addTplParam("oxid", $oUser->getId()); } else { @@ -81,10 +84,10 @@ class d3user_webauthn extends AdminDetailsController $this->setPageType('requestnew'); $this->setAuthnRegister(); } catch (Exception|ContainerExceptionInterface|NotFoundExceptionInterface|DoctrineDriverException $e) { - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); - $this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); - $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=d3user_webauthn'); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e->getMessage()); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=d3user_webauthn'); } } @@ -105,17 +108,17 @@ class d3user_webauthn extends AdminDetailsController $credential = Registry::getRequest()->getRequestEscapedParameter('credential'); if (strlen((string) $credential)) { - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname')); } } catch (WebauthnException $e) { - $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $this->getEditObjectId()]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); } catch (Exception|NotFoundExceptionInterface|ContainerExceptionInterface|DoctrineDriverException $e) { - $this->d3GetMockableLogger()->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), ['UserId' => $this->getEditObjectId()]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e->getMessage()); } } @@ -134,9 +137,9 @@ class d3user_webauthn extends AdminDetailsController */ public function setAuthnRegister(): void { - $authn = $this->d3GetMockableOxNewObject(Webauthn::class); + $authn = d3GetOxidDIC()->get(Webauthn::class); - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $user->load($this->getEditObjectId()); $publicKeyCredentialCreationOptions = $authn->getCreationOptions($user); @@ -160,10 +163,10 @@ class d3user_webauthn extends AdminDetailsController */ public function getCredentialList($userId): array { - $oUser = $this->d3GetMockableOxNewObject(User::class); + $oUser = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $oUser->load($userId); - $publicKeyCredentials = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); + $publicKeyCredentials = d3GetOxidDIC()->get(PublicKeyCredentialList::class); return $publicKeyCredentials->getAllFromUser($oUser)->getArray(); } @@ -172,7 +175,7 @@ class d3user_webauthn extends AdminDetailsController */ public function deleteKey(): void { - $credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); + $credential = d3GetOxidDIC()->get(PublicKeyCredential::class); $credential->delete(Registry::getRequest()->getRequestEscapedParameter('deleteoxid')); } } diff --git a/src/Application/Controller/Admin/d3webauthnadminlogin.php b/src/Application/Controller/Admin/d3webauthnadminlogin.php index 0a85d81..f9e2364 100755 --- a/src/Application/Controller/Admin/d3webauthnadminlogin.php +++ b/src/Application/Controller/Admin/d3webauthnadminlogin.php @@ -33,6 +33,7 @@ use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; class d3webauthnadminlogin extends AdminController { @@ -57,28 +58,28 @@ class d3webauthnadminlogin extends AdminController */ public function render(): string { - if ($this->d3GetMockableRegistryObject(Session::class) + if (d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) ) { - $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=admin_start'); - } elseif (!$this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=admin_start'); + } elseif (!d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) ) { - $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login'); + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=login'); } $this->generateCredentialRequest(); - $this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class) + $this->addTplParam('navFormParams', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); - $this->addTplParam('currentProfile', $this->d3GetMockableRegistryObject(Session::class) + $this->addTplParam('currentProfile', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE)); - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); - $this->addTplParam('currentChLanguage', $this->d3GetMockableRegistryObject(Session::class) + $this->addTplParam('currentChLanguage', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE)); - $afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class); + $afterLogin = d3GetOxidDIC()->get(WebauthnAfterLogin::class); $afterLogin->changeLanguage(); return $this->d3CallMockableFunction([AdminController::class, 'render']); @@ -93,22 +94,22 @@ class d3webauthnadminlogin extends AdminController */ public function generateCredentialRequest(): void { - $userId = $this->d3GetMockableRegistryObject(Session::class) + $userId = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER); try { - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); $publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->setVariable(WebauthnConf::WEBAUTHN_ADMIN_LOGIN_OBJECT, $publicKeyCredentialRequestOptions); $this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions); $this->addTplParam('isAdmin', isAdmin()); } catch (WebauthnException $e) { - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->setVariable(WebauthnConf::GLOBAL_SWITCH, true); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); - $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); - $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=login'); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=login'); } } @@ -118,16 +119,12 @@ class d3webauthnadminlogin extends AdminController public function d3AssertAuthn(): ?string { try { - $login = $this->d3GetMockableOxNewObject( - WebauthnLogin::class, - $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'), - $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error') - ); + $login = $this->getWebAuthnLogin(); return $login->adminLogin( - $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('profile') + d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('profile') ); } catch (WebauthnGetException $e) { - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); return 'login'; } } @@ -137,7 +134,7 @@ class d3webauthnadminlogin extends AdminController */ public function d3GetPreviousClass(): ?string { - return $this->d3GetMockableRegistryObject(Session::class) + return d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS); } @@ -147,7 +144,7 @@ class d3webauthnadminlogin extends AdminController public function previousClassIsOrderStep(): bool { $sClassKey = $this->d3GetPreviousClass(); - $resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class) + $resolvedClass = d3GetOxidDIC()->get('d3ox.webauthn.'.ControllerClassNameResolver::class) ->getClassNameById($sClassKey); $resolvedClass = $resolvedClass ?: 'start'; @@ -163,4 +160,18 @@ class d3webauthnadminlogin extends AdminController { return $this->previousClassIsOrderStep(); } + + /** + * @return WebauthnLogin + */ + protected function getWebAuthnLogin(): WebauthnLogin + { + /** @var Request $request */ + $request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class); + + return oxNew(WebauthnLogin::class, + $request->getRequestEscapedParameter('credential'), + $request->getRequestEscapedParameter('error') + ); + } } diff --git a/src/Application/Controller/Traits/checkoutGetUserTrait.php b/src/Application/Controller/Traits/checkoutGetUserTrait.php index ccc0cef..f104c81 100755 --- a/src/Application/Controller/Traits/checkoutGetUserTrait.php +++ b/src/Application/Controller/Traits/checkoutGetUserTrait.php @@ -43,11 +43,11 @@ trait checkoutGetUserTrait $user = $this->d3CallMockableFunction([$this->parentClass, 'getUser']); if ($user && $user->isLoaded() && $user->getId()) { - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); if ($webauthn->isAvailable() && $webauthn->isActive($user->getId()) - && !$this->d3GetMockableRegistryObject(Session::class) + && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) ) { return false; diff --git a/src/Application/Controller/d3_account_webauthn.php b/src/Application/Controller/d3_account_webauthn.php index be579ed..f8d2d7d 100755 --- a/src/Application/Controller/d3_account_webauthn.php +++ b/src/Application/Controller/d3_account_webauthn.php @@ -27,10 +27,12 @@ use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception as DoctrineException; use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\SeoEncoder; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; use Throwable; class d3_account_webauthn extends AccountController @@ -48,7 +50,7 @@ class d3_account_webauthn extends AccountController $sRet = parent::render(); $this->addTplParam('user', $this->getUser()); - $this->addTplParam('readonly', !($this->d3GetMockableOxNewObject(Webauthn::class)->isAvailable())); + $this->addTplParam('readonly', !(d3GetOxidDIC()->get(Webauthn::class)->isAvailable())); return $sRet; } @@ -63,7 +65,7 @@ class d3_account_webauthn extends AccountController public function getCredentialList(): PublicKeyCredentialList { $oUser = $this->getUser(); - $credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); + $credentialList = d3GetOxidDIC()->get(PublicKeyCredentialList::class); return $credentialList->getAllFromUser($oUser); } @@ -80,9 +82,9 @@ class d3_account_webauthn extends AccountController $this->setAuthnRegister(); $this->setPageType('requestnew'); } catch (WebauthnException $e) { - $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId: ' => $this->getUser()->getId()]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); } } @@ -104,7 +106,7 @@ class d3_account_webauthn extends AccountController */ public function setAuthnRegister(): void { - $publicKeyCredentialCreationOptions = $this->d3GetMockableOxNewObject(Webauthn::class) + $publicKeyCredentialCreationOptions = d3GetOxidDIC()->get(Webauthn::class) ->getCreationOptions($this->getUser()); $this->addTplParam('webauthn_publickey_create', $publicKeyCredentialCreationOptions); @@ -122,20 +124,23 @@ class d3_account_webauthn extends AccountController public function saveAuthn(): void { try { - $error = Registry::getRequest()->getRequestEscapedParameter('error'); + /** @var Request $request */ + $request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class); + $error = $request->getRequestEscapedParameter('error'); + if (strlen((string) $error)) { /** @var WebauthnCreateException $e */ $e = oxNew(WebauthnCreateException::class, $error); throw $e; } - $credential = Registry::getRequest()->getRequestEscapedParameter('credential'); + $credential = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('credential'); if (strlen((string) $credential)) { - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); - $webauthn->saveAuthn($credential, Registry::getRequest()->getRequestEscapedParameter('keyname')); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); + $webauthn->saveAuthn($credential, d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('keyname')); } } catch (WebauthnException $e) { - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); } } @@ -144,9 +149,9 @@ class d3_account_webauthn extends AccountController */ public function deleteKey(): void { - $deleteId = Registry::getRequest()->getRequestEscapedParameter('deleteoxid'); + $deleteId = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestEscapedParameter('deleteoxid'); if ($deleteId) { - $credential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); + $credential = d3GetOxidDIC()->get(PublicKeyCredential::class); $credential->delete($deleteId); } } diff --git a/src/Application/Controller/d3webauthnlogin.php b/src/Application/Controller/d3webauthnlogin.php index 932eef4..2c242a2 100755 --- a/src/Application/Controller/d3webauthnlogin.php +++ b/src/Application/Controller/d3webauthnlogin.php @@ -28,6 +28,7 @@ use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Utils; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; class d3webauthnlogin extends FrontendController { @@ -40,7 +41,7 @@ class d3webauthnlogin extends FrontendController */ public function getNavigationParams(): array { - $navparams = $this->d3GetMockableRegistryObject(Session::class)->getVariable( + $navparams = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getVariable( WebauthnConf::WEBAUTHN_SESSION_NAVPARAMS ); @@ -60,17 +61,17 @@ class d3webauthnlogin extends FrontendController */ public function render(): string { - if ($this->d3GetMockableRegistryObject(Session::class) + if (d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) || - !$this->d3GetMockableRegistryObject(Session::class) + !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER) ) { - $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=start'); + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=start'); } $this->generateCredentialRequest(); - $this->addTplParam('navFormParams', $this->d3GetMockableRegistryObject(Session::class) + $this->addTplParam('navFormParams', d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS)); return $this->d3CallMockableFunction([FrontendController::class, 'render']); @@ -85,22 +86,22 @@ class d3webauthnlogin extends FrontendController */ public function generateCredentialRequest(): void { - $userId = $this->d3GetMockableRegistryObject(Session::class) + $userId = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); try { /** @var Webauthn $webauthn */ - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); $publicKeyCredentialRequestOptions = $webauthn->getRequestOptions($userId); $this->addTplParam('webauthn_publickey_login', $publicKeyCredentialRequestOptions); $this->addTplParam('isAdmin', isAdmin()); } catch (WebauthnException $e) { - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->setVariable(WebauthnConf::GLOBAL_SWITCH, true); - $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); Registry::getUtilsView()->addErrorToDisplay($e); - $this->d3GetMockableRegistryObject(Utils::class)->redirect('index.php?cl=start'); + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect('index.php?cl=start'); } } @@ -109,7 +110,7 @@ class d3webauthnlogin extends FrontendController */ public function d3GetPreviousClass(): ?string { - return $this->d3GetMockableRegistryObject(Session::class) + return d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); } @@ -119,7 +120,7 @@ class d3webauthnlogin extends FrontendController public function previousClassIsOrderStep(): bool { $sClassKey = $this->d3GetPreviousClass(); - $resolvedClass = $this->d3GetMockableRegistryObject(ControllerClassNameResolver::class) + $resolvedClass = d3GetOxidDIC()->get('d3ox.webauthn.'.ControllerClassNameResolver::class) ->getClassNameById($sClassKey); $resolvedClass = $resolvedClass ?: 'start'; diff --git a/src/Application/Model/Credential/PublicKeyCredential.php b/src/Application/Model/Credential/PublicKeyCredential.php index cc50ad5..606448b 100755 --- a/src/Application/Model/Credential/PublicKeyCredential.php +++ b/src/Application/Model/Credential/PublicKeyCredential.php @@ -112,8 +112,13 @@ class PublicKeyCredential extends BaseModel */ public function getCredential(): ?PublicKeyCredentialSource { - return unserialize(base64_decode($this->__get($this->_getFieldLongName('credential'))->rawValue)) ?: - null; + return unserialize( + base64_decode( + $this->__get($this->_getFieldLongName('credential'))->rawValue + ), + ['allowed_classes' => [PublicKeyCredentialSource::class]] + ) ?: + null; } /** @@ -130,7 +135,7 @@ class PublicKeyCredential extends BaseModel public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $keyName = null): void { // item exist already - if ($this->d3GetMockableOxNewObject(PublicKeyCredentialList::class) + if (d3GetOxidDIC()->get(PublicKeyCredentialList::class) ->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId()) ) { return; @@ -174,7 +179,7 @@ class PublicKeyCredential extends BaseModel ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) + $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId()) ) ) ); diff --git a/src/Application/Model/Credential/PublicKeyCredentialList.php b/src/Application/Model/Credential/PublicKeyCredentialList.php index 3754f40..02a996a 100755 --- a/src/Application/Model/Credential/PublicKeyCredentialList.php +++ b/src/Application/Model/Credential/PublicKeyCredentialList.php @@ -63,12 +63,14 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) + $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId()) ) ) ); $credential = $qb->execute()->fetchOne(); - +//dumpvar($qb->getSQL()); +//dumpvar($qb->getParameters()); +//dumpvar(unserialize(base64_decode($credential))); if (!strlen((string) $credential)) { return null; } @@ -100,14 +102,17 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) + $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId()) ) ) ); // generate decoded credentials list return array_map(function (array $fields) { - return unserialize(base64_decode($fields['credential'])); + /** @var PublicKeyCredential $credential */ + $credential = clone $this->getBaseObject(); + $credential->assign(['credential' => $fields['credential']]); + return $credential->getCredential(); }, $qb->execute()->fetchAllAssociative()); } @@ -137,7 +142,7 @@ class PublicKeyCredentialList extends ListModel implements PublicKeyCredentialSo ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) + $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId()) ) ) ); diff --git a/src/Application/Model/RelyingPartyEntity.php b/src/Application/Model/RelyingPartyEntity.php index c385b79..464686f 100755 --- a/src/Application/Model/RelyingPartyEntity.php +++ b/src/Application/Model/RelyingPartyEntity.php @@ -59,6 +59,6 @@ class RelyingPartyEntity extends PublicKeyCredentialRpEntity */ public function getActiveShop(): Shop { - return $this->d3GetMockableRegistryObject(Config::class)->getActiveShop(); + return d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getActiveShop(); } } diff --git a/src/Application/Model/Webauthn.php b/src/Application/Model/Webauthn.php index bb1a237..31158cf 100644 --- a/src/Application/Model/Webauthn.php +++ b/src/Application/Model/Webauthn.php @@ -33,6 +33,7 @@ use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\UtilsView; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; use Throwable; use Webauthn\PublicKeyCredentialCreationOptions; use Webauthn\PublicKeyCredentialDescriptor; @@ -62,8 +63,8 @@ class Webauthn } $e = oxNew(WebauthnException::class, 'D3_WEBAUTHN_ERR_UNSECURECONNECTION'); - $this->d3GetMockableLogger()->info($e->getDetailedErrorMessage()); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->info($e->getDetailedErrorMessage()); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); return false; } @@ -78,7 +79,8 @@ class Webauthn */ public function getCreationOptions(User $user): string { - $userEntity = $this->d3GetMockableOxNewObject(UserEntity::class, $user); + d3GetOxidDIC()->set(UserEntity::class.'.args.user', $user); + $userEntity = d3GetOxidDIC()->get(UserEntity::class); $publicKeyCredentialCreationOptions = $this->getServer()->generatePublicKeyCredentialCreationOptions( $userEntity, @@ -86,7 +88,7 @@ class Webauthn $this->getExistingCredentials($userEntity) ); - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->setVariable(self::SESSION_CREATIONS_OPTIONS, $publicKeyCredentialCreationOptions); $json = $this->jsonEncode($publicKeyCredentialCreationOptions); @@ -108,7 +110,7 @@ class Webauthn { // Get the list of authenticators associated to the user /** @var PublicKeyCredentialList $credentialList */ - $credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); + $credentialList = d3GetOxidDIC()->get(PublicKeyCredentialList::class); $credentialSources = $credentialList->findAllForUserEntity($userEntity); // Convert the Credential Sources into Public Key Credential Descriptors @@ -135,9 +137,10 @@ class Webauthn public function getRequestOptions(string $userId): string { /** @var d3_User_Webauthn $user */ - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $user->load($userId); - $userEntity = $this->d3GetMockableOxNewObject(UserEntity::class, $user); + d3GetOxidDIC()->set(UserEntity::class.'.args.user', $user); + $userEntity = d3GetOxidDIC()->get(UserEntity::class); // We generate the set of options. $publicKeyCredentialRequestOptions = $this->getServer()->generatePublicKeyCredentialRequestOptions( @@ -145,7 +148,7 @@ class Webauthn $this->getExistingCredentials($userEntity) ); - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->setVariable(self::SESSION_ASSERTION_OPTIONS, $publicKeyCredentialRequestOptions); $json = $this->jsonEncode($publicKeyCredentialRequestOptions); @@ -162,15 +165,8 @@ class Webauthn */ protected function getServer(): Server { - /** @var RelyingPartyEntity $rpEntity */ - $rpEntity = $this->d3GetMockableOxNewObject(RelyingPartyEntity::class); - /** @var Server $server */ - $server = $this->d3GetMockableOxNewObject( - Server::class, - $rpEntity, - $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class) - ); - $server->setLogger($this->d3GetMockableLogger()); + $server = $this->getServerObject(); + $server->setLogger(d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)); return $server; } @@ -196,11 +192,11 @@ class Webauthn $publicKeyCredentialSource = $this->getServer()->loadAndCheckAttestationResponse( html_entity_decode($credential), - $this->d3GetMockableRegistryObject(Session::class)->getVariable(self::SESSION_CREATIONS_OPTIONS), + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getVariable(self::SESSION_CREATIONS_OPTIONS), $serverRequest ); - $pkCredential = $this->d3GetMockableOxNewObject(PublicKeyCredential::class); + $pkCredential = d3GetOxidDIC()->get(PublicKeyCredential::class); $pkCredential->saveCredentialSource($publicKeyCredentialSource, $keyName); } @@ -226,7 +222,7 @@ class Webauthn try { $this->getServer()->loadAndCheckAssertionResponse( html_entity_decode($response), - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(self::SESSION_ASSERTION_OPTIONS), $userEntity, $serverRequest @@ -247,10 +243,11 @@ class Webauthn protected function getUserEntityFrom($userId): UserEntity { /** @var User $user */ - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $user->load($userId); - return $this->d3GetMockableOxNewObject(UserEntity::class, $user); + d3GetOxidDIC()->set(UserEntity::class.'.args.user', $user); + return d3GetOxidDIC()->get(UserEntity::class); } /** @@ -258,7 +255,7 @@ class Webauthn */ protected function getSavedUserIdFromSession(): ?string { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); return $this->isAdmin() ? $session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : @@ -283,9 +280,9 @@ class Webauthn */ public function isActive($userId): bool { - return !$this->d3GetMockableRegistryObject(Config::class) + return !d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class) ->getConfigParam(WebauthnConf::GLOBAL_SWITCH) - && !$this->d3GetMockableRegistryObject(Session::class) + && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::GLOBAL_SWITCH) && $this->UserUseWebauthn($userId); } @@ -303,9 +300,24 @@ class Webauthn $entity = $this->getUserEntityFrom($userId); /** @var PublicKeyCredentialList $credentialList */ - $credentialList = $this->d3GetMockableOxNewObject(PublicKeyCredentialList::class); + $credentialList = d3GetOxidDIC()->get(PublicKeyCredentialList::class); $list = $credentialList->findAllForUserEntity($entity); return is_array($list) && count($list); } + + /** + * @return Server + */ + protected function getServerObject() + { + /** @var RelyingPartyEntity $rpEntity */ + $rpEntity = d3GetOxidDIC()->get(RelyingPartyEntity::class); + + return oxNew( + Server::class, + $rpEntity, + d3GetOxidDIC()->get(PublicKeyCredentialList::class) + ); + } } diff --git a/src/Application/Model/WebauthnAfterLogin.php b/src/Application/Model/WebauthnAfterLogin.php index 9f0efe9..5fab6ac 100644 --- a/src/Application/Model/WebauthnAfterLogin.php +++ b/src/Application/Model/WebauthnAfterLogin.php @@ -30,15 +30,15 @@ class WebauthnAfterLogin */ public function setDisplayProfile(): void { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); - $sProfile = $this->d3GetMockableRegistryObject(Request::class) + $sProfile = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class) ->getRequestEscapedParameter('profile') ?: $session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); $session->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_PROFILE); - $myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class); + $myUtilsServer = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class); if (isset($sProfile)) { $aProfiles = $session->getVariable("aAdminProfiles"); @@ -58,17 +58,17 @@ class WebauthnAfterLogin */ public function changeLanguage(): void { - $myUtilsServer = $this->d3GetMockableRegistryObject(UtilsServer::class); - $session = $this->d3GetMockableRegistryObject(Session::class); + $myUtilsServer = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); // languages - $iLang = $this->d3GetMockableRegistryObject(Request::class) + $iLang = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class) ->getRequestEscapedParameter('chlanguage') ?: $session->getVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE); $session->deleteVariable(WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE); - $language = $this->d3GetMockableRegistryObject(Language::class); + $language = d3GetOxidDIC()->get('d3ox.webauthn.'.Language::class); $aLanguages = $language->getAdminTplLanguageArray(); if (!isset($aLanguages[$iLang])) { diff --git a/src/Application/Model/WebauthnErrors.php b/src/Application/Model/WebauthnErrors.php index 121cead..a76072e 100644 --- a/src/Application/Model/WebauthnErrors.php +++ b/src/Application/Model/WebauthnErrors.php @@ -38,7 +38,7 @@ class WebauthnErrors */ public function translateError(string $msg, string $type = null): string { - $lang = $this->d3GetMockableRegistryObject(Language::class); + $lang = d3GetOxidDIC()->get('d3ox.webauthn.'.Language::class); $type = $type ? '_'.$type : null; switch ($this->getErrIdFromMessage($msg)) { diff --git a/src/Application/Model/WebauthnLogin.php b/src/Application/Model/WebauthnLogin.php index e0bf0b1..7c90861 100644 --- a/src/Application/Model/WebauthnLogin.php +++ b/src/Application/Model/WebauthnLogin.php @@ -31,6 +31,7 @@ use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsServer; use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\EshopCommunity\Application\Component\UserComponent; +use Psr\Log\LoggerInterface; class WebauthnLogin { @@ -104,9 +105,9 @@ class WebauthnLogin */ public function frontendLogin(UserComponent $usrCmp, bool $setSessionCookie = false) { - $myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class); + $myUtilsView = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class); /** @var d3_User_Webauthn $user */ - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $userId = $this->getUserId(); try { @@ -116,7 +117,7 @@ class WebauthnLogin $this->assertAuthn(); // relogin, don't extract from this try block - $usrCmp->setUser($this->d3GetMockableOxNewObject(User::class)); + $usrCmp->setUser(d3GetOxidDIC()->get('d3ox.webauthn.'.User::class)); $this->setFrontendSession($user); $usrCmp->setLoginStatus(USER_LOGIN_SUCCESS); @@ -136,8 +137,8 @@ class WebauthnLogin $myUtilsView->addErrorToDisplay($oEx); } catch (WebauthnException $e) { $myUtilsView->addErrorToDisplay($e); - $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); } $user->logout(); @@ -150,9 +151,9 @@ class WebauthnLogin */ public function adminLogin(string $selectedProfile): string { - $myUtilsView = $this->d3GetMockableRegistryObject(UtilsView::class); + $myUtilsView = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class); /** @var d3_User_Webauthn $user */ - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $userId = $this->getUserId(); try { @@ -164,10 +165,10 @@ class WebauthnLogin $this->handleBackendCookie(); $this->handleBackendSubshopRights($user, $session); - $oEventHandler = $this->d3GetMockableOxNewObject(SystemEventHandler::class); + $oEventHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.SystemEventHandler::class); $oEventHandler->onAdminLogin(); - $afterLogin = $this->d3GetMockableOxNewObject(WebauthnAfterLogin::class); + $afterLogin = d3GetOxidDIC()->get(WebauthnAfterLogin::class); $afterLogin->setDisplayProfile(); $afterLogin->changeLanguage(); @@ -181,15 +182,15 @@ class WebauthnLogin $myUtilsView->addErrorToDisplay('LOGIN_NO_COOKIE_SUPPORT'); } catch (WebauthnException $e) { $myUtilsView->addErrorToDisplay($e); - $this->d3GetMockableLogger()->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); - $this->d3GetMockableLogger()->debug($e->getTraceAsString()); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getDetailedErrorMessage(), ['UserId' => $userId]); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->debug($e->getTraceAsString()); } $user->logout(); $oStr = Str::getStr(); - $this->d3GetMockableRegistryObject(Config::class)->getActiveView() + d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getActiveView() ->addTplParam('user', $oStr->htmlspecialchars($userId)); - $this->d3GetMockableRegistryObject(Config::class)->getActiveView() + d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getActiveView() ->addTplParam('profile', $oStr->htmlspecialchars($selectedProfile)); return 'login'; @@ -215,7 +216,7 @@ class WebauthnLogin public function assertAuthn(): void { $credential = $this->getCredential(); - $webAuthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webAuthn = d3GetOxidDIC()->get(Webauthn::class); $webAuthn->assertAuthn($credential); } @@ -225,7 +226,7 @@ class WebauthnLogin */ public function setAdminSession($userId): Session { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); $adminProfiles = $session->getVariable("aAdminProfiles"); $session->initNewSession(); $session->setVariable("aAdminProfiles", $adminProfiles); @@ -239,11 +240,11 @@ class WebauthnLogin */ public function setSessionCookie(User $user) { - if ($this->d3GetMockableRegistryObject(Config::class)->getConfigParam('blShowRememberMe')) { - $this->d3GetMockableRegistryObject(UtilsServer::class)->setUserCookie( + if (d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getConfigParam('blShowRememberMe')) { + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class)->setUserCookie( $user->getFieldData('oxusername'), $user->getFieldData('oxpassword'), - $this->d3GetMockableRegistryObject(Config::class)->getShopId() + d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId() ); } } @@ -256,7 +257,7 @@ class WebauthnLogin */ public function assertUser($userId, bool $isBackend = false): User { - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $user->load($userId); if (!$user->isLoaded() || ($isBackend && $user->getFieldData('oxrights') === 'user') @@ -275,7 +276,7 @@ class WebauthnLogin */ public function handleBackendCookie(): void { - $cookie = $this->d3GetMockableRegistryObject(UtilsServer::class)->getOxCookie(); + $cookie = d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsServer::class)->getOxCookie(); if ($cookie === null) { /** @var CookieException $exc */ $exc = oxNew(CookieException::class, 'ERROR_MESSAGE_COOKIE_NOCOOKIE'); @@ -295,7 +296,7 @@ class WebauthnLogin if ($iSubshop) { $session->setVariable("shp", $iSubshop); $session->setVariable('currentadminshop', $iSubshop); - $this->d3GetMockableRegistryObject(Config::class)->setShopId((string) $iSubshop); + d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->setShopId((string) $iSubshop); } } @@ -304,7 +305,7 @@ class WebauthnLogin */ public function regenerateSessionId(): void { - $oSession = $this->d3GetMockableRegistryObject(Session::class); + $oSession = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); if ($oSession->isSessionStarted()) { $oSession->regenerateSessionId(); } @@ -314,9 +315,9 @@ class WebauthnLogin { // this user is blocked, deny him if ($user->inGroup('oxidblocked')) { - $sUrl = $this->d3GetMockableRegistryObject(Config::class)->getShopHomeUrl() . + $sUrl = d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopHomeUrl() . 'cl=content&tpl=user_blocked.tpl'; - $this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl); + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect($sUrl); } } @@ -325,7 +326,7 @@ class WebauthnLogin */ public function updateBasket(): void { - $oBasket = $this->d3GetMockableRegistryObject(Session::class)->getBasket(); + $oBasket = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->getBasket(); $oBasket->onUpdate(); } @@ -343,9 +344,9 @@ class WebauthnLogin public function getUserId(): string { return $this->isAdmin() ? - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER) : - $this->d3GetMockableRegistryObject(Session::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); } @@ -356,7 +357,7 @@ class WebauthnLogin */ public function setFrontendSession(User $user): void { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); $session->setVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH, $this->getCredential()); $session->setVariable(WebauthnConf::OXID_FRONTEND_AUTH, $user->getId()); } diff --git a/src/Config/oxid.yaml b/src/Config/oxid.yaml new file mode 100644 index 0000000..39f75e6 --- /dev/null +++ b/src/Config/oxid.yaml @@ -0,0 +1,138 @@ +# This Software is the property of Data Development and is protected +# by copyright law - it is NOT Freeware. +# +# Any unauthorized use of this software without a valid license +# is a violation of the license agreement and will be prosecuted by +# civil and criminal law. +# +# https://www.d3data.de +# +# @copyright (C) D3 Data Development (Inh. Thomas Dartsch) +# @author D3 Data Development - Daniel Seifert +# @link https://www.oxidmodule.com + +services: + _defaults: + autowire: false + public: true + + # config + d3ox.webauthn.OxidEsales\Eshop\Core\Config: + class: 'OxidEsales\Eshop\Core\Config' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getConfig' + shared: true + + # request + d3ox.webauthn.OxidEsales\Eshop\Core\Request: + class: 'OxidEsales\Eshop\Core\Request' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getRequest' + shared: true + + # session + d3ox.webauthn.OxidEsales\Eshop\Core\Session: + class: 'OxidEsales\Eshop\Core\Session' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getSession' + shared: true + + # language + d3ox.webauthn.OxidEsales\Eshop\Core\Language: + class: 'OxidEsales\Eshop\Core\Language' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getLang' + shared: true + + # Utils + d3ox.webauthn.OxidEsales\Eshop\Core\Utils: + class: 'OxidEsales\Eshop\Core\Utils' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getUtils' + shared: true + + # UtilsServer + d3ox.webauthn.OxidEsales\Eshop\Core\UtilsServer: + class: 'OxidEsales\Eshop\Core\UtilsServer' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getUtilsServer' + shared: true + + # UtilsView + d3ox.webauthn.OxidEsales\Eshop\Core\UtilsView: + class: 'OxidEsales\Eshop\Core\UtilsView' + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getUtilsView' + shared: true + + # DbMetaDataHandler + d3ox.webauthn.OxidEsales\Eshop\Core\DbMetaDataHandler: + class: 'OxidEsales\Eshop\Core\DbMetaDataHandler' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Core\DbMetaDataHandler' + shared: false + + # SeoEncoder + d3ox.webauthn.OxidEsales\Eshop\Core\SeoEncoder: + class: 'OxidEsales\Eshop\Core\SeoEncoder' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Core\SeoEncoder' + shared: false + + # SystemEventHandler + d3ox.webauthn.OxidEsales\Eshop\Core\SystemEventHandler: + class: 'OxidEsales\Eshop\Core\SystemEventHandler' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Core\SystemEventHandler' + shared: false + + # ControllerClassNameResolver + d3ox.webauthn.OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver: + class: 'OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Core\Routing\ControllerClassNameResolver' + shared: false + + # FrontendController + d3ox.webauthn.OxidEsales\Eshop\Application\Controller\FrontendController: + class: 'OxidEsales\Eshop\Application\Controller\FrontendController' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Application\Controller\FrontendController' + shared: false + + # User + d3ox.webauthn.OxidEsales\Eshop\Application\Model\User: + class: 'OxidEsales\Eshop\Application\Model\User' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Application\Model\User' + shared: false + + d3ox.webauthn.Psr\Log\LoggerInterface: + class: Psr\Log\LoggerInterface + factory: + - 'OxidEsales\Eshop\Core\Registry' + - 'getLogger' + shared: true + + # DB_assoc + d3ox.webauthn.OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface.assoc: + class: 'OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface' + factory: + - 'OxidEsales\Eshop\Core\DatabaseProvider' + - 'getDb' + arguments: + - 2 + shared: true \ No newline at end of file diff --git a/src/Config/services.yaml b/src/Config/services.yaml new file mode 100644 index 0000000..2efe416 --- /dev/null +++ b/src/Config/services.yaml @@ -0,0 +1,15 @@ +# For the full copyright and license information, please view the LICENSE +# file that was distributed with this source code. +# +# https://www.d3data.de +# +# @copyright (C) D3 Data Development (Inh. Thomas Dartsch) +# @author D3 Data Development - Daniel Seifert +# @link https://www.oxidmodule.com + +imports: + - { resource: oxid.yaml } + - { resource: webauthn.yaml } + + + diff --git a/src/Config/webauthn.yaml b/src/Config/webauthn.yaml new file mode 100644 index 0000000..e00bb0f --- /dev/null +++ b/src/Config/webauthn.yaml @@ -0,0 +1,160 @@ +# For the full copyright and license information, please view the LICENSE +# file that was distributed with this source code. +# +# https://www.d3data.de +# +# @copyright (C) D3 Data Development (Inh. Thomas Dartsch) +# @author D3 Data Development - Daniel Seifert +# @link https://www.oxidmodule.com + +parameters: + D3\Webauthn\Application\Model\WebauthnLogin.args.credential: + D3\Webauthn\Application\Model\WebauthnLogin.args.error: +# D3\Linkmobility4OXID\Application\Model\RequestFactory.args.message: +# D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms.args.message: +# D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException.args.smscount: + +services: + _defaults: + autowire: false + public: true + + D3\Webauthn\Application\Model\Webauthn: + class: D3\Webauthn\Application\Model + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\Webauthn + + D3\Webauthn\Application\Model\RelyingPartyEntity: + class: D3\Webauthn\Application\Model\RelyingPartyEntity + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\RelyingPartyEntity + + D3\Webauthn\Application\Model\UserEntity: + class: D3\Webauthn\Application\Model\UserEntity + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\UserEntity + - '@D3\Webauthn\Application\Model\UserEntity.args.user' + D3\Webauthn\Application\Model\UserEntity.args.user: + synthetic: true + shared: false + + D3\Webauthn\Application\Model\WebauthnAfterLogin: + class: D3\Webauthn\Application\Model\WebauthnAfterLogin + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\WebauthnAfterLogin + + D3\Webauthn\Application\Model\WebauthnLogin: + class: D3\Webauthn\Application\Model\WebauthnLogin + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\WebauthnLogin + - '%D3\Webauthn\Application\Model\WebauthnLogin.args.credential%' + - '%D3\Webauthn\Application\Model\WebauthnLogin.args.error%' + + D3\Webauthn\Application\Model\Credential\PublicKeyCredential: + class: D3\Webauthn\Application\Model\Credential\PublicKeyCredential + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\Credential\PublicKeyCredential + + D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList: + class: D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList + factory: 'oxNew' + shared: false + arguments: + - D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList + +# D3\Linkmobility4OXID\Application\Model\OrderRecipients: +# class: D3\Linkmobility4OXID\Application\Model\OrderRecipients +# factory: 'oxNew' +# shared: false +# arguments: +# - D3\Linkmobility4OXID\Application\Model\OrderRecipients +# - '@D3\Linkmobility4OXID\Application\Model\OrderRecipients.args.order' +# D3\Linkmobility4OXID\Application\Model\OrderRecipients.args.order: +# synthetic: true +# shared: false +# +# D3\Linkmobility4OXID\Application\Model\UserRecipients: +# class: D3\Linkmobility4OXID\Application\Model\UserRecipients +# factory: 'oxNew' +# shared: false +# arguments: +# - D3\Linkmobility4OXID\Application\Model\UserRecipients +# - '@D3\Linkmobility4OXID\Application\Model\UserRecipients.args.user' +# D3\Linkmobility4OXID\Application\Model\UserRecipients.args.user: +# synthetic: true +# shared: false +# +# D3\Linkmobility4OXID\Application\Model\Configuration: +# class: D3\Linkmobility4OXID\Application\Model\Configuration +# factory: 'oxNew' +# shared: true +# arguments: +# - D3\Linkmobility4OXID\Application\Model\Configuration +# +# D3\Linkmobility4OXID\Application\Model\MessageClient: +# class: D3\Linkmobility4OXID\Application\Model\MessageClient +# factory: 'oxNew' +# shared: true +# arguments: +# - D3\Linkmobility4OXID\Application\Model\MessageClient +# +# D3\Linkmobility4OXID\Application\Model\MessageSender: +# class: D3\Linkmobility4OXID\Application\Model\MessageSender +# factory: 'oxNew' +# shared: true +# arguments: +# - D3\Linkmobility4OXID\Application\Model\MessageSender +# +# D3\Linkmobility4OXID\Application\Model\RequestFactory: +# class: D3\Linkmobility4OXID\Application\Model\RequestFactory +# factory: 'oxNew' +# shared: true +# arguments: +# - D3\Linkmobility4OXID\Application\Model\RequestFactory +# - '%D3\Linkmobility4OXID\Application\Model\RequestFactory.args.message%' +# - '@D3\Linkmobility4OXID\Application\Model\RequestFactory.args.client' +# D3\Linkmobility4OXID\Application\Model\RequestFactory.args.client: +# synthetic: true +# shared: false +# +# D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms: +# class: D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms +# factory: 'oxNew' +# shared: false +# arguments: +# - D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms +# - '%D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms.args.message%' +# +# D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException: +# class: D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException +# factory: 'oxNew' +# shared: false +# arguments: +# - D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException +# - '%D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException.args.smscount%' +# +# D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException: +# class: D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException +# factory: 'oxNew' +# shared: false +# arguments: +# - D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException +# +# D3\Linkmobility4OXID\Setup\Actions: +# class: D3\Linkmobility4OXID\Setup\Actions +# factory: 'oxNew' +# shared: false +# arguments: +# - D3\Linkmobility4OXID\Setup\Actions \ No newline at end of file diff --git a/src/IntelliSenseHelper.php b/src/IntelliSenseHelper.php index b32c53d..af7cb03 100755 --- a/src/IntelliSenseHelper.php +++ b/src/IntelliSenseHelper.php @@ -11,6 +11,15 @@ * @link https://www.oxidmodule.com */ +namespace D3\Webauthn\Modules +{ + use D3\DIContainerHandler\definitionFileContainer; + + class WebauthnServices_parent extends definitionFileContainer + { + } +} + namespace D3\Webauthn\Modules\Application\Component { use OxidEsales\Eshop\Application\Component\UserComponent; diff --git a/src/Modules/Application/Component/d3_webauthn_UserComponent.php b/src/Modules/Application/Component/d3_webauthn_UserComponent.php index 2410af5..801e74c 100755 --- a/src/Modules/Application/Component/d3_webauthn_UserComponent.php +++ b/src/Modules/Application/Component/d3_webauthn_UserComponent.php @@ -58,14 +58,14 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent */ public function d3WebauthnLogin(): void { - $lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_usr'); + $lgn_user = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('lgn_usr'); /** @var d3_User_Webauthn $user */ - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $userId = $user->d3GetLoginUserId($lgn_user); if ($this->d3CanUseWebauthn($lgn_user, $userId)) { if ($this->d3HasWebauthnButNotLoggedin($userId)) { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); $session->setVariable( WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS, $this->getClassKey() != 'd3webauthnlogin' ? $this->getClassKey() : 'start' @@ -83,8 +83,8 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent $this->getParent()->getViewConfig()->getNavFormParams() ); - $sUrl = $this->d3GetMockableRegistryObject(Config::class)->getShopHomeUrl() . 'cl=d3webauthnlogin'; - $this->d3GetMockableRegistryObject(Utils::class)->redirect($sUrl); + $sUrl = d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopHomeUrl() . 'cl=d3webauthnlogin'; + d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class)->redirect($sUrl); } } } @@ -97,11 +97,11 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent */ protected function d3CanUseWebauthn($lgn_user, ?string $userId): bool { - $password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_pwd'); + $password = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('lgn_pwd'); return $lgn_user && $userId && - false === $this->d3GetMockableRegistryObject(Session::class) + false === d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->hasVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) && (! strlen(trim((string) $password))); } @@ -114,10 +114,10 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent */ protected function d3HasWebauthnButNotLoggedin($userId): bool { - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); return $webauthn->isActive($userId) - && !$this->d3GetMockableRegistryObject(Session::class) + && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH); } @@ -134,7 +134,7 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent */ public function d3WebauthnClearSessionVariables(): void { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTCLASS); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_NAVFORMPARAMS); @@ -147,19 +147,29 @@ class d3_webauthn_UserComponent extends d3_webauthn_UserComponent_parent public function d3AssertAuthn(): void { try { - $login = $this->d3GetMockableOxNewObject( - WebauthnLogin::class, - $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('credential'), - $this->d3GetMockableRegistryObject(Request::class)->getRequestEscapedParameter('error') - ); + $login = $this->d3GetWebauthnLogin(); $login->frontendLogin( $this, - (bool)$this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('lgn_cook') + (bool)d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('lgn_cook') ); $this->_afterLogin($this->getUser()); } catch (WebauthnGetException $e) { - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e); } catch (WebauthnLoginErrorException $e) { } } + + /** + * @return WebauthnLogin + */ + protected function d3GetWebauthnLogin(): WebauthnLogin + { + /** @var Request $request */ + $request = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class); + + return oxNew(WebauthnLogin::class, + $request->getRequestEscapedParameter('credential'), + $request->getRequestEscapedParameter('error') + ); + } } diff --git a/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php b/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php index dd6ec23..4ad5fd6 100755 --- a/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php +++ b/src/Modules/Application/Controller/Admin/d3_LoginController_Webauthn.php @@ -40,36 +40,36 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ public function checklogin() { - $lgn_user = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('user') ?: - $this->d3GetMockableRegistryObject(Session::class) + $lgn_user = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('user') ?: + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER); /** @var d3_User_Webauthn $user */ - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $userId = $user->d3GetLoginUserId($lgn_user, 'malladmin'); if ($this->d3CanUseWebauthn($lgn_user, $userId)) { - $this->d3GetMockableRegistryObject(Session::class)->setVariable( + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_PROFILE, - $this->d3GetMockableRegistryObject(Request::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class) ->getRequestEscapedParameter('profile') ); - $this->d3GetMockableRegistryObject(Session::class)->setVariable( + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE, - $this->d3GetMockableRegistryObject(Request::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class) ->getRequestEscapedParameter('chlanguage') ); if ($this->hasWebauthnButNotLoggedin($userId)) { - $this->d3GetMockableRegistryObject(Session::class)->setVariable( + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTCLASS, $this->getClassKey() != 'd3webauthnadminlogin' ? $this->getClassKey() : 'admin_start' ); - $this->d3GetMockableRegistryObject(Session::class)->setVariable( + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, $userId ); - $this->d3GetMockableRegistryObject(Session::class)->setVariable( + d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class)->setVariable( WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER, $lgn_user ); @@ -86,7 +86,7 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ public function d3WebauthnCancelLogin(): void { - $user = $this->d3GetMockableOxNewObject(User::class); + $user = d3GetOxidDIC()->get('d3ox.webauthn.'.User::class); $user->logout(); } @@ -98,11 +98,11 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ protected function d3CanUseWebauthn($lgn_user, ?string $userId): bool { - $password = $this->d3GetMockableRegistryObject(Request::class)->getRequestParameter('pwd'); + $password = d3GetOxidDIC()->get('d3ox.webauthn.'.Request::class)->getRequestParameter('pwd'); return $lgn_user && $userId && - false === $this->d3GetMockableRegistryObject(Session::class) + false === d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->hasVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) && (! strlen(trim((string) $password))); } @@ -115,10 +115,10 @@ class d3_LoginController_Webauthn extends d3_LoginController_Webauthn_parent */ protected function hasWebauthnButNotLoggedin($userId): bool { - $webauthn = $this->d3GetMockableOxNewObject(Webauthn::class); + $webauthn = d3GetOxidDIC()->get(Webauthn::class); return $webauthn->isActive($userId) - && !$this->d3GetMockableRegistryObject(Session::class) + && !d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class) ->getVariable(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH); } } diff --git a/src/Modules/Application/Model/d3_User_Webauthn.php b/src/Modules/Application/Model/d3_User_Webauthn.php index 1c48c75..e9dd156 100755 --- a/src/Modules/Application/Model/d3_User_Webauthn.php +++ b/src/Modules/Application/Model/d3_User_Webauthn.php @@ -20,6 +20,7 @@ use D3\Webauthn\Application\Model\WebauthnConf; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; use Doctrine\DBAL\Exception; use Doctrine\DBAL\Query\QueryBuilder; +use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Session; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; @@ -46,7 +47,7 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent */ protected function d3WebauthnLogout(): void { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH); $session->deleteVariable(WebauthnConf::WEBAUTHN_LOGIN_OBJECT); $session->deleteVariable(WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER); @@ -83,13 +84,13 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent */ protected function d3WebauthnLogin(string $userName) { - $session = $this->d3GetMockableRegistryObject(Session::class); + $session = d3GetOxidDIC()->get('d3ox.webauthn.'.Session::class); if ($session->getVariable(WebauthnConf::WEBAUTHN_SESSION_AUTH) && $userName === $session->getVariable(WebauthnConf::WEBAUTHN_SESSION_LOGINUSER) ) { $userName = $userName ?: $session->getVariable(WebauthnConf::WEBAUTHN_SESSION_LOGINUSER); - $shopId = $this->d3GetMockableRegistryObject(Config::class)->getShopId(); + $shopId = d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId(); /** private method is out of scope */ $class = new ReflectionClass($this); @@ -133,7 +134,7 @@ class d3_User_Webauthn extends d3_User_Webauthn_parent ), $qb->expr()->eq( 'oxshopid', - $qb->createNamedParameter($this->d3GetMockableRegistryObject(Config::class)->getShopId()) + $qb->createNamedParameter(d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId()) ), $rights ? $qb->expr()->eq( diff --git a/src/Modules/WebauthnServices.php b/src/Modules/WebauthnServices.php new file mode 100644 index 0000000..0ae9a92 --- /dev/null +++ b/src/Modules/WebauthnServices.php @@ -0,0 +1,29 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\Modules; + +use D3\TestingTools\Production\IsMockable; + +class WebauthnServices extends WebauthnServices_parent +{ + use IsMockable; + + public function __construct() + { + $this->d3CallMockableFunction([WebauthnServices_parent::class, '__construct']); + $this->addYamlDefinitions('d3/oxwebauthn/Config/services.yaml'); + } +} diff --git a/src/Setup/Actions.php b/src/Setup/Actions.php index f54481f..bc58bc9 100644 --- a/src/Setup/Actions.php +++ b/src/Setup/Actions.php @@ -35,6 +35,7 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +use Psr\Log\LoggerInterface; class Actions { @@ -85,7 +86,7 @@ class Actions */ public function tableExists(string $sTableName): bool { - $oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class); + $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.DbMetaDataHandler::class); return $oDbMetaDataHandler->tableExists($sTableName); } @@ -95,7 +96,9 @@ class Actions */ protected function d3GetDb(): ?DatabaseInterface { - return DatabaseProvider::getDb(); + /** @var DatabaseInterface $db */ + $db = d3GetOxidDIC()->get('d3ox.webauthn.'.DatabaseInterface::class.'.assoc'); + return $db; } /** @@ -120,7 +123,7 @@ class Actions */ public function fieldExists(string $sFieldName, string $sTableName): bool { - $oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class); + $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.DbMetaDataHandler::class); return $oDbMetaDataHandler->fieldExists($sFieldName, $sTableName); } @@ -129,7 +132,7 @@ class Actions */ public function regenerateViews() { - $oDbMetaDataHandler = $this->d3GetMockableOxNewObject(DbMetaDataHandler::class); + $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.webauthn.'.DbMetaDataHandler::class); $oDbMetaDataHandler->updateViews(); } @@ -139,12 +142,12 @@ class Actions public function clearCache() { try { - $oUtils = $this->d3GetMockableRegistryObject(Utils::class); + $oUtils = d3GetOxidDIC()->get('d3ox.webauthn.'.Utils::class); $oUtils->resetTemplateCache($this->getModuleTemplates()); $oUtils->resetLanguageCache(); } catch (ContainerExceptionInterface|NotFoundExceptionInterface|ModuleConfigurationNotFoundException $e) { - $this->d3GetMockableLogger()->error($e->getMessage(), [$this]); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($e->getMessage()); + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), [$this]); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class)->addErrorToDisplay($e->getMessage()); } } @@ -208,8 +211,8 @@ class Actions $this->createSeoUrl(); } } catch (Exception|NotFoundExceptionInterface|DoctrineDriverException|ContainerExceptionInterface $e) { - $this->d3GetMockableLogger()->error($e->getMessage(), [$this]); - $this->d3GetMockableRegistryObject(UtilsView::class) + d3GetOxidDIC()->get('d3ox.webauthn.'.LoggerInterface::class)->error($e->getMessage(), [$this]); + d3GetOxidDIC()->get('d3ox.webauthn.'.UtilsView::class) ->addErrorToDisplay('error wile creating SEO URLs: '.$e->getMessage()); } } @@ -219,9 +222,9 @@ class Actions */ public function hasSeoUrl(): bool { - $seoEncoder = $this->d3GetMockableOxNewObject(SeoEncoder::class); + $seoEncoder = d3GetOxidDIC()->get('d3ox.webauthn.'.SeoEncoder::class); $seoUrl = $seoEncoder->getStaticUrl( - $this->d3GetMockableOxNewObject(FrontendController::class)->getViewConfig()->getSelfLink() . + d3GetOxidDIC()->get('d3ox.webauthn.'.FrontendController::class)->getViewConfig()->getSelfLink() . "cl=".$this->stdClassName ); @@ -233,10 +236,10 @@ class Actions */ public function createSeoUrl() { - $seoEncoder = $this->d3GetMockableOxNewObject(SeoEncoder::class); + $seoEncoder = d3GetOxidDIC()->get('d3ox.webauthn.'.SeoEncoder::class); $seoEncoder->addSeoEntry( 'ff57646b47249ee33c6b672741ac371a', - $this->d3GetMockableRegistryObject(Config::class)->getShopId(), + d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId(), 0, 'index.php?cl='.$this->stdClassName, $this->seo_de, @@ -245,7 +248,7 @@ class Actions ); $seoEncoder->addSeoEntry( 'ff57646b47249ee33c6b672741ac371a', - $this->d3GetMockableRegistryObject(Config::class)->getShopId(), + d3GetOxidDIC()->get('d3ox.webauthn.'.Config::class)->getShopId(), 1, 'index.php?cl='.$this->stdClassName, $this->seo_en, diff --git a/src/metadata.php b/src/metadata.php index 82b392a..36652a9 100755 --- a/src/metadata.php +++ b/src/metadata.php @@ -13,6 +13,7 @@ declare(strict_types=1); +use D3\DIContainerHandler\definitionFileContainer; use D3\Webauthn\Application\Controller\Admin\d3user_webauthn; use D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin; use D3\Webauthn\Application\Controller\d3_account_webauthn; @@ -33,6 +34,7 @@ use D3\Webauthn\Modules\Application\Controller\d3_webauthn_OrderController; use D3\Webauthn\Modules\Application\Controller\d3_webauthn_PaymentController; use D3\Webauthn\Modules\Application\Controller\d3_webauthn_UserController; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; +use D3\Webauthn\Modules\WebauthnServices; use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Application\Controller\AccountDownloadsController; @@ -73,12 +75,13 @@ $aModule = [ 'email' => 'support@shopmodule.com', 'url' => 'https://www.oxidmodule.com/', 'extend' => [ - UserController::class => d3_webauthn_UserController::class, - PaymentController::class => d3_webauthn_PaymentController::class, - OrderController::class => d3_webauthn_OrderController::class, - OxidModel\User::class => d3_User_Webauthn::class, - LoginController::class => d3_LoginController_Webauthn::class, - UserComponent::class => d3_webauthn_UserComponent::class, + UserController::class => d3_webauthn_UserController::class, + PaymentController::class => d3_webauthn_PaymentController::class, + OrderController::class => d3_webauthn_OrderController::class, + OxidModel\User::class => d3_User_Webauthn::class, + LoginController::class => d3_LoginController_Webauthn::class, + UserComponent::class => d3_webauthn_UserComponent::class, + definitionFileContainer::class => WebauthnServices::class, /** workarounds for missing tpl blocks (https://github.com/OXID-eSales/wave-theme/pull/124) */ AccountController::class => d3_AccountController_Webauthn::class, diff --git a/src/tests/integration/passwordAdminAuthTest.php b/src/tests/integration/passwordAdminAuthTest.php index a60234d..3a6998d 100644 --- a/src/tests/integration/passwordAdminAuthTest.php +++ b/src/tests/integration/passwordAdminAuthTest.php @@ -17,6 +17,7 @@ namespace D3\Webauthn\tests\integration; use D3\Webauthn\Application\Model\WebauthnConf; use OxidEsales\Eshop\Application\Controller\Admin\LoginController; +use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Registry; @@ -31,7 +32,9 @@ class passwordAdminAuthTest extends integrationTestCase public function createTestData() { - $admin = DatabaseProvider::getDb()->getOne('SELECT oxid FROM oxuser WHERE oxrights = \'malladmin\''); + /** @var DatabaseInterface $db */ + $db = d3GetOxidDIC()->get('d3ox.webauthn.'.DatabaseInterface::class.'.assoc'); + $admin = $db->getOne('SELECT oxid FROM oxuser WHERE oxrights = \'malladmin\''); Registry::getSession()->setVariable(WebauthnConf::OXID_ADMIN_AUTH, $admin); $this->createUser( $this->userList[1], diff --git a/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php b/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php index 4c2b204..55a5c19 100644 --- a/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3user_webauthnTest.php @@ -21,9 +21,10 @@ use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; +use D3\Webauthn\tests\unit\WAUnitTestCase; use Exception; +use Generator; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Utils; use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; @@ -31,7 +32,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use ReflectionException; -class d3user_webauthnTest extends TestCase +class d3user_webauthnTest extends WAUnitTestCase { use IsMockable; use CanAccessRestricted; @@ -58,6 +59,7 @@ class d3user_webauthnTest extends TestCase ->onlyMethods(['isAvailable']) ->getMock(); $webauthnMock->method('isAvailable')->willReturn(false); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var d3_User_Webauthn|MockObject $userMock */ $userMock = $this->getMockBuilder(d3_User_Webauthn::class) @@ -65,30 +67,17 @@ class d3user_webauthnTest extends TestCase ->getMock(); $userMock->expects($this->atLeastOnce())->method('load')->with('editObjectId')->willReturn($canLoadUser); $userMock->method('getId')->willReturn('editObjectId'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var d3user_webauthn|MockObject $sutMock */ $sutMock = $this->getMockBuilder(d3user_webauthn::class) ->onlyMethods([ 'd3CallMockableFunction', - 'getEditObjectId', - 'd3GetMockableOxNewObject', + 'getEditObjectId' ]) ->getMock(); $sutMock->method('d3CallMockableFunction')->willReturn(true); $sutMock->method('getEditObjectId')->willReturn('editObjectId'); - $sutMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock, $webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->setValue( $sutMock, @@ -110,14 +99,12 @@ class d3user_webauthnTest extends TestCase } /** - * @return array + * @return Generator */ - public function canRenderDataProvider(): array + public function canRenderDataProvider(): Generator { - return [ - 'can load user' => [true], - 'can not load user' => [false], - ]; + yield 'can load user' => [true]; + yield 'can not load user' => [false]; } /** @@ -133,35 +120,23 @@ class d3user_webauthnTest extends TestCase ->onlyMethods(['redirect']) ->getMock(); $utilsMock->expects($this->never())->method('redirect')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var d3user_webauthn|MockObject $sutMock */ $sutMock = $this->getMockBuilder(d3user_webauthn::class) ->onlyMethods([ 'setPageType', - 'setAuthnRegister', - 'd3GetMockableLogger', - 'd3GetMockableRegistryObject', + 'setAuthnRegister' ]) ->getMock(); $sutMock->expects($this->atLeastOnce())->method('setPageType'); $sutMock->expects($this->atLeastOnce())->method('setAuthnRegister'); - $sutMock->expects($this->never())->method('d3GetMockableLogger')->willReturn($loggerMock); - $sutMock->expects($this->never())->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sutMock, @@ -182,35 +157,23 @@ class d3user_webauthnTest extends TestCase ->onlyMethods(['redirect']) ->getMock(); $utilsMock->expects($this->once())->method('redirect')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var d3user_webauthn|MockObject $sutMock */ $sutMock = $this->getMockBuilder(d3user_webauthn::class) ->onlyMethods([ 'setPageType', - 'setAuthnRegister', - 'd3GetMockableLogger', - 'd3GetMockableRegistryObject', + 'setAuthnRegister' ]) ->getMock(); $sutMock->expects($this->atLeastOnce())->method('setPageType'); $sutMock->expects($this->atLeastOnce())->method('setAuthnRegister')->willThrowException(oxNew(WebauthnException::class)); - $sutMock->expects($this->atLeastOnce())->method('d3GetMockableLogger')->willReturn($loggerMock); - $sutMock->expects($this->atLeastOnce())->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sutMock, @@ -227,34 +190,24 @@ class d3user_webauthnTest extends TestCase public function canSaveAuthnHasError() { $_POST['error'] = 'msg'; + $_GET['error'] = 'msg'; + $_REQUEST['error'] = 'msg'; /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); - /** @var d3user_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger']) - ->getMock(); - $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $oControllerMock->expects($this->atLeastOnce())->method('d3GetMockableLogger')->willReturn($loggerMock); + /** @var d3user_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3user_webauthn::class); $this->callMethod( $oControllerMock, @@ -278,39 +231,17 @@ class d3user_webauthnTest extends TestCase ->onlyMethods(['saveAuthn']) ->getMock(); $webauthnMock->expects($this->once())->method('saveAuthn'); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->never())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); - /** @var d3user_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) - ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var d3user_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3user_webauthn::class); $this->callMethod( $oControllerMock, @@ -337,45 +268,23 @@ class d3user_webauthnTest extends TestCase ->getMock(); $webauthnMock->expects($this->once())->method('saveAuthn') ->willThrowException(oxNew($excClass)); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); - /** @var d3user_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'd3GetMockableLogger']) - ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); + /** @var d3user_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3user_webauthn::class); $this->callMethod( $oControllerMock, @@ -433,22 +342,12 @@ class d3user_webauthnTest extends TestCase } else { $webAuthnMock->method('getCreationOptions')->willReturn('options'); } + d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock); /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser']) + ->onlyMethods(['addTplParam', 'getUser']) ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webAuthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webAuthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3)) ->method('addTplParam'); $oControllerMock->method('getUser')->willReturn(oxNew(User::class)); @@ -489,30 +388,19 @@ class d3user_webauthnTest extends TestCase 'oxpassword' => 'foo', ] ); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $oUser); /** @var PublicKeyCredentialList|MockObject $publicKeyCredentialListMock */ $publicKeyCredentialListMock = $this->getMockBuilder(PublicKeyCredentialList::class) ->onlyMethods(['getAllFromUser']) ->getMock(); $publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf(); + d3GetOxidDIC()->set(PublicKeyCredentialList::class, $publicKeyCredentialListMock); /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject']) + ->onlyMethods([]) ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($oUser, $publicKeyCredentialListMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $oUser; - case PublicKeyCredentialList::class: - return $publicKeyCredentialListMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->assertIsArray( $this->callMethod( @@ -540,22 +428,12 @@ class d3user_webauthnTest extends TestCase ->getMock(); $publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId)) ->willReturn(true); + d3GetOxidDIC()->set(PublicKeyCredential::class, $publicKeyCredentialMock); /** @var d3user_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3user_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject']) + ->onlyMethods([]) ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($publicKeyCredentialMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredential::class: - return $publicKeyCredentialMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->callMethod($oControllerMock, 'deleteKey'); } diff --git a/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php b/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php index eadbb4f..02b0ecc 100644 --- a/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php +++ b/src/tests/unit/Application/Controller/Admin/d3webauthnadminloginTest.php @@ -24,6 +24,7 @@ use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\tests\unit\Application\Controller\d3webauthnloginTest; +use Generator; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\Session; @@ -81,6 +82,7 @@ class d3webauthnadminloginTest extends d3webauthnloginTest [WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH, $auth], [WebauthnConf::WEBAUTHN_ADMIN_SESSION_CURRENTUSER, $userFromLogin], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Utils|MockObject $utilsMock */ $utilsMock = $this->getMockBuilder(Utils::class) @@ -88,48 +90,26 @@ class d3webauthnadminloginTest extends d3webauthnloginTest ->getMock(); $utilsMock->expects($startRedirect ? $this->once() : $this->never()) ->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var WebauthnAfterLogin|MockObject $afterLoginMock */ $afterLoginMock = $this->getMockBuilder(WebauthnAfterLogin::class) ->onlyMethods(['changeLanguage']) ->getMock(); $afterLoginMock->expects($this->once())->method('changeLanguage'); + d3GetOxidDIC()->set(WebauthnAfterLogin::class, $afterLoginMock); /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction', 'd3GetMockableOxNewObject', + ->onlyMethods(['d3CallMockableFunction', 'generateCredentialRequest', 'addTplParam', ]) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl'); // "any" because redirect doesn't stop execution $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) ->method('generateCredentialRequest'); $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) ->method('addTplParam')->willReturn(true); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($afterLoginMock) { - $args = func_get_args(); - switch ($args[0]) { - case WebauthnAfterLogin::class: - return $afterLoginMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->assertSame( 'myTemplate.tpl', @@ -141,15 +121,13 @@ class d3webauthnadminloginTest extends d3webauthnloginTest } /** - * @return array + * @return Generator */ - public function canRenderDataProvider(): array + public function canRenderDataProvider(): Generator { - return [ - 'has request' => [false, true, false, 'start'], - 'has auth' => [true, true, true, 'admin_start'], - 'missing user' => [false, false, true, 'login'], - ]; + yield 'has request' => [false, true, false, 'start']; + yield 'has auth' => [true, true, true, 'admin_start']; + yield 'missing user' => [false, false, true, 'login']; } /** @@ -234,34 +212,14 @@ class d3webauthnadminloginTest extends d3webauthnloginTest $requestMock = $this->getMockBuilder(Request::class) ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); - $requestMock->expects($this->exactly(3))->method('getRequestEscapedParameter')->willReturn('abc'); + $requestMock->expects($this->exactly(1))->method('getRequestEscapedParameter')->willReturn('abc'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var d3webauthnadminlogin|MockObject $sut */ $sut = $this->getMockBuilder(d3webauthnadminlogin::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) + ->onlyMethods(['getWebAuthnLogin']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($loginMock) { - $args = func_get_args(); - switch ($args[0]) { - case WebauthnLogin::class: - return $loginMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($requestMock) { - $args = func_get_args(); - switch ($args[0]) { - case Request::class: - return $requestMock; - default: - return Registry::get($args[0]); - } - } - ); + $sut->method('getWebAuthnLogin')->willReturn($loginMock); $this->assertSame( 'expected', @@ -285,31 +243,20 @@ class d3webauthnadminloginTest extends d3webauthnloginTest ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var Request|MockObject $requestMock */ $requestMock = $this->getMockBuilder(Request::class) ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); - $requestMock->expects($this->atLeast(2))->method('getRequestEscapedParameter')->willReturn('abc'); + $requestMock->expects($this->atLeast(0))->method('getRequestEscapedParameter')->willReturn('abc'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var d3webauthnadminlogin|MockObject $sut */ $sut = $this->getMockBuilder(d3webauthnadminlogin::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) + ->onlyMethods(['getWebAuthnLogin']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willThrowException(oxNew(WebauthnGetException::class)); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock, $requestMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - case Request::class: - return $requestMock; - default: - return Registry::get($args[0]); - } - } - ); + $sut->method('getWebAuthnLogin')->willThrowException(oxNew(WebauthnGetException::class)); $this->assertSame( 'login', @@ -321,15 +268,39 @@ class d3webauthnadminloginTest extends d3webauthnloginTest } /** - * @return array + * @return Generator */ - public function canAssertAuthnCookieSubshopDataProvider(): array + public function canAssertAuthnCookieSubshopDataProvider(): Generator { - return [ - 'missing cookie' => ['login', true, null, 'user'], - 'no admin user' => ['login', true, 'cookie', 'user'], - 'assertion succ malladmin' => ['admin_start', false, 'cookie', 'malladmin'], - 'assertion succ shop1' => ['admin_start', false, 'cookie', 1], - ]; + yield 'missing cookie' => ['login', true, null, 'user']; + yield 'no admin user' => ['login', true, 'cookie', 'user']; + yield 'assertion succ malladmin' => ['admin_start', false, 'cookie', 'malladmin']; + yield 'assertion succ shop1' => ['admin_start', false, 'cookie', 1]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Controller\Admin\d3webauthnadminlogin::getWebAuthnLogin + */ + public function canGetWebAuthnLogin() + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder(Request::class) + ->onlyMethods(['getRequestEscapedParameter']) + ->getMock(); + $requestMock->method('getRequestEscapedParameter')->willReturn('requestReturn'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); + + /** @var d3webauthnadminlogin|MockObject $sut */ + $sut = oxNew(d3webauthnadminlogin::class); + + $this->assertInstanceOf( + WebauthnLogin::class, + $this->callMethod( + $sut, + 'getWebAuthnLogin' + ) + ); } } diff --git a/src/tests/unit/Application/Controller/d3_account_webauthnTest.php b/src/tests/unit/Application/Controller/d3_account_webauthnTest.php index e79c084..95a61bd 100644 --- a/src/tests/unit/Application/Controller/d3_account_webauthnTest.php +++ b/src/tests/unit/Application/Controller/d3_account_webauthnTest.php @@ -21,15 +21,17 @@ use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Webauthn; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use ReflectionException; -class d3_account_webauthnTest extends UnitTestCase +class d3_account_webauthnTest extends WAUnitTestCase { use CanAccessRestricted; @@ -77,23 +79,13 @@ class d3_account_webauthnTest extends UnitTestCase ->onlyMethods(['isAvailable']) ->getMock(); $webAuthnMock->expects($this->atLeastOnce())->method('isAvailable')->willReturn(true); + d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock); /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['getUser', 'd3GetMockableOxNewObject']) + ->onlyMethods(['getUser']) ->getMock(); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webAuthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webAuthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->_oController = $oControllerMock; @@ -119,23 +111,13 @@ class d3_account_webauthnTest extends UnitTestCase ->onlyMethods(['isAvailable']) ->getMock(); $webAuthnMock->expects($this->atLeastOnce())->method('isAvailable')->willReturn(true); + d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock); /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['getUser', 'd3GetMockableOxNewObject']) + ->onlyMethods(['getUser']) ->getMock(); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webAuthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webAuthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->_oController = $oControllerMock; @@ -167,23 +149,13 @@ class d3_account_webauthnTest extends UnitTestCase ->onlyMethods(['getAllFromUser']) ->getMock(); $publicKeyCredentialListMock->method('getAllFromUser')->with($oUser)->willReturnSelf(); + d3GetOxidDIC()->set(PublicKeyCredentialList::class, $publicKeyCredentialListMock); /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['getUser', 'd3GetMockableOxNewObject']) + ->onlyMethods(['getUser']) ->getMock(); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($publicKeyCredentialListMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredentialList::class: - return $publicKeyCredentialListMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->_oController = $oControllerMock; @@ -216,15 +188,15 @@ class d3_account_webauthnTest extends UnitTestCase $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetMockableLogger']) + ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser']) ->getMock(); $oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister'); $oControllerMock->expects($this->atLeastOnce())->method('setPageType'); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); $this->_oController = $oControllerMock; @@ -254,16 +226,16 @@ class d3_account_webauthnTest extends UnitTestCase $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser', 'd3GetMockableLogger']) + ->onlyMethods(['setAuthnRegister', 'setPageType', 'getUser']) ->getMock(); $oControllerMock->expects($this->atLeastOnce())->method('setAuthnRegister') ->willThrowException(oxNew(WebauthnException::class)); $oControllerMock->expects($this->never())->method('setPageType'); $oControllerMock->method('getUser')->willReturn($oUser); - $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); $this->_oController = $oControllerMock; @@ -292,22 +264,12 @@ class d3_account_webauthnTest extends UnitTestCase } else { $webAuthnMock->method('getCreationOptions')->willReturn('options'); } + d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock); /** @var d3_account_webauthn|MockObject $oControllerMock */ $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', 'getUser']) + ->onlyMethods(['addTplParam', 'getUser']) ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webAuthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webAuthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $oControllerMock->expects($throwExc ? $this->never() : $this->atLeast(3)) ->method('addTplParam'); $oControllerMock->method('getUser')->willReturn(oxNew(User::class)); @@ -376,28 +338,25 @@ class d3_account_webauthnTest extends UnitTestCase ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); - /** @var d3_account_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableLogger']) + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder(Request::class) + ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); - $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $oControllerMock->method('d3GetMockableLogger')->willReturn($loggerMock); + $requestMock->method('getRequestEscapedParameter')->with( + $this->identicalTo('error') + )->willReturn('errorMsg'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); + + /** @var d3_account_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3_account_webauthn::class); $this->_oController = $oControllerMock; @@ -423,39 +382,17 @@ class d3_account_webauthnTest extends UnitTestCase ->onlyMethods(['saveAuthn']) ->getMock(); $webauthnMock->expects($this->once())->method('saveAuthn'); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->never())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); - /** @var d3_account_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) - ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var d3_account_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3_account_webauthn::class); $this->_oController = $oControllerMock; @@ -482,39 +419,17 @@ class d3_account_webauthnTest extends UnitTestCase ->getMock(); $webauthnMock->expects($this->once())->method('saveAuthn') ->willThrowException(oxNew(WebauthnException::class)); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->atLeastOnce())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); - /** @var d3_account_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) - ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $oControllerMock->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var d3_account_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3_account_webauthn::class); $this->_oController = $oControllerMock; @@ -541,22 +456,10 @@ class d3_account_webauthnTest extends UnitTestCase ->getMock(); $publicKeyCredentialMock->expects($expected)->method('delete')->with($this->identicalTo($deleteId)) ->willReturn(true); + d3GetOxidDIC()->set(PublicKeyCredential::class, $publicKeyCredentialMock); - /** @var d3_account_webauthn|MockObject $oControllerMock */ - $oControllerMock = $this->getMockBuilder(d3_account_webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $oControllerMock->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($publicKeyCredentialMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredential::class: - return $publicKeyCredentialMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var d3_account_webauthn $oControllerMock */ + $oControllerMock = oxNew(d3_account_webauthn::class); $this->_oController = $oControllerMock; diff --git a/src/tests/unit/Application/Controller/d3webauthnloginTest.php b/src/tests/unit/Application/Controller/d3webauthnloginTest.php index 245f1cd..5924a0c 100644 --- a/src/tests/unit/Application/Controller/d3webauthnloginTest.php +++ b/src/tests/unit/Application/Controller/d3webauthnloginTest.php @@ -20,6 +20,8 @@ use D3\Webauthn\Application\Controller\d3webauthnlogin; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; +use D3\Webauthn\tests\unit\WAUnitTestCase; +use Generator; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\Utils; @@ -28,7 +30,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use ReflectionException; -class d3webauthnloginTest extends UnitTestCase +class d3webauthnloginTest extends WAUnitTestCase { use CanAccessRestricted; @@ -49,22 +51,12 @@ class d3webauthnloginTest extends UnitTestCase $sessionMock->method('getVariable')->willReturn([ 'key1' => 'variable1', ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction']) + ->onlyMethods(['d3CallMockableFunction']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('d3CallMockableFunction')->willReturn(['defKey1' => 'devValues1']); $this->assertSame( @@ -101,6 +93,7 @@ class d3webauthnloginTest extends UnitTestCase [WebauthnConf::WEBAUTHN_SESSION_AUTH, $auth], [WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $userFromLogin], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Utils|MockObject $utilsMock */ $utilsMock = $this->getMockBuilder(Utils::class) @@ -108,25 +101,13 @@ class d3webauthnloginTest extends UnitTestCase ->getMock(); $utilsMock->expects($startRedirect ? $this->once() : $this->never()) ->method('redirect')->with('index.php?cl='.$redirectController)->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3CallMockableFunction', + ->onlyMethods(['d3CallMockableFunction', 'generateCredentialRequest', 'addTplParam', ]) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('d3CallMockableFunction')->willReturn('myTemplate.tpl'); $sut->expects($startRedirect ? $this->any() : $this->atLeastOnce()) ->method('generateCredentialRequest'); @@ -143,15 +124,13 @@ class d3webauthnloginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canRenderDataProvider(): array + public function canRenderDataProvider(): Generator { - return [ - 'has request' => [false, true, false, 'start'], - 'has auth' => [true, true, true, 'start'], - 'missing user' => [false, false, true, 'start'], - ]; + yield 'has request' => [false, true, false, 'start']; + yield 'has auth' => [true, true, true, 'start']; + yield 'missing user' => [false, false, true, 'start']; } /** @@ -168,6 +147,7 @@ class d3webauthnloginTest extends UnitTestCase $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->never())->method('error')->willReturn(true); $loggerMock->expects($this->never())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -176,6 +156,7 @@ class d3webauthnloginTest extends UnitTestCase $sessionMock->method('getVariable')->willReturnMap([ [$userSessionVarName, $currUserFixture], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Webauthn|MockObject $webAuthnMock */ $webAuthnMock = $this->getMockBuilder(Webauthn::class) @@ -183,36 +164,14 @@ class d3webauthnloginTest extends UnitTestCase ->getMock(); $webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture) ->willReturn('success'); + d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock); /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'addTplParam', 'd3GetMockableLogger']) + ->onlyMethods(['addTplParam']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webAuthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webAuthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $sut->expects($this->atLeast(2)) ->method('addTplParam')->willReturn(true); - $sut->method('d3GetMockableLogger')->willReturn($loggerMock); $this->callMethod( $sut, @@ -234,6 +193,7 @@ class d3webauthnloginTest extends UnitTestCase $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($this->atLeastOnce())->method('error')->willReturn(true); $loggerMock->expects($this->atLeastOnce())->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -244,6 +204,7 @@ class d3webauthnloginTest extends UnitTestCase ]); $sessionMock->expects($this->once())->method('setVariable')->with(WebauthnConf::GLOBAL_SWITCH) ->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Webauthn|MockObject $webAuthnMock */ $webAuthnMock = $this->getMockBuilder(Webauthn::class) @@ -251,6 +212,7 @@ class d3webauthnloginTest extends UnitTestCase ->getMock(); $webAuthnMock->expects($this->once())->method('getRequestOptions')->with($currUserFixture) ->willThrowException(oxNew(WebauthnException::class, 'foobar0')); + d3GetOxidDIC()->set(Webauthn::class, $webAuthnMock); /** @var Utils|MockObject $utilsMock */ $utilsMock = $this->getMockBuilder(Utils::class) @@ -258,39 +220,14 @@ class d3webauthnloginTest extends UnitTestCase ->getMock(); $utilsMock->expects($this->once())->method('redirect') ->with('index.php?cl='.$redirectClass)->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var d3webauthnlogin|MockObject $sut */ $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetMockableOxNewObject', 'addTplParam', - 'd3GetMockableLogger', 'd3GetMockableRegistryObject', ]) + ->onlyMethods(['addTplParam']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webAuthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webAuthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $sut->expects($this->never()) ->method('addTplParam')->willReturn(true); - $sut->expects($this->atLeast(2))->method('d3GetMockableLogger')->willReturn($loggerMock); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -315,22 +252,10 @@ class d3webauthnloginTest extends UnitTestCase $sessionMock->method('getVariable')->willReturnMap([ [$sessionVarName, $currClassFixture], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); - /** @var d3webauthnlogin|MockObject $sut */ - $sut = $this->getMockBuilder($this->sutClassName) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var d3webauthnlogin $sut */ + $sut = oxNew($this->sutClassName); $this->assertSame( $currClassFixture, diff --git a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php index cced323..4094452 100644 --- a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php +++ b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialListTest.php @@ -19,6 +19,8 @@ use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; use D3\Webauthn\Application\Model\UserEntity; +use D3\Webauthn\tests\unit\WAUnitTestCase; +use Hoa\Iterator\Mock; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; @@ -27,7 +29,7 @@ use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; use Webauthn\PublicKeyCredentialSource; -class PublicKeyCredentialListTest extends UnitTestCase +class PublicKeyCredentialListTest extends WAUnitTestCase { use CanAccessRestricted; @@ -73,27 +75,12 @@ class PublicKeyCredentialListTest extends UnitTestCase ->onlyMethods(['getShopId']) ->getMock(); $configMock->method('getShopId')->willReturn(55); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var PublicKeyCredentialList|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredentialList::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); - - /** @var PublicKeyCredentialSource|MockObject $pkcsMock */ - $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) - ->disableOriginalConstructor() + ->onlyMethods(['findAllForUserEntity']) ->getMock(); if ($doCreate) { @@ -106,22 +93,35 @@ class PublicKeyCredentialListTest extends UnitTestCase $pkc->assign([ 'credentialid' => base64_encode('myCredentialId'), 'oxshopid' => 55, + 'name' => __METHOD__, + // can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable + 'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9' ]); - $pkc->setCredential($pkcsMock); $pkc->save(); } - $this->assertEquals( - $expected === 'pkcsource' ? $pkcsMock : $expected, - $this->callMethod( + try { + $return = $this->callMethod( $sut, 'findOneByCredentialId', ['myCredentialId'] - ) - ); + ); - if ($doCreate) { - $pkc->delete($oxid); + if ($expected === 'pkcsource') { + $this->assertInstanceOf( + PublicKeyCredentialSource::class, + $return + ); + } else { + $this->assertEquals( + $expected, + $return + ); + } + } finally { + if ($doCreate) { + $pkc->delete($oxid); + } } } @@ -154,26 +154,12 @@ class PublicKeyCredentialListTest extends UnitTestCase ->onlyMethods(['getShopId']) ->getMock(); $configMock->method('getShopId')->willReturn(55); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var PublicKeyCredentialList|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredentialList::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); - - $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) - ->disableOriginalConstructor() + ->onlyMethods(['findOneByCredentialId']) ->getMock(); if ($doCreate) { @@ -186,8 +172,10 @@ class PublicKeyCredentialListTest extends UnitTestCase $pkc->assign([ 'oxuserid' => 'userid', 'oxshopid' => 55, + 'credentialid' => __METHOD__, + // can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable + 'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9' ]); - $pkc->setCredential($pkcsMock); $pkc->save(); } } @@ -199,21 +187,31 @@ class PublicKeyCredentialListTest extends UnitTestCase ->getMock(); $pkcUserEntity->method('getId')->willReturn('userid'); - $this->assertEquals( - $expected === 'pkcsource' ? [$pkcsMock, $pkcsMock] : [], - $this->callMethod( + try { + $list = $this->callMethod( $sut, 'findAllForUserEntity', [$pkcUserEntity] - ) - ); + ); - if ($doCreate) { - foreach ($oxids as $oxid) { - $pkc = $this->getMockBuilder(PublicKeyCredential::class) - ->onlyMethods(['allowDerivedDelete']) - ->getMock(); - $pkc->delete($oxid); + if ($expected === 'pkcsource') { + $this->assertCount( 2, $list ); + foreach ( $list as $item ) { + $this->assertInstanceOf( PublicKeyCredentialSource::class, $item ); + } + } else { + $this->assertEmpty($list); + } + } finally { + if ($doCreate) { + foreach ($oxids as $oxid) { + /** @var PublicKeyCredential $pkc */ + $pkc = $this->getMockBuilder(PublicKeyCredential::class) + ->onlyMethods(['allowDerivedDelete']) + ->getMock(); + $pkc->method('allowDerivedDelete')->willReturn(true); + $pkc->delete($oxid); + } } } } @@ -237,23 +235,13 @@ class PublicKeyCredentialListTest extends UnitTestCase ->onlyMethods(['getShopId']) ->getMock(); $configMock->method('getShopId')->willReturn(55); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var PublicKeyCredentialList|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredentialList::class) + ->onlyMethods(['saveCredentialSource']) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -263,9 +251,6 @@ class PublicKeyCredentialListTest extends UnitTestCase $userMock->method('getId')->willReturn('userid'); if ($doCreate) { - $pkcsMock = $this->getMockBuilder(PublicKeyCredentialSource::class) - ->disableOriginalConstructor() - ->getMock(); foreach ($oxids as $oxid) { $pkc = $this->getMockBuilder(PublicKeyCredential::class) ->onlyMethods(['allowDerivedDelete']) @@ -275,28 +260,35 @@ class PublicKeyCredentialListTest extends UnitTestCase $pkc->assign([ 'oxuserid' => 'userid', 'oxshopid' => 55, + 'credentialid' => __METHOD__, + // can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable + 'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9' ]); - $pkc->setCredential($pkcsMock); $pkc->save(); } } - $return = $this->callMethod( - $sut, - 'getAllFromUser', - [$userMock] - ); - - if ($doCreate) { - foreach ($oxids as $oxid) { - $pkc = $this->getMockBuilder(PublicKeyCredential::class) - ->onlyMethods(['allowDerivedDelete']) - ->getMock(); - $pkc->delete($oxid); + try { + $return = $this->callMethod( + $sut, + 'getAllFromUser', + [$userMock] + ); + } finally { + if ($doCreate) { + foreach ($oxids as $oxid) { + /** @var PublicKeyCredential|MockObject $pkc */ + $pkc = $this->getMockBuilder(PublicKeyCredential::class) + ->onlyMethods(['allowDerivedDelete']) + ->getMock(); + $pkc->method('allowDerivedDelete')->willReturn(true); + $pkc->delete($oxid); + } } } $this->assertInstanceOf(PublicKeyCredentialList::class, $return); + $this->assertCount($expectedCount, $return); } diff --git a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php index 14660c3..a7c4bcf 100644 --- a/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php +++ b/src/tests/unit/Application/Model/Credential/PublicKeyCredentialTest.php @@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Application\Model\Credential; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Credential\PublicKeyCredential; use D3\Webauthn\Application\Model\Credential\PublicKeyCredentialList; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; use OxidEsales\TestingLibrary\UnitTestCase; @@ -25,7 +26,7 @@ use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; use Webauthn\PublicKeyCredentialSource; -class PublicKeyCredentialTest extends UnitTestCase +class PublicKeyCredentialTest extends WAUnitTestCase { use CanAccessRestricted; @@ -193,16 +194,20 @@ class PublicKeyCredentialTest extends UnitTestCase */ public function canGetCredential() { - /** @var PublicKeyCredentialSource $publicKeyCredentialSourceMock */ - $publicKeyCredentialSourceMock = $this->getMockBuilder(PublicKeyCredentialSource::class) - ->disableOriginalConstructor() - ->getMock(); + // can't get mock of PublicKeyCredentialSource because of cascaded mock object is not serializable - $this->canGetField( - 'credential', - 'getCredential', - base64_encode(serialize($publicKeyCredentialSourceMock)), - $publicKeyCredentialSourceMock + /** @var PublicKeyCredential $sut */ + $sut = oxNew(PublicKeyCredential::class); + $sut->assign([ + 'credential' => 'TzozNDoiV2ViYXV0aG5cUHVibGljS2V5Q3JlZGVudGlhbFNvdXJjZSI6MTA6e3M6MjQ6IgAqAHB1YmxpY0tleUNyZWRlbnRpYWxJZCI7czo2NToiAQUtRW3vxImpllhVhp3sUeC0aBae8rFm0hBhHpVSdkdrmqZp+tnfgcuP8xJUbsjMMDyt908zZ2RXAtibmbbilOciO3M6NzoiACoAdHlwZSI7czoxMDoicHVibGljLWtleSI7czoxMzoiACoAdHJhbnNwb3J0cyI7YTowOnt9czoxODoiACoAYXR0ZXN0YXRpb25UeXBlIjtzOjQ6Im5vbmUiO3M6MTI6IgAqAHRydXN0UGF0aCI7TzozMzoiV2ViYXV0aG5cVHJ1c3RQYXRoXEVtcHR5VHJ1c3RQYXRoIjowOnt9czo5OiIAKgBhYWd1aWQiO086MzU6IlJhbXNleVxVdWlkXExhenlcTGF6eVV1aWRGcm9tU3RyaW5nIjoxOntzOjY6InN0cmluZyI7czozNjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIjt9czoyMjoiACoAY3JlZGVudGlhbFB1YmxpY0tleSI7czo3NzoipQECAyYgASFYIKelzI2/b094o/XiJmXWUkVr8cvhAucLplHTxtl0oKtrIlgguKi+0epmmjeemuzzGspNotA7uKnkk4oAmDUOKsJgLykiO3M6MTM6IgAqAHVzZXJIYW5kbGUiO3M6MTQ6Im94ZGVmYXVsdGFkbWluIjtzOjEwOiIAKgBjb3VudGVyIjtpOjA7czoxMDoiACoAb3RoZXJVSSI7Tjt9', + ]); + + $this->assertInstanceOf( + PublicKeyCredentialSource::class, + $this->callMethod( + $sut, + 'getCredential' + ) ); } @@ -231,22 +236,12 @@ class PublicKeyCredentialTest extends UnitTestCase $pkcListObjectMock->method('findOneByCredentialId')->willReturn( $credAlreadyExist ? $publicKeyCredentialSourceMock : null ); + d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pkcListObjectMock); /** @var PublicKeyCredential|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredential::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'exists', 'getIdByCredentialId', 'load', 'save']) + ->onlyMethods(['exists', 'getIdByCredentialId', 'load', 'save']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($pkcListObjectMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredentialList::class: - return $pkcListObjectMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $sut->method('exists')->willReturn($credIdExist); $sut->expects($this->exactly((int) $doSave))->method('getIdByCredentialId'); $sut->expects($this->exactly((int) ($doSave && $credIdExist)))->method('load'); @@ -296,22 +291,12 @@ class PublicKeyCredentialTest extends UnitTestCase ->onlyMethods(['getShopId']) ->getMock(); $configMock->method('getShopId')->willReturn($shopId); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var PublicKeyCredential|MockObject $sut */ $sut = $this->getMockBuilder(PublicKeyCredential::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'allowDerivedDelete']) + ->onlyMethods(['allowDerivedDelete']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('allowDerivedDelete')->willReturn(true); if ($doCreate) { @@ -323,17 +308,19 @@ class PublicKeyCredentialTest extends UnitTestCase $sut->save(); } - $this->assertSame( - $expected, - $this->callMethod( - $sut, - 'getIdByCredentialId', - [$pkcId] - ) - ); - - if ($doCreate) { - $sut->delete($oxid); + try { + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getIdByCredentialId', + [$pkcId] + ) + ); + } finally { + if ($doCreate) { + $sut->delete($oxid); + } } } diff --git a/src/tests/unit/Application/Model/Exceptions/WebauthnCreateExceptionTest.php b/src/tests/unit/Application/Model/Exceptions/WebauthnCreateExceptionTest.php index 3da0ac5..53359ce 100644 --- a/src/tests/unit/Application/Model/Exceptions/WebauthnCreateExceptionTest.php +++ b/src/tests/unit/Application/Model/Exceptions/WebauthnCreateExceptionTest.php @@ -17,11 +17,12 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Exceptions\WebauthnCreateException; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class WebauthnCreateExceptionTest extends UnitTestCase +class WebauthnCreateExceptionTest extends WAUnitTestCase { use CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php b/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php index e3053a0..228065c 100644 --- a/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php +++ b/src/tests/unit/Application/Model/Exceptions/WebauthnExceptionTest.php @@ -18,12 +18,13 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\WebauthnErrors; +use D3\Webauthn\tests\unit\WAUnitTestCase; use Exception; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class WebauthnExceptionTest extends UnitTestCase +class WebauthnExceptionTest extends WAUnitTestCase { use CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/Exceptions/WebauthnGetExceptionTest.php b/src/tests/unit/Application/Model/Exceptions/WebauthnGetExceptionTest.php index c277ae1..3d7c6d3 100644 --- a/src/tests/unit/Application/Model/Exceptions/WebauthnGetExceptionTest.php +++ b/src/tests/unit/Application/Model/Exceptions/WebauthnGetExceptionTest.php @@ -17,11 +17,12 @@ namespace D3\Webauthn\tests\unit\Application\Model\Exceptions; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Exceptions\WebauthnGetException; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class WebauthnGetExceptionTest extends UnitTestCase +class WebauthnGetExceptionTest extends WAUnitTestCase { use CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/RelyingPartyEntityTest.php b/src/tests/unit/Application/Model/RelyingPartyEntityTest.php index 6fe8bed..12d1756 100644 --- a/src/tests/unit/Application/Model/RelyingPartyEntityTest.php +++ b/src/tests/unit/Application/Model/RelyingPartyEntityTest.php @@ -17,12 +17,13 @@ namespace D3\Webauthn\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\RelyingPartyEntity; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Model\Shop; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use ReflectionException; -class RelyingPartyEntityTest extends TestCase +class RelyingPartyEntityTest extends WAUnitTestCase { use CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/UserEntityTest.php b/src/tests/unit/Application/Model/UserEntityTest.php index 1f433f3..c5853d8 100644 --- a/src/tests/unit/Application/Model/UserEntityTest.php +++ b/src/tests/unit/Application/Model/UserEntityTest.php @@ -18,12 +18,13 @@ namespace D3\Webauthn\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Exceptions\WebauthnException; use D3\Webauthn\Application\Model\UserEntity; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class UserEntityTest extends UnitTestCase +class UserEntityTest extends WAUnitTestCase { use CanAccessRestricted; diff --git a/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php b/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php index e763f64..edeef73 100644 --- a/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php +++ b/src/tests/unit/Application/Model/WebauthnAfterLoginTest.php @@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\WebauthnAfterLogin; use D3\Webauthn\Application\Model\WebauthnConf; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Core\Language; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Request; @@ -28,7 +29,7 @@ use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; use stdClass; -class WebauthnAfterLoginTest extends UnitTestCase +class WebauthnAfterLoginTest extends WAUnitTestCase { use CanAccessRestricted; @@ -53,12 +54,14 @@ class WebauthnAfterLoginTest extends UnitTestCase $this->identicalTo('oxidadminprofile'), $this->logicalOr($this->identicalTo('1@prof@No1'), $this->identicalTo('')) ); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock); /** @var Request|MockObject $requestMock */ $requestMock = $this->getMockBuilder(Request::class) ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); $requestMock->method('getRequestEscapedParameter')->willReturn($requestProfile); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -70,26 +73,10 @@ class WebauthnAfterLoginTest extends UnitTestCase ]); $sessionMock->expects($this->once())->method('deleteVariable'); $sessionMock->expects($this->exactly((int) $setSessionVar))->method('setVariable'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); - /** @var WebauthnAfterLogin|MockObject $sut */ - $sut = $this->getMockBuilder(WebauthnAfterLogin::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock, $requestMock, $utilsServerMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - case Request::class: - return $requestMock; - case UtilsServer::class: - return $utilsServerMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var WebauthnAfterLogin $sut */ + $sut = oxNew(WebauthnAfterLogin::class); $this->callMethod( $sut, @@ -133,6 +120,7 @@ class WebauthnAfterLoginTest extends UnitTestCase ); $languageMock->expects($this->once())->method('setTplLanguage') ->with($this->identicalTo($expectedLang)); + d3GetOxidDIC()->set('d3ox.webauthn.'.Language::class, $languageMock); /** @var UtilsServer|MockObject $utilsServerMock */ $utilsServerMock = $this->getMockBuilder(UtilsServer::class) @@ -142,12 +130,14 @@ class WebauthnAfterLoginTest extends UnitTestCase $this->identicalTo('oxidadminlanguage'), $this->identicalTo($expectedAbbr) ); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock); /** @var Request|MockObject $requestMock */ $requestMock = $this->getMockBuilder(Request::class) ->onlyMethods(['getRequestEscapedParameter']) ->getMock(); $requestMock->method('getRequestEscapedParameter')->willReturn($requestLang); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -157,28 +147,10 @@ class WebauthnAfterLoginTest extends UnitTestCase [WebauthnConf::WEBAUTHN_ADMIN_CHLANGUAGE, $sessionLang], ]); $sessionMock->expects($this->once())->method('deleteVariable'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); - /** @var WebauthnAfterLogin|MockObject $sut */ - $sut = $this->getMockBuilder(WebauthnAfterLogin::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock, $requestMock, $utilsServerMock, $languageMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - case Request::class: - return $requestMock; - case UtilsServer::class: - return $utilsServerMock; - case Language::class: - return $languageMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var WebauthnAfterLogin $sut */ + $sut = oxNew(WebauthnAfterLogin::class); $this->callMethod( $sut, diff --git a/src/tests/unit/Application/Model/WebauthnErrorsTest.php b/src/tests/unit/Application/Model/WebauthnErrorsTest.php index 2c57959..ffbc786 100644 --- a/src/tests/unit/Application/Model/WebauthnErrorsTest.php +++ b/src/tests/unit/Application/Model/WebauthnErrorsTest.php @@ -17,13 +17,14 @@ namespace D3\Webauthn\tests\unit\Application\Model; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\WebauthnErrors; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Core\Language; use OxidEsales\Eshop\Core\Registry; use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class WebauthnErrorsTest extends UnitTestCase +class WebauthnErrorsTest extends WAUnitTestCase { use CanAccessRestricted; @@ -50,22 +51,12 @@ class WebauthnErrorsTest extends UnitTestCase } ) )->willReturn('translated'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Language::class, $languageMock); /** @var WebauthnErrors|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnErrors::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'getErrIdFromMessage']) + ->onlyMethods(['getErrIdFromMessage']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($languageMock) { - $args = func_get_args(); - switch ($args[0]) { - case Language::class: - return $languageMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('getErrIdFromMessage')->willReturn($errId); $this->assertSame( diff --git a/src/tests/unit/Application/Model/WebauthnLoginTest.php b/src/tests/unit/Application/Model/WebauthnLoginTest.php index 20b5c0d..eac63f5 100644 --- a/src/tests/unit/Application/Model/WebauthnLoginTest.php +++ b/src/tests/unit/Application/Model/WebauthnLoginTest.php @@ -22,13 +22,14 @@ use D3\Webauthn\Application\Model\Exceptions\WebauthnLoginErrorException; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; +use D3\Webauthn\tests\unit\WAUnitTestCase; +use Generator; use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Model\Basket; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Exception\CookieException; use OxidEsales\Eshop\Core\Exception\UserException; -use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session; use OxidEsales\Eshop\Core\SystemEventHandler; use OxidEsales\Eshop\Core\Utils; @@ -40,7 +41,7 @@ use Psr\Log\LoggerInterface; use ReflectionException; use TypeError; -class WebauthnLoginTest extends UnitTestCase +class WebauthnLoginTest extends WAUnitTestCase { use CanAccessRestricted; @@ -157,15 +158,13 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function credentialErrorDataProvider(): array + public function credentialErrorDataProvider(): Generator { - return [ - 'empty credential' => ['', WebauthnGetException::class, WebauthnGetException::class], - 'spaced credential' => [' ', WebauthnGetException::class, WebauthnGetException::class], - 'null credential' => [null, TypeError::class, WebauthnGetException::class], - ]; + yield 'empty credential' => ['', WebauthnGetException::class, WebauthnGetException::class]; + yield 'spaced credential' => [' ', WebauthnGetException::class, WebauthnGetException::class]; + yield 'null credential' => [null, TypeError::class, WebauthnGetException::class]; } /** @@ -216,7 +215,7 @@ class WebauthnLoginTest extends UnitTestCase $sut = $this->getMockBuilder(WebauthnLogin::class) ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', - 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId', ]) + 'getCredential', 'regenerateSessionId', ]) ->disableOriginalConstructor() ->getMock(); $sut->expects($this->exactly((int) $setCookie))->method('setSessionCookie'); @@ -232,14 +231,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function frontendLoginSuccessDataProvider(): array + public function frontendLoginSuccessDataProvider(): Generator { - return [ - 'setCookie' => [true], - 'dontSetCookie' => [false], - ]; + yield 'setCookie' => [true]; + yield 'dontSetCookie' => [false]; } /** @@ -259,17 +256,20 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) ->onlyMethods(['logout']) ->getMock(); $userMock->expects($this->once())->method('logout'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($writeLog)->method('error')->willReturn(true); $loggerMock->expects($writeLog)->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var UserComponent|MockObject $userComponentMock */ $userComponentMock = $this->getMockBuilder(UserComponent::class) @@ -279,35 +279,12 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'd3GetMockableRegistryObject', + ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', 'setFrontendSession', 'handleBackendCookie', 'handleBackendSubshopRights', 'setSessionCookie', - 'd3GetMockableLogger', 'd3GetMockableOxNewObject', 'getCredential', 'regenerateSessionId', ]) + 'getCredential', 'regenerateSessionId', ]) ->disableOriginalConstructor() ->getMock(); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableLogger')->willReturn($loggerMock); $this->expectException(WebauthnLoginErrorException::class); @@ -321,16 +298,14 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function frontendLoginExceptionDataProvider(): array + public function frontendLoginExceptionDataProvider(): Generator { - return [ - 'userException' => [UserException::class, $this->any(), false], - 'cookieException' => [CookieException::class, $this->any(), true], - 'webauthnException' => [WebauthnException::class, $this->atLeastOnce(), false], - 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce(), true], - ]; + yield 'userException' => [UserException::class, $this->any(), false]; + yield 'cookieException' => [CookieException::class, $this->any(), true]; + yield 'webauthnException' => [WebauthnException::class, $this->atLeastOnce(), false]; + yield 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce(), true]; } /** @@ -345,25 +320,14 @@ class WebauthnLoginTest extends UnitTestCase $userMock = $this->getMockBuilder(User::class) ->disableOriginalConstructor() ->getMock(); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', - 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights', - 'd3GetMockableOxNewObject', ]) + 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->assertSame( 'admin_start', @@ -389,58 +353,35 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['onAdminLogin']) ->getMock(); $systemEventHandlerMock->expects($this->never())->method('onAdminLogin'); + d3GetOxidDIC()->set('d3ox.webauthn.'.SystemEventHandler::class, $systemEventHandlerMock); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) ->onlyMethods(['logout']) ->getMock(); $userMock->expects($this->once())->method('logout'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($writeLog)->method('error')->willReturn(true); $loggerMock->expects($writeLog)->method('debug')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->onlyMethods(['getUserId', 'handleErrorMessage', 'assertUser', 'assertAuthn', - 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights', - 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject', - 'd3GetMockableLogger', ]) + 'setAdminSession', 'handleBackendCookie', 'handleBackendSubshopRights']) ->disableOriginalConstructor() ->getMock(); $sut->method('handleErrorMessage')->willThrowException(oxNew($exceptionClass)); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $sut->method('d3GetMockableLogger')->willReturn($loggerMock); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock, $systemEventHandlerMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - case SystemEventHandler::class: - return $systemEventHandlerMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->assertSame( 'login', @@ -453,16 +394,14 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function adminLoginExceptionDataProvider(): array + public function adminLoginExceptionDataProvider(): Generator { - return [ - 'userException' => [UserException::class, $this->any()], - 'cookieException' => [CookieException::class, $this->any()], - 'webauthnException' => [WebauthnException::class, $this->atLeastOnce()], - 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce()], - ]; + yield 'userException' => [UserException::class, $this->any()]; + yield 'cookieException' => [CookieException::class, $this->any()]; + yield 'webauthnException' => [WebauthnException::class, $this->atLeastOnce()]; + yield 'webauthnGetException' => [WebauthnGetException::class, $this->atLeastOnce()]; } /** @@ -494,15 +433,13 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canHandleErrorMessageDataProvider(): array + public function canHandleErrorMessageDataProvider(): Generator { - return [ - 'has error message' => ['errorMessage', true], - 'empty error message' => ['', false], - 'error message null' => [null, false], - ]; + yield 'has error message' => ['errorMessage', true]; + yield 'empty error message' => ['', false]; + yield 'error message null' => [null, false]; } /** @@ -519,10 +456,11 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['assertAuthn']) ->getMock(); $webauthnMock->expects($doAssert)->method('assertAuthn'); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['getCredential', 'd3GetMockableOxNewObject']) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() ->getMock(); if ($throwException) { @@ -530,17 +468,6 @@ class WebauthnLoginTest extends UnitTestCase } else { $sut->method('getCredential')->willReturn('credential'); } - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); if ($throwException) { $this->expectException(WebauthnGetException::class); @@ -555,14 +482,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canAssertAuthDataProvider(): array + public function canAssertAuthDataProvider(): Generator { - return [ - 'has credential' => ['credentialFixture', $this->atLeastOnce(), false], - 'no credential' => [null, $this->never(), true], - ]; + yield 'has credential' => ['credentialFixture', $this->atLeastOnce(), false]; + yield 'no credential' => [null, $this->never(), true]; } /** @@ -586,23 +511,13 @@ class WebauthnLoginTest extends UnitTestCase $this->identicalTo('userId') ) ); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetMockableRegistryObject']) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->assertSame( $sessionMock, @@ -634,6 +549,7 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['setUserCookie']) ->getMock(); $utilsServerMock->expects($this->exactly((int) $setCookie))->method('setUserCookie'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock); /** @var Config|MockObject $configMock */ $configMock = $this->getMockBuilder(Config::class) @@ -641,25 +557,13 @@ class WebauthnLoginTest extends UnitTestCase ->getMock(); $configMock->method('getConfigParam')->with('blShowRememberMe')->willReturn($setCookie); $configMock->method('getShopId')->willReturn(1); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsServerMock, $configMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsServer::class: - return $utilsServerMock; - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -669,14 +573,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canSetSessionCookieDataProvider(): array + public function canSetSessionCookieDataProvider(): Generator { - return [ - 'set cookie' => [true], - 'dont set cookie' => [false], - ]; + yield 'set cookie' => [true]; + yield 'dont set cookie' => [false]; } /** @@ -698,23 +600,13 @@ class WebauthnLoginTest extends UnitTestCase ->getMock(); $userMock->method('isLoaded')->willReturn($userLoaded); $userMock->method('getFieldData')->with('oxrights')->willReturn($rights); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetMockableOxNewObject']) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); if ($throwsException) { $this->expectException(UserException::class); @@ -731,20 +623,18 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function canAssertUserDataProvider(): array + public function canAssertUserDataProvider(): Generator { - return [ - 'frontend, user not loaded' => [false, false, 'malladmin', true], - 'backend, user not loaded' => [true, false, 'malladmin', true], - 'frontend, frontend user loaded' => [false, true, 'user', false], - 'backend, frontend user loaded' => [true, true, 'user', true], - 'frontend, backend user loaded' => [false, true, 'malladmin', false], - 'backend, backend user loaded' => [true, true, 'malladmin', false], - 'frontend, backend 2 user loaded' => [false, true, '2', false], - 'backend, backend 2 user loaded' => [true, true, '2', false], - ]; + yield 'frontend, user not loaded' => [false, false, 'malladmin', true]; + yield 'backend, user not loaded' => [true, false, 'malladmin', true]; + yield 'frontend, frontend user loaded' => [false, true, 'user', false]; + yield 'backend, frontend user loaded' => [true, true, 'user', true]; + yield 'frontend, backend user loaded' => [false, true, 'malladmin', false]; + yield 'backend, backend user loaded' => [true, true, 'malladmin', false]; + yield 'frontend, backend 2 user loaded' => [false, true, '2', false]; + yield 'backend, backend 2 user loaded' => [true, true, '2', false]; } /** @@ -763,23 +653,13 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['getOxCookie']) ->getMock(); $utilsServerMock->method('getOxCookie')->willReturn($cookie); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsServer::class, $utilsServerMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetMockableRegistryObject']) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsServerMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsServer::class: - return $utilsServerMock; - default: - return Registry::get($args[0]); - } - } - ); if ($throwException) { $this->expectException(CookieException::class); @@ -794,14 +674,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function canHandleBackendCookieDataProvider(): array + public function canHandleBackendCookieDataProvider(): Generator { - return [ - 'has cookie' => ['cookiecontent', false], - 'has no cookie' => [null, true], - ]; + yield 'has cookie' => ['cookiecontent', false]; + yield 'has no cookie' => [null, true]; } /** @@ -820,6 +698,7 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['setShopId']) ->getMock(); $configMock->expects($setVar)->method('setShopId'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -835,20 +714,9 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetMockableRegistryObject']) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -858,15 +726,13 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canHandleBackendSubshopRightsDataProvider(): array + public function canHandleBackendSubshopRightsDataProvider(): Generator { - return [ - 'malladmin' => ['malladmin', $this->never()], - '1' => ['1', $this->atLeastOnce()], - '2' => ['2', $this->atLeastOnce()], - ]; + yield 'malladmin' => ['malladmin', $this->never()]; + yield '1' => ['1', $this->atLeastOnce()]; + yield '2' => ['2', $this->atLeastOnce()]; } /** @@ -885,23 +751,13 @@ class WebauthnLoginTest extends UnitTestCase ->getMock(); $sessionMock->method('isSessionStarted')->willReturn($sessionStarted); $sessionMock->expects($this->exactly((int) $sessionStarted))->method('regenerateSessionId'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -910,14 +766,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canRegenerateSessionIdDataProvider(): array + public function canRegenerateSessionIdDataProvider(): Generator { - return [ - 'session started' => [true], - 'session not started' => [false], - ]; + yield 'session started' => [true]; + yield 'session not started' => [false]; } /** @@ -935,12 +789,14 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['redirect']) ->getMock(); $utilsMock->expects($this->exactly((int) $inBlockedGroup))->method('redirect'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var Config|MockObject $configMock */ $configMock = $this->getMockBuilder(Config::class) ->onlyMethods(['getShopHomeUrl']) ->getMock(); $configMock->method('getShopHomeUrl')->willReturn('homeurl'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -950,22 +806,9 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) - ->onlyMethods(['d3GetMockableRegistryObject']) + ->onlyMethods(['getCredential']) ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock, $configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -975,14 +818,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canHandleBlockedUserDataProvider(): array + public function canHandleBlockedUserDataProvider(): Generator { - return [ - 'is in blocked group' => [true], - 'is not in blocked group' => [false], - ]; + yield 'is in blocked group' => [true]; + yield 'is not in blocked group' => [false]; } /** @@ -1004,23 +845,13 @@ class WebauthnLoginTest extends UnitTestCase ->onlyMethods(['getBasket']) ->getMock(); $sessionMock->method('getBasket')->willReturn($basketMock); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject']) + ->onlyMethods(['getCredential']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -1073,23 +904,13 @@ class WebauthnLoginTest extends UnitTestCase return null; } ); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableRegistryObject', 'isAdmin']) + ->onlyMethods(['isAdmin']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('isAdmin')->willReturn($isAdmin); $this->assertSame( @@ -1102,14 +923,12 @@ class WebauthnLoginTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canGetUserIdDataProvider(): array + public function canGetUserIdDataProvider(): Generator { - return [ - 'admin' => [true, 'adminUser'], - 'frontend' => [false, 'frontendUser'], - ]; + yield 'admin' => [true, 'adminUser']; + yield 'frontend' => [false, 'frontendUser']; } /** @@ -1134,6 +953,7 @@ class WebauthnLoginTest extends UnitTestCase $this->identicalTo('idFixture'), ] ); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -1144,20 +964,9 @@ class WebauthnLoginTest extends UnitTestCase /** @var WebauthnLogin|MockObject $sut */ $sut = $this->getMockBuilder(WebauthnLogin::class) ->disableOriginalConstructor() - ->onlyMethods(['getCredential', 'd3GetMockableRegistryObject']) + ->onlyMethods(['getCredential']) ->getMock(); $sut->method('getCredential')->willReturn('credentialFixture'); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, diff --git a/src/tests/unit/Application/Model/WebauthnTest.php b/src/tests/unit/Application/Model/WebauthnTest.php index 5a12c4c..247e565 100644 --- a/src/tests/unit/Application/Model/WebauthnTest.php +++ b/src/tests/unit/Application/Model/WebauthnTest.php @@ -24,7 +24,9 @@ use D3\Webauthn\Application\Model\RelyingPartyEntity; use D3\Webauthn\Application\Model\UserEntity; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; +use D3\Webauthn\tests\unit\WAUnitTestCase; use Exception; +use Generator; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; use OxidEsales\Eshop\Core\Registry; @@ -40,7 +42,7 @@ use Webauthn\PublicKeyCredentialRequestOptions; use Webauthn\PublicKeyCredentialSource; use Webauthn\Server; -class WebauthnTest extends UnitTestCase +class WebauthnTest extends WAUnitTestCase { use CanAccessRestricted; @@ -68,22 +70,10 @@ class WebauthnTest extends UnitTestCase ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($this->exactly((int) !$expected))->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); - /** @var Webauthn|MockObject $sut */ - $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var Webauthn $sut */ + $sut = oxNew(Webauthn::class); $this->assertSame( $expected, @@ -95,20 +85,18 @@ class WebauthnTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function canCheckIsAvailableDataProvider(): array + public function canCheckIsAvailableDataProvider(): Generator { - return [ - 'https' => ['on', null, null, null, true], - 'HTTP_X_FORWARDED_PROTO' => [null, 'https', null, null, true], - 'HTTP_X_FORWARDED_SSL' => [null, null, 'on', null, true], - 'REMOTE_ADDR v4' => [null, null, null, '127.0.0.1', true], - 'REMOTE_ADDR v6' => [null, null, null, '::1', true], - 'REMOTE_ADDR localhost' => [null, null, null, 'some.localhost', true], - 'unset' => [null, null, null, null, false], - 'not valid' => ['off', 'http', 'off', '160.158.23.7', false], - ]; + yield 'https' => ['on', null, null, null, true]; + yield 'HTTP_X_FORWARDED_PROTO' => [null, 'https', null, null, true]; + yield 'HTTP_X_FORWARDED_SSL' => [null, null, 'on', null, true]; + yield 'REMOTE_ADDR v4' => [null, null, null, '127.0.0.1', true]; + yield 'REMOTE_ADDR v6' => [null, null, null, '::1', true]; + yield 'REMOTE_ADDR localhost' => [null, null, null, 'some.localhost', true]; + yield 'unset' => [null, null, null, null, false]; + yield 'not valid' => ['off', 'http', 'off', '160.158.23.7', false]; } /** @@ -150,11 +138,13 @@ class WebauthnTest extends UnitTestCase $this->identicalTo(Webauthn::SESSION_CREATIONS_OPTIONS), $this->identicalTo($pubKeyCredCreationOptionsMock) ); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var UserEntity|MockObject $userEntityMock */ $userEntityMock = $this->getMockBuilder(UserEntity::class) ->disableOriginalConstructor() ->getMock(); + d3GetOxidDIC()->set(UserEntity::class, $userEntityMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -163,33 +153,9 @@ class WebauthnTest extends UnitTestCase /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'getServer', 'd3GetMockableRegistryObject', 'jsonEncode', - 'getExistingCredentials', - ]) + ->onlyMethods(['getServer', 'jsonEncode', 'getExistingCredentials']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userEntityMock) { - $args = func_get_args(); - switch ($args[0]) { - case UserEntity::class: - return $userEntityMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $sut->method('getServer')->willReturn($serverMock); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->expects($this->once())->method('jsonEncode')->willReturn($jsonReturn); $sut->expects($this->once())->method('getExistingCredentials')->willReturn([ $pubKeyCredDescriptorMock, @@ -214,14 +180,12 @@ class WebauthnTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canGetOptionsDataProvider(): array + public function canGetOptionsDataProvider(): Generator { - return [ - 'json encoded' => ['jsonstring'], - 'json failed' => [false], - ]; + yield 'json encoded' => ['jsonstring']; + yield 'json failed' => [false]; } /** @@ -251,27 +215,15 @@ class WebauthnTest extends UnitTestCase $pubKeyCredListMock->method('findAllForUserEntity')->willReturn( [$pubKeyCredSourceMock] ); + d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pubKeyCredListMock); /** @var UserEntity|MockObject $userEntityMock */ $userEntityMock = $this->getMockBuilder(UserEntity::class) ->disableOriginalConstructor() ->getMock(); - /** @var Webauthn|MockObject $sut */ - $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($pubKeyCredListMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredentialList::class: - return $pubKeyCredListMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var Webauthn $sut */ + $sut = oxNew(Webauthn::class); $return = $this->callMethod( $sut, @@ -345,6 +297,7 @@ class WebauthnTest extends UnitTestCase $this->identicalTo(Webauthn::SESSION_ASSERTION_OPTIONS), $this->identicalTo($pubKeyCredRequestOptionsMock) ); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -352,43 +305,21 @@ class WebauthnTest extends UnitTestCase ->onlyMethods(['load']) ->getMock(); $userMock->method('load')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var UserEntity|MockObject $userEntityMock */ $userEntityMock = $this->getMockBuilder(UserEntity::class) ->disableOriginalConstructor() ->getMock(); + d3GetOxidDIC()->set(UserEntity::class, $userEntityMock); /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'getServer', 'd3GetMockableRegistryObject', 'jsonEncode', + ->onlyMethods(['getServer', 'jsonEncode', 'getExistingCredentials', ]) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userEntityMock, $userMock) { - $args = func_get_args(); - switch ($args[0]) { - case UserEntity::class: - return $userEntityMock; - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $sut->method('getServer')->willReturn($serverMock); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->expects($this->once())->method('jsonEncode')->willReturn($jsonReturn); $sut->expects($this->once())->method('getExistingCredentials')->willReturn([ $pubKeyCredDescriptorMock, @@ -420,11 +351,6 @@ class WebauthnTest extends UnitTestCase */ public function canGetServer() { - /** @var PublicKeyCredentialList|MockObject $pubKeyCredListMock */ - $pubKeyCredListMock = $this->getMockBuilder(PublicKeyCredentialList::class) - ->disableOriginalConstructor() - ->getMock(); - /** @var Server|MockObject $serverMock */ $serverMock = $this->getMockBuilder(Server::class) ->disableOriginalConstructor() @@ -432,30 +358,11 @@ class WebauthnTest extends UnitTestCase ->getMock(); $serverMock->expects($this->atLeastOnce())->method('setLogger'); - /** @var RelyingPartyEntity|MockObject $rpEntityMock */ - $rpEntityMock = $this->getMockBuilder(RelyingPartyEntity::class) - ->disableOriginalConstructor() - ->getMock(); - /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject']) + ->onlyMethods(['getServerObject']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($rpEntityMock, $serverMock, $pubKeyCredListMock) { - $args = func_get_args(); - switch ($args[0]) { - case RelyingPartyEntity::class: - return $rpEntityMock; - case Server::class: - return $serverMock; - case PublicKeyCredentialList::class: - return $pubKeyCredListMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + $sut->method('getServerObject')->willReturn($serverMock); $this->assertSame( $serverMock, @@ -486,12 +393,14 @@ class WebauthnTest extends UnitTestCase ->onlyMethods(['saveCredentialSource']) ->getMock(); $pubKeyCredMock->expects($this->exactly((int) !$throwsException))->method('saveCredentialSource'); + d3GetOxidDIC()->set(PublicKeyCredential::class, $pubKeyCredMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) ->onlyMethods(['getVariable']) ->getMock(); $sessionMock->method('getVariable')->willReturn($pubKeyCredCreationsOptionsMock); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Server|MockObject $serverMock */ $serverMock = $this->getMockBuilder(Server::class) @@ -507,30 +416,8 @@ class WebauthnTest extends UnitTestCase /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'd3GetMockableOxNewObject', 'getServer']) + ->onlyMethods(['getServer']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($pubKeyCredMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredential::class: - return $pubKeyCredMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $sut->method('getServer')->willReturn($serverMock); if ($throwsException) { @@ -545,14 +432,12 @@ class WebauthnTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function loadAndCheckAssertionResponseDataProvider(): array + public function loadAndCheckAssertionResponseDataProvider(): Generator { - return [ - 'check failed' => [true], - 'check passed' => [false], - ]; + yield 'check failed' => [true]; + yield 'check passed' => [false]; } @@ -576,6 +461,7 @@ class WebauthnTest extends UnitTestCase ->onlyMethods(['getVariable']) ->getMock(); $sessionMock->method('getVariable')->willReturn($pubKeyCredRequestOptionsMock); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Server|MockObject $serverMock */ $serverMock = $this->getMockBuilder(Server::class) @@ -596,21 +482,10 @@ class WebauthnTest extends UnitTestCase /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['getUserEntityFrom', 'getServer', 'd3GetMockableRegistryObject', 'getSavedUserIdFromSession']) + ->onlyMethods(['getUserEntityFrom', 'getServer', 'getSavedUserIdFromSession']) ->getMock(); $sut->method('getUserEntityFrom')->willReturn($userEntity); $sut->method('getServer')->willReturn($serverMock); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('getSavedUserIdFromSession')->willReturn('userId'); if ($throwsException) { @@ -640,30 +515,18 @@ class WebauthnTest extends UnitTestCase $userEntityMock = $this->getMockBuilder(UserEntity::class) ->disableOriginalConstructor() ->getMock(); + d3GetOxidDIC()->set(UserEntity::class, $userEntityMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) ->onlyMethods(['load']) ->getMock(); $userMock->method('load'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableOxNewObject']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock, $userEntityMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - case UserEntity::class: - return $userEntityMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->assertSame( $userEntityMock, @@ -698,22 +561,12 @@ class WebauthnTest extends UnitTestCase [WebauthnConf::WEBAUTHN_SESSION_CURRENTUSER, $frontendUser], ] ); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'isAdmin']) + ->onlyMethods(['isAdmin']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('isAdmin')->willReturn($isAdmin); $this->assertSame( @@ -726,14 +579,12 @@ class WebauthnTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function canGetSavedUserIdFromSessionDataProvider(): array + public function canGetSavedUserIdFromSessionDataProvider(): Generator { - return [ - 'admin' => [true, 'admUsr', 'frontendUsr', 'admUsr'], - 'frontend' => [false, 'admUsr', 'frontendUsr', 'frontendUsr'], - ]; + yield 'admin' => [true, 'admUsr', 'frontendUsr', 'admUsr']; + yield 'frontend' => [false, 'admUsr', 'frontendUsr', 'frontendUsr']; } /** @@ -773,30 +624,19 @@ class WebauthnTest extends UnitTestCase ->onlyMethods(['getVariable']) ->getMock(); $sessionMock->method('getVariable')->willReturn($sessionGlobalSwitch); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Config|MockObject $configMock */ $configMock = $this->getMockBuilder(Config::class) ->onlyMethods(['getConfigParam']) ->getMock(); $configMock->method('getConfigParam')->willReturn($configGlobalSwitch); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'UserUseWebauthn']) + ->onlyMethods(['UserUseWebauthn']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('UserUseWebauthn')->willReturn($userUseWebauthn); $this->assertSame( @@ -810,17 +650,15 @@ class WebauthnTest extends UnitTestCase } /** - * @return array + * @return Generator */ - public function canCheckIsActiveDataProvider(): array + public function canCheckIsActiveDataProvider(): Generator { - return [ - 'user use webauthn' => [false, false, true, true], - 'user use webauthn, config disabled' => [true, false, true, false], - 'user use webauthn, session disabled' => [false, true, true, false], - 'user use webauthn, both disabled' => [true, true, true, false], - 'user dont use ' => [false, false, false, false], - ]; + yield 'user use webauthn' => [false, false, true, true]; + yield 'user use webauthn, config disabled' => [true, false, true, false]; + yield 'user use webauthn, session disabled' => [false, true, true, false]; + yield 'user use webauthn, both disabled' => [true, true, true, false]; + yield 'user dont use ' => [false, false, false, false]; } /** @@ -839,6 +677,7 @@ class WebauthnTest extends UnitTestCase ->addMethods(['findAllForUserEntity']) ->getMock(); $pubKeyCredListMock->method('findAllForUserEntity')->willReturn($credList); + d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pubKeyCredListMock); /** @var UserEntity|MockObject $userEntityMock */ $userEntityMock = $this->getMockBuilder(UserEntity::class) @@ -847,20 +686,9 @@ class WebauthnTest extends UnitTestCase /** @var Webauthn|MockObject $sut */ $sut = $this->getMockBuilder(Webauthn::class) - ->onlyMethods(['getUserEntityFrom', 'd3GetMockableOxNewObject']) + ->onlyMethods(['getUserEntityFrom']) ->getMock(); $sut->method('getUserEntityFrom')->willReturn($userEntityMock); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($pubKeyCredListMock) { - $args = func_get_args(); - switch ($args[0]) { - case PublicKeyCredentialList::class: - return $pubKeyCredListMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); $this->assertSame( $expected, @@ -873,14 +701,43 @@ class WebauthnTest extends UnitTestCase } /** - * @return array[] + * @return Generator */ - public function canCheckUserUseWebauthnDataProvider(): array + public function canCheckUserUseWebauthnDataProvider(): Generator { - return [ - 'no array' => [null, false], - 'no count' => [[], false], - 'filled array' => [['abc'], true], - ]; + yield 'no array' => [null, false]; + yield 'no count' => [[], false]; + yield 'filled array' => [['abc'], true]; + } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Webauthn\Application\Model\Webauthn::getServerObject + */ + public function canGetServerObject() + { + /** @var PublicKeyCredentialList|MockObject $pubKeyCredListMock */ + $pubKeyCredListMock = $this->getMockBuilder(PublicKeyCredentialList::class) + ->disableOriginalConstructor() + ->getMock(); + d3GetOxidDIC()->set(PublicKeyCredentialList::class, $pubKeyCredListMock); + + /** @var RelyingPartyEntity|MockObject $rpEntityMock */ + $rpEntityMock = $this->getMockBuilder(RelyingPartyEntity::class) + ->disableOriginalConstructor() + ->getMock(); + d3GetOxidDIC()->set(RelyingPartyEntity::class, $rpEntityMock); + + /** @var Webauthn $sut */ + $sut = oxNew(Webauthn::class); + + $this->assertInstanceOf( + Server::class, + $this->callMethod( + $sut, + 'getServerObject' + ) + ); } } diff --git a/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php b/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php index c34be92..e92720f 100644 --- a/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php +++ b/src/tests/unit/Modules/Application/Component/UserComponentWebauthnTest.php @@ -23,6 +23,7 @@ use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Application\Model\WebauthnLogin; use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent; use D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent_parent; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Component\UserComponent; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Controller\BaseController; @@ -35,7 +36,7 @@ use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class UserComponentWebauthnTest extends UnitTestCase +class UserComponentWebauthnTest extends WAUnitTestCase { use CanAccessRestricted; @@ -80,6 +81,7 @@ class UserComponentWebauthnTest extends UnitTestCase ->onlyMethods(['redirect']) ->getMock(); $utilsMock->expects($this->exactly((int) $doRedirect))->method('redirect'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var BaseController|MockObject $baseControllerMock */ $baseControllerMock = $this->getMockBuilder(BaseController::class) @@ -94,12 +96,14 @@ class UserComponentWebauthnTest extends UnitTestCase $requestMock->method('getRequestParameter')->willReturnMap([ ['lgn_usr', 'myUserName'], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) ->onlyMethods(['d3GetLoginUserId']) ->getMock(); $userMock->method('d3GetLoginUserId')->willReturn('myUserId'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -108,42 +112,15 @@ class UserComponentWebauthnTest extends UnitTestCase $sessionMock->expects($this->exactly($setVariableCount))->method('setVariable'); $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER) ->willReturn('myUserName'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var d3_webauthn_UserComponent|MockObject $sut */ $sut = $this->getMockBuilder(UserComponent::class) - ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'd3HasWebauthnButNotLoggedin', - 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject', 'getParent', - ]) + ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'd3HasWebauthnButNotLoggedin', 'getParent']) ->getMock(); $sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn); $sut->method('d3CallMockableFunction')->willReturn('parentReturn'); $sut->method('d3HasWebauthnButNotLoggedin')->willReturn($loggedin); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock, $requestMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - case Request::class: - return $requestMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('getParent')->willReturn($baseControllerMock); $this->callMethod( @@ -184,30 +161,18 @@ class UserComponentWebauthnTest extends UnitTestCase ->getMock(); $sessionMock->method('hasVariable')->with(WebauthnConf::WEBAUTHN_SESSION_AUTH) ->willReturn($hasWebauthnLogin); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Request|MockObject $requestMock */ $requestMock = $this->getMockBuilder(Request::class) ->onlyMethods(['getRequestParameter']) ->getMock(); $requestMock->method('getRequestParameter')->with('lgn_pwd')->willReturn($usedPassword); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var d3_webauthn_UserComponent|MockObject $sut */ $sut = $this->getMockBuilder(UserComponent::class) - ->onlyMethods(['d3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($requestMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Request::class: - return $requestMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->assertSame( $expected, @@ -251,39 +216,18 @@ class UserComponentWebauthnTest extends UnitTestCase ->getMock(); $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_SESSION_AUTH) ->willReturn($hasAuth); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Webauthn|MockObject $webauthnMock */ $webauthnMock = $this->getMockBuilder(Webauthn::class) ->onlyMethods(['isActive']) ->getMock(); $webauthnMock->method('isActive')->willReturn($webauthnActive); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); /** @var UserComponent|MockObject $sut */ $sut = $this->getMockBuilder(UserComponent::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->assertSame( $expected, @@ -340,22 +284,10 @@ class UserComponentWebauthnTest extends UnitTestCase ->onlyMethods(['deleteVariable']) ->getMock(); $sessionMock->expects($this->atLeast(4))->method('deleteVariable')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); - /** @var UserComponent|MockObject $sut */ - $sut = $this->getMockBuilder(UserComponent::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var UserComponent $sut */ + $sut = oxNew(UserComponent::class); $this->callMethod( $sut, @@ -377,6 +309,7 @@ class UserComponentWebauthnTest extends UnitTestCase ->onlyMethods(['addErrorToDisplay']) ->getMock(); $utilsViewMock->expects($addErrorInvocationCount)->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var WebauthnLogin|MockObject $webauthnLoginMock */ $webauthnLoginMock = $this->getMockBuilder(WebauthnLogin::class) @@ -393,31 +326,10 @@ class UserComponentWebauthnTest extends UnitTestCase /** @var UserComponent|MockObject $sut */ $sut = $this->getMockBuilder(UserComponent::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject', '_afterLogin']) + ->onlyMethods(['_afterLogin', 'd3GetWebauthnLogin']) ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnLoginMock) { - $args = func_get_args(); - switch ($args[0]) { - case WebauthnLogin::class: - return $webauthnLoginMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->expects($afterLoginInvocationCount)->method('_afterLogin'); + $sut->method('d3GetWebauthnLogin')->willReturn($webauthnLoginMock); $this->callMethod( $sut, @@ -436,4 +348,30 @@ class UserComponentWebauthnTest extends UnitTestCase 'webauthnLoginError' => [WebauthnLoginErrorException::class, $this->never(), $this->never()], ]; } + + /** + * @test + * @throws ReflectionException + * @covers \D3\Webauthn\Modules\Application\Component\d3_webauthn_UserComponent::d3GetWebauthnLogin + */ + public function canGetWebAuthnLogin() + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder(Request::class) + ->onlyMethods(['getRequestEscapedParameter']) + ->getMock(); + $requestMock->method('getRequestEscapedParameter')->willReturn('requestReturn'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); + + /** @var d3webauthnadminlogin|MockObject $sut */ + $sut = oxNew(UserComponent::class); + + $this->assertInstanceOf( + WebauthnLogin::class, + $this->callMethod( + $sut, + 'd3GetWebauthnLogin' + ) + ); + } } diff --git a/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php index c67e011..af0f40c 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountControllerTest extends UnitTestCase +class AccountControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php index 98fa661..0482f1f 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountDownloadsControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountDownloadsController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountDownloadsControllerTest extends UnitTestCase +class AccountDownloadsControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php index e31ba88..2abcd78 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountNewsletterControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountNewsletterController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountNewsletterControllerTest extends UnitTestCase +class AccountNewsletterControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php index 29c818c..001c92b 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountNoticeListControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountNoticeListController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountNoticeListControllerTest extends UnitTestCase +class AccountNoticeListControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php index aead4e1..2e03311 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountOrderControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountOrderController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountOrderControllerTest extends UnitTestCase +class AccountOrderControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php index 8080a57..a5c423e 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountPasswordControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountPasswordController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountPasswordControllerTest extends UnitTestCase +class AccountPasswordControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php index d02b832..e3bb8e2 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountRecommlistControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountRecommlistController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountRecommlistControllerTest extends UnitTestCase +class AccountRecommlistControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php index 539c2da..b9a4ea9 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountReviewControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountReviewController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountReviewControllerTest extends UnitTestCase +class AccountReviewControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php index 72bc555..3a958f8 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountUserControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountUserController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountUserControllerTest extends UnitTestCase +class AccountUserControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php b/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php index b85acea..c7e9af2 100644 --- a/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/AccountWishlistControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\AccountWishlistController; -use OxidEsales\TestingLibrary\UnitTestCase; -class AccountWishlistControllerTest extends UnitTestCase +class AccountWishlistControllerTest extends WAUnitTestCase { use AccountTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php b/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php index 6d5e785..fc9d1fc 100644 --- a/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php +++ b/src/tests/unit/Modules/Application/Controller/Admin/LoginControllerWebauthnTest.php @@ -18,6 +18,7 @@ namespace D3\Webauthn\tests\unit\Modules\Application\Controller\Admin; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\Webauthn; use D3\Webauthn\Application\Model\WebauthnConf; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\Admin\LoginController; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; @@ -27,7 +28,7 @@ use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class LoginControllerWebauthnTest extends UnitTestCase +class LoginControllerWebauthnTest extends WAUnitTestCase { use CanAccessRestricted; @@ -43,22 +44,10 @@ class LoginControllerWebauthnTest extends UnitTestCase ->onlyMethods(['logout']) ->getMock(); $userMock->expects($this->atLeastOnce())->method('logout'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); - /** @var LoginController|MockObject $sut */ - $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var LoginController $sut */ + $sut = oxNew(LoginController::class); $this->callMethod( $sut, @@ -86,30 +75,17 @@ class LoginControllerWebauthnTest extends UnitTestCase ->getMock(); $sessionMock->method('hasVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) ->willReturn($hasWebauthnLogin); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Request|MockObject $requestMock */ $requestMock = $this->getMockBuilder(Request::class) ->onlyMethods(['getRequestParameter']) ->getMock(); $requestMock->method('getRequestParameter')->with('pwd')->willReturn($usedPassword); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); - /** @var LoginController|MockObject $sut */ - $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($requestMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Request::class: - return $requestMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var LoginController $sut */ + $sut = oxNew(LoginController::class); $this->assertSame( $expected, @@ -153,39 +129,17 @@ class LoginControllerWebauthnTest extends UnitTestCase ->getMock(); $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_AUTH) ->willReturn($hasAuth); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Webauthn|MockObject $webauthnMock */ $webauthnMock = $this->getMockBuilder(Webauthn::class) ->onlyMethods(['isActive']) ->getMock(); $webauthnMock->method('isActive')->willReturn($webauthnActive); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); - /** @var LoginController|MockObject $sut */ - $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var LoginController $sut */ + $sut = oxNew(LoginController::class); $this->assertSame( $expected, @@ -230,12 +184,14 @@ class LoginControllerWebauthnTest extends UnitTestCase ['user', 'myUserName'], ['profile', 'myProfile'], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Request::class, $requestMock); /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) ->onlyMethods(['d3GetLoginUserId']) ->getMock(); $userMock->method('d3GetLoginUserId')->willReturn('myUserId'); + d3GetOxidDIC()->set('d3ox.webauthn.'.User::class, $userMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -244,40 +200,15 @@ class LoginControllerWebauthnTest extends UnitTestCase $sessionMock->expects($this->exactly($setVariableCount))->method('setVariable'); $sessionMock->method('getVariable')->with(WebauthnConf::WEBAUTHN_ADMIN_SESSION_LOGINUSER) ->willReturn('myUserName'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var LoginController|MockObject $sut */ $sut = $this->getMockBuilder(LoginController::class) - ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'hasWebauthnButNotLoggedin', - 'd3GetMockableOxNewObject', 'd3GetMockableRegistryObject', - ]) + ->onlyMethods(['d3CanUseWebauthn', 'd3CallMockableFunction', 'hasWebauthnButNotLoggedin']) ->getMock(); $sut->method('d3CanUseWebauthn')->willReturn($canUseWebauthn); $sut->method('d3CallMockableFunction')->willReturn('parentReturn'); $sut->method('hasWebauthnButNotLoggedin')->willReturn($loggedin); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($userMock) { - $args = func_get_args(); - switch ($args[0]) { - case User::class: - return $userMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($requestMock, $sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Request::class: - return $requestMock; - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->assertSame( $expected, diff --git a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php index a2f1967..4c9d43b 100644 --- a/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php +++ b/src/tests/unit/Modules/Application/Controller/CheckoutTestTrait.php @@ -38,6 +38,8 @@ trait CheckoutTestTrait public function setUp(): void { + parent::setUp(); + $this->userFixture = oxNew(User::class); $this->userFixture->setId($this->userFixtureId); $this->userFixture->assign(['oxlname' => __METHOD__]); @@ -47,6 +49,8 @@ trait CheckoutTestTrait public function tearDown(): void { + parent::tearDown(); + $this->userFixture->delete($this->userFixtureId); } @@ -75,6 +79,7 @@ trait CheckoutTestTrait ->getMock(); $sessionMock->method('getVariable') ->with($this->identicalTo(WebauthnConf::WEBAUTHN_SESSION_AUTH))->willReturn($sessionAuth); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var Webauthn|MockObject $webauthnMock */ $webauthnMock = $this->getMockBuilder(Webauthn::class) @@ -82,33 +87,10 @@ trait CheckoutTestTrait ->getMock(); $webauthnMock->method('isAvailable')->willReturn($isAvailable); $webauthnMock->method('isActive')->willReturn($isActive); + d3GetOxidDIC()->set(Webauthn::class, $webauthnMock); - /** @var PaymentController|OrderController|UserController|MockObject $sut */ - $sut = $this->getMockBuilder($this->sutClass) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($webauthnMock) { - $args = func_get_args(); - switch ($args[0]) { - case Webauthn::class: - return $webauthnMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var PaymentController|OrderController|UserController $sut */ + $sut = oxNew($this->sutClass); if ($hasUser) { $sut->setUser($this->userFixture); } diff --git a/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php b/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php index ff7e98a..6ad869e 100644 --- a/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/OrderControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\OrderController; -use OxidEsales\TestingLibrary\UnitTestCase; -class OrderControllerTest extends UnitTestCase +class OrderControllerTest extends WAUnitTestCase { use CheckoutTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php b/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php index 20c1870..932bf38 100644 --- a/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/PaymentControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\PaymentController; -use OxidEsales\TestingLibrary\UnitTestCase; -class PaymentControllerTest extends UnitTestCase +class PaymentControllerTest extends WAUnitTestCase { use CheckoutTestTrait; diff --git a/src/tests/unit/Modules/Application/Controller/UserControllerTest.php b/src/tests/unit/Modules/Application/Controller/UserControllerTest.php index c7b3bb6..066d760 100644 --- a/src/tests/unit/Modules/Application/Controller/UserControllerTest.php +++ b/src/tests/unit/Modules/Application/Controller/UserControllerTest.php @@ -15,10 +15,10 @@ declare(strict_types=1); namespace D3\Webauthn\tests\unit\Modules\Application\Controller; +use D3\Webauthn\tests\unit\WAUnitTestCase; use OxidEsales\Eshop\Application\Controller\UserController; -use OxidEsales\TestingLibrary\UnitTestCase; -class UserControllerTest extends UnitTestCase +class UserControllerTest extends WAUnitTestCase { use CheckoutTestTrait; diff --git a/src/tests/unit/Modules/Application/Model/UserWebauthnTest.php b/src/tests/unit/Modules/Application/Model/UserWebauthnTest.php index 15da6a6..f966e33 100644 --- a/src/tests/unit/Modules/Application/Model/UserWebauthnTest.php +++ b/src/tests/unit/Modules/Application/Model/UserWebauthnTest.php @@ -19,6 +19,7 @@ use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Application\Model\WebauthnConf; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn; use D3\Webauthn\Modules\Application\Model\d3_User_Webauthn_parent; +use D3\Webauthn\tests\unit\WAUnitTestCase; use Exception; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Config; @@ -29,7 +30,7 @@ use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class UserWebauthnTest extends UnitTestCase +class UserWebauthnTest extends WAUnitTestCase { use CanAccessRestricted; @@ -96,22 +97,12 @@ class UserWebauthnTest extends UnitTestCase ->onlyMethods(['deleteVariable']) ->getMock(); $sessionMock->expects($this->atLeast(11))->method('deleteVariable')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var User|MockObject $sut */ $sut = $this->getMockBuilder(User::class) - ->onlyMethods(['d3GetMockableRegistryObject']) + ->disableOriginalConstructor() ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -157,6 +148,7 @@ class UserWebauthnTest extends UnitTestCase ->onlyMethods(['getShopId']) ->getMock(); $configMock->method('getShopId')->willReturn(1); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); /** @var Session|MockObject $sessionMock */ $sessionMock = $this->getMockBuilder(Session::class) @@ -166,24 +158,12 @@ class UserWebauthnTest extends UnitTestCase [WebauthnConf::WEBAUTHN_SESSION_AUTH, $authInSession], [WebauthnConf::WEBAUTHN_SESSION_LOGINUSER, $userNameInSession], ]); + d3GetOxidDIC()->set('d3ox.webauthn.'.Session::class, $sessionMock); /** @var User|MockObject $sut */ $sut = $this->getMockBuilder(User::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'load']) + ->onlyMethods(['load']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($sessionMock, $configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Session::class: - return $sessionMock; - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->expects($this->exactly((int) ($canLoad)))->method('load')->will( $userIsLoadable ? $this->returnValue(true) : @@ -235,22 +215,10 @@ class UserWebauthnTest extends UnitTestCase ->onlyMethods(['getShopId']) ->getMock(); $configMock->method('getShopId')->willReturn($shopId); + d3GetOxidDIC()->set('d3ox.webauthn.'.Config::class, $configMock); - /** @var User|MockObject $sut */ - $sut = $this->getMockBuilder(User::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($configMock) { - $args = func_get_args(); - switch ($args[0]) { - case Config::class: - return $configMock; - default: - return Registry::get($args[0]); - } - } - ); + /** @var User $sut */ + $sut = oxNew(User::class); $this->assertSame( $expected, diff --git a/src/tests/unit/Modules/WebauthnServicesTest.php b/src/tests/unit/Modules/WebauthnServicesTest.php new file mode 100644 index 0000000..6ab943d --- /dev/null +++ b/src/tests/unit/Modules/WebauthnServicesTest.php @@ -0,0 +1,51 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit\Modules; + +use D3\TestingTools\Development\CanAccessRestricted; +use D3\Webauthn\Modules\WebauthnServices; +use D3\Webauthn\tests\unit\WAUnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class WebauthnServicesTest extends WAUnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Webauthn\Modules\WebauthnServices::__construct + */ + public function canConstruct() + { + /** @var WebauthnServices|MockObject $sut */ + $sut = $this->getMockBuilder(WebauthnServices::class) + ->disableOriginalConstructor() + ->onlyMethods(['addYamlDefinitions', 'd3CallMockableFunction']) + ->getMock(); + $sut->expects($this->atLeastOnce())->method('addYamlDefinitions')->with( + $this->identicalTo('d3/oxwebauthn/Config/services.yaml') + ); + $sut->method('d3CallMockableFunction')->willReturn(true); + + $this->callMethod( + $sut, + '__construct' + ); + } +} diff --git a/src/tests/unit/Setup/ActionsTest.php b/src/tests/unit/Setup/ActionsTest.php index d221482..5779716 100644 --- a/src/tests/unit/Setup/ActionsTest.php +++ b/src/tests/unit/Setup/ActionsTest.php @@ -17,6 +17,7 @@ namespace D3\Webauthn\tests\unit\Setup; use D3\TestingTools\Development\CanAccessRestricted; use D3\Webauthn\Setup\Actions; +use D3\Webauthn\tests\unit\WAUnitTestCase; use Exception; use OxidEsales\Eshop\Application\Controller\FrontendController; use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface; @@ -32,25 +33,16 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\Sho use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ModuleConfiguration; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\DataObject\ShopConfiguration; use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Exception\ModuleConfigurationNotFoundException; -use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use ReflectionException; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerBuilder; -class ActionsTest extends UnitTestCase +class ActionsTest extends WAUnitTestCase { use CanAccessRestricted; - public function setUp(): void - { - parent::setUp(); - - //$seoEncoder = oxNew(SeoEncoder::class); - //$seoEncoder->addSeoEntry(); - } - /** * @test * @param $tableExist @@ -101,22 +93,10 @@ class ActionsTest extends UnitTestCase ->onlyMethods(['tableExists']) ->getMock(); $DbMetaDataMock->expects($this->once())->method('tableExists')->willReturn($expected); + d3GetOxidDIC()->set('d3ox.webauthn.'.DbMetaDataHandler::class, $DbMetaDataMock); - /** @var Actions|MockObject $sut */ - $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($DbMetaDataMock) { - $args = func_get_args(); - switch ($args[0]) { - case DbMetaDataHandler::class: - return $DbMetaDataMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var Actions $sut */ + $sut = oxNew(Actions::class); $this->assertSame( $expected, @@ -188,22 +168,10 @@ class ActionsTest extends UnitTestCase ->onlyMethods(['fieldExists']) ->getMock(); $DbMetaDataMock->expects($this->once())->method('fieldExists')->willReturn($expected); + d3GetOxidDIC()->set('d3ox.webauthn.'.DbMetaDataHandler::class, $DbMetaDataMock); - /** @var Actions|MockObject $sut */ - $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($DbMetaDataMock) { - $args = func_get_args(); - switch ($args[0]) { - case DbMetaDataHandler::class: - return $DbMetaDataMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var Actions $sut */ + $sut = oxNew(Actions::class); $this->assertSame( $expected, @@ -228,22 +196,10 @@ class ActionsTest extends UnitTestCase ->onlyMethods(['updateViews']) ->getMock(); $DbMetaDataMock->expects($this->once())->method('updateViews'); + d3GetOxidDIC()->set('d3ox.webauthn.'.DbMetaDataHandler::class, $DbMetaDataMock); - /** @var Actions|MockObject $sut */ - $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($DbMetaDataMock) { - $args = func_get_args(); - switch ($args[0]) { - case DbMetaDataHandler::class: - return $DbMetaDataMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var Actions $sut */ + $sut = oxNew(Actions::class); $this->callMethod( $sut, @@ -263,6 +219,7 @@ class ActionsTest extends UnitTestCase $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($throwException ? $this->atLeastOnce() : $this->never()) ->method('error')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class) @@ -270,6 +227,7 @@ class ActionsTest extends UnitTestCase ->getMock(); $utilsViewMock->expects($throwException ? $this->atLeastOnce() : $this->never()) ->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var Utils|MockObject $utilsMock */ $utilsMock = $this->getMockBuilder(Utils::class) @@ -279,30 +237,17 @@ class ActionsTest extends UnitTestCase ->method('resetTemplateCache'); $utilsMock->expects($throwException ? $this->never() : $this->once()) ->method('resetLanguageCache'); + d3GetOxidDIC()->set('d3ox.webauthn.'.Utils::class, $utilsMock); /** @var Actions|MockObject $sut */ $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'getModuleTemplates', 'd3GetMockableLogger']) + ->onlyMethods(['getModuleTemplates']) ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsMock, $utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case Utils::class: - return $utilsMock; - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); $sut->method('getModuleTemplates')->will( $throwException ? $this->throwException(oxNew(ModuleConfigurationNotFoundException::class)) : $this->returnValue([]) ); - $sut->method('d3GetMockableLogger')->willReturn($loggerMock); $this->callMethod( $sut, @@ -462,15 +407,17 @@ class ActionsTest extends UnitTestCase ->getMock(); $utilsViewMock->expects($throwException ? $this->atLeastOnce() : $this->never()) ->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.webauthn.'.UtilsView::class, $utilsViewMock); /** @var LoggerInterface|MockObject $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class, [], '', true, true, true, ['error', 'debug']); $loggerMock->expects($throwException ? $this->atLeastOnce() : $this->never()) ->method('error')->willReturn(true); + d3GetOxidDIC()->set('d3ox.webauthn.'.LoggerInterface::class, $loggerMock); /** @var Actions|MockObject $sut */ $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['hasSeoUrl', 'createSeoUrl', 'd3GetMockableLogger', 'd3GetMockableRegistryObject']) + ->onlyMethods(['hasSeoUrl', 'createSeoUrl']) ->getMock(); $sut->method('hasSeoUrl')->willReturn($hasSeoUrl); $sut->expects($hasSeoUrl ? $this->never() : $this->once())->method('createSeoUrl')->will( @@ -478,18 +425,6 @@ class ActionsTest extends UnitTestCase $this->throwException(oxNew(Exception::class)) : $this->returnValue(true) ); - $sut->method('d3GetMockableLogger')->willReturn($loggerMock); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); $this->callMethod( $sut, @@ -522,6 +457,7 @@ class ActionsTest extends UnitTestCase ->onlyMethods(['getStaticUrl']) ->getMock(); $seoEncoderMock->method('getStaticUrl')->willReturn($staticUrl); + d3GetOxidDIC()->set('d3ox.webauthn.'.SeoEncoder::class, $seoEncoderMock); /** @var ViewConfig|MockObject $viewConfigMock */ $viewConfigMock = $this->getMockBuilder(ViewConfig::class) @@ -534,24 +470,10 @@ class ActionsTest extends UnitTestCase ->onlyMethods(['getViewConfig']) ->getMock(); $controllerMock->method('getViewConfig')->willReturn($viewConfigMock); + d3GetOxidDIC()->set('d3ox.webauthn.'.FrontendController::class, $controllerMock); - /** @var Actions|MockObject $sut */ - $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($controllerMock, $seoEncoderMock) { - $args = func_get_args(); - switch ($args[0]) { - case FrontendController::class: - return $controllerMock; - case SeoEncoder::class: - return $seoEncoderMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var Actions $sut */ + $sut = oxNew(Actions::class); $this->assertSame( $expected, @@ -586,22 +508,10 @@ class ActionsTest extends UnitTestCase ->onlyMethods(['addSeoEntry']) ->getMock(); $seoEncoderMock->expects($this->exactly(2))->method('addSeoEntry'); + d3GetOxidDIC()->set('d3ox.webauthn.'.SeoEncoder::class, $seoEncoderMock); - /** @var Actions|MockObject $sut */ - $sut = $this->getMockBuilder(Actions::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($seoEncoderMock) { - $args = func_get_args(); - switch ($args[0]) { - case SeoEncoder::class: - return $seoEncoderMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); + /** @var Actions $sut */ + $sut = oxNew(Actions::class); $this->callMethod( $sut, diff --git a/src/tests/unit/WAUnitTestCase.php b/src/tests/unit/WAUnitTestCase.php new file mode 100644 index 0000000..a3ce844 --- /dev/null +++ b/src/tests/unit/WAUnitTestCase.php @@ -0,0 +1,60 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Webauthn\tests\unit; + +use D3\DIContainerHandler\d3DicHandler; +use OxidEsales\TestingLibrary\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\DependencyInjection\ContainerInterface; + +abstract class WAUnitTestCase extends UnitTestCase +{ + /** + * setup basic requirements + */ + public function setUp(): void + { + parent::setUp(); + + d3DicHandler::getUncompiledInstance(); + } + + public function tearDown(): void + { + parent::tearDown(); + + d3DicHandler::removeInstance(); + } + + /** + * @param $serviceName + * @param $serviceMock + * + * @return MockObject + */ + protected function getContainerMock($serviceName, $serviceMock): MockObject + { + $container = $this->getMockBuilder(ContainerInterface::class) + ->onlyMethods(['get', 'has']) + ->getMock(); + $container->expects($this->any()) + ->method('get') + ->with($this->equalTo($serviceName)) + ->will($this->returnValue($serviceMock)); + + return $container; + } +}