|
1
|
|
|
<?php |
|
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Deployer\Task; |
|
9
|
|
|
|
|
10
|
|
|
use Deployer\Host\Host; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
use function Deployer\invoke; |
|
14
|
|
|
use function Deployer\task; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class TaskTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
protected function tearDown(): void |
|
19
|
|
|
{ |
|
20
|
|
|
StubTask::$runned = 0; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testTask() |
|
24
|
|
|
{ |
|
25
|
|
|
$mock = self::getMockBuilder('stdClass') |
|
|
|
|
|
|
26
|
|
|
->addMethods(['callback']) |
|
27
|
|
|
->getMock(); |
|
28
|
|
|
$mock |
|
29
|
|
|
->expects(self::exactly(1)) |
|
|
|
|
|
|
30
|
|
|
->method('callback'); |
|
31
|
|
|
|
|
32
|
|
|
$task = new Task('task_name', function () use ($mock) { |
|
33
|
|
|
$mock->callback(); |
|
34
|
|
|
}); |
|
35
|
|
|
|
|
36
|
|
|
$context = self::getMockBuilder(Context::class) |
|
37
|
|
|
->disableOriginalConstructor() |
|
38
|
|
|
->getMock(); |
|
39
|
|
|
$task->run($context); |
|
40
|
|
|
|
|
41
|
|
|
self::assertEquals('task_name', $task->getName()); |
|
42
|
|
|
|
|
43
|
|
|
$task->desc('Task description.'); |
|
44
|
|
|
self::assertEquals('Task description.', $task->getDescription()); |
|
45
|
|
|
|
|
46
|
|
|
$task->hidden(); |
|
47
|
|
|
self::assertTrue($task->isHidden()); |
|
48
|
|
|
|
|
49
|
|
|
$task->once(); |
|
50
|
|
|
self::assertTrue($task->isOnce()); |
|
51
|
|
|
|
|
52
|
|
|
$task->oncePerNode(); |
|
53
|
|
|
self::assertTrue($task->isOncePerNode()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testInit() |
|
57
|
|
|
{ |
|
58
|
|
|
$context = self::getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
// Test create task with [$object, 'method'] |
|
61
|
|
|
$mock1 = self::getMockBuilder('stdClass') |
|
|
|
|
|
|
62
|
|
|
->addMethods(['callback']) |
|
63
|
|
|
->getMock(); |
|
64
|
|
|
$mock1 |
|
65
|
|
|
->expects(self::once()) |
|
|
|
|
|
|
66
|
|
|
->method('callback'); |
|
67
|
|
|
$task1 = new Task('task1', [$mock1, 'callback']); |
|
68
|
|
|
$task1->run($context); |
|
69
|
|
|
|
|
70
|
|
|
// Test create task with anonymous functions |
|
71
|
|
|
$mock2 = self::getMockBuilder('stdClass') |
|
|
|
|
|
|
72
|
|
|
->addMethods(['callback']) |
|
73
|
|
|
->getMock(); |
|
74
|
|
|
$mock2 |
|
75
|
|
|
->expects(self::once()) |
|
76
|
|
|
->method('callback'); |
|
77
|
|
|
$task2 = new Task('task2', function () use ($mock2) { |
|
78
|
|
|
$mock2->callback(); |
|
79
|
|
|
}); |
|
80
|
|
|
$task2->run($context); |
|
81
|
|
|
|
|
82
|
|
|
self::assertEquals(0, StubTask::$runned); |
|
83
|
|
|
$task3 = new Task('task3', new StubTask()); |
|
84
|
|
|
$task3->run($context); |
|
85
|
|
|
self::assertEquals(1, StubTask::$runned); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testGroupInvoke(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$spy = new StubTask(); |
|
91
|
|
|
|
|
92
|
|
|
task('foo', $spy); |
|
93
|
|
|
task('bar', $spy); |
|
94
|
|
|
task('group', ['foo', 'bar']); |
|
95
|
|
|
|
|
96
|
|
|
(new Task('group:invoke', function () { |
|
97
|
|
|
invoke('group'); |
|
98
|
|
|
}))->run(new Context(new Host('localhost'))); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertSame(2, StubTask::$runned); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Stub class for task callable by __invoke() |
|
106
|
|
|
*/ |
|
107
|
|
|
class StubTask |
|
108
|
|
|
{ |
|
109
|
|
|
public static $runned = 0; |
|
110
|
|
|
|
|
111
|
|
|
public function __invoke() |
|
112
|
|
|
{ |
|
113
|
|
|
self::$runned++; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: