|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yawik\Migration\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use Auth\Entity\User; |
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
9
|
|
|
use MongoDB\Client; |
|
10
|
|
|
use MongoDB\Collection; |
|
11
|
|
|
use MongoDB\Database; |
|
12
|
|
|
use MongoDB\GridFS\Bucket; |
|
13
|
|
|
use Psr\Container\ContainerInterface; |
|
14
|
|
|
use Yawik\Migration\Util\MongoUtilTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Trait DatabaseConcernTrait |
|
18
|
|
|
* @package Yawik\Migration\Tests |
|
19
|
|
|
*/ |
|
20
|
|
|
trait DatabaseConcernTrait |
|
21
|
|
|
{ |
|
22
|
|
|
use MongoUtilTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ContainerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $dbConcernContainer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var DocumentManager |
|
31
|
|
|
*/ |
|
32
|
|
|
private DocumentManager $dbConcernDM; |
|
33
|
|
|
|
|
34
|
|
|
protected function initialize(ContainerInterface $dbConcernContainer) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->dbConcernContainer = $dbConcernContainer; |
|
37
|
|
|
$this->dbConcernDM = $dbConcernContainer->get(DocumentManager::class); |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getMongodbClient(): Client |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getDoctrine()->getClient(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getDatabase($className = User::class): Database |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->dbConcernDM->getDocumentDatabase($className); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getBucket(string $bucketName): Bucket |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->getDatabase()->selectGridFSBucket(['bucketName' => $bucketName]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getCollection(string $name, array $options = array()): Collection |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getDatabase()->selectCollection($name, $options); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function createFile(string $bucketName, string $source = __FILE__, $fileName = "test.php") |
|
63
|
|
|
{ |
|
64
|
|
|
$stream = fopen($source, 'rb'); |
|
65
|
|
|
$bucket = $this->getBucket($bucketName); |
|
66
|
|
|
$fileId = $bucket->uploadFromStream($fileName, $stream); |
|
67
|
|
|
$meta = $bucket->findOne(['_id' => $fileId]); |
|
68
|
|
|
$meta['name'] = "name.jpeg"; |
|
69
|
|
|
$meta['mimetype'] = 'test/content'; |
|
70
|
|
|
|
|
71
|
|
|
$collection = $this->getCollection($bucketName.'.files'); |
|
72
|
|
|
$collection->updateOne( |
|
73
|
|
|
['_id' => $fileId], |
|
74
|
|
|
['$set' => [ |
|
75
|
|
|
'name' => $fileName, |
|
76
|
|
|
'mimetype' => 'test/content', |
|
77
|
|
|
'dateuploaded' => [ |
|
78
|
|
|
"date" => "2016-06-13T13:48:05.000+00:00", |
|
79
|
|
|
"tz" => "Europe/Berlin" |
|
80
|
|
|
], |
|
81
|
|
|
'permissions' => [ |
|
82
|
|
|
"type" => "Core\\Entity\\Permissions" |
|
83
|
|
|
], |
|
84
|
|
|
"filename" => "/tmp/yk-copy", |
|
85
|
|
|
"uploadedDate" => "2016-06-13T13:48:05.473+00:00" |
|
86
|
|
|
]] |
|
87
|
|
|
); |
|
88
|
|
|
return $fileId; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function loadJson(string $file) |
|
92
|
|
|
{ |
|
93
|
|
|
$content = file_get_contents($file); |
|
94
|
|
|
return json_decode($content, true); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function insert(string $collectionName, array $document) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->getCollection($collectionName)->insertOne($document); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function drop(string $collectionName) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->getCollection($collectionName)->drop(); |
|
105
|
|
|
} |
|
106
|
|
|
} |