Skip to content

Commit

Permalink
Reset captured. Test "run" as array. Implement "toString"
Browse files Browse the repository at this point in the history
* Reset captured commands.
* Test that a command passed as an array is executed.
* Implemented the "toString" method that returns the current (or given) command as a string.
  • Loading branch information
TitasGailius authored Apr 9, 2020
1 parent c2b5f27 commit 502c94e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ protected function getSeconds($timeout)
return $timeout;
}

/**
* Get the current command as string.
*
* @param string|array|null $command
* @return string
*/
public function toString($command = null)
{
if (! is_null($command)) {
$this->command($command);
}

if (is_string($this->command)) {
return $this->command;
}

return $this->process()->getCommandLine();
}

/**
* Dynamically forward calls to the process instance.
*
Expand Down
28 changes: 18 additions & 10 deletions src/Fakes/BuilderFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public static function capture(Builder $builder)
static::$captured[] = $builder;
}

/**
* Set captured commands.
*
* @param array $captured
* @return void
*/
public static function setCaptured(array $captured)
{
static::$captured = $captured;
}

/**
* Set fake commands.
*
Expand Down Expand Up @@ -91,11 +102,7 @@ protected function runProcess(Process $process)
{
Terminal::capture($this);

// if (static::shouldRun($process->getCommandLine())) {
// return parent::runProcess($process);
// }

return static::$commands[$this->command] ?? Terminal::response();
return static::$commands[$this->toString()] ?? Terminal::response();
}

/**
Expand All @@ -106,13 +113,14 @@ protected function runProcess(Process $process)
*/
public static function assertExecuted($command, int $times = 1)
{
$count = count(array_filter(static::$captured, is_callable($command) ?: function ($captured) use ($command) {
return $captured->getCommand() == $command;
}));
$filter = is_callable($command) ? $command : function ($captured) use ($command) {
return $captured->toString() == Terminal::toString($command);
};

$count = count(array_filter(static::$captured, $filter));

Assert::assertTrue($count === $times, sprintf(
'The expected command [%s] was executed %s times instead of %s times.',
$command, $count, $times
'The command was executed %s times instead of expected %s times.', $count, $times
));
}
}
3 changes: 2 additions & 1 deletion src/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function reset()
static::$fake = false;

Fakes\BuilderFake::setCommands([]);
Fakes\BuilderFake::setCaptured([]);
}

/**
Expand Down Expand Up @@ -129,4 +130,4 @@ public static function __callStatic(string $method, array $parameters)
{
return call_user_func([static::builder(), $method], ...$parameters);
}
}
}
37 changes: 36 additions & 1 deletion tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ public function testExecute()
$this->assertEquals($builder->process(), $response->process());
}

/**
* Test that the terminal can execute array commands.
*
* @return void
*/
public function testExecuteArrayCommands()
{
$response = (new Builder)->run(['echo', 'Hello, World']);

$this->assertEquals("Hello, World\n", (string) $response);
}

/**
* Test that "execute" method starts the process.
*
Expand Down Expand Up @@ -259,6 +271,29 @@ public function testRetry()
->execute('echo Hello, World');
}

/**
* Test that terminal can convert commands to string.
*
* @return void
*/
public function testToString()
{
$this->assertEquals(
'\'echo\' \'Hello, World\'',
(new Builder)->toString(['echo', 'Hello, World'])
);

$this->assertEquals(
'\'echo\' \'Hello, World\'',
(new Builder)->command(['echo', 'Hello, World'])->toString()
);

$this->assertEquals(
'echo "Hello, World"',
(new Builder)->toString('echo "Hello, World"')
);
}

/**
* Create a new builder instance with a mocked process instance.
*
Expand All @@ -285,4 +320,4 @@ protected function builderWithMockedProccess($mocker = null)

return $builder;
}
}
}
33 changes: 32 additions & 1 deletion tests/FakeTerminalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ public function testCaptureAndAssertExecuted()
Terminal::assertExecuted($expected);
}

/**
* Test that Terminal can capture and assert executions using a custom filter.
*
* @return void
*/
public function testCaptureAndAssertExecutedUsingCustomFilter()
{
Terminal::fake();

Terminal::execute($expected = 'echo "Hello, World"');

Terminal::assertExecuted(function ($captured) use ($expected) {
return $captured->toString() == $expected;
});
}

public function testResponseLinesAsStrings()
{
Terminal::fake([
Expand Down Expand Up @@ -113,4 +129,19 @@ public function testEmptyResponse()
->assertEmpty()
->assertOk();
}
}

public function testCaptureArrayCommands()
{
$expected = ['rm', '-rf', 'vendor'];

Terminal::fake([
Terminal::toString($expected) => 'success',
]);

$response = Terminal::run($expected);

Terminal::assertExecuted($expected);

$this->assertEquals('success', $response->output());
}
}
2 changes: 1 addition & 1 deletion tests/TerminalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ public function testBuilder()
{
$this->assertEquals(new Builder, Terminal::builder());
}
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ protected function tearDown(): void
Mockery::close();
Terminal::reset();
}
}
}

0 comments on commit 502c94e

Please sign in to comment.