Completed
Push — master ( a5a753...454108 )
by Wachter
07:05
created

ScheduleSystemTasksCommandTest::testExecute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Task\TaskBundle\Unit\Command;
4
5
use Cron\CronExpression;
6
use Prophecy\Argument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Task\Execution\TaskExecutionInterface;
10
use Task\Scheduler\TaskSchedulerInterface;
11
use Task\Storage\TaskExecutionRepositoryInterface;
12
use Task\TaskBundle\Builder\TaskBuilder;
13
use Task\TaskBundle\Command\ScheduleSystemTasksCommand;
14
use Task\TaskBundle\Entity\Task;
15
use Task\TaskBundle\Entity\TaskRepository;
16
use Task\TaskBundle\Tests\Functional\TestHandler;
17
use Task\TaskStatus;
18
19
class ScheduleSystemTasksCommandTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var TaskSchedulerInterface
23
     */
24
    private $scheduler;
25
26
    /**
27
     * @var TaskRepository
28
     */
29
    private $taskRepository;
30
31
    /**
32
     * @var TaskExecutionRepositoryInterface
33
     */
34
    private $taskExecutionRepository;
35
36
    protected function setUp()
37
    {
38
        $this->scheduler = $this->prophesize(TaskSchedulerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...edulerInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Scheduler\TaskSchedulerInterface> of property $scheduler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        $this->taskRepository = $this->prophesize(TaskRepository::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...\TaskRepository::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\TaskBundle\Entity\TaskRepository> of property $taskRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
        $this->taskExecutionRepository = $this->prophesize(TaskExecutionRepositoryInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...sitoryInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Storage\Task...ionRepositoryInterface> of property $taskExecutionRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /**
44
     * @param array $systemTasks
45
     *
46
     * @return ScheduleSystemTasksCommand
47
     */
48
    protected function createCommand(array $systemTasks)
49
    {
50
        return new ScheduleSystemTasksCommand(
51
            'task:schedule:system-tasks',
52
            $systemTasks,
53
            $this->scheduler->reveal(),
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Scheduler\TaskSchedulerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            $this->taskRepository->reveal(),
0 ignored issues
show
Documentation Bug introduced by
The method reveal does not exist on object<Task\TaskBundle\Entity\TaskRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
55
            $this->taskExecutionRepository->reveal()
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Storage\Task...ionRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        );
57
    }
58
59
    public function testExecute()
60
    {
61
        $command = $this->createCommand(
62
            [
63
                'testing' => [
64
                    'enabled' => true,
65
                    'handler_class' => TestHandler::class,
66
                    'workload' => 'test',
67
                    'cron_expression' => '* * * * *',
68
                ],
69
            ]
70
        );
71
72
        $task = $this->prophesize(Task::class);
73
        $task->getSystemKey()->willReturn('testing');
74
75
        $taskBuilder = $this->prophesize(TaskBuilder::class);
76
77
        $this->taskRepository->findBySystemKey('testing')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
        $this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->taskRepository->findSystemTasks() (of type array<integer,object<Task\TaskInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
79
80
        $this->scheduler->createTask(TestHandler::class, 'test')->shouldBeCalled()->willReturn($taskBuilder->reveal());
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Builder\TaskBuilderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
82
        $taskBuilder->setSystemKey('testing')->shouldBeCalled();
83
        $taskBuilder->cron('* * * * *')->shouldBeCalled();
84
        $taskBuilder->schedule()->shouldBeCalled();
85
86
        $command->run(
87
            $this->prophesize(InputInterface::class)->reveal(),
88
            $this->prophesize(OutputInterface::class)->reveal()
89
        );
90
    }
91
92
    public function testExecuteMultiple()
93
    {
94
        $command = $this->createCommand(
95
            [
96
                'testing-1' => [
97
                    'enabled' => true,
98
                    'handler_class' => TestHandler::class,
99
                    'workload' => 'test-1',
100
                    'cron_expression' => '* * * * *',
101
                ],
102
                'testing-2' => [
103
                    'enabled' => true,
104
                    'handler_class' => TestHandler::class,
105
                    'workload' => 'test-2',
106
                    'cron_expression' => '* * * * *',
107
                ],
108
            ]
109
        );
110
111
        $task1 = $this->prophesize(Task::class);
112
        $task1->getSystemKey()->willReturn('testing-1');
113
        $task2 = $this->prophesize(Task::class);
114
        $task2->getSystemKey()->willReturn('testing-2');
115
116
        $this->taskRepository->findBySystemKey('testing-1')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
        $this->taskRepository->findBySystemKey('testing-2')->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
        $this->taskRepository->findSystemTasks()->willReturn([$task1->reveal(), $task2->reveal()]);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->taskRepository->findSystemTasks() (of type array<integer,object<Task\TaskInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
119
120
        $taskBuilder1 = $this->prophesize(TaskBuilder::class);
121
        $this->scheduler->createTask(TestHandler::class, 'test-1')->shouldBeCalled()->willReturn(
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Builder\TaskBuilderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
            $taskBuilder1->reveal()
123
        );
124
        $taskBuilder1->setSystemKey('testing-1')->shouldBeCalled();
125
        $taskBuilder1->cron('* * * * *')->shouldBeCalled();
126
        $taskBuilder1->schedule()->shouldBeCalled();
127
128
        $taskBuilder2 = $this->prophesize(TaskBuilder::class);
129
        $this->scheduler->createTask(TestHandler::class, 'test-2')->shouldBeCalled()->willReturn(
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Builder\TaskBuilderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            $taskBuilder2->reveal()
131
        );
132
        $taskBuilder2->setSystemKey('testing-2')->shouldBeCalled();
133
        $taskBuilder2->cron('* * * * *')->shouldBeCalled();
134
        $taskBuilder2->schedule()->shouldBeCalled();
135
136
        $command->run(
137
            $this->prophesize(InputInterface::class)->reveal(),
138
            $this->prophesize(OutputInterface::class)->reveal()
139
        );
140
    }
141
142
    public function testExecuteDisable()
143
    {
144
        $command = $this->createCommand(
145
            [
146
                'testing' => [
147
                    'enabled' => false,
148
                    'handler_class' => TestHandler::class,
149
                    'workload' => 'test',
150
                    'cron_expression' => '* * * * *',
151
                ],
152
            ]
153
        );
154
155
        $task = $this->prophesize(Task::class);
156
        $task->getInterval()->willReturn(CronExpression::factory('* * * * *'));
157
        $task->getFirstExecution()->willReturn(new \DateTime());
158
        $task->getSystemKey()->willReturn('testing');
159
160
        $task->setInterval(
161
            $task->reveal()->getInterval(),
162
            $task->reveal()->getFirstExecution(),
163
            Argument::that(
164
                function ($date) {
165
                    return $date <= new \DateTime('+1 Minute');
166
                }
167
            )
168
        )->shouldBeCalled();
169
170
        $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
        $this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->taskRepository->findSystemTasks() (of type array<integer,object<Task\TaskInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
172
173
        $execution = $this->prophesize(TaskExecutionInterface::class);
174
        $execution->setStatus(TaskStatus::ABORTED);
175
176
        $this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
        $this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Storage\Task...ionRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
179
        $command->run(
180
            $this->prophesize(InputInterface::class)->reveal(),
181
            $this->prophesize(OutputInterface::class)->reveal()
182
        );
183
    }
184
185
    public function testExecuteUpdate()
186
    {
187
        $command = $this->createCommand(
188
            [
189
                'testing' => [
190
                    'enabled' => true,
191
                    'handler_class' => TestHandler::class,
192
                    'workload' => 'test',
193
                    'cron_expression' => '* * * * *',
194
                ],
195
            ]
196
        );
197
198
        $task = $this->prophesize(Task::class);
199
        $task->getSystemKey()->willReturn('testing');
200
        $task->getHandlerClass()->willReturn(TestHandler::class);
201
        $task->getWorkload()->willReturn('test');
202
        $task->getInterval()->willReturn(CronExpression::factory('@daily'));
203
        $task->getFirstExecution()->willReturn(new \DateTime());
204
205
        $task->setInterval(CronExpression::factory('* * * * *'), $task->reveal()->getFirstExecution())->shouldBeCalled(
206
            );
207
208
        $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
209
        $this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->taskRepository->findSystemTasks() (of type array<integer,object<Task\TaskInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
210
211
        $execution = $this->prophesize(TaskExecutionInterface::class);
212
        $execution->setStatus(TaskStatus::ABORTED);
213
214
        $this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
        $this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Storage\Task...ionRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
217
        $this->scheduler->scheduleTasks()->shouldBeCalled();
218
219
        $command->run(
220
            $this->prophesize(InputInterface::class)->reveal(),
221
            $this->prophesize(OutputInterface::class)->reveal()
222
        );
223
    }
224
225
    public function testExecuteUpdateNotSupported()
226
    {
227
        $command = $this->createCommand(
228
            [
229
                'testing' => [
230
                    'enabled' => true,
231
                    'handler_class' => TestHandler::class,
232
                    'workload' => 'test',
233
                    'cron_expression' => '* * * * *',
234
                ],
235
            ]
236
        );
237
238
        $task = $this->prophesize(Task::class);
239
        $task->getSystemKey()->willReturn('testing');
240
        $task->getHandlerClass()->willReturn('not-existing');
241
        $task->getWorkload()->willReturn('new-workload');
242
        $task->getInterval()->willReturn(CronExpression::factory('@daily'));
243
        $task->getFirstExecution()->willReturn(new \DateTime());
244
245
        $task->setInterval(Argument::cetera())->shouldNotBeCalled();
246
247
        $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
248
        $this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->taskRepository->findSystemTasks() (of type array<integer,object<Task\TaskInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
249
250
        $this->taskExecutionRepository->save(Argument::cetera())->shouldNotBeCalled();
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::cetera() is of type object<Prophecy\Argument\Token\AnyValuesToken>, but the function expects a object<Task\Execution\TaskExecutionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method shouldNotBeCalled() does not seem to exist on object<Task\Storage\Task...ionRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
251
252
        $this->scheduler->scheduleTasks()->shouldNotBeCalled();
253
254
        $command->run(
255
            $this->prophesize(InputInterface::class)->reveal(),
256
            $this->prophesize(OutputInterface::class)->reveal()
257
        );
258
    }
259
260
    public function testExecuteRemove()
261
    {
262
        $command = $this->createCommand([]);
263
264
        $task = $this->prophesize(Task::class);
265
        $task->getInterval()->willReturn(CronExpression::factory('* * * * *'));
266
        $task->getFirstExecution()->willReturn(new \DateTime());
267
        $task->getSystemKey()->willReturn('testing');
268
269
        $task->setInterval(
270
            $task->reveal()->getInterval(),
271
            $task->reveal()->getFirstExecution(),
272
            Argument::that(
273
                function ($date) {
274
                    return $date <= new \DateTime('+1 Minute');
275
                }
276
            )
277
        )->shouldBeCalled();
278
279
        $this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
280
        $this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->taskRepository->findSystemTasks() (of type array<integer,object<Task\TaskInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
281
282
        $execution = $this->prophesize(TaskExecutionInterface::class);
283
        $execution->setStatus(TaskStatus::ABORTED);
284
285
        $this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal());
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
286
        $this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Storage\Task...ionRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
287
288
        $command->run(
289
            $this->prophesize(InputInterface::class)->reveal(),
290
            $this->prophesize(OutputInterface::class)->reveal()
291
        );
292
    }
293
}
294