/** * @return void * @author Christopher Hlubek <*****@*****.**> */ public function setUp() { parent::setUp(); $configurationManager = $this->objectManager->get('TYPO3\\Flow\\Configuration\\ConfigurationManager'); $backendOptions = $this->objectManager->getSettingsByPath(array('TYPO3', 'Flow', 'persistence', 'backendOptions')); $this->client = new \TYPO3\CouchDB\Client($backendOptions['dataSourceName']); if (isset($backendOptions['database']) && $backendOptions['database'] !== '') { $this->client->setDatabaseName($backendOptions['database']); } $this->design = new \TYPO3\CouchDB\Persistence\Backend\FlowDesign($this->client); if ($this->client->databaseExists()) { $this->client->deleteDatabase(); } }
/** * @test * @author Christopher Hlubek <*****@*****.**> */ public function viewGetsCreatedIfDatabaseExists() { $this->client->createDatabase($this->databaseName); $this->backend->append('Test entry', LOG_WARNING); $logs = $this->backend->read(0, 100, LOG_WARNING); $this->assertEquals(1, count($logs)); }
/** * @test * @author Christopher Hlubek <*****@*****.**> */ public function synchronizeWithNoDesignDocumentCollectsDeclarationsAndCreatesDesignDocument() { $mockReflectionService = $this->getMock('TYPO3\\Flow\\Reflection\\ReflectionService'); $design = new Fixtures\Design\CompanyDesign(); $design->setClient($this->client); $design->injectReflectionService($mockReflectionService); $mockReflectionService->expects($this->any())->method('isMethodStatic')->will($this->returnCallback(function ($className, $methodName) { if ($methodName === 'totalPurchasesDeclaration') { return TRUE; } return FALSE; })); $this->client->expects($this->any())->method('getDocument')->with('_design/company')->will($this->throwException(new \TYPO3\CouchDB\Client\NotFoundException('{"error":"not_found","reason":"missing"}'))); $this->client->expects($this->once())->method('createDocument')->with(array('_id' => '_design/company', 'language' => 'javascript', 'views' => array('totalPurchases' => array('map' => 'function(doc) { if (doc.Type == "purchase") { emit(doc.Customer, doc.Amount); } }', 'reduce' => 'function(keys, values) { return sum(values) }')))); $design->synchronize(); }
/** * @test * @author Christopher Hlubek <*****@*****.**> */ public function getDocumentInformationWorks() { $response = $this->client->createDocument(array('name' => 'Bar')); $id = $response->getId(); $information = $this->client->getDocumentInformation($id); $this->assertEquals($response->getRevision(), $information->getRevision()); }
/** * @test * @author Christopher Hlubek <*****@*****.**> */ public function callingViewReturnsResult() { $design = new Fixtures\Design\CompanyDesign(); $design->setClient($this->client); $design->synchronize(); for ($i = 0; $i < 10; $i++) { $this->client->createDocument(array('Type' => 'purchase', 'Customer' => '12345678', 'Amount' => 13.95 * ($i + 1))); } $result = $design->totalPurchasesAmount('12345678'); $this->assertEquals(767.25, $result); }
/** * Do a CouchDB operation and handle error conversion and creation of * the database on the fly. * * @param \Closure $couchDbOperation * @return mixed */ protected function doOperation(\Closure $couchDbOperation) { try { return $couchDbOperation($this->client); } catch (Client\ClientException $clientException) { $information = $clientException->getInformation(); if (isset($information['error']) && $information['error'] === 'not_found' && $information['reason'] === 'no_db_file') { if ($this->client->createDatabase($this->databaseName)) { return $this->doOperation($couchDbOperation); } else { throw new \TYPO3\Flow\Persistence\Exception('Could not create database ' . $this->database, 1286901880); } } else { throw $clientException; } } }
/** * @throws \TYPO3\CouchDB\Client\NotFoundException * @return void */ public function synchronize() { try { $designDocument = $this->client->getDocument('_design/' . $this->getDesignDocumentName(), array('decodeAssociativeArray' => TRUE)); } catch (\TYPO3\CouchDB\Client\NotFoundException $notFoundException) { $information = $notFoundException->getInformation(); if ($information['reason'] === 'no_db_file') { $this->client->createDatabase(); } if ($information['reason'] === 'no_db_file' || $information['reason'] === 'missing') { $designDocument = array('_id' => '_design/' . $this->getDesignDocumentName(), 'language' => $this->language); } else { throw $notFoundException; } } $declarations = $this->getDeclarations(); foreach ($declarations as $declaration) { $viewDeclaration = call_user_func(get_class($this) . '::' . $declaration . 'Declaration'); $designDocument['views'][$declaration] = $viewDeclaration; } $this->client->createDocument($designDocument); }
/** * Initializes an empty database and setup the needed view code to * query for log messages * * @return void * @author Christopher Hlubek <*****@*****.**> */ protected function initializeDatabase() { if (!$this->client->databaseExists($this->databaseName)) { $this->client->createDatabase($this->databaseName); } }