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

Tests with data providers in parent class don't work anymore #3879

Closed
l-x opened this issue Oct 4, 2019 · 12 comments
Closed

Tests with data providers in parent class don't work anymore #3879

l-x opened this issue Oct 4, 2019 · 12 comments
Labels
type/bug Something is broken

Comments

@l-x
Copy link

l-x commented Oct 4, 2019

Q A
PHPUnit version 8.4.0
PHP version 7.3.9
Installation Method Composer

Summary

Tests classes that contain a test method utilizing a data provider defined in a parent class will produce a warning.

Previous Behavior

No warnings will be shown

Current Behavior

Warning:

The data provider specified for FuTest::testFu is invalid.
Cannot instantiate abstract class AbstractFuTestCase

How to reproduce

abstract class AbstractFuTestCase extends \PHPUnit\Framework\TestCase
{
    public function dataProvider(): array
    {
        return [
            ['fu'],
            ['bar'],
        ];
    }

    /**
     * @dataProvider dataProvider
     */
    public function testFu(string $bar): void
    {
        $this->assertTrue(true);
    }
}

class FuTest extends AbstractFuTestCase
{

}

Just run the FuTest

@l-x l-x added type/backward-compatibility Something will be/is intentionally broken type/bug Something is broken labels Oct 4, 2019
@pavbis
Copy link

pavbis commented Oct 4, 2019

Can also confirm the issue. Just updated to 8.4.0.

@sebastianbergmann
Copy link
Owner

@Ocramius This looks like it could be related to #3836. PHPUnit tries to create an instance of AbstractFuTestCase instead of FuTest to call the data provider method on.

@sebastianbergmann sebastianbergmann removed the type/backward-compatibility Something will be/is intentionally broken label Oct 4, 2019
@Ocramius
Copy link
Sponsor Contributor

Ocramius commented Oct 4, 2019

@sebastianbergmann basically the patch takes the parent class instead of the child class when considering an annotation, right?

@panvid
Copy link
Contributor

panvid commented Oct 4, 2019

The problem also appear on simple abstraction, see: #3881 (comment)

@sebastianbergmann
Copy link
Owner

I am not sure whether this is related to annotation parsing. Might as well be related to #3381 (or something else). I cannot look into this right now, will do so over the weekend.

@l-x
Copy link
Author

l-x commented Oct 4, 2019

To clarify the problem I reported: It is not necessary to put the two classes in a single file to reproduce this behaviour.

@sebastianbergmann sebastianbergmann added the event/eu-fossa/2019-10 EU-FOSSA Hackathon: October 2019 label Oct 4, 2019
@sebastianbergmann
Copy link
Owner

@Ocramius According to git bisect, 9309062 is the first "bad commit":

$ cat run.sh               
#!/bin/sh
composer update
./phpunit --fail-on-warning /tmp/FuTest.php
$ git bisect start
$ git bisect good 8.3.5
$ git bisect bad 8.4.0
$ git bisect run ./run.sh

The contents of /tmp/FuTest.php is the example from #3879 (comment).

@gleb-svitelskiy
Copy link
Contributor

Here is the bug 9309062
This is all about reflection. For example:

class Foo
{
    public function baz() {}
}

class Bar extends Foo {}

$reflectionMethod = new \ReflectionMethod('Bar', 'baz');
var_dump($reflectionMethod->getDeclaringClass()->getName());

It will be string(3) "Foo"
So it's not a good idea to get class name from declaring class.
It works before because class name passed to getDataFromDataProviderAnnotation() was received in TestUtil::getProvidedData(), not from ReflectionMethod

@wgevaert
Copy link
Contributor

wgevaert commented Oct 6, 2019

Should be fixed with the following adjustments:

index 027737c8d..c96971bee 100644
--- a/src/Util/Annotation/DocBlock.php
+++ b/src/Util/Annotation/DocBlock.php
@@ -101,7 +101,7 @@ public static function ofClass(\ReflectionClass $class): self
         );
     }
 
-    public static function ofMethod(\ReflectionMethod $method): self
+    public static function ofMethod(\ReflectionMethod $method, string $className = null): self
     {
         return new self(
             (string) $method->getDocComment(),
@@ -111,7 +111,7 @@ public static function ofMethod(\ReflectionMethod $method): self
             $method->getEndLine(),
             $method->getFileName(),
             $method->getName(),
-            $method->getDeclaringClass()->getName()
+            $className ?? $method->getDeclaringClass()->getName()
         );
     }
 
diff --git a/src/Util/Annotation/Registry.php b/src/Util/Annotation/Registry.php
index 795dbf6a9..fa6cf3641 100644
--- a/src/Util/Annotation/Registry.php
+++ b/src/Util/Annotation/Registry.php
@@ -80,6 +80,6 @@ public function forMethod(string $class, string $method): DocBlock
             );
         }
 
-        return $this->methodDocBlocks[$class][$method] = DocBlock::ofMethod($reflection);
+        return $this->methodDocBlocks[$class][$method] = DocBlock::ofMethod($reflection, $class);
     }
 }

I am quite new to github, and trying to make a pull request gave me remote: Permission to sebastianbergmann/phpunit.git denied to wgevaert.. How can I do this?

All I've done so far was clone the repo, make a local branch and do changes on this branch. I do know how git works, but I am new to github.

@pavbis
Copy link

pavbis commented Oct 6, 2019

@wgevaert you should fork the repository first.
See https://guides.github.com/activities/forking/ .

@wgevaert
Copy link
Contributor

wgevaert commented Oct 6, 2019

Thanks! I've made a pull request that should solve this issue

@Ocramius
Copy link
Sponsor Contributor

Ocramius commented Oct 7, 2019 via email

@sebastianbergmann sebastianbergmann removed the event/eu-fossa/2019-10 EU-FOSSA Hackathon: October 2019 label Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

7 participants