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

Incorrect behavior of TestWithAnnotationToAttributeRector with integers #8941

Closed
Seldaek opened this issue Dec 18, 2024 · 9 comments · Fixed by rectorphp/rector-src#6608 or rectorphp/rector-src#6610
Labels

Comments

@Seldaek
Copy link

Seldaek commented Dec 18, 2024

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/322b336f-f232-4614-a264-6077e0a8f206

<?php

class UserControllerTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @testWith ["2"]
     *           ["[email protected]"]
     */
    public function testSomething(string $userId): void
    {
    }
}

Responsible rules

  • TestWithAnnotationToAttributeRector

Expected Behavior

I'd expect #[TestWith('2')] as the annotation was using a string, but somehow it thinks it is smart to convert that to an int because I guess the string is numeric. Anyway this breaks the test as the $userId param requires a string.

@samsonasik
Copy link
Member

Re-open per rectorphp/rector-src#6608 (comment)

the fix for numeric string, "true", "false" should be in a single PR to ease rollback.

@staabm
Copy link
Contributor

staabm commented Dec 18, 2024

First time I see someone using this super ugly annotation based data-provider stuff instead of a proper php coded one :-).

Recently @sebastianbergmann and me were discussing whether anyone on the planet needs this phpunit feature 😅

@Seldaek
Copy link
Author

Seldaek commented Dec 19, 2024

I believe @heiglandreas was the author of this particular @testWith instance, which is indeed the only one I've got in my codebase here, maybe he can give you more info, maybe there is a secret society of testWith users out there 😅

But I think it's kinda nice if you have just 2-3 variations of something, then the "overhead" of a test provider method can feel overkill.

@staabm
Copy link
Contributor

staabm commented Dec 19, 2024

But I think it's kinda nice if you have just 2-3 variations of something, then the "overhead" of a test provider method can feel overkill.

IMO with this phpdoc based version you are loosing static analysis and other tooling support, which is a footgun.

maybe there is a secret society of testWith users out there 😅

I guess its a dark net thing ;-)

@heiglandreas
Copy link

I was working on several code-bases that heavily rely upon this 😂

And the attribute-version might actually be better in terms of types.

But yes: It does come with a gun pointing at your foot out of the box!

OTOH: Which data-provider is providing more type-safety...

/**
 * @return array{string, int, bool}[]
 */

or

#[TestWith(['foo', 1, true])]

Yes, the annotation one is about the same level of type-safety.

But for small datasets the TestWith really is a nice way to have everything together.

@staabm
Copy link
Contributor

staabm commented Dec 19, 2024

I don't like both. I would use a dataprovider method 😅
(just personal preference of course. use what you like)

@heiglandreas
Copy link

heiglandreas commented Dec 19, 2024

The first one is from a data-provider method 😉

@staabm
Copy link
Contributor

staabm commented Dec 19, 2024

OTOH: Which data-provider is providing more type-safety...

when time allows I will revive a phpstan-phpunit PR, which will verify types of dataprovider vs. tests without the need for the manual phpdoc return type

phpstan/phpstan-phpunit#152 (comment)

@Seldaek
Copy link
Author

Seldaek commented Dec 19, 2024

It could also inspect TestWith attribute types ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants