1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Jarobe\TaskRunnerBundle\Tests\Processor; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Jarobe\TaskRunnerBundle\Driver\Factory\DriverFactory; |
8
|
|
|
use Jarobe\TaskRunnerBundle\Driver\TaskDriverInterface; |
9
|
|
|
use Jarobe\TaskRunnerBundle\Exception\TaskException; |
10
|
|
|
use Jarobe\TaskRunnerBundle\Model\TaskResult; |
11
|
|
|
use Jarobe\TaskRunnerBundle\Processor\Processor; |
12
|
|
|
use Jarobe\TaskRunnerBundle\Processor\ProcessorInterface; |
13
|
|
|
use Jarobe\TaskRunnerBundle\TaskType\TaskTypeInterface; |
14
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
15
|
|
|
|
16
|
|
|
class ProcessorTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var DriverFactory|ObjectProphecy */ |
19
|
|
|
private $driverFactory; |
20
|
|
|
|
21
|
|
|
/** @var Processor */ |
22
|
|
|
private $processor; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->driverFactory = $this->prophesize(DriverFactory::class); |
27
|
|
|
$this->processor = new Processor($this->driverFactory->reveal()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
* @dataProvider processExamples |
33
|
|
|
*/ |
34
|
|
|
public function it_can_process_tasks( |
35
|
|
|
$success, |
36
|
|
|
array $validationErrors, |
37
|
|
|
array $runErrors |
38
|
|
|
) |
39
|
|
|
{ |
40
|
|
|
/** @var TaskDriverInterface|ObjectProphecy $driver */ |
41
|
|
|
$driver = $this->prophesize(TaskDriverInterface::class); |
42
|
|
|
|
43
|
|
|
/** @var TaskTypeInterface|ObjectProphecy $task */ |
44
|
|
|
$task = $this->prophesize(TaskTypeInterface::class); |
45
|
|
|
|
46
|
|
|
//Try for a driver |
47
|
|
|
$driver->canRun($task->reveal())->willReturn($validationErrors); |
|
|
|
|
48
|
|
|
$driver->run($task->reveal())->willReturn($runErrors); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->driverFactory->getDriverForTask($task->reveal())->willReturn($driver->reveal()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$result = $this->processor->process($task->reveal()); |
53
|
|
|
$this->assertInstanceOf(TaskResult::class, $result); |
54
|
|
|
$this->assertEquals($success, $result->isSuccess()); |
55
|
|
|
|
56
|
|
|
if (count($validationErrors) > 0) { |
57
|
|
|
$this->assertEquals($validationErrors, $result->getErrors()); |
58
|
|
|
} else { |
59
|
|
|
$this->assertEquals($runErrors, $result->getErrors()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function processExamples() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
[true, [], []], |
67
|
|
|
[false, ['error'], []], |
68
|
|
|
[false, [], ['error']], |
69
|
|
|
[false, ['error'], ['error']] |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
*/ |
76
|
|
|
public function it_handles_an_unsupported_task() |
77
|
|
|
{ |
78
|
|
|
/** @var TaskTypeInterface|ObjectProphecy $task */ |
79
|
|
|
$task = $this->prophesize(TaskTypeInterface::class); |
80
|
|
|
|
81
|
|
|
//Try for a driver that doesn't exist |
82
|
|
|
$this->driverFactory->getDriverForTask($task->reveal())->willThrow(TaskException::class); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$result = $this->processor->process($task->reveal()); |
85
|
|
|
$this->assertInstanceOf(TaskResult::class, $result); |
86
|
|
|
$this->assertEquals(false, $result->isSuccess()); |
87
|
|
|
$this->assertCount(1, $result->getErrors()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: