1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Test; |
15
|
|
|
|
16
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
17
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
18
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; |
19
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
20
|
|
|
use MongoDB\Client; |
21
|
|
|
use MongoDB\Model\DatabaseInfo; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @see https://github.com/doctrine/mongodb-odm/blob/2.0.0-RC1/tests/Doctrine/ODM/MongoDB/Tests/BaseTest.php |
26
|
|
|
*/ |
27
|
|
|
abstract class DoctrineMongoDbOdmTestCase extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var DocumentManager |
31
|
|
|
*/ |
32
|
|
|
protected $dm; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var UnitOfWork |
36
|
|
|
*/ |
37
|
|
|
protected $uow; |
38
|
|
|
|
39
|
|
|
protected function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->dm = $this->createTestDocumentManager(); |
42
|
|
|
$this->uow = $this->dm->getUnitOfWork(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function tearDown() |
46
|
|
|
{ |
47
|
|
|
if (null === $this->dm) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Check if the database exists. Calling listCollections on a non-existing |
52
|
|
|
// database in a sharded setup will cause an invalid command cursor to be |
53
|
|
|
// returned |
54
|
|
|
$client = $this->dm->getClient(); |
55
|
|
|
$databases = iterator_to_array($client->listDatabases()); |
56
|
|
|
$databaseNames = array_map(static function (DatabaseInfo $database) { |
57
|
|
|
return $database->getName(); |
58
|
|
|
}, $databases); |
59
|
|
|
if (!\in_array(\DOCTRINE_MONGODB_DATABASE, $databaseNames, true)) { |
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$collections = $client->selectDatabase(\DOCTRINE_MONGODB_DATABASE)->listCollections(); |
64
|
|
|
|
65
|
|
|
foreach ($collections as $collection) { |
66
|
|
|
// See https://jira.mongodb.org/browse/SERVER-16541 |
67
|
|
|
if ('system.indexes' === $collection->getName()) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$client->selectCollection(\DOCTRINE_MONGODB_DATABASE, $collection->getName())->drop(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getConfiguration() |
76
|
|
|
{ |
77
|
|
|
$config = new Configuration(); |
78
|
|
|
|
79
|
|
|
$config->setProxyDir(__DIR__.'/../../../../Proxies'); |
80
|
|
|
$config->setProxyNamespace('Proxies'); |
81
|
|
|
$config->setHydratorDir(__DIR__.'/../../../../Hydrators'); |
82
|
|
|
$config->setHydratorNamespace('Hydrators'); |
83
|
|
|
$config->setPersistentCollectionDir(__DIR__.'/../../../../PersistentCollections'); |
84
|
|
|
$config->setPersistentCollectionNamespace('PersistentCollections'); |
85
|
|
|
$config->setDefaultDB(\DOCTRINE_MONGODB_DATABASE); |
|
|
|
|
86
|
|
|
$config->setMetadataDriverImpl($this->createMetadataDriverImpl()); |
87
|
|
|
|
88
|
|
|
return $config; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function createMetadataDriverImpl() |
92
|
|
|
{ |
93
|
|
|
return AnnotationDriver::create(__DIR__.'/../../../../Documents'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function createTestDocumentManager() |
97
|
|
|
{ |
98
|
|
|
$config = $this->getConfiguration(); |
99
|
|
|
$client = new Client(getenv('DOCTRINE_MONGODB_SERVER') ?: \DOCTRINE_MONGODB_SERVER, [], ['typeMap' => ['root' => 'array', 'document' => 'array']]); |
|
|
|
|
100
|
|
|
|
101
|
|
|
return DocumentManager::create($client, $config); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function getServerVersion() |
105
|
|
|
{ |
106
|
|
|
$result = $this->dm->getClient()->selectDatabase(\DOCTRINE_MONGODB_DATABASE)->command(['buildInfo' => 1])->toArray()[0]; |
|
|
|
|
107
|
|
|
|
108
|
|
|
return $result['version']; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function skipTestIfNotSharded($className) |
112
|
|
|
{ |
113
|
|
|
$result = $this->dm->getDocumentDatabase($className)->command(['listCommands' => true])->toArray()[0]; |
114
|
|
|
if (!$result['ok']) { |
115
|
|
|
$this->markTestSkipped('Could not check whether server supports sharding'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (\array_key_exists('shardCollection', $result['commands'])) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->markTestSkipped('Test skipped because server does not support sharding'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function requireVersion($installedVersion, $requiredVersion, $operator, $message) |
126
|
|
|
{ |
127
|
|
|
if (!version_compare($installedVersion, $requiredVersion, $operator)) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->markTestSkipped($message); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function requireMongoDB32($message) |
135
|
|
|
{ |
136
|
|
|
$this->requireVersion($this->getServerVersion(), '3.2.0', '<', $message); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected function skipOnMongoDB34($message) |
140
|
|
|
{ |
141
|
|
|
$this->requireVersion($this->getServerVersion(), '3.4.0', '>=', $message); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
protected function requireMongoDB34($message) |
145
|
|
|
{ |
146
|
|
|
$this->requireVersion($this->getServerVersion(), '3.4.0', '<', $message); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|