Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 5.1 rector tests + connectionhelper rector #298

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this prefix also be replaced?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to modify with userland prefixes.

'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();
}
}
Loading