Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Commit

Permalink
Extract method tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BladeMF committed May 8, 2021
1 parent b8f81ed commit 2190010
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class ExtractMethodCommand implements Command
{
public const NAME = 'extract_method';
public const DEFAULT_METHOD_NAME = 'newMethod';

/**
* @var ExtractMethod
Expand Down Expand Up @@ -52,7 +53,7 @@ public function __invoke(string $uri, int $startOffset, int $endOffset): Promise
SourceCode::fromStringAndPath($textDocument->text, $textDocument->uri),
$startOffset,
$endOffset,
'newMethod'
self::DEFAULT_METHOD_NAME
);
} catch (TransformException $error) {
$this->clientApi->window()->showMessage()->warning($error->getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Phpactor\Extension\LanguageServerCodeTransform\Tests\Unit\CodeAction;

use PHPUnit\Framework\TestCase;
use Phpactor\CodeTransform\Domain\Refactor\ExtractMethod;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\LanguageServerCodeTransform\CodeAction\ExtractMethodProvider;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\ExtractMethodCommand;
use Phpactor\LanguageServerProtocol\CodeAction;
use Phpactor\LanguageServerProtocol\Command;
use Phpactor\LanguageServerProtocol\TextDocumentItem;
use Phpactor\LanguageServer\Test\ProtocolFactory;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use function Amp\Promise\wait;
use Generator;

class ExtractMethodProviderTest extends TestCase
{
use ProphecyTrait;

const EXAMPLE_SOURCE = 'foobar';
const EXAMPLE_FILE = 'file:///somefile.php';
/**
* @var ObjectProphecy
*/
private $extractMethod;

public function setUp(): void
{
$this->extractMethod = $this->prophesize(ExtractMethod::class);
}
/**
* @dataProvider provideActionsData
*/
public function testProvideActions(bool $shouldSucceed, array $expectedValue): void
{
$textDocumentItem = new TextDocumentItem(self::EXAMPLE_FILE, 'php', 1, self::EXAMPLE_SOURCE);
$range = ProtocolFactory::range(0, 0, 0, 5);

// @phpstan-ignore-next-line
$this->extractMethod
->canExtractMethod(
SourceCode::fromStringAndPath($textDocumentItem->text, $textDocumentItem->uri),
$range->start->character,
$range->end->character
)
->willReturn($shouldSucceed)
->shouldBeCalled();

$this->assertEquals(
$expectedValue,
wait($this->createProvider()->provideActionsFor(
$textDocumentItem,
$range
))
);
}

public function provideActionsData(): Generator
{
yield 'Fail' => [
false,
[]
];
yield 'Success' => [
true,
[
CodeAction::fromArray([
'title' => 'Extract method',
'kind' => ExtractMethodProvider::KIND,
'diagnostics' => [],
'command' => new Command(
'Extract method',
ExtractMethodCommand::NAME,
[
self::EXAMPLE_FILE,
0,
5
]
)
])
]
];
}

private function createProvider(): ExtractMethodProvider
{
// @phpstan-ignore-next-line
return new ExtractMethodProvider($this->extractMethod->reveal());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Phpactor\Extension\LanguageServerCodeTransform\Tests\Unit\LspCommand;

use Exception;
use PHPUnit\Framework\TestCase;
use Phpactor\CodeTransform\Domain\Exception\TransformException;
use Phpactor\CodeTransform\Domain\Refactor\ExtractMethod;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\LanguageServerBridge\Converter\TextEditConverter;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\ExtractMethodCommand;
use Phpactor\LanguageServerProtocol\ApplyWorkspaceEditResponse;
use Phpactor\LanguageServerProtocol\MessageType;
use Phpactor\LanguageServerProtocol\WorkspaceEdit;
use Phpactor\LanguageServer\LanguageServerTesterBuilder;
use Phpactor\TextDocument\TextDocumentEdits;
use Phpactor\TextDocument\TextDocumentUri;
use Phpactor\TextDocument\TextEdits;
use Phpactor\TextDocument\TextEdit;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

class ExtractMethodCommandTest extends TestCase
{
use ProphecyTrait;

const EXAMPLE_SOURCE = '<?php ';
const EXAMPLE_URI = 'file:///file.php';
const EXAMPLE_OFFSET = 5;

public function testSuccessfulCall(): void
{
$textEdits = new TextDocumentEdits(
TextDocumentUri::fromString(self::EXAMPLE_URI),
new TextEdits(TextEdit::create(self::EXAMPLE_OFFSET, 1, 'test'))
);

$extractMethod = $this->prophesize(ExtractMethod::class);
// @phpstan-ignore-next-line
$extractMethod->extractMethod(Argument::type(SourceCode::class), 0, self::EXAMPLE_OFFSET, ExtractMethodCommand::DEFAULT_METHOD_NAME)
->shouldBeCalled()
->willReturn($textEdits);

[$tester, $builder] = $this->createTester($extractMethod);
$tester->workspace()->executeCommand('extract_method', [self::EXAMPLE_URI, 0, self::EXAMPLE_OFFSET]);
$builder->responseWatcher()->resolveLastResponse(new ApplyWorkspaceEditResponse(true));

$applyEdit = $builder->transmitter()->filterByMethod('workspace/applyEdit')->shiftRequest();

self::assertNotNull($applyEdit);
self::assertEquals([
'edit' => new WorkspaceEdit([
(string)$textEdits->uri() => TextEditConverter::toLspTextEdits(
$textEdits->textEdits(),
self::EXAMPLE_SOURCE
)
]),
'label' => 'Extract method'
], $applyEdit->params);
}
/**
* @dataProvider provideExceptions
*/
public function testFailedCall(Exception $exception): void
{
$extractMethod = $this->prophesize(ExtractMethod::class);
// @phpstan-ignore-next-line
$extractMethod->extractMethod(Argument::type(SourceCode::class), 0, self::EXAMPLE_OFFSET, ExtractMethodCommand::DEFAULT_METHOD_NAME)
->shouldBeCalled()
->willThrow($exception);

[$tester, $builder] = $this->createTester($extractMethod);
$tester->workspace()->executeCommand('extract_method', [self::EXAMPLE_URI, 0, self::EXAMPLE_OFFSET]);
$showMessage = $builder->transmitter()->filterByMethod('window/showMessage')->shiftNotification();

self::assertNotNull($showMessage);
self::assertEquals([
'type' => MessageType::WARNING,
'message' => $exception->getMessage()
], $showMessage->params);
}

public function provideExceptions(): array
{
return [
TransformException::class => [ new TransformException('Error message!') ],
];
}
/**
* @return array
*/
private function createTester(ObjectProphecy $extractMethod): array
{
$builder = LanguageServerTesterBuilder::createBare()
->enableTextDocuments()
->enableCommands();
$builder->addCommand('extract_method', new ExtractMethodCommand(
$builder->clientApi(),
$builder->workspace(),
$extractMethod->reveal()
));

$tester = $builder->build();
$tester->textDocument()->open(self::EXAMPLE_URI, self::EXAMPLE_SOURCE);

return [$tester, $builder];
}
}

0 comments on commit 2190010

Please sign in to comment.