Skip to content

Commit 4ff1233

Browse files
added testcases for run handler command and fixed code-style
1 parent 1fbb7ec commit 4ff1233

File tree

4 files changed

+80
-32
lines changed

4 files changed

+80
-32
lines changed

src/Command/RunHandlerCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Run pending tasks.
1414
*
15-
* @author @wachterjohannes <johannes.wachter@massiveart.com>
15+
* @author Alexander Schranz <alexander.schranz@massiveart.com>
1616
*/
1717
class RunHandlerCommand extends Command
1818
{
@@ -43,6 +43,13 @@ protected function configure()
4343
*/
4444
protected function execute(InputInterface $input, OutputInterface $output)
4545
{
46-
$this->registry->run($input->getArgument('handler'), $input->getArgument('workload'));
46+
$handler = $input->getArgument('handler');
47+
$workload = $input->getArgument('workload');
48+
49+
$output->writeln(sprintf('Run command "%s" with workload "%s"', $handler, $workload));
50+
51+
$result = $this->registry->run($handler, $workload);
52+
53+
$output->writeln(sprintf('Result: "%s"', $result));
4754
}
4855
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Unit\Command;
4+
5+
use Prophecy\Argument;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Task\Handler\RegistryInterface;
9+
use Task\TaskBundle\Command\RunHandlerCommand;
10+
11+
class RunHandlerCommandTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testConfigure()
14+
{
15+
$scheduler = $this->prophesize(RegistryInterface::class);
16+
$command = new RunHandlerCommand($scheduler->reveal());
17+
18+
$this->assertEquals('task:run:handler', $command->getName());
19+
}
20+
21+
public function runProvider()
22+
{
23+
return [
24+
['test-handler', 'test-workload'],
25+
['test-handler-1', 'test-workload-1'],
26+
];
27+
}
28+
29+
/**
30+
* @dataProvider runProvider
31+
*/
32+
public function testRun($handlerName, $workload)
33+
{
34+
$input = $this->prophesize(InputInterface::class);
35+
$output = $this->prophesize(OutputInterface::class);
36+
37+
$input->bind(Argument::any())->willReturn(true);
38+
$input->validate()->willReturn(true);
39+
$input->isInteractive()->willReturn(false);
40+
$input->hasArgument('command')->willReturn(false);
41+
42+
$input->getArgument('handler')->willReturn($handlerName);
43+
$input->getArgument('workload')->willReturn($workload);
44+
45+
$registry = $this->prophesize(RegistryInterface::class);
46+
$command = new RunHandlerCommand($registry->reveal());
47+
48+
$command->run($input->reveal(), $output->reveal());
49+
50+
$registry->run($handlerName, $workload)->shouldBeCalledTimes(1);
51+
}
52+
}

tests/Unit/DependencyInjection/TaskCompilerPassTest.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,15 @@ public function testProcess()
5757

5858
$schedulerDefinition->addMethodCall(
5959
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-
)
60+
['handler-1', 'test-workload', 'daily', 'test-key']
6961
)->shouldBeCalledTimes(1);
7062
$schedulerDefinition->addMethodCall(
7163
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-
)
64+
['handler-2', null, 'daily', 'test-key-1']
8165
)->shouldBeCalledTimes(1);
8266
$schedulerDefinition->addMethodCall(
8367
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-
)
68+
['handler-2', 'test-workload-2', 'daily', 'handler-2_daily_s:15:"test-workload-2";']
9369
)->shouldBeCalledTimes(1);
9470

9571
// TODO this test always returns true should be extended

tests/Unit/Storage/DoctrineStorageTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,24 @@ public function testStoreTaskForKeyExists($date, $completed, $uuid, $key = null)
8181
$oldTask = $this->prophesize(TaskInterface::class);
8282

8383
$repository->findOneBy(['key' => $key, 'completed' => false])->willReturn($oldTask)->shouldBeCalledTimes(1);
84+
85+
$entityManager->persist(Argument::any())->shouldNotBeCalled();
86+
$entityManager->flush()->shouldNotBeCalled();
87+
} else {
88+
$entityManager->persist(
89+
Argument::that(
90+
function (TaskEntity $entity) use ($date, $completed, $uuid) {
91+
$this->assertEquals($uuid, $entity->getUuid());
92+
$this->assertEquals($completed, $entity->isCompleted());
93+
$this->assertEquals($date, $entity->getExecutionDate());
94+
95+
return true;
96+
}
97+
)
98+
)->shouldBeCalledTimes(1);
99+
$entityManager->flush()->shouldBeCalledTimes(1);
84100
}
85101

86102
$storage->store($task->reveal());
87-
88-
$entityManager->persist(Argument::any())->shouldNotBeCalled();
89-
$entityManager->flush()->shouldNotBeCalled();
90103
}
91104
}

0 commit comments

Comments
 (0)