1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService; |
6
|
|
|
|
7
|
|
|
class ProxyTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var DocumentManager |
11
|
|
|
*/ |
12
|
|
|
private $dm; |
13
|
|
|
|
14
|
|
|
public function setUp() |
15
|
|
|
{ |
16
|
|
|
$this->type = 'Doctrine\Tests\ODM\CouchDB\Functional\Article'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
$this->dm = $this->createDocumentManager(); |
|
|
|
|
19
|
|
|
|
20
|
|
|
$database = $this->dm->getDatabase(); |
21
|
|
|
$httpClient = $this->dm->getHttpClient(); |
22
|
|
|
|
23
|
|
|
$httpClient->request('DELETE', '/' . $database); |
24
|
|
|
$resp = $httpClient->request('PUT', '/' . $database); |
25
|
|
|
$this->assertEquals(201, $resp->status); |
26
|
|
|
|
27
|
|
|
$data = json_encode(array( |
28
|
|
|
'_id' => "1", |
29
|
|
|
'title' => 'foo', |
30
|
|
|
'body' => 'bar', |
31
|
|
|
'type' => $this->type |
32
|
|
|
)); |
33
|
|
|
$resp = $httpClient->request('PUT', '/' . $database . '/1', $data); |
34
|
|
|
$this->assertEquals(201, $resp->status); |
35
|
|
|
|
36
|
|
|
$cmf = $this->dm->getClassMetadataFactory(); |
37
|
|
|
$metadata = new \Doctrine\ODM\CouchDB\Mapping\ClassMetadata($this->type); |
38
|
|
|
$metadata->mapField(array('fieldName' => 'id', 'id' => true)); |
39
|
|
|
$metadata->mapField(array('fieldName' => 'title', 'type' => 'string')); |
40
|
|
|
$metadata->mapField(array('fieldName' => 'body', 'type' => 'string')); |
41
|
|
|
$metadata->idGenerator = \Doctrine\ODM\CouchDB\Mapping\ClassMetadata::IDGENERATOR_UUID; |
42
|
|
|
$metadata->initializeReflection(new RuntimeReflectionService()); |
43
|
|
|
$metadata->wakeupReflection(new RuntimeReflectionService()); |
44
|
|
|
$cmf->setMetadataFor($this->type, $metadata); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGetReference() |
48
|
|
|
{ |
49
|
|
|
$proxy = $this->dm->getReference($this->type, 1); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf('Doctrine\ODM\CouchDB\Proxy\Proxy', $proxy); |
52
|
|
|
$this->assertFalse($proxy->__isInitialized__); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('foo', $proxy->getTitle()); |
55
|
|
|
$this->assertTrue($proxy->__isInitialized__); |
56
|
|
|
$this->assertEquals('bar', $proxy->getBody()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testProxyFactorySetsProxyMetadata() |
60
|
|
|
{ |
61
|
|
|
$proxy = $this->dm->getReference($this->type, 1); |
62
|
|
|
|
63
|
|
|
$proxyClass = get_class($proxy); |
64
|
|
|
$this->assertTrue($this->dm->getClassMetadataFactory()->hasMetadataFor($proxyClass), "Proxy class '" . $proxyClass . "' should be registered as metadata."); |
65
|
|
|
$this->assertSame($this->dm->getClassMetadata($proxyClass), $this->dm->getClassMetadata($this->type), "Metadata instances of proxy class and real instance have to be the same."); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
class Article |
70
|
|
|
{ |
71
|
|
|
private $id; |
72
|
|
|
private $title; |
73
|
|
|
private $body; |
74
|
|
|
|
75
|
|
|
public function __construct($title, $body) |
76
|
|
|
{ |
77
|
|
|
$this->title = $title; |
78
|
|
|
$this->body = $body; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getTitle() |
82
|
|
|
{ |
83
|
|
|
return $this->title; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getBody() |
87
|
|
|
{ |
88
|
|
|
return $this->body; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: