|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB\View; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\CouchDB\HTTP\SocketClient; |
|
6
|
|
|
use Doctrine\CouchDB\View\Query; |
|
7
|
|
|
use Doctrine\ODM\CouchDB\View\DoctrineAssociations; |
|
8
|
|
|
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata; |
|
9
|
|
|
use Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase; |
|
10
|
|
|
|
|
11
|
|
|
class DoctrineAssociationsTest extends CouchDBFunctionalTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
private $dm; |
|
14
|
|
|
|
|
15
|
|
|
public function setUp() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->dm = $this->createDocumentManager(); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return NativeQuery |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function createDoctrineViewQuery() |
|
24
|
|
|
{ |
|
25
|
|
|
return new Query( |
|
26
|
|
|
$this->dm->getHttpClient(), |
|
27
|
|
|
$this->dm->getDatabase(), |
|
28
|
|
|
'doctrine', |
|
29
|
|
|
'inverse_associations', |
|
30
|
|
|
new DoctrineAssociations() |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function addTestData() |
|
35
|
|
|
{ |
|
36
|
|
|
$db = $this->dm->getHttpClient(); |
|
37
|
|
|
|
|
38
|
|
|
// Force empty test database |
|
39
|
|
|
try { |
|
40
|
|
|
$db->request( 'DELETE', '/' . $this->getTestDatabase() . '' ); |
|
41
|
|
|
} catch ( \Exception $e ) { /* Irrelevant exception */ } |
|
42
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '' ); |
|
43
|
|
|
|
|
44
|
|
|
// Create some "interesting" documents |
|
45
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/doc_a', json_encode( array( |
|
46
|
|
|
"_id" => "doc_a", |
|
47
|
|
|
"type" => "type_a", |
|
48
|
|
|
"doctrine_metadata" => array( |
|
49
|
|
|
"associations" => array("type_b", "type_c"), |
|
50
|
|
|
), |
|
51
|
|
|
"type_b" => array( "doc_b" ), |
|
52
|
|
|
"type_c" => array( "doc_d" ), |
|
53
|
|
|
) ) ); |
|
54
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/doc_b', json_encode( array( |
|
55
|
|
|
"_id" => "doc_b", |
|
56
|
|
|
"doctrine_metadata" => array( |
|
57
|
|
|
"type" => "type_b", |
|
58
|
|
|
"associations" => array("type_c") |
|
59
|
|
|
), |
|
60
|
|
|
"type_c" => array( "doc_c", "doc_d" ), |
|
61
|
|
|
) ) ); |
|
62
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/doc_c', json_encode( array( |
|
63
|
|
|
"_id" => "doc_c", |
|
64
|
|
|
"doctrine_metadata" => array( |
|
65
|
|
|
"type" => "type_c", |
|
66
|
|
|
"associations" => array(), |
|
67
|
|
|
), |
|
68
|
|
|
) ) ); |
|
69
|
|
|
$db->request( 'PUT', '/' . $this->getTestDatabase() . '/doc_d', json_encode( array( |
|
70
|
|
|
"_id" => "doc_d", |
|
71
|
|
|
"doctrine_metadata" => array( |
|
72
|
|
|
"type" => "type_c", |
|
73
|
|
|
"associations" => array(), |
|
74
|
|
|
), |
|
75
|
|
|
) ) ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testFetchReverseRelations() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->addTestData(); |
|
81
|
|
|
|
|
82
|
|
|
$view = $this->createDoctrineViewQuery(); |
|
83
|
|
|
$result = $view->setStartKey( array("doc_b", "type_b") ) |
|
|
|
|
|
|
84
|
|
|
->setEndKey( array("doc_b", "type_b", "z") ) |
|
|
|
|
|
|
85
|
|
|
->execute(); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals(array("_id" => "doc_a"), $result[0]['value']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testFetchReverseRelations1() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->addTestData(); |
|
93
|
|
|
|
|
94
|
|
|
$view = $this->createDoctrineViewQuery(); |
|
95
|
|
|
$result = $view->setStartKey( array("doc_d", "type_c") ) |
|
|
|
|
|
|
96
|
|
|
->setEndKey( array("doc_d", "type_c", "z") ) |
|
|
|
|
|
|
97
|
|
|
->execute(); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals(array("_id" => "doc_a"), $result[0]['value']); |
|
100
|
|
|
$this->assertEquals(array("_id" => "doc_b"), $result[1]['value']); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
View Code Duplication |
public function testFetchReverseRelations2() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$this->addTestData(); |
|
106
|
|
|
|
|
107
|
|
|
$view = $this->createDoctrineViewQuery(); |
|
108
|
|
|
$result = $view->setStartKey( array("doc_d", "type_b") ) |
|
|
|
|
|
|
109
|
|
|
->setEndKey( array("doc_d", "type_b", "z") ) |
|
|
|
|
|
|
110
|
|
|
->execute(); |
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals(array(), $result->toArray() ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
public function testFetchNoReverseRelations() |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$this->addTestData(); |
|
119
|
|
|
|
|
120
|
|
|
$view = $this->createDoctrineViewQuery(); |
|
121
|
|
|
$result = $view->setStartKey( array("doc_c", "type_a") ) |
|
|
|
|
|
|
122
|
|
|
->setEndKey( array("doc_c", "type_a", "z") ) |
|
|
|
|
|
|
123
|
|
|
->execute(); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals(array(), $result->toArray() ); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: