2010-02-02 21:17:00 +00:00
< ? php
namespace Doctrine\Tests\ORM\Mapping ;
2016-06-18 13:01:59 +02:00
use Doctrine\Common\Annotations\AnnotationException ;
2016-05-11 01:55:12 +07:00
use Doctrine\Common\Annotations\AnnotationReader ;
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService ;
2016-12-08 18:01:04 +01:00
use Doctrine\ORM\Mapping ;
2010-02-02 21:17:00 +00:00
use Doctrine\ORM\Mapping\ClassMetadata ;
2016-05-11 01:55:12 +07:00
use Doctrine\ORM\Mapping\ClassMetadataFactory ;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver ;
2016-06-18 13:01:59 +02:00
use Doctrine\ORM\Mapping\MappingException ;
2016-12-08 18:01:04 +01:00
use Doctrine\Tests\Models\CMS\CmsUser ;
use Doctrine\Tests\Models\DDC1872\DDC1872ExampleEntityWithoutOverride ;
use Doctrine\Tests\Models\DDC1872\DDC1872ExampleEntityWithOverride ;
use Doctrine\Tests\Models\DirectoryTree\Directory ;
use Doctrine\Tests\Models\DirectoryTree\File ;
use Doctrine\Tests\Models\ECommerce\ECommerceCart ;
2010-02-02 21:17:00 +00:00
2010-02-14 19:38:22 +00:00
class AnnotationDriverTest extends AbstractMappingDriverTest
2010-02-02 21:17:00 +00:00
{
/**
* @ group DDC - 268
*/
public function testLoadMetadataForNonEntityThrowsException ()
{
$cm = new ClassMetadata ( 'stdClass' );
2016-05-11 01:55:12 +07:00
$cm -> initializeReflection ( new RuntimeReflectionService ());
$reader = new AnnotationReader ();
$annotationDriver = new AnnotationDriver ( $reader );
2010-02-02 21:17:00 +00:00
2016-06-18 13:01:59 +02:00
$this -> expectException ( \Doctrine\ORM\Mapping\MappingException :: class );
2010-02-02 21:17:00 +00:00
$annotationDriver -> loadMetadataForClass ( 'stdClass' , $cm );
}
2015-06-20 14:24:01 +02:00
/**
* @ expectedException Doctrine\ORM\Cache\CacheException
* @ expectedExceptionMessage Entity association field " Doctrine \T ests \ ORM \ Mapping \ AnnotationSLC#foo " not configured as part of the second - level cache .
*/
public function testFailingSecondLevelCacheAssociation ()
{
$mappingDriver = $this -> _loadDriver ();
2016-12-08 18:01:04 +01:00
$class = new ClassMetadata ( AnnotationSLC :: class );
$mappingDriver -> loadMetadataForClass ( AnnotationSLC :: class , $class );
2015-06-20 14:24:01 +02:00
}
2010-02-02 21:17:00 +00:00
/**
* @ group DDC - 268
*/
2010-02-09 17:13:49 +00:00
public function testColumnWithMissingTypeDefaultsToString ()
2010-02-02 21:17:00 +00:00
{
2016-12-08 18:01:04 +01:00
$cm = new ClassMetadata ( ColumnWithoutType :: class );
2016-05-11 01:55:12 +07:00
$cm -> initializeReflection ( new RuntimeReflectionService ());
2010-08-08 12:29:14 +02:00
$annotationDriver = $this -> _loadDriver ();
2010-02-02 21:17:00 +00:00
2016-12-08 18:01:04 +01:00
$annotationDriver -> loadMetadataForClass ( Mapping\InvalidColumn :: class , $cm );
2010-02-09 17:13:49 +00:00
$this -> assertEquals ( 'string' , $cm -> fieldMappings [ 'id' ][ 'type' ]);
2010-02-02 21:17:00 +00:00
}
2010-02-14 19:38:22 +00:00
2010-03-18 23:04:21 +00:00
/**
* @ 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 ()
{
2016-12-08 18:01:04 +01:00
$this -> _ensureIsLoaded ( CmsUser :: class );
2010-03-18 23:04:21 +00:00
$annotationDriver = $this -> _loadDriverForCMSModels ();
$classes = $annotationDriver -> getAllClassNames ();
2016-12-08 18:01:04 +01:00
$this -> assertContains ( CmsUser :: class , $classes );
2010-03-18 23:04:21 +00:00
}
/**
* @ group DDC - 318
*/
public function testGetClassNamesReturnsOnlyTheAppropriateClasses ()
{
2016-12-08 18:01:04 +01:00
$this -> _ensureIsLoaded ( ECommerceCart :: class );
2010-03-18 23:04:21 +00:00
$annotationDriver = $this -> _loadDriverForCMSModels ();
$classes = $annotationDriver -> getAllClassNames ();
2016-12-08 18:01:04 +01:00
$this -> assertNotContains ( ECommerceCart :: class , $classes );
2010-03-18 23:04:21 +00:00
}
protected function _loadDriverForCMSModels ()
{
$annotationDriver = $this -> _loadDriver ();
2016-12-07 23:33:41 +01:00
$annotationDriver -> addPaths ([ __DIR__ . '/../../Models/CMS/' ]);
2010-03-18 23:04:21 +00:00
return $annotationDriver ;
}
2010-02-14 19:38:22 +00:00
protected function _loadDriver ()
{
2011-05-25 00:26:20 +02:00
return $this -> createAnnotationDriver ();
2010-02-14 19:38:22 +00:00
}
2010-03-18 23:04:21 +00:00
protected function _ensureIsLoaded ( $entityClassName )
{
new $entityClassName ;
}
2010-09-21 23:14:45 +02:00
/**
* @ group DDC - 671
*
* Entities for this test are in AbstractMappingDriverTest
*/
public function testJoinTablesWithMappedSuperclassForAnnotationDriver ()
{
2010-09-21 23:53:26 +02:00
$annotationDriver = $this -> _loadDriver ();
2016-12-07 23:33:41 +01:00
$annotationDriver -> addPaths ([ __DIR__ . '/../../Models/DirectoryTree/' ]);
2010-09-21 23:14:45 +02:00
2010-09-21 23:53:26 +02:00
$em = $this -> _getTestEntityManager ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $annotationDriver );
2016-05-11 01:55:12 +07:00
$factory = new ClassMetadataFactory ();
2010-11-27 20:53:26 +01:00
$factory -> setEntityManager ( $em );
2010-09-21 23:14:45 +02:00
2016-12-08 18:01:04 +01:00
$classPage = $factory -> getMetadataFor ( File :: class );
$this -> assertEquals ( File :: class , $classPage -> associationMappings [ 'parentDirectory' ][ 'sourceEntity' ]);
2010-09-21 23:14:45 +02:00
2016-12-08 18:01:04 +01:00
$classDirectory = $factory -> getMetadataFor ( Directory :: class );
$this -> assertEquals ( Directory :: class , $classDirectory -> associationMappings [ 'parentDirectory' ][ 'sourceEntity' ]);
2010-09-21 23:14:45 +02:00
}
2010-12-28 11:59:51 +01:00
/**
* @ group DDC - 945
*/
public function testInvalidMappedSuperClassWithManyToManyAssociation ()
{
$annotationDriver = $this -> _loadDriver ();
$em = $this -> _getTestEntityManager ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $annotationDriver );
2016-05-11 01:55:12 +07:00
$factory = new ClassMetadataFactory ();
2010-12-28 11:59:51 +01:00
$factory -> setEntityManager ( $em );
2016-06-18 13:01:59 +02:00
$this -> expectException ( MappingException :: class );
$this -> expectExceptionMessage (
" It is illegal to put an inverse side one-to-many or many-to-many association on " .
" mapped superclass 'Doctrine \T ests \ ORM \ Mapping \ InvalidMappedSuperClass#users' "
);
2016-12-08 18:01:04 +01:00
$usingInvalidMsc = $factory -> getMetadataFor ( UsingInvalidMappedSuperClass :: class );
2010-12-28 11:59:51 +01:00
}
2011-12-19 22:56:19 +01:00
2011-03-03 22:51:53 +01:00
/**
* @ group DDC - 1050
*/
public function testInvalidMappedSuperClassWithInheritanceInformation ()
{
$annotationDriver = $this -> _loadDriver ();
$em = $this -> _getTestEntityManager ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $annotationDriver );
2016-05-11 01:55:12 +07:00
$factory = new ClassMetadataFactory ();
2011-03-03 22:51:53 +01:00
$factory -> setEntityManager ( $em );
2016-06-18 13:01:59 +02:00
$this -> expectException ( MappingException :: class );
$this -> expectExceptionMessage (
" It is not supported to define inheritance information on a mapped " .
2016-12-08 18:01:04 +01:00
" superclass ' " . MappedSuperClassInheritence :: class . " '. "
2016-06-18 13:01:59 +02:00
);
2016-12-08 18:01:04 +01:00
$usingInvalidMsc = $factory -> getMetadataFor ( MappedSuperClassInheritence :: class );
2011-03-03 22:51:53 +01:00
}
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
/**
* @ group DDC - 1034
*/
public function testInheritanceSkipsParentLifecycleCallbacks ()
{
$annotationDriver = $this -> _loadDriver ();
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
$em = $this -> _getTestEntityManager ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $annotationDriver );
2016-05-11 01:55:12 +07:00
$factory = new ClassMetadataFactory ();
2011-03-04 23:00:54 +01:00
$factory -> setEntityManager ( $em );
2011-12-19 22:56:19 +01:00
2016-12-08 18:01:04 +01:00
$cm = $factory -> getMetadataFor ( AnnotationChild :: class );
2016-12-07 23:33:41 +01:00
$this -> assertEquals ([ " postLoad " => [ " postLoad " ], " preUpdate " => [ " preUpdate " ]], $cm -> lifecycleCallbacks );
2011-12-19 22:56:19 +01:00
2016-12-08 18:01:04 +01:00
$cm = $factory -> getMetadataFor ( AnnotationParent :: class );
2016-12-07 23:33:41 +01:00
$this -> assertEquals ([ " postLoad " => [ " postLoad " ], " preUpdate " => [ " preUpdate " ]], $cm -> lifecycleCallbacks );
2011-03-04 23:00:54 +01:00
}
2011-12-19 22:56:19 +01:00
2011-06-05 15:00:49 +02:00
/**
* @ group DDC - 1156
*/
2013-03-11 00:08:58 +00:00
public function testMappedSuperclassInMiddleOfInheritanceHierarchy ()
2011-06-05 15:00:49 +02:00
{
$annotationDriver = $this -> _loadDriver ();
2011-12-19 22:56:19 +01:00
2011-06-05 15:00:49 +02:00
$em = $this -> _getTestEntityManager ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $annotationDriver );
2016-05-11 01:55:12 +07:00
$factory = new ClassMetadataFactory ();
2011-06-05 15:00:49 +02:00
$factory -> setEntityManager ( $em );
2011-12-19 22:56:19 +01:00
2016-12-08 18:01:04 +01:00
$cm = $factory -> getMetadataFor ( ChildEntity :: class );
2011-06-05 15:00:49 +02:00
}
2011-11-08 10:01:22 +01:00
public function testInvalidFetchOptionThrowsException ()
{
$annotationDriver = $this -> _loadDriver ();
$em = $this -> _getTestEntityManager ();
$em -> getConfiguration () -> setMetadataDriverImpl ( $annotationDriver );
2016-05-11 01:55:12 +07:00
$factory = new ClassMetadataFactory ();
2011-11-08 10:01:22 +01:00
$factory -> setEntityManager ( $em );
2016-06-18 13:01:59 +02:00
$this -> expectException ( AnnotationException :: class );
$this -> expectExceptionMessage ( '[Enum Error] Attribute "fetch" of @Doctrine\ORM\Mapping\OneToMany declared on property Doctrine\Tests\ORM\Mapping\InvalidFetchOption::$collection accept only [LAZY, EAGER, EXTRA_LAZY], but got eager.' );
2016-12-08 18:01:04 +01:00
$factory -> getMetadataFor ( InvalidFetchOption :: class );
2011-11-08 10:01:22 +01:00
}
2012-07-29 00:03:25 -05:00
public function testAttributeOverridesMappingWithTrait ()
{
$factory = $this -> createClassMetadataFactory ();
2016-12-08 18:01:04 +01:00
$metadataWithoutOverride = $factory -> getMetadataFor ( DDC1872ExampleEntityWithoutOverride :: class );
$metadataWithOverride = $factory -> getMetadataFor ( DDC1872ExampleEntityWithOverride :: class );
2012-07-29 00:03:25 -05:00
$this -> assertEquals ( 'trait_foo' , $metadataWithoutOverride -> fieldMappings [ 'foo' ][ 'columnName' ]);
$this -> assertEquals ( 'foo_overridden' , $metadataWithOverride -> fieldMappings [ 'foo' ][ 'columnName' ]);
$this -> assertArrayHasKey ( 'example_trait_bar_id' , $metadataWithoutOverride -> associationMappings [ 'bar' ][ 'joinColumnFieldNames' ]);
$this -> assertArrayHasKey ( 'example_entity_overridden_bar_id' , $metadataWithOverride -> associationMappings [ 'bar' ][ 'joinColumnFieldNames' ]);
}
2010-02-02 21:17:00 +00:00
}
/**
* @ Entity
*/
2010-02-14 19:38:22 +00:00
class ColumnWithoutType
2010-02-02 21:17:00 +00:00
{
/** @Id @Column */
public $id ;
}
2010-12-28 11:59:51 +01:00
/**
* @ MappedSuperclass
*/
class InvalidMappedSuperClass
{
/**
2010-12-31 14:39:01 +01:00
* @ ManyToMany ( targetEntity = " Doctrine \T ests \ Models \ CMS \ CmsUser " , mappedBy = " invalid " )
2010-12-28 11:59:51 +01:00
*/
private $users ;
}
/**
* @ Entity
*/
class UsingInvalidMappedSuperClass extends InvalidMappedSuperClass
{
/**
* @ Id @ Column ( type = " integer " ) @ GeneratedValue
*/
private $id ;
2011-03-03 22:51:53 +01:00
}
/**
* @ MappedSuperclass
* @ InheritanceType ( " JOINED " )
* @ DiscriminatorMap ({ " test " = " ColumnWithoutType " })
*/
class MappedSuperClassInheritence
{
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
}
/**
* @ Entity
* @ InheritanceType ( " JOINED " )
* @ DiscriminatorMap ({ " parent " = " AnnotationParent " , " child " = " AnnotationChild " })
* @ HasLifecycleCallbacks
*/
class AnnotationParent
{
/**
* @ Id @ Column ( type = " integer " ) @ GeneratedValue
*/
private $id ;
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
/**
* @ PostLoad
*/
public function postLoad ()
{
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
}
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
/**
* @ PreUpdate
*/
public function preUpdate ()
{
2011-12-19 22:56:19 +01:00
2011-03-04 23:00:54 +01:00
}
}
/**
* @ Entity
* @ HasLifecycleCallbacks
*/
class AnnotationChild extends AnnotationParent
{
2011-12-19 22:56:19 +01:00
2011-06-05 15:00:49 +02:00
}
/**
* @ Entity
* @ InheritanceType ( " SINGLE_TABLE " )
* @ DiscriminatorMap ({ " s " = " SuperEntity " , " c " = " ChildEntity " })
*/
class SuperEntity
{
/** @Id @Column(type="string") */
private $id ;
}
/**
* @ MappedSuperclass
*/
class MiddleMappedSuperclass extends SuperEntity
{
/** @Column(type="string") */
private $name ;
}
/**
* @ Entity
*/
class ChildEntity extends MiddleMappedSuperclass
{
/**
* @ Column ( type = " string " )
*/
private $text ;
2011-11-08 10:01:22 +01:00
}
/**
* @ Entity
*/
class InvalidFetchOption
{
/**
* @ OneToMany ( targetEntity = " Doctrine \T ests \ Models \ CMS \ CmsUser " , fetch = " eager " )
*/
private $collection ;
2014-04-07 14:43:25 +02:00
}
2015-06-20 14:24:01 +02:00
/**
* @ Entity
* @ Cache
*/
class AnnotationSLC
{
/**
* @ Id
* @ ManyToOne ( targetEntity = " AnnotationSLCFoo " )
*/
public $foo ;
}
/**
* @ Entity
*/
class AnnotationSLCFoo
{
/**
* @ Column ( type = " string " )
*/
public $id ;
}