-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 5.1 rector tests + connectionhelper rector (#298)
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 deletions.
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
72 changes: 72 additions & 0 deletions
72
src/Rector/Rector/MethodCall/StaticConnectionHelperRector.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,72 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Cake\Upgrade\Rector\Rector\MethodCall; | ||
|
||
use Cake\TestSuite\ConnectionHelper; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class StaticConnectionHelperRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Transform ConnectionHelper instance method calls to static calls', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$connectionHelper = new ConnectionHelper(); | ||
$connectionHelper->runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
ConnectionHelper::runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, Assign::class]; | ||
} | ||
|
||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node instanceof Assign) { | ||
if ($node->expr instanceof New_ && $this->isName($node->expr->class, 'ConnectionHelper')) { | ||
// Remove the instantiation statement | ||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE); | ||
if ($parent instanceof Expression) { | ||
$this->removeNode($parent); | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
|
||
// Ensure the node is a method call on the ConnectionHelper instance | ||
if (! $this->isObjectType($node->var, new ObjectType(ConnectionHelper::class))) { | ||
return null; | ||
} | ||
|
||
// Replace with a static method call | ||
return new StaticCall( | ||
new Node\Name\FullyQualified(ConnectionHelper::class), | ||
$node->name, | ||
$node->args | ||
); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
tests/test_apps/original/RectorCommand-testApply51/src/SomeTest.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,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MyPlugin; | ||
|
||
use Cake\Datasource\ConnectionManager; | ||
use Cake\TestSuite\ConnectionHelper; | ||
use Cake\TestSuite\IntegrationTestTrait; | ||
use Cake\TestSuite\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
use IntegrationTestTrait; | ||
|
||
public function testSomething() | ||
{ | ||
$config = [ | ||
'_cake_core_' => [ | ||
'className' => 'FileEngine', | ||
'prefix' => 'myapp_cake_core_', | ||
'path' => 'persistent', | ||
'serialize' => true, | ||
'duration' => '+1 years', | ||
], | ||
]; | ||
} | ||
|
||
public function testConnectionHelper() | ||
{ | ||
$connectionHelper = new ConnectionHelper(); | ||
$connection = ConnectionManager::get('test'); | ||
$connectionHelper->runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
$connectionHelper->dropTables('test', ['table']); | ||
$connectionHelper->enableQueryLogging(['test']); | ||
$connectionHelper->truncateTables('test', ['table']); | ||
$connectionHelper->addTestAliases(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.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,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace MyPlugin; | ||
|
||
use Cake\Datasource\ConnectionManager; | ||
use Cake\TestSuite\ConnectionHelper; | ||
use Cake\TestSuite\IntegrationTestTrait; | ||
use Cake\TestSuite\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
use IntegrationTestTrait; | ||
|
||
public function testSomething() | ||
{ | ||
$config = [ | ||
'_cake_translations_' => [ | ||
'className' => 'FileEngine', | ||
'prefix' => 'myapp_cake_core_', | ||
'path' => 'persistent', | ||
'serialize' => true, | ||
'duration' => '+1 years', | ||
], | ||
]; | ||
} | ||
|
||
public function testConnectionHelper() | ||
{ | ||
$connectionHelper = new ConnectionHelper(); | ||
$connection = ConnectionManager::get('test'); | ||
\Cake\TestSuite\ConnectionHelper::runWithoutConstraints($connection, function ($connection) { | ||
$connection->execute('SELECT * FROM table'); | ||
}); | ||
\Cake\TestSuite\ConnectionHelper::dropTables('test', ['table']); | ||
\Cake\TestSuite\ConnectionHelper::enableQueryLogging(['test']); | ||
\Cake\TestSuite\ConnectionHelper::truncateTables('test', ['table']); | ||
\Cake\TestSuite\ConnectionHelper::addTestAliases(); | ||
} | ||
} |