Skip to content

Commit

Permalink
add test for PhpReaderEntryPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i committed Jul 31, 2020
1 parent 401eade commit 765fc73
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
11 changes: 10 additions & 1 deletion config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

declare(strict_types=1);

use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreator;
use PhpProfiler\Inspector\Daemon\Reader\Context\PhpReaderContextCreatorInterface;
use PhpProfiler\Inspector\Daemon\Reader\PhpReaderTask;
use PhpProfiler\Inspector\Daemon\Reader\PhpReaderTaskInterface;
use PhpProfiler\Lib\Amphp\ContextCreator;
use PhpProfiler\Lib\Amphp\ContextCreatorInterface;
use PhpProfiler\Lib\ByteStream\IntegerByteSequence\IntegerByteSequenceReader;
use PhpProfiler\Lib\ByteStream\IntegerByteSequence\LittleEndianReader;
use PhpProfiler\Lib\Elf\SymbolResolver\Elf64SymbolResolverCreator;
Expand All @@ -32,5 +37,9 @@
SymbolResolverCreatorInterface::class => autowire(Elf64SymbolResolverCreator::class),
FileReaderInterface::class => autowire(CatFileReader::class),
IntegerByteSequenceReader::class => autowire(LittleEndianReader::class),
ContextCreator::class => autowire()->constructorParameter('di_config_file', __DIR__ . '/di.php'),
ContextCreator::class => autowire()
->constructorParameter('di_config_file', __DIR__ . '/di.php'),
ContextCreatorInterface::class => autowire(ContextCreator::class)
->constructorParameter('di_config_file', __DIR__ . '/di.php'),
PhpReaderTaskInterface::class => autowire(PhpReaderTask::class),
];
6 changes: 3 additions & 3 deletions src/Inspector/Daemon/Reader/Context/PhpReaderEntryPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
use PhpProfiler\Inspector\Daemon\Reader\Message\AttachMessage;
use PhpProfiler\Inspector\Daemon\Reader\Message\SetSettingsMessage;
use PhpProfiler\Inspector\Settings\TargetProcessSettings;
use PhpProfiler\Inspector\Daemon\Reader\PhpReaderTask;
use PhpProfiler\Inspector\Daemon\Reader\PhpReaderTaskInterface;
use PhpProfiler\Lib\Amphp\ContextEntryPointInterface;
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderException;

final class PhpReaderEntryPoint implements ContextEntryPointInterface
{
private PhpReaderTask $php_reader_task;
private PhpReaderTaskInterface $php_reader_task;

public function __construct(PhpReaderTask $php_reader_task)
public function __construct(PhpReaderTaskInterface $php_reader_task)
{
$this->php_reader_task = $php_reader_task;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Inspector/Daemon/Reader/PhpReaderTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use PhpProfiler\Lib\PhpProcessReader\PhpGlobalsFinder;
use PhpProfiler\Lib\PhpProcessReader\PhpMemoryReader\ExecutorGlobalsReader;

final class PhpReaderTask
final class PhpReaderTask implements PhpReaderTaskInterface
{
private PhpGlobalsFinder $php_globals_finder;
private ExecutorGlobalsReader $executor_globals_reader;
Expand Down
32 changes: 32 additions & 0 deletions src/Inspector/Daemon/Reader/PhpReaderTaskInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Inspector\Daemon\Reader;

use Amp\Parallel\Sync\Channel;
use Generator;
use PhpProfiler\Inspector\Settings\GetTraceSettings;
use PhpProfiler\Inspector\Settings\TargetPhpSettings;
use PhpProfiler\Inspector\Settings\TargetProcessSettings;
use PhpProfiler\Inspector\Settings\TraceLoopSettings;

interface PhpReaderTaskInterface
{
public function run(
Channel $channel,
TargetProcessSettings $target_process_settings,
TraceLoopSettings $loop_settings,
TargetPhpSettings $target_php_settings,
GetTraceSettings $get_trace_settings
): Generator;
}
116 changes: 116 additions & 0 deletions tests/Inspector/Daemon/Reader/Context/PhpReaderEntryPointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

/**
* This file is part of the sj-i/php-profiler package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpProfiler\Inspector\Daemon\Reader\Context;

use Amp\Parallel\Sync\Channel;
use Amp\Success;
use Hamcrest\Matchers;
use Mockery;
use PhpProfiler\Inspector\Daemon\Dispatcher\Message\DetachWorkerMessage;
use PhpProfiler\Inspector\Daemon\Reader\Message\AttachMessage;
use PhpProfiler\Inspector\Daemon\Reader\Message\SetSettingsMessage;
use PhpProfiler\Inspector\Daemon\Reader\PhpReaderTaskInterface;
use PhpProfiler\Inspector\Settings\GetTraceSettings;
use PhpProfiler\Inspector\Settings\TargetPhpSettings;
use PhpProfiler\Inspector\Settings\TargetProcessSettings;
use PhpProfiler\Inspector\Settings\TraceLoopSettings;
use PHPUnit\Framework\TestCase;

class PhpReaderEntryPointTest extends TestCase
{
public function testRun(): void
{
$php_reader_task = Mockery::mock(PhpReaderTaskInterface::class);
$channel = Mockery::mock(Channel::class);
$channel->expects()->receive()->andReturns(new Success(1))->once();
$channel->expects()->receive()->andReturns(new Success(2))->once();
$php_reader_task->shouldReceive('run')
->withArgs(
function (
$channel_passed,
$target_process_settings,
$trace_loop_serrings,
$target_php_settings,
$get_trace_settings
) use ($channel) {
$this->assertSame($channel, $channel_passed);
$this->assertEquals(
[
new TargetProcessSettings(123),
new TraceLoopSettings(1, 'q', 10),
new TargetPhpSettings(),
new GetTraceSettings(PHP_INT_MAX),
],
[
$target_process_settings,
$trace_loop_serrings,
$target_php_settings,
$get_trace_settings,
]
);
return true;
}
)
->andReturns(
(function () {
yield new Success(3);
yield new Success(4);
yield new Success(5);
})()
)
;
$channel->shouldReceive('send')
->with(Matchers::equalTo(new DetachWorkerMessage(123)))
->andReturns(new Success(6))
->once();

$php_reader_entry_point = new PhpReaderEntryPoint($php_reader_task);

$generator = $php_reader_entry_point->run($channel);

$promise = $generator->current();
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(1, $value));

$promise = $generator->send(
new SetSettingsMessage(
new TargetPhpSettings(),
new TraceLoopSettings(1, 'q', 10),
new GetTraceSettings(PHP_INT_MAX)
)
);
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(1, $value));

$promise = $generator->send(new AttachMessage(123));
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(2, $value));

$promise = $generator->send(null);
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(3, $value));

$promise = $generator->send(null);
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(4, $value));

$promise = $generator->send(null);
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(5, $value));

$promise = $generator->send(null);
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(fn ($error, $value) => $this->assertSame(6, $value));
}
}

0 comments on commit 765fc73

Please sign in to comment.