Skip to content

Commit fd1ae3d

Browse files
committed
Improve recipe test
1 parent 6bcf74e commit fd1ae3d

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<filter>
1616
<whitelist>
1717
<directory suffix=".php">src</directory>
18+
<directory suffix=".php">recipe</directory>
1819
</whitelist>
1920
</filter>
2021
<logging>

tests/RecipeTest.php

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
namespace IntegerNet\DeployerTimer;
55

6+
use Deployer\Console\Application;
7+
use Deployer\Deployer;
68
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Console\Input\ArgvInput;
10+
use Symfony\Component\Console\Input\ArrayInput;
11+
use Symfony\Component\Console\Output\BufferedOutput;
12+
use Symfony\Component\Console\Output\ConsoleOutput;
13+
use Symfony\Component\Process\Process;
714

815
class RecipeTest extends TestCase
916
{
@@ -29,10 +36,43 @@ protected function tearDown(): void
2936
}
3037
}
3138

39+
/**
40+
* Stable integration test
41+
*/
42+
public function testTimerWithCsvResultInSeparateProcess()
43+
{
44+
$csvFile = $this->createTmpFile();
45+
$this->givenDeployFileWithCsvResultTask($csvFile);
46+
$this->whenExecuted(['vendor/bin/dep', '--file=' . $this->deployFile, 'test'], $process);
47+
$this->thenTestTaskOutputShouldBeVisibleIn(explode("\n", $process->getOutput()));
48+
$this->andCsvFileShouldContainTestTaskResults($csvFile);
49+
}
50+
51+
/**
52+
* Same test that runs deployer directly, without exiting, to measure code coverage
53+
*/
3254
public function testTimerWithCsvResult()
3355
{
34-
$recipeFile = __DIR__ . '/../recipe/timer.php';
3556
$csvFile = $this->createTmpFile();
57+
$this->givenDeployFileWithCsvResultTask($csvFile);
58+
$this->whenDeployerRuns($output);
59+
$this->thenTestTaskOutputShouldBeVisibleIn(explode("\n", $output->fetch()));
60+
$this->andCsvFileShouldContainTestTaskResults($csvFile);
61+
}
62+
63+
private function createTmpFile()
64+
{
65+
$fileName = tempnam(sys_get_temp_dir(), 'integer-net-deployer-timer');
66+
if (!$fileName) {
67+
throw new \RuntimeException('Could not create temporary file');
68+
}
69+
$this->tmpFiles[] = $fileName;
70+
return $fileName;
71+
}
72+
73+
private function givenDeployFileWithCsvResultTask($csvFile): void
74+
{
75+
$recipeFile = __DIR__ . '/../recipe/timer.php';
3676
file_put_contents(
3777
$this->deployFile,
3878
<<<PHP
@@ -46,29 +86,49 @@ public function testTimerWithCsvResult()
4686
after('test', timer()->createCsvResultTask('{$csvFile}'));
4787
PHP
4888
);
49-
exec('vendor/bin/dep --file=' . $this->deployFile . ' test', $output);
89+
}
90+
91+
private function thenTestTaskOutputShouldBeVisibleIn(array $outputLines): void
92+
{
5093
$this->assertContains(
5194
'Test Output',
52-
$output,
53-
'Test task should be executed.' . "\n" .
54-
'Expected "Test Output"' . "\n" .
55-
'Actual Output: ' . print_r($output, true)
95+
$outputLines,
96+
'Test task should be executed.' . "\n" . 'Expected "Test Output"' . "\n" . 'Actual Output: ' . print_r(
97+
$outputLines,
98+
true
99+
)
56100
);
57-
$this->assertRegExp(<<<'REGEX'
101+
}
102+
103+
private function andCsvFileShouldContainTestTaskResults($csvFile): void
104+
{
105+
$this->assertRegExp(
106+
<<<'REGEX'
58107
{BEGIN,test,[\d.]+,[\d.]+
59108
END,test,[\d.]+,[\d.]+}
60109
REGEX
61110

62-
, (string)file_get_contents($csvFile), 'CSV file should be written with timer results');
111+
,
112+
(string)file_get_contents($csvFile),
113+
'CSV file should be written with timer results'
114+
);
115+
}
116+
117+
private function whenExecuted(array $command, ?Process &$process): void
118+
{
119+
$process = new Process($command);
120+
$process->run();
63121
}
64122

65-
private function createTmpFile()
123+
private function whenDeployerRuns(?BufferedOutput &$output): void
66124
{
67-
$fileName = tempnam(sys_get_temp_dir(), 'integer-net-deployer-timer');
68-
if (!$fileName) {
69-
throw new \RuntimeException('Could not create temporary file');
70-
}
71-
$this->tmpFiles[] = $fileName;
72-
return $fileName;
125+
$input = new ArrayInput(['test']);
126+
$output = new BufferedOutput();
127+
$console = new Application('Deployer', 'master');
128+
$console->setAutoExit(false);
129+
$deployer = new Deployer($console);
130+
require $this->deployFile;
131+
$deployer->init();
132+
$console->run($input, $output);
73133
}
74134
}

0 commit comments

Comments
 (0)