From aa8d73e3678ac17783986b4506edf7ab5477a84d Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Fri, 13 Sep 2024 15:32:35 +0200 Subject: [PATCH] add 5.1 rector tests + connectionhelper rector (#298) --- config/rector/sets/cakephp51.php | 3 + .../StaticConnectionHelperRector.php | 72 +++++++++++++++++++ tests/TestCase/Command/RectorCommandTest.php | 7 ++ .../src/SomeTest.php | 40 +++++++++++ .../src/SomeTest.php | 40 +++++++++++ 5 files changed, 162 insertions(+) create mode 100644 src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php create mode 100644 tests/test_apps/original/RectorCommand-testApply51/src/SomeTest.php create mode 100644 tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php diff --git a/config/rector/sets/cakephp51.php b/config/rector/sets/cakephp51.php index 6955e9c..e520e2c 100644 --- a/config/rector/sets/cakephp51.php +++ b/config/rector/sets/cakephp51.php @@ -1,6 +1,7 @@ '_cake_translations_', ]); + + $rectorConfig->rule(StaticConnectionHelperRector::class); }; diff --git a/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php b/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php new file mode 100644 index 0000000..d967e2d --- /dev/null +++ b/src/Rector/Rector/MethodCall/StaticConnectionHelperRector.php @@ -0,0 +1,72 @@ +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 + ); + } +} diff --git a/tests/TestCase/Command/RectorCommandTest.php b/tests/TestCase/Command/RectorCommandTest.php index 4a447d5..4b27913 100644 --- a/tests/TestCase/Command/RectorCommandTest.php +++ b/tests/TestCase/Command/RectorCommandTest.php @@ -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(); + } } diff --git a/tests/test_apps/original/RectorCommand-testApply51/src/SomeTest.php b/tests/test_apps/original/RectorCommand-testApply51/src/SomeTest.php new file mode 100644 index 0000000..1b61226 --- /dev/null +++ b/tests/test_apps/original/RectorCommand-testApply51/src/SomeTest.php @@ -0,0 +1,40 @@ + [ + '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(); + } +} diff --git a/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php b/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php new file mode 100644 index 0000000..5906488 --- /dev/null +++ b/tests/test_apps/upgraded/RectorCommand-testApply51/src/SomeTest.php @@ -0,0 +1,40 @@ + [ + '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(); + } +}