Skip to content

Commit 2b32466

Browse files
added missing unit-tests
1 parent 5daed86 commit 2b32466

File tree

6 files changed

+148
-4
lines changed

6 files changed

+148
-4
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"Task\\TaskBundle\\": "src"
3030
}
3131
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Task\\TaskBundle\\Tests": "tests"
35+
}
36+
},
3237
"extra": {
3338
"branch-alias": {
3439
"dev-master": "1.0-dev"

tests/Functional/BootstrapTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Functional;
12+
namespace Task\TaskBundle\Tests\Functional;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515
use Task\Scheduler\TaskSchedulerInterface;
@@ -18,6 +18,9 @@
1818
use Task\TaskBundle\Entity\TaskExecutionRepository;
1919
use Task\TaskBundle\Entity\TaskRepository;
2020

21+
/**
22+
* Tests the service definitions.
23+
*/
2124
class BootstrapTest extends KernelTestCase
2225
{
2326
public function testBootstrap()

tests/Functional/TaskHandlerFactoryTest.php renamed to tests/Functional/Handler/TaskHandlerFactoryTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Functional;
12+
namespace Task\TaskBundle\Tests\Functional\Handler;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Task\Handler\TaskHandlerNotExistsException;
1516
use Task\TaskBundle\Handler\TaskHandlerFactory;
1617

18+
/**
19+
* Functional tests for task-handler definitions.
20+
*/
1721
class TaskHandlerFactoryTest extends KernelTestCase
1822
{
1923
/**
@@ -29,8 +33,15 @@ protected function setUp()
2933
$this->taskHandlerFactory = self::$kernel->getContainer()->get('task.handler.factory');
3034
}
3135

32-
public function testRun()
36+
public function testCreate()
3337
{
3438
$this->assertInstanceOf(\TestHandler::class, $this->taskHandlerFactory->create(\TestHandler::class));
3539
}
40+
41+
public function testCreateNotExists()
42+
{
43+
$this->setExpectedException(TaskHandlerNotExistsException::class);
44+
45+
$this->taskHandlerFactory->create(\stdClass::class);
46+
}
3647
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 Unit\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Task\TaskBundle\DependencyInjection\HandlerCompilerPass;
18+
19+
/**
20+
* Tests for class HandlerCompilerPass.
21+
*/
22+
class HandlerCompilerPassTest extends \PHPUnit_Framework_TestCase
23+
{
24+
public function testProcess()
25+
{
26+
$container = $this->prophesize(ContainerBuilder::class);
27+
28+
$container->has(HandlerCompilerPass::REGISTRY_ID)->willReturn(true);
29+
$container->findTaggedServiceIds(HandlerCompilerPass::HANDLER_TAG)->willReturn(
30+
['service1' => [], 'service2' => []]
31+
);
32+
$container->getDefinition('service1')->willReturn(new Definition(\stdClass::class));
33+
$container->getDefinition('service2')->willReturn(new Definition(self::class));
34+
35+
$serviceDefinition = $this->prophesize(Definition::class);
36+
$container->findDefinition(HandlerCompilerPass::REGISTRY_ID)->willReturn($serviceDefinition);
37+
38+
$compilerPass = new HandlerCompilerPass();
39+
$compilerPass->process($container->reveal());
40+
41+
$serviceDefinition->replaceArgument(
42+
0,
43+
[\stdClass::class => new Reference('service1'), self::class => new Reference('service2')]
44+
)->shouldBeCalled();
45+
}
46+
47+
public function testProcessNoTaggedService()
48+
{
49+
$container = $this->prophesize(ContainerBuilder::class);
50+
51+
$container->has(HandlerCompilerPass::REGISTRY_ID)->willReturn(true);
52+
$container->findTaggedServiceIds(HandlerCompilerPass::HANDLER_TAG)->willReturn([]);
53+
54+
$serviceDefinition = $this->prophesize(Definition::class);
55+
$container->findDefinition(HandlerCompilerPass::REGISTRY_ID)->willReturn($serviceDefinition);
56+
57+
$compilerPass = new HandlerCompilerPass();
58+
$compilerPass->process($container->reveal());
59+
60+
$serviceDefinition->replaceArgument(0, [])->shouldBeCalled();
61+
}
62+
}

tests/Unit/EventListener/RunListenerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Unit\EventListener;
12+
namespace Task\TaskBundle\Tests\Unit\EventListener;
1313

1414
use Symfony\Component\EventDispatcher\Event;
1515
use Task\Runner\TaskRunnerInterface;
1616
use Task\TaskBundle\EventListener\RunListener;
1717

18+
/**
19+
* Tests for class RunListener.
20+
*/
1821
class RunListenerTest extends \PHPUnit_Framework_TestCase
1922
{
2023
public function testRun()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Unit\Handler;
13+
14+
use Task\Handler\TaskHandlerInterface;
15+
use Task\Handler\TaskHandlerNotExistsException;
16+
use Task\TaskBundle\Handler\TaskHandlerFactory;
17+
18+
/**
19+
* Tests for class TaskHandlerFactory.
20+
*/
21+
class TaskHandlerFactoryTest extends \PHPUnit_Framework_TestCase
22+
{
23+
public function testCreate()
24+
{
25+
$handler = new TestHandler();
26+
$taskHandlerFactory = new TaskHandlerFactory([TestHandler::class => $handler]);
27+
28+
$this->assertEquals($handler, $taskHandlerFactory->create(TestHandler::class));
29+
}
30+
31+
public function testCreateNotExists()
32+
{
33+
$this->setExpectedException(TaskHandlerNotExistsException::class);
34+
35+
$taskHandlerFactory = new TaskHandlerFactory([TestHandler::class => new TestHandler()]);
36+
37+
$taskHandlerFactory->create(\stdClass::class);
38+
}
39+
40+
public function testCreateNoHandler()
41+
{
42+
$this->setExpectedException(TaskHandlerNotExistsException::class);
43+
44+
$taskHandlerFactory = new TaskHandlerFactory([]);
45+
46+
$taskHandlerFactory->create(\stdClass::class);
47+
}
48+
}
49+
50+
class TestHandler implements TaskHandlerInterface
51+
{
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function handle($workload)
56+
{
57+
return strrev($workload);
58+
}
59+
}
60+

0 commit comments

Comments
 (0)