This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/LanguageServerCodeTransform/Unit/CodeAction/ExtractMethodProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
tests/LanguageServerCodeTransform/Unit/LspCommand/ExtractMethodCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |