Skip to content

Commit

Permalink
add test for PhpSearcherEntryPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i committed Jul 31, 2020
1 parent cdfdd1d commit a6484fe
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PhpProfiler\Lib\Process\MemoryReader\MemoryReader;
use PhpProfiler\Lib\Process\MemoryReader\MemoryReaderInterface;

use PhpProfiler\Lib\Process\Search\ProcessSearcherInterface;
use function DI\autowire;

return [
Expand All @@ -42,4 +43,5 @@
ContextCreatorInterface::class => autowire(ContextCreator::class)
->constructorParameter('di_config_file', __DIR__ . '/di.php'),
PhpReaderTaskInterface::class => autowire(PhpReaderTask::class),
ProcessSearcherInterface::class => autowire(\PhpProfiler\Lib\Process\Search\ProcessSearcher::class),
];
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
use PhpProfiler\Inspector\Daemon\Dispatcher\Message\UpdateTargetProcessMessage;
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessList;
use PhpProfiler\Lib\Amphp\ContextEntryPointInterface;
use PhpProfiler\Lib\Process\Search\ProcessSearcher;
use PhpProfiler\Lib\Process\Search\ProcessSearcherInterface;

final class PhpSearcherEntryPoint implements ContextEntryPointInterface
{
private ProcessSearcher $process_searcher;
private ProcessSearcherInterface $process_searcher;

public function __construct(ProcessSearcher $process_searcher)
public function __construct(ProcessSearcherInterface $process_searcher)
{
$this->process_searcher = $process_searcher;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Process/Search/ProcessSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use PhpProfiler\Lib\Process\ProcFileSystem\CommandLineEnumerator;

final class ProcessSearcher
final class ProcessSearcher implements ProcessSearcherInterface
{
/**
* @param string $regex
Expand Down
23 changes: 23 additions & 0 deletions src/Lib/Process/Search/ProcessSearcherInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\Lib\Process\Search;

interface ProcessSearcherInterface
{
/**
* @param string $regex
* @return int[]
*/
public function searchByRegex(string $regex): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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\Searcher\Context;

use Amp\Parallel\Sync\Channel;
use Amp\Success;
use Mockery;
use PhpProfiler\Inspector\Daemon\Dispatcher\Message\UpdateTargetProcessMessage;
use PhpProfiler\Inspector\Daemon\Dispatcher\TargetProcessList;
use PhpProfiler\Lib\Process\Search\ProcessSearcherInterface;
use PHPUnit\Framework\TestCase;

class PhpSearcherEntryPointTest extends TestCase
{
public function testRun()
{
$channel = Mockery::mock(Channel::class);
$channel->expects()->receive()->andReturns(new Success(1))->once();
$channel->shouldReceive('send')
->withArgs(
function (UpdateTargetProcessMessage $message) {
$diff = $message->target_process_list->getDiff(
new TargetProcessList(1, 2, 3)
)->getArray();
$this->assertSame([], $diff);
return true;
}
)
->andReturns(
new Success(2),
new Success(3),
)
->once();

$process_searcher = Mockery::mock(ProcessSearcherInterface::class);
$process_searcher->expects()->searchByRegex('regex_to_search_process');

$entry_point = new PhpSearcherEntryPoint($process_searcher);

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

$promise = $generator->current();
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(function ($error, $value) use (&$result) {
$result = $value;
});
$this->assertSame(1, $result);

$promise = $generator->send('regex_to_search_process');
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(function ($error, $value) use (&$result) {
$result = $value;
});
$this->assertSame(2, $result);

$promise = $generator->send(null);
$this->assertInstanceOf(Success::class, $promise);
$promise->onResolve(function ($error, $value) use (&$result) {
$result = $value;
});
$this->assertSame(3, $result);
}
}

0 comments on commit a6484fe

Please sign in to comment.