rectorphp / rector-phpunit

Rector upgrade rules for PHPUnit
http://getrector.com
MIT License
61 stars 46 forks source link

Replace testWith to dataproviders #281

Closed eerison closed 8 months ago

eerison commented 8 months ago

Hello guys I want to build a rule to replace @testWith to dataProvider.

In case you have any rule that do something similar, please post here, it could help me to provide a rule for it ;)

eerison commented 8 months ago

How can I add a new method 👀

samsonasik commented 8 months ago

You may looking for ClassInsertManipulator::addAsFirstMethod()

https://github.com/rectorphp/rector-src/blob/59bfa4da42fd30bace742b7916508062b8642aab/src/NodeManipulator/ClassInsertManipulator.php#L26

eerison commented 8 months ago

Is there any class that convert "[0,0,0]" (string) to [0,0,0] array?

atm I am getting PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode, but the value is string :/

samsonasik commented 8 months ago

what before vs after you want to achieve?

TomasVotruba commented 8 months ago

What about TestWithAnnotationToAttributeRector?

eerison commented 8 months ago

What about TestWithAnnotationToAttributeRector?

yeah it helped me, But I faced other thing :/, is there any doc explain how to use it?

atm, I am stuck how to return an array for the new method.

I'am trying this

        $array = new Expr\Array_([1,2,3]);
        $returnValue = new \PhpParser\Node\Stmt\Return_($array);
        $providerMethod = new ClassMethod('dataProvider');
        $providerMethod->stmts[] = $returnValue;
        $this->visibilityManipulator->makePublic($providerMethod);
        $this->classInsertManipulator->addAsFirstMethod($node, $providerMethod);

But I am getting this issue :/

Error: Call to a member function getComments() on int
eerison commented 8 months ago

What about TestWithAnnotationToAttributeRector?

yeah it helped me, But I faced other thing :/, is there any doc explain how to use it?

atm, I am stuck how to return an array for the new method.

I'am trying this

        $array = new Expr\Array_([1,2,3]);
        $returnValue = new \PhpParser\Node\Stmt\Return_($array);
        $providerMethod = new ClassMethod('dataProvider');
        $providerMethod->stmts[] = $returnValue;
        $this->visibilityManipulator->makePublic($providerMethod);
        $this->classInsertManipulator->addAsFirstMethod($node, $providerMethod);

But I am getting this issue :/

Error: Call to a member function getComments() on int

I found it: https://github.com/rectorphp/php-parser-nodes-docs

I guess it can help me.