123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- /*
- * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- namespace JMS\Serializer\Tests\Metadata\Driver;
- use JMS\Serializer\Metadata\Driver\AnnotationDriver;
- use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
- use Doctrine\Common\Annotations\AnnotationReader;
- use Doctrine\ORM\Configuration;
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Mapping\Driver\AnnotationDriver as DoctrineDriver;
- class DoctrineDriverTest extends \PHPUnit_Framework_TestCase
- {
- public function getMetadata()
- {
- $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\Doctrine\BlogPost');
- $metadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);
- return $metadata;
- }
- public function testTypelessPropertyIsGivenTypeFromDoctrineMetadata()
- {
- $metadata = $this->getMetadata();
- $this->assertEquals(
- array('name'=> 'DateTime', 'params' => array()),
- $metadata->propertyMetadata['createdAt']->type
- );
- }
- public function testSingleValuedAssociationIsProperlyHinted()
- {
- $metadata = $this->getMetadata();
- $this->assertEquals(
- array('name'=> 'JMS\Serializer\Tests\Fixtures\Doctrine\Author', 'params' => array()),
- $metadata->propertyMetadata['author']->type
- );
- }
- public function testMultiValuedAssociationIsProperlyHinted()
- {
- $metadata = $this->getMetadata();
- $this->assertEquals(
- array('name'=> 'ArrayCollection', 'params' => array(
- array('name' => 'JMS\Serializer\Tests\Fixtures\Doctrine\Comment', 'params' => array()))
- ),
- $metadata->propertyMetadata['comments']->type
- );
- }
- public function testTypeGuessByDoctrineIsOverwrittenByDelegateDriver()
- {
- $metadata = $this->getMetadata();
- // This would be guessed as boolean but we've overriden it to integer
- $this->assertEquals(
- array('name'=> 'integer', 'params' => array()),
- $metadata->propertyMetadata['published']->type
- );
- }
- public function testUnknownDoctrineTypeDoesNotResultInAGuess()
- {
- $metadata = $this->getMetadata();
- $this->assertNull($metadata->propertyMetadata['slug']->type);
- }
- public function testNonDoctrineEntityClassIsNotModified()
- {
- // Note: Using regular BlogPost fixture here instead of Doctrine fixture
- // because it has no Doctrine metadata.
- $refClass = new \ReflectionClass('JMS\Serializer\Tests\Fixtures\BlogPost');
- $plainMetadata = $this->getAnnotationDriver()->loadMetadataForClass($refClass);
- $doctrineMetadata = $this->getDoctrineDriver()->loadMetadataForClass($refClass);
- // Do not compare timestamps
- if (abs($doctrineMetadata->createdAt - $plainMetadata->createdAt) < 2) {
- $plainMetadata->createdAt = $doctrineMetadata->createdAt;
- }
- $this->assertEquals($plainMetadata, $doctrineMetadata);
- }
- protected function getEntityManager()
- {
- $config = new Configuration();
- $config->setProxyDir(sys_get_temp_dir() . '/JMSDoctrineTestProxies');
- $config->setProxyNamespace('JMS\Tests\Proxies');
- $config->setMetadataDriverImpl(
- new DoctrineDriver(new AnnotationReader(), __DIR__.'/../../Fixtures/Doctrine')
- );
- $conn = array(
- 'driver' => 'pdo_sqlite',
- 'memory' => true,
- );
- return EntityManager::create($conn, $config);
- }
- public function getAnnotationDriver()
- {
- return new AnnotationDriver(new AnnotationReader());
- }
- protected function getDoctrineDriver()
- {
- $registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
- $registry->expects($this->atLeastOnce())
- ->method('getManagerForClass')
- ->will($this->returnValue($this->getEntityManager()));
- return new DoctrineTypeDriver(
- $this->getAnnotationDriver(),
- $registry
- );
- }
- }
|