Skip to content

Commit

Permalink
add 5.1 rector tests + connectionhelper rector (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal authored Sep 13, 2024
1 parent 1db9833 commit aa8d73e
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/rector/sets/cakephp51.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
declare(strict_types=1);

use Cake\Upgrade\Rector\Rector\MethodCall\StaticConnectionHelperRector;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\String_\RenameStringRector;

Expand All @@ -11,4 +12,6 @@
// Rename the cache configuration used by translations.
'_cake_core_' => '_cake_translations_',
]);

$rectorConfig->rule(StaticConnectionHelperRector::class);
};
72 changes: 72 additions & 0 deletions src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php
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
);
}
}
7 changes: 7 additions & 0 deletions tests/TestCase/Command/RectorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,11 @@ public function testApply50()
$this->exec('upgrade rector --rules cakephp50 ' . TEST_APP);
$this->assertTestAppUpgraded();
}

public function testApply51()
{
$this->setupTestApp(__FUNCTION__);
$this->exec('upgrade rector --rules cakephp51 ' . TEST_APP);
$this->assertTestAppUpgraded();
}
}
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();
}
}
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();
}
}

0 comments on commit aa8d73e

Please sign in to comment.