1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace JMS\Serializer\Tests\Metadata\Driver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver as DoctrineDriver;
use JMS\Serializer\Metadata\Driver\AnnotationDriver;
use JMS\Serializer\Metadata\Driver\DoctrineTypeDriver;
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);
}
public function testExcludePropertyNoPublicAccessorException()
{
$first = $this->getAnnotationDriver()
->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));
$this->assertArrayHasKey('id', $first->propertyMetadata);
$this->assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
}
public function testVirtualPropertiesAreNotModified()
{
$doctrineMetadata = $this->getMetadata();
$this->assertNull($doctrineMetadata->propertyMetadata['ref']->type);
}
public function testGuidPropertyIsGivenStringType()
{
$metadata = $this->getMetadata();
$this->assertEquals(
array('name' => 'string', 'params' => array()),
$metadata->propertyMetadata['id']->type
);
}
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->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
$registry->expects($this->atLeastOnce())
->method('getManagerForClass')
->will($this->returnValue($this->getEntityManager()));
return new DoctrineTypeDriver(
$this->getAnnotationDriver(),
$registry
);
}
}