forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskTest.php
More file actions
125 lines (101 loc) · 3.11 KB
/
TaskTest.php
File metadata and controls
125 lines (101 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Task;
use Deployer\Deployer;
use Deployer\Host\Host;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\Input;
use Symfony\Component\Console\Output\Output;
use function Deployer\invoke;
use function Deployer\task;
interface MockableCallback
{
public function callback(): void;
}
class TaskTest extends TestCase
{
protected function setUp(): void
{
$console = new Application();
$deployer = new Deployer($console);
$deployer['input'] = $this->createStub(Input::class);
$deployer['output'] = $this->createStub(Output::class);
}
protected function tearDown(): void
{
StubTask::$runned = 0;
Deployer::resetInstance();
}
public function testTask()
{
$mock = $this->createMock(MockableCallback::class);
$mock
->expects(self::exactly(1))
->method('callback');
$task = new Task('task_name', function () use ($mock) {
$mock->callback();
});
$context = $this->createStub(Context::class);
$task->run($context);
self::assertEquals('task_name', $task->getName());
$task->desc('Task description.');
self::assertEquals('Task description.', $task->getDescription());
$task->hidden();
self::assertTrue($task->isHidden());
$task->once();
self::assertTrue($task->isOnce());
$task->oncePerNode();
self::assertTrue($task->isOncePerNode());
}
public function testInit()
{
$context = $this->createStub(Context::class);
// Test create task with [$object, 'method']
$mock1 = $this->createMock(MockableCallback::class);
$mock1
->expects(self::once())
->method('callback');
$task1 = new Task('task1', [$mock1, 'callback']);
$task1->run($context);
// Test create task with anonymous functions
$mock2 = $this->createMock(MockableCallback::class);
$mock2
->expects(self::once())
->method('callback');
$task2 = new Task('task2', function () use ($mock2) {
$mock2->callback();
});
$task2->run($context);
self::assertEquals(0, StubTask::$runned);
$task3 = new Task('task3', new StubTask());
$task3->run($context);
self::assertEquals(1, StubTask::$runned);
}
public function testGroupInvoke(): void
{
$spy = new StubTask();
task('foo', $spy);
task('bar', $spy);
task('group', ['foo', 'bar']);
(new Task('group:invoke', function () {
invoke('group');
}))->run(new Context(new Host('localhost')));
$this->assertSame(2, StubTask::$runned);
}
}
/**
* Stub class for task callable by __invoke()
*/
class StubTask
{
public static $runned = 0;
public function __invoke()
{
self::$runned++;
}
}