Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
ScheduledTaskEntity example
use
Symfony\Component\Console\Tester\CommandTester;
/** * @internal * * @covers \Shopware\Core\Framework\MessageQueue\Command\ListScheduledTaskCommand */
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
]
)
)
;
$this
->scheduleTaskRepository =
$this
->
createMock
(
EntityRepository::
class
)
;
}
public
function
testNewTasksAreCreated
(
)
: void
{
$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
(
[
$taskRunner
->
runSingleTask
(
'task-id', Context::
createDefaultContext
(
)
)
;
static
::
assertTrue
(
$handler
->called
)
;
static
::
assertFalse
(
$handler2
->called
)
;
}
/** * @return StaticEntityRepository<ScheduledTaskCollection> */
private
function
getRepository
(
)
: StaticEntityRepository
{
$task
=
new
ScheduledTaskEntity
(
)
;
$task
->
setId
(
'task-id'
)
;
$task
->
setScheduledTaskClass
(
TestTask::
class
)
;
// @phpstan-ignore-next-line
return
new
StaticEntityRepository
(
[
new
ScheduledTaskCollection
(
[
$task
]
)
]
)
;
}
}
/** * @internal */
$bus
,
new
ParameterBag
(
)
)
;
$scheduler
->
queueScheduledTasks
(
)
;
}
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
)
;