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 joy; |
9
|
|
|
|
10
|
|
|
use Deployer\Deployer; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Symfony\Component\Console\Application; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Tester\ApplicationTester; |
15
|
|
|
|
16
|
|
|
use const __TEMP_DIR__; |
17
|
|
|
|
18
|
|
|
abstract class JoyTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ApplicationTester |
22
|
|
|
*/ |
23
|
|
|
protected $tester; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Deployer |
27
|
|
|
*/ |
28
|
|
|
protected $deployer; |
29
|
|
|
|
30
|
|
|
public static function setUpBeforeClass(): void |
31
|
|
|
{ |
32
|
|
|
self::cleanUp(); |
33
|
|
|
mkdir(__TEMP_DIR__); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function tearDownAfterClass(): void |
37
|
|
|
{ |
38
|
|
|
self::cleanUp(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected static function cleanUp() |
42
|
|
|
{ |
43
|
|
|
if (is_dir(__TEMP_DIR__)) { |
44
|
|
|
exec('rm -rf ' . __TEMP_DIR__); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function init(string $recipe) |
49
|
|
|
{ |
50
|
|
|
$console = new Application(); |
51
|
|
|
$console->setAutoExit(false); |
52
|
|
|
$this->tester = new ApplicationTester($console); |
53
|
|
|
|
54
|
|
|
$this->deployer = new Deployer($console); |
55
|
|
|
$this->deployer->importer->import($recipe); |
56
|
|
|
$this->deployer->init(); |
57
|
|
|
$this->deployer->config->set('deploy_path', __TEMP_DIR__ . '/{{hostname}}'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function dep(string $task, array $args = []): int |
61
|
|
|
{ |
62
|
|
|
$recipe = __TEMP_DIR__ . '/' . get_called_class() . '.php'; |
63
|
|
|
file_put_contents($recipe, $this->recipe()); |
64
|
|
|
$this->init($recipe); |
65
|
|
|
return $this->tester->run(array_merge([ |
66
|
|
|
$task, |
67
|
|
|
'selector' => 'all', |
68
|
|
|
'--file' => $recipe, |
69
|
|
|
'--limit' => 1, |
70
|
|
|
], $args), [ |
71
|
|
|
'verbosity' => OutputInterface::VERBOSITY_VERBOSE, |
72
|
|
|
'interactive' => false, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
abstract protected function recipe(): string; |
77
|
|
|
} |
78
|
|
|
|