Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
setRunInterval example
$tasks
=
[
new
InvalidateCacheTask
(
)
,
new
CreateAliasTask
(
)
,
new
CleanupCartTask
(
)
]
;
$parameterBag
=
new
ParameterBag
(
[
'shopware.cache.invalidation.delay' => 10,
'elasticsearch.enabled' => false,
]
)
;
$registeredTask
=
new
ScheduledTaskEntity
(
)
;
$registeredTask
->
setId
(
'1'
)
;
$registeredTask
->
setName
(
CleanupCartTask::
getTaskName
(
)
)
;
$registeredTask
->
setRunInterval
(
CleanupCartTask::
getDefaultInterval
(
)
)
;
$registeredTask
->
setDefaultRunInterval
(
CleanupCartTask::
getDefaultInterval
(
)
)
;
$registeredTask
->
setStatus
(
ScheduledTaskDefinition::STATUS_SCHEDULED
)
;
$registeredTask
->
setNextExecutionTime
(
new
\
DateTimeImmutable
(
)
)
;
$registeredTask
->
setScheduledTaskClass
(
CleanupCartTask::
class
)
;
/** @var StaticEntityRepository<ScheduledTaskCollection> $staticRepository */
$staticRepository
=
new
StaticEntityRepository
(
[
new
ScheduledTaskCollection
(
[
$registeredTask
]
)
,
]
)
;
(
new
TaskRegistry
(
$tasks
,
$staticRepository
,
$parameterBag
)
)
->
registerTasks
(
)
;
public
function
testScheduleShouldNotRunTask
(
)
: void
{
$scheduledTaskRepository
=
$this
->
createMock
(
EntityRepository::
class
)
;
$scheduledTask
=
new
ScheduledTaskEntity
(
)
;
$nextExecutionTime
=
new
\
DateTimeImmutable
(
)
;
$nextExecutionTime
=
$nextExecutionTime
->
modify
(
sprintf
(
'-%d seconds', InvalidateCacheTask::
getDefaultInterval
(
)
+ 100
)
)
;
$scheduledTask
->
setId
(
'1'
)
;
$scheduledTask
->
setRunInterval
(
InvalidateCacheTask::
getDefaultInterval
(
)
)
;
$scheduledTask
->
setNextExecutionTime
(
$nextExecutionTime
)
;
$scheduledTask
->
setScheduledTaskClass
(
InvalidateCacheTask::
class
)
;
$result
=
$this
->
createMock
(
EntitySearchResult::
class
)
;
$result
->
method
(
'getEntities'
)
->
willReturn
(
new
ScheduledTaskCollection
(
[
$scheduledTask
]
)
)
;
$scheduledTaskRepository
->
expects
(
static
::
once
(
)
)
->
method
(
'search'
)
->
willReturn
(
$result
)
;
$scheduledTaskRepository
->
expects
(
static
::
once
(
)
)
->
method
(
'update'
)
->
willReturnCallback
(
function
Darray
$data
, Context
$context
)
{
static
::
assertCount
(
1,
$data
)
;
$data
=
$data
[
0
]
;
static
::
assertArrayHasKey
(
'id',
$data
)
;
static
::
assertArrayHasKey
(
'nextExecutionTime',
$data
)
;
static
::
assertArrayHasKey
(
'status',
$data
)
;
class
ListScheduledTaskCommandTest
extends
TestCase
{
public
function
testListTasks
(
)
: void
{
$entity
=
new
ScheduledTaskEntity
(
)
;
$entity
->
setId
(
'test'
)
;
$entity
->
setName
(
'TestTask.ID'
)
;
$entity
->
setNextExecutionTime
(
new
\
DateTime
(
)
)
;
$entity
->
setRunInterval
(
100
)
;
$entity
->
setStatus
(
ScheduledTaskDefinition::STATUS_QUEUED
)
;
$taskRegistry
=
$this
->
createMock
(
TaskRegistry::
class
)
;
$taskRegistry
->
method
(
'getAllTasks'
)
->
willReturn
(
new
ScheduledTaskCollection
(
[
$entity
]
)
)
;
$command
=
new
ListScheduledTaskCommand
(
$taskRegistry
)
;
$tester
=
new
CommandTester
(
$command
)
;
$tester
->
execute
(
[
]
)
;