Completed
Pull Request — master (#20)
by Wachter
02:54
created

BaseCommandTestCase::createTaskExecution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\TaskBundle\Tests\Functional;
13
14
use Cron\CronExpression;
15
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
16
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
17
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
18
use Doctrine\DBAL\Driver\PDOMySql\Driver;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Symfony\Bundle\FrameworkBundle\Console\Application;
21
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Tester\CommandTester;
24
use Task\Execution\TaskExecutionInterface;
25
use Task\Runner\TaskRunnerInterface;
26
use Task\Scheduler\TaskSchedulerInterface;
27
use Task\Storage\TaskExecutionRepositoryInterface;
28
use Task\Storage\TaskRepositoryInterface;
29
use Task\TaskInterface;
30
use Task\TaskStatus;
31
32
/**
33
 * Base class for testing commands.
34
 */
35
abstract class BaseCommandTestCase extends KernelTestCase
36
{
37
    /**
38
     * @var Application
39
     */
40
    protected $application;
41
42
    /**
43
     * @var TaskRepositoryInterface
44
     */
45
    protected $taskRepository;
46
47
    /**
48
     * @var TaskExecutionRepositoryInterface
49
     */
50
    protected $taskExecutionRepository;
51
52
    /**
53
     * @var TaskSchedulerInterface
54
     */
55
    protected $taskScheduler;
56
57
    /**
58
     * @var TaskRunnerInterface
59
     */
60
    protected $taskRunner;
61
62
    /**
63
     * @var Command
64
     */
65
    protected $command;
66
67
    /**
68
     * @var CommandTester
69
     */
70
    protected $commandTester;
71
72
    public function setUp()
73
    {
74
        self::bootKernel();
75
76
        $this->taskRunner = self::$kernel->getContainer()->get('task.runner');
77
        $this->taskScheduler = self::$kernel->getContainer()->get('task.scheduler');
78
        $this->taskRepository = self::$kernel->getContainer()->get('task.storage.task');
79
        $this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution');
80
81
        $command = $this->getCommand();
82
83
        $this->application = new Application(self::$kernel);
84
        $this->application->add($command);
85
86
        $this->command = $this->application->find($command->getName());
87
        $this->commandTester = new CommandTester($this->command);
88
89
        $this->purgeDatabase();
90
    }
91
92
    /**
93
     * Create new task.
94
     *
95
     * @param string $workload
96
     * @param CronExpression $cronExpression
97
     * @param string $handlerClass
98
     *
99
     * @return TaskInterface
100
     */
101
    protected function createTask($workload, CronExpression $cronExpression = null, $handlerClass = TestHandler::class)
102
    {
103
        $task = $this->taskRepository->create($handlerClass, $workload);
104
        if ($cronExpression) {
105
            $task->setInterval($cronExpression, new \DateTime(), new \DateTime('+1 year'));
106
        }
107
        $this->taskRepository->persist($task);
108
        $this->taskRepository->flush();
109
110
        return $task;
111
    }
112
113
    /**
114
     * Create task-execution.
115
     *
116
     * @param TaskInterface $task
117
     * @param \DateTime $scheduleTime
118
     * @param string $status
119
     *
120
     * @return TaskExecutionInterface
121
     */
122
    protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
123
    {
124
        $execution = $this->taskExecutionRepository->create($task, $scheduleTime);
125
        $execution->setStatus($status);
126
        $this->taskExecutionRepository->persist($execution);
127
        $this->taskExecutionRepository->flush();
128
129
        return $execution;
130
    }
131
132
    /**
133
     * Purge the Doctrine ORM database.
134
     */
135
    protected function purgeDatabase()
136
    {
137
        if (!self::$kernel->getContainer()->has('doctrine')) {
138
            return;
139
        }
140
        
141
        $manager = $this->getEntityManager();
142
        $connection = $manager->getConnection();
143
144
        if ($connection->getDriver() instanceof Driver) {
145
            $connection->executeUpdate('SET foreign_key_checks = 0;');
146
        }
147
148
        $purger = new ORMPurger();
149
        $executor = new ORMExecutor($manager, $purger);
150
        $referenceRepository = new ProxyReferenceRepository($manager);
151
        $executor->setReferenceRepository($referenceRepository);
152
        $executor->purge();
153
154
        if ($connection->getDriver() instanceof Driver) {
155
            $connection->executeUpdate('SET foreign_key_checks = 1;');
156
        }
157
    }
158
159
    /**
160
     * Returns entity-manager.
161
     *
162
     * @return EntityManagerInterface
163
     */
164
    protected function getEntityManager()
165
    {
166
        return self::$kernel->getContainer()->get('doctrine')->getManager();
167
    }
168
169
    /**
170
     * Returns command.
171
     *
172
     * @return Command
173
     */
174
    abstract protected function getCommand();
175
}
176