/** * Returns the path to the lock file. * * @return string */ protected function _getLockPath() { $lockPath = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('lockPath'); static $lockPathExists = -1; if ($lockPathExists === -1) { if (!file_exists($lockPath)) { mkdir($lockPath, 0777, TRUE); } $lockPathExists = TRUE; } return $lockPath . 'lock_' . sha1($this->getName()); }
/** * Returns the dependency injection container * * @return \DI\Container */ public function getDiContainer() { if (!$this->diContainer) { $builder = new ContainerBuilder(); // $builder->setDefinitionCache(new ArrayCache()); $builder->setDefinitionCache(new FilesystemCache(ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('cachePath'))); $this->diContainer = $builder->build(); $builder->addDefinitions(__DIR__ . '/Configuration/dependencyInjectionConfiguration.php'); $this->diContainer = $builder->build(); // $this->diContainer = ContainerBuilder::buildDevContainer(); } return $this->diContainer; }
/** * @test */ public function removeFromDatabaseTest() { $databaseIdentifier = 'contacts'; $testEmail = '*****@*****.**'; /** @var Database $database */ $database = $this->fixture->getDatabase($databaseIdentifier); $dataInstance = new Document(); $dataInstance->setData(array('firstName' => 'Paul', 'lastName' => 'McKenzy', 'email' => $testEmail)); $database->remove($dataInstance); $this->assertEquals($this->numberOfContacts - 1, $database->count()); // A database just loaded from the filesystem should only contain the original number of entries /** @var DatabaseInterface $newlyLoadedDatabase */ $newlyLoadedDatabase = $this->databaseReader->loadDatabase($databaseIdentifier); $this->assertEquals($this->numberOfContacts, $newlyLoadedDatabase->count()); $this->assertEquals($database->count() + 1, $newlyLoadedDatabase->count()); // A database again retrieved from the coordinator should contain the added entry /** @var DatabaseInterface $databaseRetrievedFromTheCoordinator */ $databaseRetrievedFromTheCoordinator = $this->fixture->getDatabase($databaseIdentifier); $this->assertEquals($this->numberOfContacts - 1, $databaseRetrievedFromTheCoordinator->count()); $this->assertEquals($database->count(), $databaseRetrievedFromTheCoordinator->count()); $expectedPath = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('writeDataPath') . $databaseIdentifier . '.json'; $this->fixture->commitDatabase($database); $this->assertFileExists($expectedPath); $writtenData = json_decode(file_get_contents($expectedPath), TRUE); $this->assertTrue($writtenData !== FALSE); foreach ($writtenData as $data) { $this->assertNotEquals($testEmail, isset($data['email']) ? $data['email'] : 'no-email-at-all'); } // $this->assertEquals($database->count(), count($writtenData)); // $this->assertEquals($this->numberOfContacts - 1, count($writtenData)); unlink($expectedPath); }
<?php /** * Created by PhpStorm. * User: daniel * Date: 31.08.14 * Time: 16:25 */ use Cundd\PersistentObjectStore\Configuration\ConfigurationManager; $XHPROF_ROOT = __DIR__ . '/../../xhprof-0.9.4/'; if (file_exists($XHPROF_ROOT)) { require_once $XHPROF_ROOT . '/xhprof_lib/utils/xhprof_lib.php'; require_once $XHPROF_ROOT . '/xhprof_lib/utils/xhprof_runs.php'; } require_once __DIR__ . '/../../vendor/autoload.php'; require_once __DIR__ . '/AbstractCase.php'; require_once __DIR__ . '/AbstractDataBasedCase.php'; ConfigurationManager::getSharedInstance()->setConfigurationForKeyPath('dataPath', __DIR__ . '/../Resources/'); ConfigurationManager::getSharedInstance()->setConfigurationForKeyPath('writeDataPath', __DIR__ . '/../../var/Temp/');
/** * Returns the path to store the rescue data in * * @return string */ protected function getRescueDirectory() { $backupDirectory = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('rescuePath'); $backupDirectory .= gmdate('Y-m-d-H-i-s') . '/'; if (!file_exists($backupDirectory)) { mkdir($backupDirectory, 0770, TRUE); } return $backupDirectory; }
/** * Loads the given meta database * * @param string $databaseIdentifier * @return array<Document> */ protected function _loadMetaDataCollection($databaseIdentifier) { $path = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('dataPath') . $databaseIdentifier . '.meta.json'; if (!file_exists($path)) { return array(); } $lock = Factory::createLock($databaseIdentifier); $lock->lock(); $fileData = file_get_contents($path); $lock->unlock(); // DebugUtility::printMemorySample(); $serializer = new JsonSerializer(); $dataCollection = $serializer->unserialize($fileData); // DebugUtility::printMemorySample(); return $dataCollection; }
/** * @test */ public function createDatabaseTest() { $databaseIdentifier = 'test-db-' . time(); $expectedPath = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('writeDataPath') . $databaseIdentifier . '.json'; $requestInfo = RequestInfoFactory::buildRequestInfoFromRequest(new Request('PUT', sprintf('/%s/', $databaseIdentifier))); $databaseOptions = array('type' => 'memory'); $handlerResult = $this->fixture->create($requestInfo, $databaseOptions); $this->assertInstanceOf('Cundd\\PersistentObjectStore\\Server\\Handler\\HandlerResultInterface', $handlerResult); $this->assertEquals(201, $handlerResult->getStatusCode()); $this->assertNotNull($handlerResult->getData()); $this->assertFileExists($expectedPath); unlink($expectedPath); }
/** * Configure Xhprof */ protected function setUpXhprof() { if (!self::$useXhprof) { return; } if (!self::$didSetupXhprof && extension_loaded('xhprof') && class_exists('XHProfRuns_Default')) { ini_set('xhprof.output_dir', ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('tempPath')); xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); self::$didSetupXhprof = TRUE; register_shutdown_function(array(__CLASS__, 'tearDownXhprof')); echo PHP_EOL . 'Manually start xhprof server if needed:' . PHP_EOL; printf('php -S 127.0.0.1:8080 -d xhprof.output_dir="%s" -t path/to/xhprof_html' . PHP_EOL, ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('tempPath')); } }
/** * Returns the directory path to read data from * * @return string */ protected function _getReadDirectory() { return ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('dataPath'); }
/** * Prints the given log message very fast * * @experimental * * @param string $format * @param null $vars * @throws RuntimeException */ protected function log($format, $vars = NULL) { if (func_num_args() > 1) { $arguments = func_get_args(); array_shift($arguments); $writeData = vsprintf($format, $arguments); } else { $writeData = $format; } $writeData = gmdate('r') . ': ' . $writeData . PHP_EOL; $logFileDirectory = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('logPath'); $logFilePath = $logFileDirectory . 'log-' . getmypid() . '.log'; $logFilePath = $logFileDirectory . 'log-' . gmdate('Y-m-d') . '.log'; if (!file_exists($logFileDirectory)) { mkdir($logFileDirectory); } $fileHandle = fopen($logFilePath, 'w'); if (!$fileHandle) { throw new RuntimeException(sprintf('Could not open file %s', $logFilePath), 1413294319); } stream_set_blocking($fileHandle, 0); fwrite($fileHandle, $writeData); fclose($fileHandle); }
/** * Start the server * * @param int $autoShutdownTime */ protected function _startServer($autoShutdownTime = 7) { // Start the server $serverBinPath = ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('basePath') . 'bin/server'; $phpBinPath = defined('PHP_BINARY') ? PHP_BINARY : PHP_BINDIR . '/php'; $phpIniFile = php_ini_loaded_file(); $commandParts = array($phpBinPath, $phpIniFile ? '-c' . $phpIniFile : '', escapeshellcmd(sprintf('"%s"', $serverBinPath))); if ($autoShutdownTime > -1) { $commandParts[] = '--test=' . $autoShutdownTime; // Run the server in test mode } $commandParts[] = '> /dev/null &'; // Run the server in the background // printf('Run %s' . PHP_EOL, implode(' ', $commandParts)); exec(implode(' ', $commandParts), $output, $returnValue); // Wait for the server to boot sleep(1); }