Skip to content

Commit 7c9ec72

Browse files
added testcases for new feature
1 parent e496fe9 commit 7c9ec72

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

src/DependencyInjection/TaskCompilerPass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
7-
use Symfony\Component\DependencyInjection\Reference;
87

98
/**
109
* Compiler pass which collects worker services.
@@ -36,6 +35,7 @@ public function process(ContainerBuilder $container)
3635
$handlerDefinition = $container->getDefinition($id);
3736
$tag = $handlerDefinition->getTag(HandlerCompilerPass::HANDLER_TAG);
3837

38+
// TODO handle also multiple handler tag here
3939
$handler = $tag[0][HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE];
4040

4141
// remove all tasks with $id and not completed
@@ -48,13 +48,12 @@ public function process(ContainerBuilder $container)
4848
$key = $handler . '_' . $interval . '_' . serialize($workload);
4949
}
5050

51-
5251
$schedulerDefinition->addMethodCall(
5352
self::CREATE_FUNCTION_NAME,
5453
[
5554
$handler,
56-
$interval,
5755
$workload,
56+
$interval,
5857
$key,
5958
]
6059
);

tests/Unit/DependencyInjection/HandlerCompilerPassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public function testProcess()
2121
->willReturn(
2222
[
2323
'id-1' => [
24-
['handler-name' => 'name-1'],
24+
[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'name-1'],
2525
],
2626
'id-2' => [
27-
['handler-name' => 'name-2-1'],
28-
['handler-name' => 'name-2-2'],
27+
[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'name-2-1'],
28+
[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'name-2-2'],
2929
],
3030
]
3131
);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Unit\DependencyInjection;
4+
5+
use Prophecy\Argument;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Definition;
8+
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
9+
use Task\TaskBundle\DependencyInjection\TaskCompilerPass;
10+
11+
class TaskCompilerPassTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testProcess()
14+
{
15+
$containerBuilder = $this->prophesize(ContainerBuilder::class);
16+
$schedulerDefinition = $this->prophesize(Definition::class);
17+
$handler1Definition = $this->prophesize(Definition::class);
18+
$handler2Definition = $this->prophesize(Definition::class);
19+
20+
$containerBuilder->has(TaskCompilerPass::SCHEDULER_ID)->willReturn(true);
21+
$containerBuilder->getDefinition(TaskCompilerPass::SCHEDULER_ID)->willReturn($schedulerDefinition->reveal());
22+
$containerBuilder->getDefinition('id-1')->willReturn($handler1Definition->reveal());
23+
$containerBuilder->getDefinition('id-2')->willReturn($handler2Definition->reveal());
24+
25+
$containerBuilder->findTaggedServiceIds(TaskCompilerPass::INTERVAL_TAG)
26+
->willReturn(
27+
[
28+
'id-1' => [
29+
[
30+
TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
31+
TaskCompilerPass::WORKLOAD_ATTRIBUTE => 'test-workload',
32+
TaskCompilerPass::KEY_ATTRIBUTE => 'test-key'
33+
],
34+
],
35+
'id-2' => [
36+
[
37+
TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
38+
TaskCompilerPass::KEY_ATTRIBUTE => 'test-key-1'
39+
],
40+
[
41+
TaskCompilerPass::INTERVAL_ATTRIBUTE => 'daily',
42+
TaskCompilerPass::WORKLOAD_ATTRIBUTE => 'test-workload-2'
43+
],
44+
],
45+
]
46+
);
47+
48+
$handler1Definition->getTag(HandlerCompilerPass::HANDLER_TAG)->willReturn(
49+
[[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'handler-1']]
50+
);
51+
$handler2Definition->getTag(HandlerCompilerPass::HANDLER_TAG)->willReturn(
52+
[[HandlerCompilerPass::HANDLER_NAME_ATTRIBUTE => 'handler-2']]
53+
);
54+
55+
$compilerPass = new TaskCompilerPass();
56+
$compilerPass->process($containerBuilder->reveal());
57+
58+
$schedulerDefinition->addMethodCall(
59+
TaskCompilerPass::CREATE_FUNCTION_NAME,
60+
Argument::that(
61+
function ($arguments) {
62+
return
63+
$arguments[0] === 'handler-1'
64+
&& $arguments[1] === 'test-workload'
65+
&& $arguments[2] === 'daily'
66+
&& $arguments[3] === 'test-key';
67+
}
68+
)
69+
)->shouldBeCalledTimes(1);
70+
$schedulerDefinition->addMethodCall(
71+
TaskCompilerPass::CREATE_FUNCTION_NAME,
72+
Argument::that(
73+
function ($arguments) {
74+
return
75+
$arguments[0] === 'handler-2'
76+
&& $arguments[1] === null
77+
&& $arguments[2] === 'daily'
78+
&& $arguments[3] === 'test-key';
79+
}
80+
)
81+
)->shouldBeCalledTimes(1);
82+
$schedulerDefinition->addMethodCall(
83+
TaskCompilerPass::CREATE_FUNCTION_NAME,
84+
Argument::that(
85+
function ($arguments) {
86+
return
87+
$arguments[0] === 'handler-2'
88+
&& $arguments[1] === 'test-workload-2'
89+
&& $arguments[2] === 'daily'
90+
&& $arguments[3] === 'handler-2_daily_s:15:"test-workload-2";';
91+
}
92+
)
93+
)->shouldBeCalledTimes(1);
94+
95+
// TODO this test always returns true should be extended
96+
}
97+
}

tests/Unit/Storage/DoctrineStorageTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ public function storeDataProvider()
1515
{
1616
return [
1717
[new \DateTime(), true, '123-123-123'],
18+
[new \DateTime(), true, '123-123-123', 'test-key'],
1819
[new \DateTime(), true, '321-321-321'],
20+
[new \DateTime(), true, '321-321-321', 'test-key'],
1921
[new \DateTime('1 day ago'), true, '321-321-321'],
22+
[new \DateTime('1 day ago'), true, '321-321-321', 'test-key'],
2023
[new \DateTime(), false, '123-123-123'],
24+
[new \DateTime(), false, '123-123-123', 'test-key'],
2125
];
2226
}
2327

@@ -37,6 +41,10 @@ public function testStore($date, $completed, $uuid, $key = null)
3741
$task->isCompleted()->willReturn($completed);
3842
$task->getExecutionDate()->willReturn($date);
3943

44+
if ($key) {
45+
$repository->findOneBy(['key' => $key, 'completed' => false])->willReturn(null)->shouldBeCalledTimes(1);
46+
}
47+
4048
$storage->store($task->reveal());
4149

4250
$entityManager->persist(
@@ -52,4 +60,32 @@ function (TaskEntity $entity) use ($date, $completed, $uuid) {
5260
)->shouldBeCalledTimes(1);
5361
$entityManager->flush()->shouldBeCalledTimes(1);
5462
}
63+
64+
/**
65+
* @dataProvider storeDataProvider
66+
*/
67+
public function testStoreTaskForKeyExists($date, $completed, $uuid, $key = null)
68+
{
69+
$entityManager = $this->prophesize(EntityManagerInterface::class);
70+
$repository = $this->prophesize(TaskRepository::class);
71+
72+
$storage = new DoctrineStorage($entityManager->reveal(), $repository->reveal());
73+
74+
$task = $this->prophesize(TaskInterface::class);
75+
$task->getUuid()->willReturn($uuid);
76+
$task->getKey()->willReturn($key);
77+
$task->isCompleted()->willReturn($completed);
78+
$task->getExecutionDate()->willReturn($date);
79+
80+
if ($key) {
81+
$oldTask = $this->prophesize(TaskInterface::class);
82+
83+
$repository->findOneBy(['key' => $key, 'completed' => false])->willReturn($oldTask)->shouldBeCalledTimes(1);
84+
}
85+
86+
$storage->store($task->reveal());
87+
88+
$entityManager->persist(Argument::any())->shouldNotBeCalled();
89+
$entityManager->flush()->shouldNotBeCalled();
90+
}
5591
}

0 commit comments

Comments
 (0)