-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MongoHelper to reduce duplicaion in MongoTest
- Loading branch information
Showing
2 changed files
with
70 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace XHGui\Test\Searcher; | ||
|
||
use ArrayIterator; | ||
use MongoDB; | ||
use MultipleIterator; | ||
|
||
class MongoHelper | ||
{ | ||
/** @var MongoDB */ | ||
private $mongodb; | ||
/** @var array */ | ||
private $indexes = []; | ||
|
||
public function __construct(MongoDB $mongodb) | ||
{ | ||
$this->mongodb = $mongodb; | ||
} | ||
|
||
public function dropCollection(string $collectionName): void | ||
{ | ||
$collection = $this->mongodb->selectCollection($collectionName); | ||
$collection->drop(); | ||
} | ||
|
||
public function createCollection(string $collectionName, array $indexes): void | ||
{ | ||
$collection = $this->mongodb->createCollection($collectionName); | ||
|
||
foreach ($indexes as [$keys, $options]) { | ||
$collection->createIndex($keys, $options); | ||
} | ||
$this->indexes[$collectionName] = $indexes; | ||
} | ||
|
||
public function getIndexes(string $collectionName): MultipleIterator | ||
{ | ||
$collection = $this->mongodb->selectCollection($collectionName); | ||
$expectedIndexes = $this->indexes[$collectionName]; | ||
|
||
// assert that all indexes are intact after truncating | ||
// fetch indexes | ||
$resultIndexInfo = $collection->getIndexInfo(); | ||
$resultIndexes = array_column($resultIndexInfo, 'key'); | ||
$resultIndexNames = array_column($resultIndexInfo, 'name'); | ||
|
||
// setup iterators | ||
$iterator = new MultipleIterator(); | ||
$iterator->attachIterator(new ArrayIterator($resultIndexes)); | ||
$iterator->attachIterator(new ArrayIterator($resultIndexNames)); | ||
$iterator->attachIterator(new ArrayIterator($expectedIndexes)); | ||
|
||
return $iterator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters