diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index 72dd544fa..e0a53394c 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -431,7 +431,7 @@ class AnnotationDriver implements Driver $classes = array(); if ($this->_paths) { - $declared = get_declared_classes(); + $includedFiles = array(); foreach ((array) $this->_paths as $path) { if ( ! is_dir($path)) { @@ -447,15 +447,19 @@ class AnnotationDriver implements Driver if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) { continue; } - - require_once $file->getPathName(); + + $sourceFile = realpath($file->getPathName()); + require_once $sourceFile; + $includedFiles[] = $sourceFile; } } - $declared = array_diff(get_declared_classes(), $declared); + $declared = get_declared_classes(); foreach ($declared as $className) { - if ( ! $this->isTransient($className)) { + $rc = new \ReflectionClass($className); + $sourceFile = $rc->getFileName(); + if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) { $classes[] = $className; } } @@ -466,4 +470,4 @@ class AnnotationDriver implements Driver return $classes; } -} \ No newline at end of file +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php index 5c3341619..484ce01ba 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/AnnotationDriverTest.php @@ -36,6 +36,68 @@ class AnnotationDriverTest extends AbstractMappingDriverTest $this->assertEquals('string', $cm->fieldMappings['id']['type']); } + /** + * @group DDC-318 + */ + public function testGetAllClassNamesIsIdempotent() + { + $annotationDriver = $this->_loadDriverForCMSModels(); + $original = $annotationDriver->getAllClassNames(); + + $annotationDriver = $this->_loadDriverForCMSModels(); + $afterTestReset = $annotationDriver->getAllClassNames(); + + $this->assertEquals($original, $afterTestReset); + } + + /** + * @group DDC-318 + */ + public function testGetAllClassNamesIsIdempotentEvenWithDifferentDriverInstances() + { + $annotationDriver = $this->_loadDriverForCMSModels(); + $original = $annotationDriver->getAllClassNames(); + + $annotationDriver = $this->_loadDriverForCMSModels(); + $afterTestReset = $annotationDriver->getAllClassNames(); + + $this->assertEquals($original, $afterTestReset); + } + + /** + * @group DDC-318 + */ + public function testGetAllClassNamesReturnsAlreadyLoadedClassesIfAppropriate() + { + $rightClassName = 'Doctrine\Tests\Models\CMS\CmsUser'; + $this->_ensureIsLoaded($rightClassName); + + $annotationDriver = $this->_loadDriverForCMSModels(); + $classes = $annotationDriver->getAllClassNames(); + + $this->assertContains($rightClassName, $classes); + } + + /** + * @group DDC-318 + */ + public function testGetClassNamesReturnsOnlyTheAppropriateClasses() + { + $extraneousClassName = 'Doctrine\Tests\Models\ECommerce\ECommerceCart'; + $this->_ensureIsLoaded($extraneousClassName); + + $annotationDriver = $this->_loadDriverForCMSModels(); + $classes = $annotationDriver->getAllClassNames(); + + $this->assertNotContains($extraneousClassName, $classes); + } + + protected function _loadDriverForCMSModels() + { + $annotationDriver = $this->_loadDriver(); + $annotationDriver->addPaths(array(__DIR__ . '/../../Models/CMS/')); + return $annotationDriver; + } protected function _loadDriver() { @@ -44,6 +106,11 @@ class AnnotationDriverTest extends AbstractMappingDriverTest $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\'); return new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader); } + + protected function _ensureIsLoaded($entityClassName) + { + new $entityClassName; + } } /**