Skip to content

Commit

Permalink
Updated to support 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Mar 21, 2016
1 parent 35c0145 commit 790d0a4
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"mongodb/mongodb": "^1.0",
"nilportugues/repository": "2.0.*",
"nilportugues/repository": "2.2.*",
"nilportugues/serializer": "1.1.*",
"nilportugues/assert": "1.0.*"
},
Expand Down
71 changes: 64 additions & 7 deletions src/Infrastructure/Model/Repository/MongoDB/MongoDBRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ public function findAll(Pageable $pageable = null)
$filterArray = [];
$this->applyFiltering($pageable->filters(), $filterArray);
$this->applySorting($pageable->sortings(), $options);
$this->fetchSpecificFields($pageable->fields(), $options);

$total = $collection->count($filterArray, $options);
$page = $pageable->pageNumber() - 1;
Expand All @@ -409,12 +408,18 @@ public function findAll(Pageable $pageable = null)
$options['limit'] = $pageable->pageSize();
$options['skip'] = $pageable->pageSize() * ($page);

return new ResultPage(
$collection->find($filterArray, $options)->toArray(),
$total,
$pageable->pageNumber(),
ceil($total / $pageable->pageSize())
);
$distinct = $pageable->distinctFields()->get();
if (count($distinct) > 0) {
if (count($distinct) > 1) {
throw new \Exception('Mongo cannot select more than one field when calling distinct.');
}
$results = (array) $collection->distinct(array_shift($distinct), $filterArray, $this->options);
} else {
$this->fetchSpecificFields($pageable->fields(), $options);
$results = $collection->find($filterArray, $options)->toArray();
}

return new ResultPage($results, $total, $pageable->pageNumber(), ceil($total / $pageable->pageSize()));
}

$bsonDocumentArray = $collection->find([], $options);
Expand Down Expand Up @@ -455,4 +460,56 @@ protected function addMany(array &$store)
/* @var \MongoDB\InsertManyResult $result */
return $this->getCollection()->insertMany($store, $this->options);
}

/**
* Returns all instances of the type meeting $distinctFields values.
*
* @param Fields $distinctFields
* @param Filter|null $filter
* @param Sort|null $sort
*
* @return array
*
* @throws \Exception
*/
public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null)
{
$collection = $this->getCollection();
$options = $this->options;
$filterArray = [];

$this->applyFiltering($filter, $filterArray);
$this->applySorting($sort, $options);

$fields = $distinctFields->get();

if (count($fields) > 1) {
throw new \Exception('Mongo cannot select more than one field when calling distinct.');
}

$results = (array) $collection->distinct(array_shift($fields), $filterArray, $this->options);

foreach ($results as &$r) {
$r = $this->recursiveArrayCopy($r);
}

return $results;
}

/**
* Repository data is added or removed as a whole block.
* Must work or fail and rollback any persisted/erased data.
*
* @param callable $transaction
*
* @throws \Exception
*/
public function transactional(callable $transaction)
{
try {
$transaction();
} catch (\Exception $e) {
throw $e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,104 @@ public function tearDown()
$this->repository->removeAll();
}

public function testFindByDistinct()
{
$distinctFields = new Fields(['name']);
$clients = new Clients(
5,
'John Doe',
new DateTime('2014-12-11'),
3,
[
new DateTime('2014-12-16'),
new DateTime('2014-12-31'),
new DateTime('2015-03-11'),
],
25.125
);

$this->repository->add($clients);
$results = $this->repository->findByDistinct($distinctFields);

$this->assertEquals(4, count($results));
}

public function testFindAllWithDistinct()
{
$clients = new Clients(
5,
'John Doe',
new DateTime('2014-12-11'),
3,
[
new DateTime('2014-12-16'),
new DateTime('2014-12-31'),
new DateTime('2015-03-11'),
],
25.125
);

$this->repository->add($clients);

$pageable = new Pageable(
1,
10,
null,
null,
null,
new Fields(['name'])
);

$result = $this->repository->findAll($pageable);
$this->assertEquals(4, count($result->content()));
}

public function testTransactional()
{
$clients = new Clients(
5,
'John Doe',
new DateTime('2014-12-11'),
3,
[
new DateTime('2014-12-16'),
new DateTime('2014-12-31'),
new DateTime('2015-03-11'),
],
25.125
);

$transaction = function () use ($clients) {
$this->repository->add($clients);
};

$this->repository->transactional($transaction);

$this->assertEquals(5, $this->repository->count());
}

public function testTransactionalFailsAndDoesNotGetAdded()
{
$clients = new Clients(
5,
'John Doe',
new DateTime('2014-12-11'),
3,
[
new DateTime('2014-12-16'),
new DateTime('2014-12-31'),
new DateTime('2015-03-11'),
],
25.125
);

$transaction = function () use ($clients) {
throw new \Exception('Just making it fail');
};

$this->setExpectedException(Exception::class);
$this->repository->transactional($transaction);
}

public function testItCanUpdateAnExistingClient()
{
Expand All @@ -74,7 +172,7 @@ public function testItCanUpdateAllClientsName()

$this->repository->addAll([$client1, $client2, $client3, $client4]);

for($i=1; $i<=4; $i++) {
for ($i = 1; $i <= 4; ++$i) {
$client = $this->repository->find(new ClientId($i));
$this->assertEquals('Homer Simpson', $client['name']);
}
Expand Down Expand Up @@ -145,8 +243,6 @@ public function testExists()
$this->assertTrue($this->repository->exists(new ClientId(1)));
}



/**
*
*/
Expand Down

0 comments on commit 790d0a4

Please sign in to comment.