Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 65a2205

Browse files
committed
Modify output and tests
1 parent 87fe2ba commit 65a2205

File tree

6 files changed

+92
-21
lines changed

6 files changed

+92
-21
lines changed

src/Console/TinkerServerCommand.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Console\Command;
88
use Laravel\Tinker\ClassAliasAutoloader;
99
use BeyondCode\LaravelTinkerServer\Server;
10+
use Symfony\Component\Console\Output\BufferedOutput;
1011
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1112

1213
class TinkerServerCommand extends Command
@@ -15,20 +16,24 @@ class TinkerServerCommand extends Command
1516

1617
public function handle()
1718
{
18-
$this->createWarningFormatter();
19+
$output = $this->createWarningFormatter();
1920

20-
$server = new Server(config('laravel-tinker-server.host'), $this->createPsyShell(), $this->output);
21+
$server = new Server(config('laravel-tinker-server.host'), $this->createPsyShell(), $output);
2122

2223
$server->start();
2324
}
2425

25-
protected function createWarningFormatter()
26+
protected function createWarningFormatter(): BufferedOutput
2627
{
27-
if (! $this->output->getFormatter()->hasStyle('warning')) {
28+
$output = new BufferedOutput();
29+
30+
if (! $output->getFormatter()->hasStyle('warning')) {
2831
$style = new OutputFormatterStyle('yellow');
2932

30-
$this->output->getFormatter()->setStyle('warning', $style);
33+
$output->getFormatter()->setStyle('warning', $style);
3134
}
35+
36+
return $output;
3237
}
3338

3439
protected function createPsyShell()

src/Server.php

+15-9
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ class Server
2525
/** @var Shell */
2626
protected $shell;
2727

28-
/** @var OutputInterface */
28+
/** @var BufferedOutput */
2929
protected $output;
3030

31-
public function __construct($host, Shell $shell, OutputInterface $output, LoopInterface $loop = null)
32-
{
33-
$loop = $loop ?? Factory::create();
31+
/** @var Stdio */
32+
protected $stdio;
3433

34+
public function __construct($host, Shell $shell, BufferedOutput $output, LoopInterface $loop = null, Stdio $stdio = null)
35+
{
3536
$this->host = $host;
36-
$this->loop = $loop;
37+
$this->loop = $loop ?? Factory::create();
3738
$this->shell = $shell;
3839
$this->output = $output;
3940
$this->shellOutput = new BufferedOutput();
41+
$this->stdio = $stdio ?? $this->createStdio();
4042
}
4143

4244
public function start()
@@ -45,8 +47,6 @@ public function start()
4547

4648
$this->createSocketServer();
4749

48-
$this->createStdio();
49-
5050
$this->loop->run();
5151
}
5252

@@ -60,20 +60,22 @@ protected function createSocketServer()
6060

6161
$this->shell->setScopeVariables(array_merge($unserializedData, $this->shell->getScopeVariables()));
6262

63-
$this->output->write(PHP_EOL);
63+
$this->stdio->write(PHP_EOL);
6464

6565
collect($unserializedData)->keys()->map(function ($variableName) {
6666
$this->output->writeln('>> $'.$variableName);
6767

6868
$this->executeCode('$'.$variableName);
6969

7070
$this->output->write($this->shellOutput->fetch());
71+
72+
$this->stdio->write($this->output->fetch());
7173
});
7274
});
7375
});
7476
}
7577

76-
protected function createStdio()
78+
protected function createStdio(): Stdio
7779
{
7880
$stdio = new Stdio($this->loop);
7981

@@ -87,7 +89,11 @@ protected function createStdio()
8789
$this->executeCode($line);
8890

8991
$this->output->write(PHP_EOL.$this->shellOutput->fetch());
92+
93+
$this->stdio->write($this->output->fetch());
9094
});
95+
96+
return $stdio;
9197
}
9298

9399
protected function executeCode($code)

src/Shell/ExecutionClosure.php

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psy\Shell;
66
use Psy\Exception\BreakException;
77
use Psy\Exception\ErrorException;
8+
use Psy\CodeCleaner\NoReturnValue;
89
use Psy\Exception\ThrowUpException;
910
use Psy\Exception\TypeErrorException;
1011
use Psy\ExecutionClosure as BaseExecutionClosure;
@@ -19,6 +20,8 @@ public function __construct(Shell $__psysh__, $__line__)
1920
// Restore execution scope variables
2021
\extract($__psysh__->getScopeVariables(false), EXTR_SKIP);
2122

23+
$_ = new NoReturnValue();
24+
2225
$__psysh__->addCode($__line__);
2326

2427
// Convert all errors to exceptions

tests/ConnectionTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ public function testDump()
4040
//
4141
}
4242

43-
$this->assertSame('
44-
>> $i
45-
=> 10
46-
', $dumped);
43+
$this->assertSame("\r\e[K
44+
\r\e[K>> \$i
45+
=> 10\n", $dumped);
4746
}
4847

4948
/** @test */

tests/fixtures/EchoStream.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
class EchoStream implements \React\Stream\WritableStreamInterface
4+
{
5+
6+
public function on($event, callable $listener)
7+
{
8+
// TODO: Implement on() method.
9+
}
10+
11+
public function once($event, callable $listener)
12+
{
13+
// TODO: Implement once() method.
14+
}
15+
16+
public function removeListener($event, callable $listener)
17+
{
18+
// TODO: Implement removeListener() method.
19+
}
20+
21+
public function removeAllListeners($event = null)
22+
{
23+
// TODO: Implement removeAllListeners() method.
24+
}
25+
26+
public function listeners($event = null)
27+
{
28+
// TODO: Implement listeners() method.
29+
}
30+
31+
public function emit($event, array $arguments = [])
32+
{
33+
// TODO: Implement emit() method.
34+
}
35+
36+
public function isWritable()
37+
{
38+
return true;
39+
}
40+
public function write($data)
41+
{
42+
echo $data;
43+
}
44+
45+
public function end($data = null)
46+
{
47+
// TODO: Implement end() method.
48+
}
49+
public function close()
50+
{
51+
// TODO: Implement close() method.
52+
}
53+
}

tests/fixtures/server.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22

33
use Psy\Configuration;
44
use BeyondCode\LaravelTinkerServer\Server;
5-
use Symfony\Component\Console\Output\ConsoleOutput;
5+
use Symfony\Component\Console\Output\BufferedOutput;
66

77
$componentRoot = $_SERVER['COMPONENT_ROOT'] ?? __DIR__.'/../..';
88

99
$file = $componentRoot.'/vendor/autoload.php';
1010

1111
require $file;
12+
require $_SERVER['COMPONENT_ROOT'].'/tests/fixtures/EchoStream.php';
1213

13-
$output = new ConsoleOutput();
14+
$loop = \React\EventLoop\Factory::create();
15+
16+
$output = new BufferedOutput();
1417

1518
$config = new Configuration([
1619
'updateCheck' => 'never',
1720
]);
1821

22+
$stdio = new \Clue\React\Stdio\Stdio($loop, null, new \EchoStream());
23+
1924
$shell = new \Psy\Shell($config);
2025

21-
$server = new Server(getenv('TINKER_SERVER_HOST'), $shell, $output);
26+
$server = new Server(getenv('TINKER_SERVER_HOST'), $shell, $output, $loop, $stdio);
2227

2328
echo "READY\n";
2429

0 commit comments

Comments
 (0)