From 502c94e57c67b1f314243d742814d98ff34f90bc Mon Sep 17 00:00:00 2001 From: Titas Gailius Date: Thu, 9 Apr 2020 22:24:38 +0200 Subject: [PATCH] Reset captured. Test "run" as array. Implement "toString" * 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. --- src/Builder.php | 19 +++++++++++++++++++ src/Fakes/BuilderFake.php | 28 ++++++++++++++++++---------- src/Terminal.php | 3 ++- tests/BuilderTest.php | 37 ++++++++++++++++++++++++++++++++++++- tests/FakeTerminalTest.php | 33 ++++++++++++++++++++++++++++++++- tests/TerminalTest.php | 2 +- tests/TestCase.php | 2 +- 7 files changed, 109 insertions(+), 15 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index edd92c7..b668f29 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -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. * diff --git a/src/Fakes/BuilderFake.php b/src/Fakes/BuilderFake.php index b115947..da2dd0e 100644 --- a/src/Fakes/BuilderFake.php +++ b/src/Fakes/BuilderFake.php @@ -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. * @@ -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(); } /** @@ -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 )); } } diff --git a/src/Terminal.php b/src/Terminal.php index 42e35a1..977ff64 100644 --- a/src/Terminal.php +++ b/src/Terminal.php @@ -36,6 +36,7 @@ public static function reset() static::$fake = false; Fakes\BuilderFake::setCommands([]); + Fakes\BuilderFake::setCaptured([]); } /** @@ -129,4 +130,4 @@ public static function __callStatic(string $method, array $parameters) { return call_user_func([static::builder(), $method], ...$parameters); } -} \ No newline at end of file +} diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index a00304d..49003db 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -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. * @@ -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. * @@ -285,4 +320,4 @@ protected function builderWithMockedProccess($mocker = null) return $builder; } -} \ No newline at end of file +} diff --git a/tests/FakeTerminalTest.php b/tests/FakeTerminalTest.php index e7f283c..b22c1ff 100644 --- a/tests/FakeTerminalTest.php +++ b/tests/FakeTerminalTest.php @@ -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([ @@ -113,4 +129,19 @@ public function testEmptyResponse() ->assertEmpty() ->assertOk(); } -} \ No newline at end of file + + 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()); + } +} diff --git a/tests/TerminalTest.php b/tests/TerminalTest.php index 4e790a0..278c500 100644 --- a/tests/TerminalTest.php +++ b/tests/TerminalTest.php @@ -53,4 +53,4 @@ public function testBuilder() { $this->assertEquals(new Builder, Terminal::builder()); } -} \ No newline at end of file +} diff --git a/tests/TestCase.php b/tests/TestCase.php index e40de8d..74bcae1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,4 +22,4 @@ protected function tearDown(): void Mockery::close(); Terminal::reset(); } -} \ No newline at end of file +}