Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
RateLimitExceededException example
$newsletterRecipientEntity
->
setConfirmedAt
(
new
\
DateTime
(
)
)
;
$requestStack
=
new
RequestStack
(
)
;
$request
=
new
Request
(
)
;
$request
->server->
set
(
'REMOTE_ADDR', '127.0.0.1'
)
;
$requestStack
->
push
(
$request
)
;
$rateLimiterMock
=
$this
->
createMock
(
RateLimiter::
class
)
;
$rateLimiterMock
->
expects
(
static
::
once
(
)
)
->
method
(
'ensureAccepted'
)
->
willThrowException
(
new
RateLimitExceededException
(
2
)
)
;
$newsletterSubscribeRoute
=
new
NewsletterSubscribeRoute
(
$this
->
createMock
(
EntityRepository::
class
)
,
$this
->
createMock
(
DataValidator::
class
)
,
$this
->
createMock
(
EventDispatcherInterface::
class
)
,
$this
->
createMock
(
SystemConfigService::
class
)
,
$rateLimiterMock
,
$requestStack
,
)
;
if
(
!Feature::
isActive
(
'v6.6.0.0'
)
)
{
public
function
reset
(
string
$route
, string
$key
)
: void
{
$this
->
getFactory
(
$route
)
->
create
(
$key
)
->
reset
(
)
;
}
public
function
ensureAccepted
(
string
$route
, string
$key
)
: void
{
$limiter
=
$this
->
getFactory
(
$route
)
->
create
(
$key
)
->
consume
(
)
;
if
(
!
$limiter
->
isAccepted
(
)
)
{
throw
new
RateLimitExceededException
(
$limiter
->
getRetryAfter
(
)
->
getTimestamp
(
)
)
;
}
}
public
function
registerLimiterFactory
(
string
$route
, RateLimiterFactory
$factory
)
: void
{
$this
->factories
[
$route
]
=
$factory
;
}
private
function
getFactory
(
string
$route
)
: RateLimiterFactory
{
$factory
=
$this
->factories
[
$route
]
?? null;
return
$this
->accepted;
}
/** * @return $this * * @throws RateLimitExceededException if not accepted */
public
function
ensureAccepted
(
)
:
static
{
if
(
!
$this
->accepted
)
{
throw
new
RateLimitExceededException
(
$this
)
;
}
return
$this
;
}
public
function
getRetryAfter
(
)
: \DateTimeImmutable
{
return
$this
->retryAfter;
}
public
function
getRemainingTokens
(
)
: int
{
$session
=
$this
->
getSession
(
)
;
static
::
assertInstanceOf
(
Session::
class
,
$session
)
;
$session
->
getFlashBag
(
)
->
clear
(
)
;
$this
->translator =
$this
->
getContainer
(
)
->
get
(
'translator'
)
;
}
public
function
testGenerateAccountRecoveryRateLimit
(
)
: void
{
$passwordRecoveryMailRoute
=
$this
->
createMock
(
SendPasswordRecoveryMailRoute::
class
)
;
$passwordRecoveryMailRoute
->
method
(
'sendRecoveryMail'
)
->
willThrowException
(
new
RateLimitExceededException
(
time
(
)
+ 10
)
)
;
$controller
=
new
AuthController
(
$this
->
getContainer
(
)
->
get
(
AccountLoginPageLoader::
class
)
,
$passwordRecoveryMailRoute
,
$this
->
getContainer
(
)
->
get
(
ResetPasswordRoute::
class
)
,
$this
->
getContainer
(
)
->
get
(
LoginRoute::
class
)
,
$this
->
getContainer
(
)
->
get
(
LogoutRoute::
class
)
,
$this
->
getContainer
(
)
->
get
(
StorefrontCartFacade::
class
)
,
$this
->
getContainer
(
)
->
get
(
AccountRecoverPasswordPageLoader::
class
)
,
$this
->
getContainer
(
)
->
get
(
SalesChannelContextService::
class
)
)
;