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

Missing annotations with the latest composer version #120

Merged
merged 1 commit into from
Feb 9, 2017
Merged

Missing annotations with the latest composer version #120

merged 1 commit into from
Feb 9, 2017

Conversation

gnat42
Copy link
Contributor

@gnat42 gnat42 commented Feb 9, 2017

This PR demonstrates the issue with #115 as well as a potential fix.

There's one oddity here however, I can only cause the failure if I only run that one test. This is why the test I added has a '@group gnat' annotation.

When I run

phpunit --group gnat

Without my fix in place in DocParser it fails, running all tests for some reason doesn't fail. I'm not sure why. I also realize that really I should have a test against DocParser instead, since that is where the issue lies. However this was a proof of concept of the issue. If you comment out my changes in DocParser, the error we're getting in symfony based projects happens.

@@ -728,6 +728,11 @@ private function Annotation()
}
}

// strip the first \ off if used
if( '\\' == $name[0]) {
$name = substr($name,1);
Copy link
Member

Choose a reason for hiding this comment

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

ltrim($name, '\\')

@@ -108,4 +110,23 @@ public function testPropertyAnnotationIsIgnored()

self::assertEmpty($reader->getPropertyAnnotations($ref->getProperty('property')));
}

/**
* @group gnat
Copy link
Member

Choose a reason for hiding this comment

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

to be removed


$annotations = $reader->getClassAnnotations($ref);
} catch(AnnotationException $exception) {
$this->fail('Class with full path use statement unable to load annotation');
Copy link
Member

Choose a reason for hiding this comment

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

If you don't expect an exception, let it bubble up

$this->fail('Class with full path use statement unable to load annotation');
}

$this->assertTrue(true);
Copy link
Member

Choose a reason for hiding this comment

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

Please assert for the annotations to be at least of the type you expected

@@ -0,0 +1,22 @@
<?php
/**
Copy link
Member

Choose a reason for hiding this comment

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

to be removed


namespace Doctrine\Tests\Common\Annotations\Fixtures;

use \Doctrine\Tests\Common\Annotations\Fixtures\Annotation as Annotations;
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment explaining that the leading \ is intentional

use \Doctrine\Tests\Common\Annotations\Fixtures\Annotation as Annotations;

/**
* Class ClassWithFullPathUseStatement
Copy link
Member

Choose a reason for hiding this comment

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

remove


/**
* Class ClassWithFullPathUseStatement
* @package Doctrine\Tests\Common\Annotations\Fixtures
Copy link
Member

Choose a reason for hiding this comment

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

remove

@@ -728,6 +728,11 @@ private function Annotation()
}
}

// strip the first \ off if used
Copy link
Member

Choose a reason for hiding this comment

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

Can actually just call ltrim($name, '\\') all the time, and make this comment and conditional redundant.

@Ocramius Ocramius added the bug label Feb 9, 2017
@Ocramius Ocramius added this to the v1.3.2 milestone Feb 9, 2017
@stof
Copy link
Member

stof commented Feb 9, 2017

@gnat42 the bug does not appear when running all tests, because you reused an annotation used in other tests, and so the class is already loaded when reaching your test. But the bug only happens when the class is not yet loaded (classes already loaded are detected using class_exists($name, false), which goes through the PHP processing of class names and so is not buggy).
to make the tests usable in the full testsuite, use an annotation class created specially for this test. And add $this->assertFalse(class_exists(YourAnnotation::class, false), 'The YourAnnotation must not be used in other tests for this test to be useful'); assertion at the beginning of your test to forbid breaking the test in the future by reusing this annotation by mistake.

@Ocramius
Copy link
Member

Ocramius commented Feb 9, 2017

$this->assertFalse(class_exists(YourAnnotation::class, false), 'The YourAnnotation must not be used in other tests for this test to be useful');

very clever! Indeed, please add that one :-)

@stof he's already using @runInSeparateProcess

@stof
Copy link
Member

stof commented Feb 9, 2017

@Ocramius I prefer using a separate annotation class used only for this test. Much less overhead than forcing a subprocess for the test, for the same result

@Ocramius
Copy link
Member

Ocramius commented Feb 9, 2017

Agree, good point. The assertion to be added is obviously needed too.

@stof
Copy link
Member

stof commented Feb 9, 2017

thus, I not even sure that @runInSeparateProcess would solve the issue here. IIRC, PHPUnit forces reloading defined classes when launching the subprocess (at least, PHPUnit 4.x was doing it; I haven't checked whether it changed in 5.x)

@stof
Copy link
Member

stof commented Feb 9, 2017

even better than the assertion:

if (class_exists(YourAnnotation::class, false)) {
    throw new LogicException('The YourAnnotation must not be used in other tests for this test to be useful');
}

We want to mark the test as an error when someone messes with the testsuite requirement, not as failed. The issue is not in the code but in the testsuite

@gnat42
Copy link
Contributor Author

gnat42 commented Feb 9, 2017

I think that I've addressed all changes/suggestions. Let me know if I missed any.

@mikeSimonson mikeSimonson merged commit 2b0630f into doctrine:master Feb 9, 2017
@mikeSimonson
Copy link
Contributor

@gnat42 Thanks

@mikeSimonson
Copy link
Contributor

mikeSimonson commented Feb 9, 2017

Fixes #115

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

Successfully merging this pull request may close these issues.

4 participants