Repository navigation
GenerateAccessor handles both offset and names #24
Changes from all commits
3fb1b38
d9633b6
750a744
e262697
594e998
2cdc4aa
fc7036d
921cada
891b9a3
beab497
e37b4d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| use Phpactor\CodeTransform\Tests\Adapter\WorseReflection\WorseTestCase; | ||
| use Phpactor\CodeTransform\Adapter\WorseReflection\Refactor\WorseGenerateAccessor; | ||
| use Phpactor\CodeTransform\Domain\SourceCode; | ||
| use Phpactor\CodeTransform\Domain\Exception\TransformException; | ||
| use Phpactor\WorseReflection\Core\Exception\ItemNotFound; | ||
|
|
||
| class WorseGenerateAccessorTest extends WorseTestCase | ||
| { | ||
|
|
@@ -14,47 +14,70 @@ class WorseGenerateAccessorTest extends WorseTestCase | |
| */ | ||
| public function testGenerateAccessor( | ||
| string $test, | ||
| string $propertyName, | ||
| string $prefix = '', | ||
| bool $upperCaseFirst = false | ||
| ) { | ||
| list($source, $expected, $offset) = $this->sourceExpectedAndOffset(__DIR__ . '/fixtures/' . $test); | ||
| list($source, $expected, $offset) = $this->sourceExpectedAndOffset( | ||
| __DIR__ . '/fixtures/' . $test | ||
| ); | ||
|
|
||
| $generateAccessor = new WorseGenerateAccessor($this->reflectorFor($source), $this->updater(), $prefix, $upperCaseFirst); | ||
| $transformed = $generateAccessor->generateAccessor(SourceCode::fromString($source), $offset); | ||
| $generateAccessor = new WorseGenerateAccessor( | ||
| $this->reflectorFor($source), | ||
| $this->updater(), | ||
| $prefix, | ||
| $upperCaseFirst | ||
| ); | ||
| $transformed = $generateAccessor->generate( | ||
| SourceCode::fromString($source), | ||
| $propertyName, | ||
| $offset | ||
| ); | ||
|
|
||
| $this->assertEquals(trim($expected), trim($transformed)); | ||
| } | ||
|
|
||
| public function provideExtractAccessor() | ||
| { | ||
| $propertyName = 'method'; | ||
|
|
||
| return [ | ||
| 'property' => [ | ||
| 'generateAccessor1.test', | ||
| $propertyName, | ||
| ], | ||
| 'prefix and ucfirst' => [ | ||
| 'generateAccessor2.test', | ||
| $propertyName, | ||
| 'get', | ||
| true, | ||
| ], | ||
| 'return type' => [ | ||
| 'generateAccessor3.test', | ||
| $propertyName, | ||
| ], | ||
| 'namespaced' => [ | ||
| 'generateAccessor4.test', | ||
| $propertyName, | ||
| ], | ||
| 'pseudo-type' => [ | ||
| 'generateAccessor5.test', | ||
| $propertyName, | ||
| ], | ||
| 'multiple-classes' => [ | ||
| 'generateAccessor6.test', | ||
| $propertyName, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we add $propertyName when it is always the same?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would have slightly preferred to keep "constants" as a value in the test rather than repeating here (we don't vary in this test). But not important :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I always try to keep tests configurable, not saying it's a good thing. |
||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function testNonProperty() | ||
| { | ||
| $this->expectException(TransformException::class); | ||
| $this->expectExceptionMessage('Symbol at offset "9" is not a property'); | ||
| $source = '<?php echo "hello";'; | ||
| $this->expectException(ItemNotFound::class); | ||
| $this->expectExceptionMessage('Unknown item "bar", known items: "foo"'); | ||
| $source = '<?php class Foo { private $foo; }'; | ||
|
|
||
| $generateAccessor = new WorseGenerateAccessor($this->reflectorFor(''), $this->updater()); | ||
| $generateAccessor->generateAccessor(SourceCode::fromString($source), 9); | ||
| $generateAccessor->generate(SourceCode::fromString($source), 'bar', 0); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| class generateMethod | ||
| { | ||
| private $met<>hod; | ||
| pr<>ivate $method; | ||
| } | ||
| // File: expected | ||
| <?php | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ | |
|
|
||
| class generateMethod | ||
| { | ||
| private $met<>hod; | ||
| } | ||
| private $method; | ||
| <>} | ||
| // File: expected | ||
| <?php | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // File: source | ||
|
|
||
| <?php | ||
|
|
||
| namespace Acme; | ||
|
|
||
| class generateMethod | ||
| { | ||
| /** | ||
| * @var Type1[] | ||
| */ | ||
| private $method; | ||
| <>} | ||
|
|
||
| class Bar | ||
| { | ||
| } | ||
| // File: expected | ||
| <?php | ||
|
|
||
| namespace Acme; | ||
|
|
||
| class generateMethod | ||
| { | ||
| /** | ||
| * @var Type1[] | ||
| */ | ||
| private $method; | ||
|
|
||
| public function method(): array | ||
| { | ||
| return $this->method; | ||
| } | ||
| } | ||
|
|
||
| class Bar | ||
| { | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.