Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHash committed Apr 10, 2017
1 parent 622ca13 commit fc506da
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 45 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@
[![Build Status](https://travis-ci.org/honeybee/couchdb-adapter.svg?branch=master)](https://travis-ci.org/honeybee/couchdb-adapter)
[![Coverage Status](https://coveralls.io/repos/github/honeybee/couchdb-adapter/badge.svg?branch=master)](https://coveralls.io/github/honeybee/couchdb-adapter?branch=master)
[![Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/honeybee/Lobby)

Find us on:

* Gitter: https://gitter.im/honeybee/Lobby
* IRC: freenode.net #honeybee
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Storage/DomainEvent/DomainEventReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class DomainEventReader extends CouchDbStorage implements StorageReaderInterface
{
protected $lastKey = null;
protected $lastKey;

public function read($identifier, SettingsInterface $settings = null)
{
Expand Down
69 changes: 39 additions & 30 deletions src/Storage/StructureVersionList/StructureVersionListReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Honeybee\CouchDb\Storage\StructureVersionList;

use Assert\Assertion;
use GuzzleHttp\Exception\RequestException;
use Honeybee\Common\Error\RuntimeError;
use Honeybee\CouchDb\Storage\CouchDbStorage;
use Honeybee\Infrastructure\DataAccess\Storage\StorageReaderInterface;
use Honeybee\Infrastructure\Config\Settings;
Expand All @@ -15,7 +17,28 @@ class StructureVersionListReader extends CouchDbStorage implements StorageReader
{
const READ_ALL_LIMIT = 10;

protected $nextStartKey = null;
protected $lastKey;

public function read($identifier, SettingsInterface $settings = null)
{
Assertion::string($identifier);
Assertion::notBlank($identifier);

try {
$response = $this->request($identifier, self::METHOD_GET);
$resultData = json_decode($response->getBody(), true);
} catch (RequestException $error) {
if ($error->getResponse()->getStatusCode() === 404) {
return null;
} else {
throw $error;
}
}

$resultData['revision'] = $resultData['_rev'];

return $this->createStructureVersionList($resultData['_id'], $resultData['versions']);
}

public function readAll(SettingsInterface $settings = null)
{
Expand All @@ -30,62 +53,48 @@ public function readAll(SettingsInterface $settings = null)
];

if (!$settings->get('first', true)) {
if (!$this->nextStartKey) {
if (!$this->lastKey) {
return $data;
}

$requestParams['startkey'] = sprintf('"%s"', $this->nextStartKey);
$requestParams['skip'] = 1;
$requestParams['startkey'] = sprintf('"%s"', $this->lastKey);
}

// @todo catch RequestException?
$response = $this->request('_all_docs', self::METHOD_GET, [], $requestParams);
$resultData = json_decode($response->getBody(), true);

if (!isset($resultData['rows'])) {
throw new RuntimeError(sprintf('Invalid rows response from CouchDb: %s', var_export($resultData, true)));
}

foreach ($resultData['rows'] as $row) {
$data[] = $this->createStructureVersionList($row);
$data[] = $this->createStructureVersionList($row['_id'], $row['versions']);
}

if ($resultData['total_rows'] === $resultData['offset'] + 1) {
$this->nextStartKey = null;
$this->lastKey = null;
} else {
$lastRow = end($data);
$this->nextStartKey = $lastRow[self::DOMAIN_FIELD_ID];
$this->lastKey = $lastRow->getIdentifier();
}

return $data;
}

public function read($identifier, SettingsInterface $settings = null)
{
try {
$response = $this->request($identifier, self::METHOD_GET);
$resultData = json_decode($response->getBody(), true);
} catch (RequestException $error) {
if ($error->getResponse()->getStatusCode() === 404) {
return null;
} else {
throw $error;
}
}

$resultData['revision'] = $resultData['_rev'];

return $this->createStructureVersionList($resultData);
}

public function getIterator()
{
return new StorageReaderIterator($this);
}

protected function createStructureVersionList(array $data)
protected function createStructureVersionList($identifier, array $versions)
{
$structureVersionList = new StructureVersionList($data['_id']);

foreach ($data['versions'] as $versionData) {
$structureVersionList->push(new StructureVersion($versionData));
$structureVersions = [];
foreach ($versions as $version) {
$structureVersions[] = new StructureVersion($version);
}

return $structureVersionList;
return new StructureVersionList($identifier, $structureVersions);
}
}
1 change: 1 addition & 0 deletions tests/Connector/CouchDbConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testGetConnectionWorks()
$connection = $connector->getConnection();
$this->assertTrue($connector->isConnected(), 'Connector should be connected after getConnection() call');
$this->assertTrue(is_object($connection), 'A getConnection() call should yield a client/connection object');
$this->assertSame($connection, $connector->getConnection());
}

public function testDisconnectWorks()
Expand Down
7 changes: 4 additions & 3 deletions tests/Storage/EventStream/EventStreamReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ public function setUp()
$this->mockResponse = Mockery::mock(Response::CLASS);
}


public function testGetIterator()
{
$stubbedEventReader = Mockery::mock(
EventStreamReader::CLASS.'[readAll]',
[$this->mockConnector, new ArrayConfig([]), new NullLogger]
);
$stubbedEventReader->shouldReceive('readAll')->once()->andReturn([]);
$this->assertInstanceOf(StorageReaderIterator::CLASS, $stubbedEventReader->getIterator());
$stubbedEventReader->shouldReceive('readAll')->once()->andReturn(['something']);
$iterator = $stubbedEventReader->getIterator();
$this->assertInstanceOf(StorageReaderIterator::CLASS, $iterator);
$this->assertTrue($iterator->valid());
}

/**
Expand Down
Loading

0 comments on commit fc506da

Please sign in to comment.