Skip to content

Commit 70248b7

Browse files
committed
Separate timer initialization from result collection
1 parent faa84a1 commit 70248b7

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

recipe/timer.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,15 @@
33

44
namespace Deployer;
55

6-
use Deployer\Task\Task;
76
use IntegerNet\DeployerTimer\DecorateAllTasks;
7+
use IntegerNet\DeployerTimer\ResultTaskFactory;
88
use IntegerNet\DeployerTimer\TimerTaskDecorator;
99

10-
function timerCsv(string $fileName)
10+
function timer(): ResultTaskFactory
1111
{
1212
$deployer = Deployer::get();
1313
$decorateAllTasks = new DecorateAllTasks($deployer);
1414
$timer = new TimerTaskDecorator();
1515
$decorateAllTasks->with($timer);
16-
$tasks = $deployer->tasks->toArray();
17-
/** @var Task $lastTask */
18-
$lastTask = array_pop($tasks);
19-
$collectResultTask = uniqid('timer_result-', true);
20-
task(
21-
$collectResultTask, function() use ($timer, $fileName) {
22-
file_put_contents($fileName, $timer->resultsAsCsv());
23-
});
24-
after($lastTask->getName(), $collectResultTask);
25-
}
16+
return new ResultTaskFactory($deployer, $timer);
17+
}

src/ResultTaskFactory.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
use Deployer\Deployer;
7+
use Deployer\Task\Task;
8+
9+
class ResultTaskFactory
10+
{
11+
/**
12+
* @var TimerTaskDecorator
13+
*/
14+
private $timer;
15+
/**
16+
* @var Deployer
17+
*/
18+
private $deployer;
19+
20+
public function __construct(Deployer $deployer, TimerTaskDecorator $timer)
21+
{
22+
$this->deployer = $deployer;
23+
$this->timer = $timer;
24+
}
25+
26+
public function createCsvResultTask($fileName): string
27+
{
28+
$taskName = uniqid('timer_result-', true);
29+
$taskBody = function () use ($fileName) {
30+
file_put_contents($fileName, $this->timer->resultsAsCsv());
31+
};
32+
$this->deployer->tasks->set($taskName, new Task($taskName, $taskBody));
33+
34+
return $taskName;
35+
}
36+
}

tests/RecipeTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
namespace IntegerNet\DeployerTimer;
55

6-
use Deployer\Deployer;
76
use PHPUnit\Framework\TestCase;
8-
use Symfony\Component\Process\Process;
97

108
class RecipeTest extends TestCase
119
{
@@ -31,7 +29,7 @@ protected function tearDown(): void
3129
}
3230
}
3331

34-
public function testTimer()
32+
public function testTimerWithCsvResult()
3533
{
3634
$recipeFile = __DIR__ . '/../recipe/timer.php';
3735
$csvFile = $this->createTmpFile();
@@ -45,7 +43,7 @@ public function testTimer()
4543
4644
task('test', function() { writeln('Test Output');});
4745
48-
timerCsv('{$csvFile}');
46+
after('test', timer()->createCsvResultTask('{$csvFile}'));
4947
PHP
5048
);
5149
exec('vendor/bin/dep --file=' . $this->deployFile . ' test', $output);

0 commit comments

Comments
 (0)