Completed
Push — master ( cfc46b...5a6c91 )
by Thomas
09:47 queued 07:37
created

SeparateProcessExecutorTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 16
nc 1
nop 0
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\Executor;
13
14
use Symfony\Component\Process\Process;
15
use Task\Execution\TaskExecutionInterface;
16
use Task\Executor\FailedException;
17
use Task\Executor\RetryTaskHandlerInterface;
18
use Task\Handler\TaskHandlerFactoryInterface;
19
use Task\Handler\TaskHandlerInterface;
20
use Task\Storage\TaskExecutionRepositoryInterface;
21
use Task\TaskBundle\Executor\ExecutionProcessFactory;
22
use Task\TaskBundle\Executor\SeparateProcessException;
23
use Task\TaskBundle\Executor\SeparateProcessExecutor;
24
25
class SeparateProcessExecutorTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var TaskHandlerFactoryInterface
29
     */
30
    private $handlerFactory;
31
32
    /**
33
     * @var TaskExecutionRepositoryInterface
34
     */
35
    private $executionRepository;
36
37
    /**
38
     * @var ExecutionProcessFactory
39
     */
40
    private $processFactory;
41
42
    /**
43
     * @var SeparateProcessExecutor
44
     */
45
    private $executor;
46
47
    /**
48
     * @var TaskHandlerInterface
49
     */
50
    private $handler;
51
52
    /**
53
     * @var RetryTaskHandlerInterface
54
     */
55
    private $retryHandler;
56
57
    /**
58
     * @var TaskExecutionInterface
59
     */
60
    private $execution;
61
62
    /**
63
     * @var Process
64
     */
65
    private $process;
66
67
    protected function setUp()
68
    {
69
        $this->handlerFactory = $this->prophesize(TaskHandlerFactoryInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...actoryInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Handler\TaskHandlerFactoryInterface> of property $handlerFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
        $this->executionRepository = $this->prophesize(TaskExecutionRepositoryInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...sitoryInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Storage\Task...ionRepositoryInterface> of property $executionRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        $this->processFactory = $this->prophesize(ExecutionProcessFactory::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...nProcessFactory::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\TaskBundle\E...xecutionProcessFactory> of property $processFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
73
        $this->executor = new SeparateProcessExecutor(
74
            $this->handlerFactory->reveal(), $this->executionRepository->reveal(), $this->processFactory->reveal()
75
        );
76
77
        $this->handler = $this->prophesize(TaskHandlerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...andlerInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Handler\TaskHandlerInterface> of property $handler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
        $this->retryHandler = $this->prophesize(TaskHandlerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...andlerInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Executor\RetryTaskHandlerInterface> of property $retryHandler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
        $this->retryHandler->willImplement(RetryTaskHandlerInterface::class);
80
81
        $this->handlerFactory->create('TaskHandler')->willReturn($this->handler->reveal());
82
        $this->handlerFactory->create('RetryTaskHandler')->willReturn($this->retryHandler->reveal());
83
84
        $this->execution = $this->prophesize(TaskExecutionInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Task\...cutionInterface::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Task\Execution\TaskExecutionInterface> of property $execution.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
        $this->execution->getUuid()->willReturn('123-123-123');
86
        $this->execution->getAttempts()->willReturn(1);
87
88
        $this->process = $this->prophesize(Process::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Symfo...Process\Process::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Symfony\Component\Process\Process> of property $process.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
89
        $this->processFactory->create('123-123-123')->willReturn($this->process->reveal());
90
    }
91
92
    public function testExecute()
93
    {
94
        $this->execution->getHandlerClass()->willReturn('TaskHandler');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->execution->getHandlerClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
95
96
        $this->process->run()->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->process->run() (of type integer|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
97
        $this->process->isSuccessful()->willReturn(true);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->isSuccessful() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
98
99
        $this->process->getOutput()->willReturn('TEST');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->getOutput() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
100
101
        $result = $this->executor->execute($this->execution->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        $this->assertEquals('TEST', $result);
103
    }
104
105
    public function testExecuteException()
106
    {
107
        $this->execution->getHandlerClass()->willReturn('TaskHandler');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->execution->getHandlerClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
108
109
        $this->process->run()->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->process->run() (of type integer|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
110
        $this->process->isSuccessful()->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->isSuccessful() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
111
112
        $this->process->getErrorOutput()->willReturn('TEST');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->getErrorOutput() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
113
114
        try {
115
            $this->executor->execute($this->execution->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
117
            $this->fail('No FailedException was thrown');
118
        } catch (\Exception $exception) {
119
            $this->assertInstanceOf(FailedException::class, $exception);
120
            $this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious());
121
            $this->assertEquals('TEST', $exception->getPrevious()->__toString());
122
        }
123
    }
124
125
    public function testExecuteFailedException()
126
    {
127
        $this->execution->getHandlerClass()->willReturn('TaskHandler');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->execution->getHandlerClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
128
129
        $this->process->run()->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled cannot be called on $this->process->run() (of type integer|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
130
        $this->process->isSuccessful()->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->isSuccessful() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
131
132
        $this->process->getErrorOutput()->willReturn(FailedException::class . PHP_EOL . 'TEST');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->getErrorOutput() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
133
134
        try {
135
            $this->executor->execute($this->execution->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
137
            $this->fail('No FailedException was thrown');
138
        } catch (\Exception $exception) {
139
            $this->assertInstanceOf(FailedException::class, $exception);
140
            $this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious());
141
            $this->assertEquals('TEST', $exception->getPrevious()->__toString());
142
        }
143
    }
144
145
    public function testExecuteRetryFailedException()
146
    {
147
        $this->execution->getHandlerClass()->willReturn('RetryTaskHandler');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->execution->getHandlerClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
148
        $this->retryHandler->getMaximumAttempts()->willReturn(3);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->retryHandler->getMaximumAttempts() (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
150
        $this->execution->incrementAttempts()->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldNotBeCalled() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
152
        $this->process->run()->shouldBeCalledTimes(1);
0 ignored issues
show
Bug introduced by
The method shouldBeCalledTimes cannot be called on $this->process->run() (of type integer|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
153
        $this->process->isSuccessful()->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->isSuccessful() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
154
155
        $this->process->getErrorOutput()->willReturn(FailedException::class . PHP_EOL . 'TEST');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->getErrorOutput() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
156
157
        try {
158
            $this->executor->execute($this->execution->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
160
            $this->fail('No FailedException was thrown');
161
        } catch (\Exception $exception) {
162
            $this->assertInstanceOf(FailedException::class, $exception);
163
            $this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious());
164
            $this->assertEquals('TEST', $exception->getPrevious()->__toString());
165
        }
166
    }
167
168
    public function testExecuteRetryException()
169
    {
170
        $this->execution->getHandlerClass()->willReturn('RetryTaskHandler');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->execution->getHandlerClass() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
171
        $this->retryHandler->getMaximumAttempts()->willReturn(3);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->retryHandler->getMaximumAttempts() (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
172
173
        $attempts = 1;
174
        $this->execution->incrementAttempts()->will(
0 ignored issues
show
Bug introduced by
The method will() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
            function () use (&$attempts) {
176
                ++$attempts;
177
178
                return $this;
179
            }
180
        );
181
        $this->execution->getAttempts()->will(
0 ignored issues
show
Bug introduced by
The method will cannot be called on $this->execution->getAttempts() (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
182
            function () use (&$attempts) {
183
                return $attempts;
184
            }
185
        );
186
187
        $this->executionRepository->save($this->execution->reveal())->shouldBeCalled(2);
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method shouldBeCalled() does not seem to exist on object<Task\Storage\Task...ionRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188
189
        $this->process->run()->shouldBeCalledTimes(3);
0 ignored issues
show
Bug introduced by
The method shouldBeCalledTimes cannot be called on $this->process->run() (of type integer|null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
190
        $this->process->isSuccessful()->willReturn(false);
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->isSuccessful() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
191
192
        $this->process->getErrorOutput()->willReturn('TEST');
0 ignored issues
show
Bug introduced by
The method willReturn cannot be called on $this->process->getErrorOutput() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
193
194
        try {
195
            $this->executor->execute($this->execution->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Task\Execution\TaskExecutionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
197
            $this->fail('No FailedException was thrown');
198
        } catch (\Exception $exception) {
199
            $this->assertInstanceOf(FailedException::class, $exception);
200
            $this->assertInstanceOf(SeparateProcessException::class, $exception->getPrevious());
201
            $this->assertEquals('TEST', $exception->getPrevious()->__toString());
202
            $this->assertEquals(3, $attempts);
203
        }
204
    }
205
}
206