Skip to content

Commit 7ac6a59

Browse files
added unit tests
1 parent c2e4879 commit 7ac6a59

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

src/Command/ScheduleTaskCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function configure()
3434
{
3535
$this
3636
->setDescription('Run pending tasks')
37-
->addArgument('workerName', InputArgument::REQUIRED)
37+
->addArgument('handler', InputArgument::REQUIRED)
3838
->addArgument('workload', InputArgument::OPTIONAL);
3939
}
4040

@@ -44,7 +44,7 @@ protected function configure()
4444
protected function execute(InputInterface $input, OutputInterface $output)
4545
{
4646
$this->scheduler
47-
->createTask($input->getArgument('workerName'), $input->getArgument('workload'))
47+
->createTask($input->getArgument('handler'), $input->getArgument('workload'))
4848
->schedule();
4949
}
5050
}

tests/Unit/Command/RunCommandTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Unit\Command;
4+
5+
use Symfony\Component\Console\Input\InputInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use Task\SchedulerInterface;
8+
use Task\TaskBundle\Command\RunCommand;
9+
10+
class RunCommandTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testConfigure()
13+
{
14+
$scheduler = $this->prophesize(SchedulerInterface::class);
15+
$command = new RunCommand($scheduler->reveal());
16+
17+
$this->assertEquals('task:run', $command->getName());
18+
}
19+
20+
public function testRun()
21+
{
22+
$input = $this->prophesize(InputInterface::class);
23+
$output = $this->prophesize(OutputInterface::class);
24+
25+
$scheduler = $this->prophesize(SchedulerInterface::class);
26+
$command = new RunCommand($scheduler->reveal());
27+
28+
$command->run($input->reveal(), $output->reveal());
29+
30+
$scheduler->run()->shouldBeCalledTimes(1);
31+
}
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\SchedulerInterface;
9+
use Task\TaskBuilderInterface;
10+
use Task\TaskBundle\Command\ScheduleTaskCommand;
11+
12+
class ScheduleTaskCommandTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testConfigure()
15+
{
16+
$scheduler = $this->prophesize(SchedulerInterface::class);
17+
$command = new ScheduleTaskCommand($scheduler->reveal());
18+
19+
$this->assertEquals('task:schedule:task', $command->getName());
20+
$this->assertTrue($command->getDefinition()->hasArgument('handler'));
21+
$this->assertTrue($command->getDefinition()->hasArgument('workload'));
22+
}
23+
24+
public function runProvider()
25+
{
26+
return [
27+
['test-handler', 'test-workload'],
28+
['test-handler-1', 'test-workload-1'],
29+
];
30+
}
31+
32+
/**
33+
* @dataProvider runProvider
34+
*/
35+
public function testRun($handler, $workload)
36+
{
37+
$taskBuilder = $this->prophesize(TaskBuilderInterface::class);
38+
39+
$input = $this->prophesize(InputInterface::class);
40+
$output = $this->prophesize(OutputInterface::class);
41+
42+
$input->bind(Argument::any())->willReturn(true);
43+
$input->validate()->willReturn(true);
44+
$input->isInteractive()->willReturn(false);
45+
$input->hasArgument('command')->willReturn(false);
46+
47+
$input->getArgument('handler')->willReturn($handler);
48+
$input->getArgument('workload')->willReturn($workload);
49+
50+
$scheduler = $this->prophesize(SchedulerInterface::class);
51+
$command = new ScheduleTaskCommand($scheduler->reveal());
52+
53+
$scheduler->createTask($handler, $workload)->shouldBeCalledTimes(1)->willReturn($taskBuilder->reveal());
54+
55+
$command->run($input->reveal(), $output->reveal());
56+
57+
$taskBuilder->schedule()->shouldBeCalledTimes(1);
58+
}
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
10+
class HandlerCompilerPassTest extends \PHPUnit_Framework_TestCase
11+
{
12+
public function testProcess()
13+
{
14+
$containerBuilder = $this->prophesize(ContainerBuilder::class);
15+
$definition = $this->prophesize(Definition::class);
16+
17+
$containerBuilder->has(HandlerCompilerPass::REGISTRY_ID)->willReturn(true);
18+
$containerBuilder->findDefinition(HandlerCompilerPass::REGISTRY_ID)->willReturn($definition->reveal());
19+
20+
$containerBuilder->findTaggedServiceIds(HandlerCompilerPass::HANDLER_TAG)
21+
->willReturn(
22+
[
23+
'id-1' => [
24+
['name' => 'name-1'],
25+
],
26+
'id-2' => [
27+
['name' => 'name-2-1'],
28+
['name' => 'name-2-2'],
29+
],
30+
]
31+
);
32+
33+
$compilerPass = new HandlerCompilerPass();
34+
$compilerPass->process($containerBuilder->reveal());
35+
36+
$definition->addMethodCall(
37+
HandlerCompilerPass::ADD_FUNCTION_NAME,
38+
Argument::that(
39+
function ($arguments) {
40+
return $arguments[0] === 'name-1' && $arguments[1]->__toString() === 'id-1';
41+
}
42+
)
43+
)->shouldBeCalledTimes(1);
44+
$definition->addMethodCall(
45+
HandlerCompilerPass::ADD_FUNCTION_NAME,
46+
Argument::that(
47+
function ($arguments) {
48+
return $arguments[0] === 'name-2-1' && $arguments[1]->__toString() === 'id-2';
49+
}
50+
)
51+
)->shouldBeCalledTimes(1);
52+
$definition->addMethodCall(
53+
HandlerCompilerPass::ADD_FUNCTION_NAME,
54+
Argument::that(
55+
function ($arguments) {
56+
return $arguments[0] === 'name-2-2' && $arguments[1]->__toString() === 'id-2';
57+
}
58+
)
59+
)->shouldBeCalledTimes(1);
60+
}
61+
}

0 commit comments

Comments
 (0)