-
Notifications
You must be signed in to change notification settings - Fork 13
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
Support @covers annotation #46
Comments
Can you give us an example why this would be useful to you and what problem this would solve? Preferably with a short code-example. Ref: https://phpunit.readthedocs.io/en/9.5/annotations.html#covers |
I don't say this is code you should write but just to show what's potentially the problem without final class Something
{
public function returnSomething(): string
{
return 'something';
}
}
final class TestSubject
{
private Something $something;
public function __construct(Something $something)
{
$this->something = $something;
}
public function doIt(): void
{
return $this->something->returnSomething();
}
}
// PHPspec test
final class TestSubjectSpec extends ObjectBehavior
{
public function let(): void
{
$this->beConstructedWith(new Something());
}
public function it_returns_something(): void
{
$this->doIt()->shouldBe('something');
}
} if I now don't add |
I think that's hard to implement. We are using So I think we would have to either hack the phpspec testrunner or use the phpunit testrunner, which seems like a complex task... but maybe I'm wrong. |
Out of curiosity I just copied the code section you linked above into the CodeCoverageListener class. And it actually works (of course with code from the phpunit package.) But if we just copy over the code into this package from PHPUnit it would work. Beside the obvious Here the changed code with which I got the feature working: namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener;
class CodeCoverageListener implements EventSubscriberInterface
{
// ... stripped code
public function afterExample(ExampleEvent $event): void
{
if ($this->skipCoverage) {
return;
}
$testFunctionReflection = $event->getExample()->getFunctionReflection();
$testClassReflection = $event->getSpecification()->getClassReflection();
try {
$linesToBeCovered = \PHPUnit\Util\Test::getLinesToBeCovered(
$testClassReflection->getName(),
$testFunctionReflection->getName()
);
$linesToBeUsed = \PHPUnit\Util\Test::getLinesToBeUsed(
$testClassReflection->getName(),
$testFunctionReflection->getName()
);
} catch (InvalidCoversTargetException $cce) {
$result->addWarning(
$test,
new Warning(
$cce->getMessage()
),
$time
);
}
$this->coverage->stop(true, $linesToBeCovered, $linesToBeUsed);
}
// ...
} |
I guess I was wrong. 😅 I think it's not worth to include two additional packages just for this feature. But I think we could make this package compatible with |
I'll do a PR with a suggestion today so we can discuss it. See #47 |
It would be great to have support for the
@covers
annotation like PHPUnit has. Ensuring you only cover code with your test that's actually meant to be covered.I'm fully aware that this might be plenty of work. What do you think? Should this be a feature of this package?
The text was updated successfully, but these errors were encountered: