Skip to content

[PhpUnitBridge] Added a CoverageListener to enhance the code coverage report #23149

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

Closed
wants to merge 10 commits into from

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Jun 12, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR symfony/symfony-docs#8416

The code coverage computed by PHPUnit is not very accurate by default as it marks a line as tested as soon as it has been executed.
For example, if you have two classes A and B where A is using B and you write test only for the class A then the class B will be marked as tested.

You can fix this issue by adding @covers A on top of the class ATest, but it's a bit boring.

This Listener add this annotation on each test if it's applicable:

  • If an annotation already exists, we do nothing.
  • We try to find the SUT thanks to the Test class name, if it does not exist, we do nothing

If you wan to see it in action: https://github.com/lyrixx/phpunit-auto-cover


The PR is not finished, I think we could add this listener to symfony itself.

What do you think?

@stof
Copy link
Member

stof commented Jun 12, 2017

@lyrixx is there a way to opt-out the guessing for some test cases (smoke tests used to cover things through functional tests for instance) ?

@nicolas-grekas
Copy link
Member

I like it :)

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jun 12, 2017
@lyrixx
Copy link
Member Author

lyrixx commented Jun 12, 2017

@stof As soon as you add an annotation on your test class, the listener adds nothing.
Thus, if it's really needed, we could make this listener configurable.

@lyrixx lyrixx force-pushed the phpunit-auto-cover branch from 0afa3cf to 16fe7dc Compare June 12, 2017 15:10
@dunglas
Copy link
Member

dunglas commented Jun 14, 2017

Nice!

@lyrixx
Copy link
Member Author

lyrixx commented Jun 22, 2017

This PR did not get so much attraction. IMHO, it's nice.

Should I finished it or it will be a waste of time?
(This is really easy, as we have to deal with many PHPUnit version (PSR4 vs Pear))

@Simperfit
Copy link
Contributor

IMO It should be finished, I may use it ;)

@sebastianbergmann
Copy link
Contributor

sebastianbergmann commented Jun 22, 2017

A test listener should only listen. In the future, it will only be able to listen.

The proper solution to this problem is to follow best practices and set forceCoversAnnotation="true" in your PHPUnit configuration.

@stof
Copy link
Member

stof commented Jun 22, 2017

@sebastianbergmann well, this listener is an attempt at guessing the covered class based on a convention instead of forcing to put the annotation everywhere. What would be the proper way to implement such convention-based coverage configuration in PHPUnit ?

@fabpot
Copy link
Member

fabpot commented Jun 22, 2017

My first reaction was: why is it related to Symfony? Why isn't it something part of PHPUnit itself it that's something useful?

@lyrixx
Copy link
Member Author

lyrixx commented Jun 22, 2017

@fabpot that's a good question. I guess I created the PR in Symfony because I contribute more to Symfony than to PHPUnit. And we already have some listener in Symfony.

@medinae
Copy link

medinae commented Jun 22, 2017

Useful feature 👍 I agree with @lyrixx as others similar listeners are existing in SF.

@fabpot
Copy link
Member

fabpot commented Jul 3, 2017

@lyrixx Can you add some information in the documentation?

@lyrixx
Copy link
Member Author

lyrixx commented Jul 4, 2017

@fabpot Keep in mind this PR is really not ready. I have to make it work for different PHPUnit version.

anyway, what documentation are you referring to? (local PHP doc or the symfony/symfony-docs repo?)

@lyrixx lyrixx force-pushed the phpunit-auto-cover branch 3 times, most recently from 652c5ef to 8e4f1aa Compare July 4, 2017 09:37
@lyrixx
Copy link
Member Author

lyrixx commented Jul 4, 2017

I updated the PR to support PHPUnit 5.* and 6.*.

Locally, I also updated all phpunit.xml.dist to add the listener, but the phpunit bridge is not required. Should I update them too?

@keradus
Copy link
Member

keradus commented Jul 9, 2017

IMO, if we already try to figure out what is the expected class to be tested, we could already put it into code and do it only once, commit and forget about it. no need to do it all the times on runtime.
Then, it could be watched to have the annotation for new tests, eg with suggestion of @sebastianbergmann and PHP CS Fixer/fabbot php_unit_test_class_requires_covers rule

anyway, big 👍 for strict coverage

@nicolas-grekas
Copy link
Member

Would it be possible to add a test, by mean of a phpt for example? this would ensure that we can detect whenever phpunit changes its internals and breaks the feat.
Since this is purely opt-in, I'm personally OK to accept it. With a doc PR :)

@keradus
Copy link
Member

keradus commented Jul 19, 2017

@nicolas-grekas , why do it in runtime instead of actually store it as annotation at all test files, as I suggest ?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 19, 2017

@keradus the purpose of this PR is exactly to leverage a convention in order to not have to add @cover to all test files... because it's boring to repeat yourself in each and every test class I guess? :)

@keradus
Copy link
Member

keradus commented Jul 19, 2017

explicit is better than implicit ;)
on top of that, if automate guess wrongly, you don't know (as you don't even see what was chosen) and collect data not how you wish to. if for some reason you wish to have some non-standard cover restriction in given test, guessing on the fly won't help.

@lyrixx
Copy link
Member Author

lyrixx commented Jul 19, 2017

if automate guess wrongly,

how is it possible?

and BTW this feature is opt-in ; So if you don't like it, don't use it ;)

@keradus
Copy link
Member

keradus commented Jul 19, 2017

how is it possible?

it could as there is no tests, so if one day there will be some newer autoloader spec, dir structure or anything, it could silently fail matching src class for given test class

and BTW this feature is opt-in ; So if you don't like it, don't use it ;)

of course, but still I think it's nice to think about other possible solutions.
on one side you have logic hidden, on other you have it explicit

@lyrixx
Copy link
Member Author

lyrixx commented Sep 21, 2017

Arf, I don't know how to exclude my "Fixture" test from the Symfony test suite. I have try many things without success :/

@lyrixx
Copy link
Member Author

lyrixx commented Sep 22, 2017

Hi.
So:

  • I managed to fix test on travis & appveyor
  • fabbot is wrong
  • I addressed issues raised by @keradus
  • I added a note in the CHANGELOG

@lyrixx
Copy link
Member Author

lyrixx commented Sep 22, 2017

@nicolas-grekas
Copy link
Member

Still 👍 on my side, ping @symfony/deciders

@dunglas
Copy link
Member

dunglas commented Sep 26, 2017

👍

@nicolas-grekas
Copy link
Member

@lyrixx can you add an entry in the CHANGELOG file please?

@lyrixx
Copy link
Member Author

lyrixx commented Sep 26, 2017

Oups, I forgot to push a commit. Fixed.

class_alias('Symfony\Bridge\PhpUnit\Legacy\CoverageListener', 'Symfony\Bridge\PhpUnit\CoverageListener');
// Using an early return instead of a else does not work when using the PHPUnit
// phar due to some weird PHP behavior (the class gets defined without executing
// the code before it and so the definition is not properly conditional)
Copy link
Member

Choose a reason for hiding this comment

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

fabbot does not seem to be happy with the comments not being indented

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a false positive. I ignored it.

Copy link
Member

Choose a reason for hiding this comment

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

How is that a false positive?

@fabpot
Copy link
Member

fabpot commented Sep 26, 2017

Thank you @lyrixx.

fabpot added a commit that referenced this pull request Sep 26, 2017
…e code coverage report (lyrixx)

This PR was squashed before being merged into the 3.4 branch (closes #23149).

Discussion
----------

[PhpUnitBridge] Added a CoverageListener to enhance the code coverage report

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | symfony/symfony-docs#8416

---

The code coverage computed by PHPUnit is not very accurate by default as it marks a line as tested as soon as it has been executed.
For example, if you have two classes A and B where A is using B and you write test only for the class A then the class B will be marked as tested.

You can fix this issue by adding `@covers A` on top of the class ATest, but it's a bit boring.

This Listener add this annotation on each test if it's applicable:
* If an annotation already exists, we do nothing.
* We try to find the SUT thanks to the Test class name, if it does not exist, we do nothing

---

If you wan to see it in action: https://github.com/lyrixx/phpunit-auto-cover

---

The PR is not finished, I think we could add this listener to symfony itself.

What do you think?

Commits
-------

e17206d [PhpUnitBridge] Added a CoverageListener to enhance the code coverage report
@fabpot fabpot closed this Sep 26, 2017
@lyrixx lyrixx deleted the phpunit-auto-cover branch September 27, 2017 08:22
This was referenced Oct 18, 2017
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Oct 29, 2017
…rixx, javiereguiluz)

This PR was merged into the 3.4 branch.

Discussion
----------

[PHPUnitBridge] Added docs for the CoverageListener

For symfony/symfony#23149

Commits
-------

c21ab16 Fixed code indentation
e78804b Minor rewords
81365f3 [PHPUnitBridge] Added docs for the CoverageListener
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.