Skip to content

Commit 9d682b5

Browse files
committed
Add support in DummyExecute to report the last executed command
That way we'll be able, in all the test using DummyExecute, to perform assertions against the commands executed, so we can cover options logic and so on.
1 parent d313896 commit 9d682b5

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

tests/Fake/Process/DummyExecute.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
class DummyExecute extends Execute
1919
{
2020
public string $returnOutput = '';
21+
public string $lastCmd = ''; // We need this for assertions against the command run.
2122

22-
private function getMockProcess($cmd)
23+
private function getMockProcess(string $cmd)
2324
{
2425
$process = \Mockery::mock('Symfony\Component\Process\Process');
2526
$process->shouldReceive(
@@ -43,22 +44,12 @@ private function getMockProcess($cmd)
4344

4445
public function run($cmd, ?string $error = null): Process
4546
{
46-
if ($cmd instanceof Process) {
47-
// Get the command line from process.
48-
$cmd = $cmd->getCommandLine();
49-
}
50-
51-
return $this->helper->run($this->output, $this->getMockProcess($cmd), $error);
47+
return $this->helper->run($this->output, $this->getMockProcess($this->getCommandLine($cmd)), $error);
5248
}
5349

5450
public function mustRun($cmd, ?string $error = null): Process
5551
{
56-
if ($cmd instanceof Process) {
57-
// Get the command line from process.
58-
$cmd = $cmd->getCommandLine();
59-
}
60-
61-
return $this->helper->mustRun($this->output, $this->getMockProcess($cmd), $error);
52+
return $this->helper->mustRun($this->output, $this->getMockProcess($this->getCommandLine($cmd)), $error);
6253
}
6354

6455
public function runAll(array $processes): void
@@ -73,15 +64,29 @@ public function mustRunAll(array $processes): void
7364

7465
public function passThrough(array $commandline, ?string $cwd = null, ?float $timeout = null): Process
7566
{
76-
return $this->passThroughProcess($this->getMockProcess($commandline));
67+
return $this->getMockProcess($this->getCommandLine($commandline));
7768
}
7869

7970
public function passThroughProcess(Process $process): Process
8071
{
81-
if ($process instanceof \Mockery\MockInterface) {
82-
return $process;
72+
return $this->getMockProcess($this->getCommandLine($process));
73+
}
74+
75+
/**
76+
* Helper function to get the command line from a Process object or array.
77+
*
78+
* @param Process|array $cmd the command to run and its arguments listed as different entities
79+
*
80+
* @return string the command to run in a shell
81+
*/
82+
private function getCommandLine($cmd): string
83+
{
84+
if (is_array($cmd)) {
85+
$this->lastCmd = (new Process($cmd))->getCommandLine();
86+
} else {
87+
$this->lastCmd = $cmd->getCommandLine();
8388
}
8489

85-
return $this->getMockProcess($process->getCommandLine());
90+
return $this->lastCmd;
8691
}
8792
}

tests/MoodleTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class MoodleTestCase extends FilesystemTestCase
1616
{
1717
protected string $moodleDir;
1818
protected string $pluginDir;
19+
protected string $lastCmd = ''; // We need this for assertions against the command run.
1920

2021
protected function setUp(): void
2122
{
2223
parent::setUp();
2324

2425
$this->moodleDir = $this->tempDir;
2526
$this->pluginDir = $this->tempDir . '/local/ci';
27+
$this->lastCmd = '';
2628

2729
$this->fs->mirror(__DIR__ . '/Fixture/moodle', $this->moodleDir);
2830
$this->fs->mirror(__DIR__ . '/Fixture/moodle-local_ci', $this->pluginDir);

0 commit comments

Comments
 (0)