diff --git a/README.md b/README.md index 560185a1..08d4e26a 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ Quickstart ```php require_once("tripod.inc.php"); -\Tripod\Mongo\Config::setConfig($conf); // set the config, usually read in as JSON from a file +\Tripod\Config::setConfig($conf); // set the config, usually read in as JSON from a file $tripod = new Driver( "CBD_users", // pod (read: MongoDB collection) we're working with diff --git a/composer.json b/composer.json index 20419223..7b323ef3 100644 --- a/composer.json +++ b/composer.json @@ -27,5 +27,8 @@ "require-dev": { "phpunit/phpunit": "4.1.*", "squizlabs/php_codesniffer": "3.2.*" - } + }, + "autoload": { + "classmap": ["src/"] + } } diff --git a/docs/config.md b/docs/config.md index caa63970..3c73a83f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -5,7 +5,7 @@ Tripod config is typically defined as a JSON file or stream which is added to th ```php $conf = json_decode(file_get_contents('tripod_config.json'); -\Tripod\Mongo\Config::setConfig($conf); // set the config, usually read in as JSON from a file +\Tripod\Config::setConfig($conf); // set the config, usually read in as JSON from a file ``` Namespaces diff --git a/scripts/mongo/BSONToQuads.php b/scripts/mongo/BSONToQuads.php index 0fa269ae..ea923170 100644 --- a/scripts/mongo/BSONToQuads.php +++ b/scripts/mongo/BSONToQuads.php @@ -13,7 +13,7 @@ array_shift($argv); $config = json_decode(file_get_contents($argv[0]), true); -\Tripod\Mongo\Config::setConfig($config); +\Tripod\Config::setConfig($config); $tu = new \Tripod\Mongo\TriplesUtil(); diff --git a/scripts/mongo/BSONToTriples.php b/scripts/mongo/BSONToTriples.php index eb942271..088883c1 100644 --- a/scripts/mongo/BSONToTriples.php +++ b/scripts/mongo/BSONToTriples.php @@ -11,7 +11,7 @@ } array_shift($argv); $config = json_decode(file_get_contents($argv[0]), true); -\Tripod\Mongo\Config::setConfig($config); +\Tripod\Config::setConfig($config); $tu = new \Tripod\Mongo\TriplesUtil(); diff --git a/scripts/mongo/createSearchDocuments.php b/scripts/mongo/createSearchDocuments.php index fe18e38b..a97e36c9 100644 --- a/scripts/mongo/createSearchDocuments.php +++ b/scripts/mongo/createSearchDocuments.php @@ -58,10 +58,10 @@ function showUsage($scriptName) */ function generateSearchDocuments($id, $specId, $storeName, $stat = null, $queue = null) { - $spec = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecification($storeName, $specId); + $spec = \Tripod\Config::getInstance()->getSearchDocumentSpecification($storeName, $specId); if (array_key_exists("from",$spec)) { - \Tripod\Mongo\Config::getInstance()->setMongoCursorTimeout(-1); + \Tripod\Config::getInstance()->setMongoCursorTimeout(-1); print "Generating $specId"; $tripod = new \Tripod\Mongo\Driver($spec['from'], $storeName, array('stat'=>$stat)); @@ -82,7 +82,7 @@ function generateSearchDocuments($id, $specId, $storeName, $stat = null, $queue $t = new \Tripod\Timer(); $t->start(); -\Tripod\Mongo\Config::setConfig(json_decode(file_get_contents($configLocation),true)); +\Tripod\Config::setConfig(json_decode(file_get_contents($configLocation),true)); if(isset($options['s']) || isset($options['storename'])) { @@ -120,7 +120,7 @@ function generateSearchDocuments($id, $specId, $storeName, $stat = null, $queue } else { - $queue = \Tripod\Mongo\Config::getInstance()->getApplyQueueName(); + $queue = \Tripod\Config::getInstance()->getApplyQueueName(); } } @@ -137,7 +137,7 @@ function generateSearchDocuments($id, $specId, $storeName, $stat = null, $queue } else { - foreach(\Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecifications($storeName) as $searchSpec) + foreach(\Tripod\Config::getInstance()->getSearchDocumentSpecifications($storeName) as $searchSpec) { generateSearchDocuments($id, $searchSpec['_id'], $storeName, $stat, $queue); } diff --git a/scripts/mongo/createTables.php b/scripts/mongo/createTables.php index 7be07e50..13f8417b 100644 --- a/scripts/mongo/createTables.php +++ b/scripts/mongo/createTables.php @@ -59,10 +59,10 @@ function showUsage() */ function generateTables($id, $tableId, $storeName, $stat = null, $queue = null) { - $tableSpec = \Tripod\Mongo\Config::getInstance()->getTableSpecification($storeName, $tableId); + $tableSpec = \Tripod\Config::getInstance()->getTableSpecification($storeName, $tableId); if (array_key_exists("from",$tableSpec)) { - \Tripod\Mongo\Config::getInstance()->setMongoCursorTimeout(-1); + \Tripod\Config::getInstance()->setMongoCursorTimeout(-1); print "Generating $tableId"; $tripod = new \Tripod\Mongo\Driver($tableSpec['from'], $storeName, array('stat'=>$stat)); @@ -83,7 +83,7 @@ function generateTables($id, $tableId, $storeName, $stat = null, $queue = null) $t = new \Tripod\Timer(); $t->start(); -\Tripod\Mongo\Config::setConfig(json_decode(file_get_contents($configLocation),true)); +\Tripod\Config::setConfig(json_decode(file_get_contents($configLocation),true)); if(isset($options['s']) || isset($options['storename'])) { @@ -121,7 +121,7 @@ function generateTables($id, $tableId, $storeName, $stat = null, $queue = null) } else { - $queue = \Tripod\Mongo\Config::getInstance()->getApplyQueueName(); + $queue = \Tripod\Config::getInstance()->getApplyQueueName(); } } @@ -138,7 +138,7 @@ function generateTables($id, $tableId, $storeName, $stat = null, $queue = null) } else { - foreach(\Tripod\Mongo\Config::getInstance()->getTableSpecifications($storeName) as $tableSpec) + foreach(\Tripod\Config::getInstance()->getTableSpecifications($storeName) as $tableSpec) { generateTables($id, $tableSpec['_id'], $storeName, $stat, $queue); } diff --git a/scripts/mongo/createViews.php b/scripts/mongo/createViews.php index 438e9dfb..99b92b12 100644 --- a/scripts/mongo/createViews.php +++ b/scripts/mongo/createViews.php @@ -59,10 +59,10 @@ function showUsage() */ function generateViews($id, $viewId, $storeName, $stat, $queue) { - $viewSpec = \Tripod\Mongo\Config::getInstance()->getViewSpecification($storeName, $viewId); + $viewSpec = \Tripod\Config::getInstance()->getViewSpecification($storeName, $viewId); if (array_key_exists("from",$viewSpec)) { - \Tripod\Mongo\Config::getInstance()->setMongoCursorTimeout(-1); + \Tripod\Config::getInstance()->setMongoCursorTimeout(-1); print "Generating $viewId"; $tripod = new \Tripod\Mongo\Driver($viewSpec['from'], $storeName, array('stat'=>$stat)); @@ -83,7 +83,7 @@ function generateViews($id, $viewId, $storeName, $stat, $queue) $t = new \Tripod\Timer(); $t->start(); -\Tripod\Mongo\Config::setConfig(json_decode(file_get_contents($configLocation),true)); +\Tripod\Config::setConfig(json_decode(file_get_contents($configLocation),true)); if(isset($options['s']) || isset($options['storename'])) { @@ -121,7 +121,7 @@ function generateViews($id, $viewId, $storeName, $stat, $queue) } else { - $queue = \Tripod\Mongo\Config::getInstance()->getApplyQueueName(); + $queue = \Tripod\Config::getInstance()->getApplyQueueName(); } } @@ -138,7 +138,7 @@ function generateViews($id, $viewId, $storeName, $stat, $queue) } else { - foreach(\Tripod\Mongo\Config::getInstance()->getViewSpecifications($storeName) as $viewSpec) + foreach(\Tripod\Config::getInstance()->getViewSpecifications($storeName) as $viewSpec) { generateViews($id, $viewSpec['_id'], $storeName, $stat, $queue); } diff --git a/scripts/mongo/detectNamespaces.php b/scripts/mongo/detectNamespaces.php index 99f3e820..74dbf872 100644 --- a/scripts/mongo/detectNamespaces.php +++ b/scripts/mongo/detectNamespaces.php @@ -34,7 +34,7 @@ ), ); -\Tripod\Mongo\Config::setConfig($config); +\Tripod\Config::setConfig($config); $util = new \Tripod\Mongo\TriplesUtil(); @@ -85,7 +85,7 @@ $newNsConfig[$prefix] = $n; echo "\nFound ns $n suggest prefix $prefix"; $config["namespaces"] = array_merge($config["namespaces"],$newNsConfig); - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); } } $ns = $util->extractMissingObjectNs($triples); @@ -111,7 +111,7 @@ $newNsConfig[$prefix] = $n; echo "\nFound object ns $n occurs > 500 times, suggest prefix $prefix"; $config["namespaces"] = array_merge($config["namespaces"],$newNsConfig); - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); } } } diff --git a/scripts/mongo/ensureIndexes.php b/scripts/mongo/ensureIndexes.php index f1ccf81d..bfed1a44 100644 --- a/scripts/mongo/ensureIndexes.php +++ b/scripts/mongo/ensureIndexes.php @@ -10,13 +10,13 @@ } array_shift($argv); -\Tripod\Mongo\Config::setConfig(json_decode(file_get_contents($argv[0]),true)); +\Tripod\Config::setConfig(json_decode(file_get_contents($argv[0]),true)); $storeName = (isset($argv[1])) ? $argv[1] : null; $forceReindex = (isset($argv[2])&&($argv[2]=="true")) ? true : false; $background = (isset($argv[3])&&($argv[3]=="false")) ? false : true; -\Tripod\Mongo\Config::getInstance()->setMongoCursorTimeout(-1); +\Tripod\Config::getInstance()->setMongoCursorTimeout(-1); $ei = new \Tripod\Mongo\Jobs\EnsureIndexes(); diff --git a/scripts/mongo/loadTriples.php b/scripts/mongo/loadTriples.php index dbe823c0..ad14cdc6 100644 --- a/scripts/mongo/loadTriples.php +++ b/scripts/mongo/loadTriples.php @@ -36,7 +36,7 @@ function load(\Tripod\Mongo\TriplesUtil $loader,$subject,Array $triples,Array &$ $storeName = $argv[0]; $podName = $argv[1]; -\Tripod\Mongo\Config::setConfig(json_decode(file_get_contents($argv[2]),true)); +\Tripod\Config::setConfig(json_decode(file_get_contents($argv[2]),true)); $i=0; $currentSubject = ""; diff --git a/scripts/mongo/triplesToBSON.php b/scripts/mongo/triplesToBSON.php index f4c0a33d..ae06c530 100644 --- a/scripts/mongo/triplesToBSON.php +++ b/scripts/mongo/triplesToBSON.php @@ -11,7 +11,7 @@ array_shift($argv); $config = json_decode(file_get_contents($argv[0]), true); -\Tripod\Mongo\Config::setConfig($config); +\Tripod\Config::setConfig($config); $currentSubject = ""; $triples = array(); @@ -32,7 +32,7 @@ if ($currentSubject!=$subject) // once subject changes, we have all triples for that subject, flush to Mongo { - print(json_encode($tu->getTArrayAbout($currentSubject,$triples,\Tripod\Mongo\Config::getInstance()->getDefaultContextAlias()))."\n"); + print(json_encode($tu->getTArrayAbout($currentSubject,$triples,\Tripod\Config::getInstance()->getDefaultContextAlias()))."\n"); $currentSubject=$subject; // reset current subject to next subject $triples = array(); // reset triples } @@ -41,7 +41,7 @@ } // last doc -print(json_encode($tu->getTArrayAbout($currentSubject,$triples,\Tripod\Mongo\Config::getInstance()->getDefaultContextAlias()))."\n"); +print(json_encode($tu->getTArrayAbout($currentSubject,$triples,\Tripod\Config::getInstance()->getDefaultContextAlias()))."\n"); ?> diff --git a/scripts/mongo/validateConfig.php b/scripts/mongo/validateConfig.php index 69c75f1f..cb95bb02 100644 --- a/scripts/mongo/validateConfig.php +++ b/scripts/mongo/validateConfig.php @@ -35,10 +35,10 @@ function showUsage() \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); -\Tripod\Mongo\Config::setConfig(json_decode(file_get_contents($configLocation),true)); +\Tripod\Config::setConfig(json_decode(file_get_contents($configLocation),true)); try { - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); echo "\nConfig OK\n"; } diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 00000000..c83a44c1 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,72 @@ +config = $config; if (array_key_exists('namespaces',$config)) { $this->ns = $config['namespaces']; @@ -1075,20 +1075,6 @@ public function checkModifierFunctions(array $array, $parent, $parentKey = null) } } - /** - * @codeCoverageIgnore - * @static - * @internal param string $specName - * @return Array|null - */ - public static function getConfig() - { - if(isset(self::$config)) - { - return self::$config; - } - return null; - } /** * Returns an alias curie of the default context (i.e. graph name) @@ -1102,42 +1088,50 @@ public function getDefaultContextAlias() /** * Since this is a singleton class, use this method to create a new config instance. - * @uses Config::setConfig() Configuration must be set prior to calling this method. To generate a completely new object, set a new config * @codeCoverageIgnore - * @static + * @deprecated * @throws \Tripod\Exceptions\ConfigException - * @internal param string $specName * @return Config */ public static function getInstance() { - if (!isset(self::$config)) - { - throw new \Tripod\Exceptions\ConfigException("Call Config::setConfig() first"); - } - if (!isset(self::$instance)) - { - self::$instance = new Config(); - self::$instance->loadConfig(self::$config); - } - return self::$instance; + self::getLogger()->warn( + '\Tripod\Mongo\Config::getInstance deprecated, use \Tripod\Config::getInstance instead' + ); + return \Tripod\Config::getInstance(); } /** - * set the config - * @usedby Config::getInstance() + * Sets the tripod configuration + * @deprecated * @param array $config */ - public static function setConfig(Array $config) + public static function setConfig(array $config) { - self::$config = $config; - self::$instance = null; // this will force a reload next time getInstance() is called + self::getLogger()->warn( + '\Tripod\Mongo\Config::setConfig deprecated, use \Tripod\Config::setConfig instead' + ); + \Tripod\Config::setConfig($config); + } + + /** + * Returns configuration array + * + * @deprecated + * @return array + */ + public static function getConfig() + { + self::getLogger()->warn( + '\Tripod\Mongo\Config::getConfig deprecated, use \Tripod\Config::getConfig instead' + ); + return \Tripod\Config::getConfig(); } /** * Returns a list of the configured indexes grouped by collection * @param string $storeName - * @return mixed + * @return array */ public function getIndexesGroupedByCollection($storeName) { @@ -1195,7 +1189,8 @@ public function getIndexesGroupedByCollection($storeName) * @param string $storeName The database name to use. * @param string $collName The collection in the database. * @param string $qName Either the qname to get the values for or empty for all cardinality values. - * @return mixed If no qname is specified then returns an array of cardinality options, otherwise returns the cardinality value for the given qname. + * @return array|int If no qname is specified then returns an array of cardinality options, + * otherwise returns the cardinality value for the given qname. */ public function getCardinality($storeName,$collName,$qName=null) { @@ -1551,16 +1546,6 @@ public function getDbs() return array_keys($this->dbConfig); } - /** - * This method was added to allow us to test the getInstance() method - * @codeCoverageIgnore - */ - public function destroy() - { - self::$instance = null; - self::$config = null; - } - /* PROTECTED FUNCTIONS */ /** @@ -2176,4 +2161,30 @@ public static function getLogger() } return self::$logger; } + + /** + * Sets the Tripod config + * + * @param array $config + * @return Config + */ + public static function deserialize(array $config) + { + if (isset($config['class']) && isset($config['config'])) { + $config = $config['config']; + } + $instance = new self(); + $instance->loadConfig($config); + return $instance; + } + + /** + * Serializes the config into an array that can be passed to jobs, etc. + * + * @return array + */ + public function serialize() + { + return $this->config; + } } diff --git a/src/mongo/Driver.class.php b/src/mongo/Driver.class.php index bf83eec6..f493eaa1 100755 --- a/src/mongo/Driver.class.php +++ b/src/mongo/Driver.class.php @@ -74,7 +74,7 @@ public function __construct($podName, $storeName, $opts=array()) $this->podName = $podName; $this->storeName = $storeName; - $this->config = $this->getTripodConfigInstance(); + $this->config = $this->getConfigInstance(); $this->labeller = $this->getLabeller(); @@ -649,17 +649,6 @@ public function registerHook($eventType, IEventHook $hook) } } - - /** - * For mocking - * @return Config - */ - protected function getTripodConfigInstance() - { - return Config::getInstance(); - } - - /** * Returns the composite that can perform the supported operation * @param $operation string must be either OP_VIEWS, OP_TABLES or OP_SEARCH diff --git a/src/mongo/IConfigInstance.php b/src/mongo/IConfigInstance.php new file mode 100644 index 00000000..8ad37ed2 --- /dev/null +++ b/src/mongo/IConfigInstance.php @@ -0,0 +1,308 @@ +collection)) { - $config = Config::getInstance(); + $config = \Tripod\Config::getInstance(); $this->collection = $config->getCollectionForJobGroups($this->storeName); } diff --git a/src/mongo/Labeller.class.php b/src/mongo/Labeller.class.php index cfa64514..8ac52ca4 100644 --- a/src/mongo/Labeller.class.php +++ b/src/mongo/Labeller.class.php @@ -2,16 +2,12 @@ namespace Tripod\Mongo; -/** @noinspection PhpIncludeInspection */ -require_once TRIPOD_DIR.'classes/Labeller.class.php'; -require_once TRIPOD_DIR . 'exceptions/LabellerException.class.php'; - /** * Class Labeller * @package Tripod\Mongo */ -class Labeller extends \Tripod\Labeller { - +class Labeller extends \Tripod\Labeller +{ /** * Constructor */ @@ -24,7 +20,7 @@ function __construct() 'owl' => 'http://www.w3.org/2002/07/owl#', 'cs' => 'http://purl.org/vocab/changeset/schema#', ); - $config = Config::getInstance(); + $config = \Tripod\Config::getInstance(); $ns = $config->getNamespaces(); foreach ($ns as $prefix=>$uri) { @@ -89,4 +85,4 @@ public function get_prefix($ns) { throw new \Tripod\Exceptions\LabellerException($ns); } } -} \ No newline at end of file +} diff --git a/src/mongo/MongoGraph.class.php b/src/mongo/MongoGraph.class.php index f4924854..23b27827 100644 --- a/src/mongo/MongoGraph.class.php +++ b/src/mongo/MongoGraph.class.php @@ -2,11 +2,7 @@ namespace Tripod\Mongo; -require_once TRIPOD_DIR.'classes/ExtendedGraph.class.php'; require_once TRIPOD_DIR.'mongo/MongoTripodConstants.php'; -require_once TRIPOD_DIR . 'mongo/Config.class.php'; -require_once TRIPOD_DIR . 'mongo/Labeller.class.php'; -require_once TRIPOD_DIR . 'mongo/serializers/NQuadSerializer.class.php'; /** * Class MongoGraph @@ -267,4 +263,4 @@ private function index_to_tarray($graph=null,$contextAlias) return null; } -} \ No newline at end of file +} diff --git a/src/mongo/base/DriverBase.class.php b/src/mongo/base/DriverBase.class.php index b01820fe..4473b04f 100644 --- a/src/mongo/base/DriverBase.class.php +++ b/src/mongo/base/DriverBase.class.php @@ -122,7 +122,7 @@ public function getStatsConfig() protected $labeller; /** - * @var Config + * @var IConfigInstance */ protected $config = null; @@ -139,10 +139,10 @@ protected function getExpirySecFromNow($secs) * @param string|null $context * @return mixed */ - protected function getContextAlias($context=null) + protected function getContextAlias($context = null) { $contextAlias = $this->labeller->uri_to_alias((empty($context)) ? $this->defaultContext : $context); - return (empty($contextAlias)) ? Config::getInstance()->getDefaultContextAlias() : $contextAlias; + return (empty($contextAlias)) ? $this->getConfigInstance()->getDefaultContextAlias() : $contextAlias; } /** @@ -427,11 +427,11 @@ protected function addIdToImpactIndex(array $id, &$target, $buildImpactIndex=tru /** * For mocking - * @return Config + * @return IConfigInstance */ protected function getConfigInstance() { - return Config::getInstance(); + return \Tripod\Config::getInstance(); } /** diff --git a/src/mongo/base/JobBase.class.php b/src/mongo/base/JobBase.class.php index c580fd4c..c08ad8f7 100644 --- a/src/mongo/base/JobBase.class.php +++ b/src/mongo/base/JobBase.class.php @@ -1,9 +1,14 @@ getInstance()->validateArgs(); + } + + public function setUp() + { + $this->debugLog( + '[JOBID ' . $this->job->payload['id'] . '] ' . get_class($this) . '::perform() start' + ); + + $this->timer = new \Tripod\Timer(); + $this->timer->start(); + + $this->setStatsConfig(); + + if ($this->configRequired) { + $this->setTripodConfig(); + } + } + + public function tearDown() + { + // stat time taken to process item, from time it was created (queued) + $this->timer->stop(); + $this->debugLog( + '[JOBID ' . $this->job->payload['id'] . '] ' . get_class($this) . + "::perform() done in {$this->timer->result()}ms" + ); + $this->getStat()->timer($this->getStatTimerSuccessKey(), $this->timer->result()); + } + + /** + * Resque event when a job failures + * + * @param \Exception $e Exception + * @param \Resque_Job $job The failed job + * @return void + */ + public static function onFailure(\Exception $e, \Resque_Job $job) + { + /** @var JobBase $failedJob */ + $failedJob = $job->getInstance(); + $failedJob->errorLog('Caught exception in '. get_called_class() . ': ' . $e->getMessage()); + $failedJob->getStat()->increment($failedJob->getStatFailureIncrementKey()); + throw $e; + } + /** * For mocking * @param string $storeName * @param string $podName - * @param array $opts + * @param array $opts * @return \Tripod\Mongo\Driver */ - protected function getTripod($storeName,$podName,$opts=array()) { - $opts = array_merge($opts,array( - 'stat'=>$this->getStat(), - 'readPreference' => ReadPreference::RP_PRIMARY // important: make sure we always read from the primary - )); + protected function getTripod($storeName, $podName, $opts = []) + { + $this->getTripodConfig(); + + $opts = array_merge( + $opts, + [ + 'stat' => $this->getStat(), + 'readPreference' => ReadPreference::RP_PRIMARY // important: make sure we always read from the primary + ] + ); if ($this->tripod == null) { $this->tripod = new \Tripod\Mongo\Driver( $podName, @@ -37,10 +138,13 @@ protected function getTripod($storeName,$podName,$opts=array()) { } /** - * Make sure each job considers how to validate it's args + * Make sure each job considers how to validate its args * @return array */ - protected abstract function getMandatoryArgs(); + protected function getMandatoryArgs() + { + return $this->mandatoryArgs; + } /** * Validate the arguments for this job @@ -48,20 +152,32 @@ protected abstract function getMandatoryArgs(); */ protected function validateArgs() { - foreach ($this->getMandatoryArgs() as $arg) - { - if (!isset($this->args[$arg])) - { - $message = "Argument $arg was not present in supplied job args for job ".get_class($this); + $message = null; + foreach ($this->getMandatoryArgs() as $arg) { + if (!isset($this->args[$arg])) { + $message = "Argument $arg was not present in supplied job args for job " . get_class($this); $this->errorLog($message); throw new \Exception($message); } } + if ($this->configRequired) { + $this->ensureConfig(); + } + } + + protected function ensureConfig() + { + if (!isset($this->args[self::TRIPOD_CONFIG_KEY]) && !isset($this->args[self::TRIPOD_CONFIG_GENERATOR])) { + $message = 'Argument ' . self::TRIPOD_CONFIG_KEY . ' or ' . self::TRIPOD_CONFIG_GENERATOR . + ' was not present in supplied job args for job ' . get_class($this); + $this->errorLog($message); + throw new \Exception($message); + } } /** - * @param string $message - * @param mixed $params + * @param string $message Log message + * @param mixed $params Log params */ public function debugLog($message, $params = null) { @@ -69,8 +185,8 @@ public function debugLog($message, $params = null) } /** - * @param string $message - * @param mixed $params + * @param string $message Log message + * @param mixed $params Log params */ public function errorLog($message, $params = null) { @@ -79,42 +195,36 @@ public function errorLog($message, $params = null) /** - * @param string $queueName - * @param string $class - * @param array $data - * @param int $retryAttempts if queue fails, retry x times before throwing an exception - * @return a tracking token for the submitted job - * @throws JobException if there is a problem queuing the job + * @param string $queueName Queue name + * @param string $class Class name + * @param array $data Job arguments + * @param int $retryAttempts If queue fails, retry x times before throwing an exception + * @return string A tracking token for the submitted job + * @throws JobException If there is a problem queuing the job */ - protected function submitJob($queueName, $class, Array $data, $retryAttempts=5) + protected function submitJob($queueName, $class, array $data, $retryAttempts = 5) { // @see https://github.com/chrisboulton/php-resque/issues/228, when this PR is merged we can stop tracking the status in this way - try - { + try { + if (isset($data[self::TRIPOD_CONFIG_GENERATOR]) && $data[self::TRIPOD_CONFIG_GENERATOR]) { + $data[self::TRIPOD_CONFIG_GENERATOR] = $this->serializeConfig($data[self::TRIPOD_CONFIG_GENERATOR]); + } $token = $this->enqueue($queueName, $class, $data); - if(!$this->getJobStatus($token)) - { + if (!$this->getJobStatus($token)) { $this->errorLog("Could not retrieve status for queued $class job - job $token failed to $queueName"); throw new \Exception("Could not retrieve status for queued job - job $token failed to $queueName"); - } - else - { + } else { $this->debugLog("Queued $class job with $token to $queueName"); return $token; } - } - catch (\Exception $e) - { - if ($retryAttempts>0) - { + } catch (\Exception $e) { + if ($retryAttempts > 0) { sleep(1); // back off for 1 sec $this->warningLog("Exception queuing $class job - {$e->getMessage()}, retrying $retryAttempts times"); - return $this->submitJob($queueName,$class,$data,--$retryAttempts); - } - else - { + return $this->submitJob($queueName, $class, $data, --$retryAttempts); + } else { $this->errorLog("Exception queuing $class job - {$e->getMessage()}"); - throw new JobException("Exception queuing job - {$e->getMessage()}",$e->getCode(),$e); + throw new JobException("Exception queuing job - {$e->getMessage()}", $e->getCode(), $e); } } } @@ -148,12 +258,131 @@ protected function getJobStatus($token) */ public function getStat() { - if((!isset($this->statsConfig) || empty($this->statsConfig)) && isset($this->args['statsConfig'])) - { - $this->statsConfig = $this->args['statsConfig']; + if (!isset($this->statsConfig)) { + $this->getStatsConfig(); } return parent::getStat(); } -} + /** + * Take a Tripod Config Serializer and return a config array + * + * @param ITripodConfigSerializer|array $configSerializer An object that implements ITripodConfigSerializer + * @return array + */ + protected function serializeConfig($configSerializer) + { + if ($configSerializer instanceof ITripodConfigSerializer) { + return $configSerializer->serialize(); + } elseif (is_array($configSerializer)) { + return $configSerializer; + } else { + throw new \InvalidArgumentException( + '$configSerializer must an ITripodConfigSerializer or array' + ); + } + } + + /** + * Deserialize a tripodConfigGenerator argument to a Tripod Config object + * + * @param array $config The serialized Tripod config + * @return \Tripod\ITripodConfig + */ + protected function deserializeConfig(array $config) + { + Config::setConfig($config); + return Config::getInstance(); + } + + /** + * Sets the Tripod config for the job + * + * @return void + */ + protected function setTripodConfig() + { + if (isset($this->args[self::TRIPOD_CONFIG_GENERATOR])) { + $config = $this->args[self::TRIPOD_CONFIG_GENERATOR]; + } else { + $config = $this->args[self::TRIPOD_CONFIG_KEY]; + } + $this->tripodConfig = $this->deserializeConfig($config); + } + + /** + * Returns the Tripod config required by the job + * + * @return \Tripod\ITripodConfig + */ + protected function getTripodConfig() + { + if (!isset($this->tripodConfig)) { + $this->ensureConfig(); + $this->setConfig(); + } + return $this->tripodConfig; + } + + /** + * Sets the stats config for the job + * + * @return void + */ + protected function setStatsConfig() + { + if (isset($this->args['statsConfig'])) { + $this->statsConfig = $this->args['statsConfig']; + } + } + + /** + * Gets the stats config for the job + * + * @return array + */ + public function getStatsConfig() + { + if (empty($this->statsConfig)) { + $this->setStatsConfig(); + } + return $this->statsConfig; + } + /** + * Tripod options to pass between jobs + * + * @return array + */ + protected function getTripodOptions() + { + $statsConfig = $this->getStatsConfig(); + $options = []; + if (!empty($statsConfig)) { + $options['statsConfig'] = $statsConfig; + } + return $options; + } + + /** + * Convenience method to pass config to job data + * + * @return array + */ + protected function generateConfigJobArgs() + { + $configInstance = $this->getConfigInstance(); + $args = []; + if ($configInstance instanceof ITripodConfigSerializer) { + $config = $configInstance->serialize(); + } else { + $config = \Tripod\Config::getConfig(); + } + if (isset($config['class'])) { + $args[self::TRIPOD_CONFIG_GENERATOR] = $config; + } else { + $args[self::TRIPOD_CONFIG_KEY] = $config; + } + return $args; + } +} diff --git a/src/mongo/delegates/SearchDocuments.class.php b/src/mongo/delegates/SearchDocuments.class.php index 302a4e91..617719a1 100644 --- a/src/mongo/delegates/SearchDocuments.class.php +++ b/src/mongo/delegates/SearchDocuments.class.php @@ -78,7 +78,7 @@ public function generateSearchDocumentBasedOnSpecId($specId, $resource, $context 'r'=>$this->labeller->uri_to_alias($resource), 'c'=>$this->labeller->uri_to_alias($context)); - if (Config::getInstance()->getCollectionForCBD($this->storeName, $irFrom)->findOne($indexRules['condition'])) + if ($this->getConfigInstance()->getCollectionForCBD($this->storeName, $irFrom)->findOne($indexRules['condition'])) { // match found, add this spec id to those that should be generated $proceedWithGeneration = true; @@ -101,7 +101,7 @@ public function generateSearchDocumentBasedOnSpecId($specId, $resource, $context 'c'=>$this->labeller->uri_to_alias($context) ); - $sourceDocument = Config::getInstance()->getCollectionForCBD($this->storeName, $from)->findOne(array('_id'=>$_id)); + $sourceDocument = $this->getConfigInstance()->getCollectionForCBD($this->storeName, $from)->findOne(array('_id'=>$_id)); if(empty($sourceDocument)){ $this->debugLog("Source document not found for $resource, cannot proceed generating $specId search document"); @@ -156,7 +156,7 @@ public function generateSearchDocumentsBasedOnRdfTypes(Array $rdfTypes, $resourc foreach($rdfTypes as $rdfType) { - $specs = Config::getInstance()->getSearchDocumentSpecifications($this->storeName, $rdfType); + $specs = $this->getConfigInstance()->getSearchDocumentSpecifications($this->storeName, $rdfType); if(empty($specs)) continue; // no point doing anything else if there is no spec for the type @@ -180,7 +180,7 @@ protected function doJoin($source, $joins, &$target, $from) { // expand sequences before proceeding $this->expandSequence($joins, $source); - $config = Config::getInstance(); + $config = $this->getConfigInstance(); foreach($joins as $predicate=>$rules){ if(isset($source[$predicate])){ $joinUris = array(); @@ -203,7 +203,7 @@ protected function doJoin($source, $joins, &$target, $from) ); $cursor = $collection->find(array('_id'=>array('$in'=>$joinUris)), array( - 'maxTimeMS' => \Tripod\Mongo\Config::getInstance()->getMongoCursorTimeout() + 'maxTimeMS' => $this->getConfigInstance()->getMongoCursorTimeout() )); // add to impact index @@ -289,7 +289,7 @@ protected function addFields(Array $source, Array $fieldsOrIndices, Array &$targ */ protected function getSearchDocumentSpecification($specId) { - return Config::getInstance()->getSearchDocumentSpecification($this->storeName, $specId); + return $this->getConfigInstance()->getSearchDocumentSpecification($this->storeName, $specId); } /** diff --git a/src/mongo/delegates/SearchIndexer.class.php b/src/mongo/delegates/SearchIndexer.class.php index eeb3231c..29f2b175 100644 --- a/src/mongo/delegates/SearchIndexer.class.php +++ b/src/mongo/delegates/SearchIndexer.class.php @@ -7,7 +7,6 @@ require_once TRIPOD_DIR . 'mongo/providers/MongoSearchProvider.class.php'; require_once TRIPOD_DIR . 'exceptions/SearchException.class.php'; -use Tripod\Mongo\Config; use Tripod\Mongo\ImpactedSubject; use Tripod\Mongo\Labeller; use Tripod\Mongo\Jobs\ApplyOperation; @@ -44,10 +43,10 @@ public function __construct(\Tripod\Mongo\Driver $tripod, $readPreference = Read $this->podName = $tripod->podName; $this->labeller = new Labeller(); $this->stat = $tripod->getStat(); - $this->config = Config::getInstance(); + $this->config = $this->getConfigInstance(); $provider = $this->config->getSearchProviderClassName($this->tripod->getStoreName()); - if(class_exists($provider)){ + if (class_exists($provider)) { $this->configuredProvider = new $provider($this->tripod); } else { throw new \Tripod\Exceptions\SearchException("Did not recognise Search Provider, or could not find class: $provider"); @@ -158,8 +157,10 @@ public function generateAndIndexSearchDocuments($resourceUri, $context, $podName } } - foreach($documentsToIndex as $document) { - if(!empty($document)) $searchProvider->indexDocument($document); + foreach ($documentsToIndex as $document) { + if (!empty($document)) { + $searchProvider->indexDocument($document); + } } } @@ -170,16 +171,19 @@ public function generateAndIndexSearchDocuments($resourceUri, $context, $podName * @param string|null $queueName * @return array|null Will return an array with a count and group id, if $queueName is sent and $resourceUri is null */ - public function generateSearchDocuments($searchDocumentType, $resourceUri=null, $context=null, $queueName=null) - { + public function generateSearchDocuments( + $searchDocumentType, + $resourceUri = null, + $context = null, + $queueName = null + ) { $t = new \Tripod\Timer(); $t->start(); // default the context $contextAlias = $this->getContextAlias($context); - $spec = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecification($this->getStoreName(), $searchDocumentType); + $spec = $this->getConfigInstance()->getSearchDocumentSpecification($this->getStoreName(), $searchDocumentType); - if($resourceUri) - { + if ($resourceUri) { $this->generateAndIndexSearchDocuments($resourceUri, $contextAlias, $spec['from'], $searchDocumentType); return; } diff --git a/src/mongo/delegates/Tables.class.php b/src/mongo/delegates/Tables.class.php index a20992fe..ea8314b4 100644 --- a/src/mongo/delegates/Tables.class.php +++ b/src/mongo/delegates/Tables.class.php @@ -3,10 +3,8 @@ namespace Tripod\Mongo\Composites; require_once TRIPOD_DIR . 'mongo/MongoTripodConstants.php'; -require_once TRIPOD_DIR . 'mongo/base/DriverBase.class.php'; use \Tripod\Mongo\Jobs\ApplyOperation; -use \Tripod\Mongo\Config; use \Tripod\Mongo\ImpactedSubject; use \Tripod\Mongo\Labeller; use \Tripod\Mongo\JobGroup; @@ -78,13 +76,18 @@ class Tables extends CompositeBase * @param string $readPreference * todo: MongoCollection -> podName */ - public function __construct($storeName, Collection $collection,$defaultContext,$stat=null,$readPreference = ReadPreference::RP_PRIMARY) - { + public function __construct( + $storeName, + Collection $collection, + $defaultContext, + $stat = null, + $readPreference = ReadPreference::RP_PRIMARY + ) { $this->labeller = new Labeller(); $this->storeName = $storeName; $this->collection = $collection; $this->podName = $collection->getCollectionName(); - $this->config = Config::getInstance(); + $this->config = $this->getConfigInstance(); $this->defaultContext = $this->labeller->uri_to_alias($defaultContext); // make sure default context is qnamed if applicable $this->stat = $stat; $this->readPreference = $readPreference; @@ -119,38 +122,32 @@ public function getTypesInSpecifications() * @param string $contextAlias * @return array */ - public function findImpactedComposites(Array $resourcesAndPredicates, $contextAlias) + public function findImpactedComposites(array $resourcesAndPredicates, $contextAlias) { $contextAlias = $this->getContextAlias($contextAlias); // belt and braces $tablePredicates = array(); - foreach(Config::getInstance()->getTableSpecifications($this->storeName) as $tableSpec) - { - if(isset($tableSpec[_ID_KEY])) - { - $tablePredicates[$tableSpec[_ID_KEY]] = Config::getInstance()->getDefinedPredicatesInSpec($this->storeName, $tableSpec[_ID_KEY]); + foreach ($this->getConfigInstance()->getTableSpecifications($this->storeName) as $tableSpec) { + if (isset($tableSpec[_ID_KEY])) { + $tablePredicates[$tableSpec[_ID_KEY]] = $this->getConfigInstance() + ->getDefinedPredicatesInSpec($this->storeName, $tableSpec[_ID_KEY]); } } // build a filter - will be used for impactIndex detection and finding direct tables to re-gen $tableFilters = array(); $resourceFilters = array(); - foreach ($resourcesAndPredicates as $resource=>$resourcePredicates) - { + foreach ($resourcesAndPredicates as $resource => $resourcePredicates) { $resourceAlias = $this->labeller->uri_to_alias($resource); $id = array(_ID_RESOURCE=>$resourceAlias,_ID_CONTEXT=>$contextAlias); // If we don't have a working config or there are no predicates listed, remove all // rows associated with the resource in all tables - if(empty($tablePredicates) || empty($resourcePredicates)) - { + if (empty($tablePredicates) || empty($resourcePredicates)) { // build $filter for queries to impact index $resourceFilters[] = $id; - } - else - { - foreach($tablePredicates as $tableType=>$predicates) - { + } else { + foreach ($tablePredicates as $tableType => $predicates) { // Only look for table rows if the changed predicates are actually defined in the tablespec if(array_intersect($resourcePredicates, $predicates)) { @@ -367,7 +364,7 @@ protected function deleteTableRowsForResource($resource, $context=null, $specTyp public function deleteTableRowsByTableId($tableId, $timestamp = null) { $t = new \Tripod\Timer(); $t->start(); - $tableSpec = Config::getInstance()->getTableSpecification($this->storeName, $tableId); + $tableSpec = $this->getConfigInstance()->getTableSpecification($this->storeName, $tableId); if ($tableSpec == null) { $this->debugLog("Could not find a table specification for $tableId"); return; @@ -455,14 +452,14 @@ public function generateTableRowsForType($rdfType,$subject=null,$context=null, $ if(empty($specTypes)) { - $tableSpecs = Config::getInstance()->getTableSpecifications($this->storeName); + $tableSpecs = $this->getConfigInstance()->getTableSpecifications($this->storeName); } else { $tableSpecs = array(); foreach($specTypes as $specType) { - $spec = Config::getInstance()->getTableSpecification($this->storeName, $specType); + $spec = $this->getConfigInstance()->getTableSpecification($this->storeName, $specType); if($spec) { $tableSpecs[$specType] = $spec; @@ -506,7 +503,7 @@ public function generateTableRows($tableType, $resource = null, $context = null, $t = new \Tripod\Timer(); $t->start(); $this->temporaryFields = array(); - $tableSpec = Config::getInstance()->getTableSpecification($this->storeName, $tableType); + $tableSpec = $this->getConfigInstance()->getTableSpecification($this->storeName, $tableType); $collection = $this->config->getCollectionForTable($this->storeName, $tableType); if ($tableSpec==null) { diff --git a/src/mongo/delegates/TransactionLog.class.php b/src/mongo/delegates/TransactionLog.class.php index d84bffbe..6ee5ab71 100644 --- a/src/mongo/delegates/TransactionLog.class.php +++ b/src/mongo/delegates/TransactionLog.class.php @@ -1,7 +1,6 @@ config = $config->getTransactionLogConfig(); $this->transaction_db = $config->getTransactionLogDatabase(); $this->transaction_collection = $this->transaction_db->selectCollection($this->config['collection']); diff --git a/src/mongo/delegates/Updates.class.php b/src/mongo/delegates/Updates.class.php index 0dbef6b0..8706e005 100644 --- a/src/mongo/delegates/Updates.class.php +++ b/src/mongo/delegates/Updates.class.php @@ -9,14 +9,14 @@ use \MongoDB\Collection; use \MongoDB\Operation\FindOneAndUpdate; use \MongoDB\BSON\ObjectId; - -require_once TRIPOD_DIR . 'mongo/Config.class.php'; +use Tripod\Mongo\Jobs\DiscoverImpactedSubjects; /** * Class Updates * @package Tripod\Mongo */ -class Updates extends DriverBase { +class Updates extends DriverBase +{ /** * $var TransactionLog @@ -167,11 +167,10 @@ public function saveChanges( )); $this->setReadPreferenceToPrimary(); - try{ + try { $contextAlias = $this->getContextAlias($context); - if (!Config::getInstance()->isPodWithinStore($this->getStoreName(),$this->getPodName())) - { + if (!$this->getConfigInstance()->isPodWithinStore($this->getStoreName(), $this->getPodName())) { throw new \Tripod\Exceptions\Exception("database:collection " . $this->getStoreName() . ":" . $this->getPodName(). " is not referenced within config, so cannot be written to"); } @@ -304,7 +303,7 @@ protected function resetOriginalReadPreference(){ */ protected function validateGraphCardinality(\Tripod\ExtendedGraph $graph) { - $config = Config::getInstance(); + $config = $this->getConfigInstance(); $cardinality = $config->getCardinality($this->getStoreName(), $this->getPodName()); $namespaces = $config->getNamespaces(); $graphSubjects = $graph->get_subjects(); @@ -851,29 +850,25 @@ protected function processSyncOperations(Array $subjectsAndPredicatesOfChange, $ * @param array $subjectsAndPredicatesOfChange * @param string $contextAlias */ - protected function queueASyncOperations(Array $subjectsAndPredicatesOfChange,$contextAlias) + protected function queueASyncOperations(array $subjectsAndPredicatesOfChange, $contextAlias) { $operations = $this->getAsyncOperations(); if (!empty($operations)) { - $data = array( - "changes" => $subjectsAndPredicatesOfChange, - "operations" => $operations, - "tripodConfig" => Config::getConfig(), - "storeName" => $this->storeName, - "podName" => $this->podName, - "contextAlias" => $contextAlias, - "statsConfig"=>$this->getStatsConfig() - ); - - - if(isset($this->queueName)) - { + $data = [ + 'changes' => $subjectsAndPredicatesOfChange, + 'operations' => $operations, + 'storeName' => $this->storeName, + 'podName' => $this->podName, + 'contextAlias' => $contextAlias, + 'statsConfig' => $this->getStatsConfig() + ]; + + if (isset($this->queueName)) { $data[OP_QUEUE] = $this->queueName; $queueName = $this->queueName; - } - else - { - $queueName = Config::getDiscoverQueueName(); + } else { + $configInstance = $this->getConfigInstance(); + $queueName = $configInstance::getDiscoverQueueName(); } $this->getDiscoverImpactedSubjects()->createJob($data, $queueName); @@ -886,8 +881,7 @@ protected function queueASyncOperations(Array $subjectsAndPredicatesOfChange,$co */ protected function getDiscoverImpactedSubjects() { - if(!isset($this->discoverImpactedSubjects)) - { + if (!isset($this->discoverImpactedSubjects)) { $this->discoverImpactedSubjects = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); } return $this->discoverImpactedSubjects; @@ -1216,15 +1210,6 @@ protected function getAuditManualRollbacksCollection() return $this->config->getCollectionForManualRollbackAudit($this->storeName); } - /** - * For mocking - * @return Config - */ - protected function getConfigInstance() - { - return Config::getInstance(); - } - /** * @return ObjectId */ @@ -1357,7 +1342,7 @@ protected function updateCollection($query, $update, $options) protected function getContextAlias($context=null) { $contextAlias = $this->labeller->uri_to_alias((empty($context)) ? $this->defaultContext : $context); - return (empty($contextAlias)) ? Config::getInstance()->getDefaultContextAlias() : $contextAlias; + return (empty($contextAlias)) ? $this->getConfigInstance()->getDefaultContextAlias() : $contextAlias; } /** @@ -1414,4 +1399,4 @@ private function getSyncOperations() } return $types; } -} \ No newline at end of file +} diff --git a/src/mongo/delegates/Views.class.php b/src/mongo/delegates/Views.class.php index f405a4f4..c1ba8f1b 100644 --- a/src/mongo/delegates/Views.class.php +++ b/src/mongo/delegates/Views.class.php @@ -2,10 +2,7 @@ namespace Tripod\Mongo\Composites; -require_once TRIPOD_DIR . 'mongo/base/DriverBase.class.php'; - use \Tripod\Mongo\Jobs\ApplyOperation; -use \Tripod\Mongo\Config; use \Tripod\Mongo\ImpactedSubject; use \Tripod\Mongo\Labeller; use \MongoDB\Driver\ReadPreference; @@ -35,7 +32,7 @@ public function __construct($storeName, Collection $collection,$defaultContext,$ $this->collection = $collection; $this->podName = $collection->getCollectionName(); $this->defaultContext = $defaultContext; - $this->config = Config::getInstance(); + $this->config = $this->getConfigInstance(); $this->stat = $stat; $this->readPreference = $readPreference; } @@ -194,7 +191,7 @@ public function getViewForResource($resource,$viewType,$context=null) if ($graph->is_empty()) { $this->getStat()->increment(MONGO_VIEW_CACHE_MISS.".$viewType"); - $viewSpec = Config::getInstance()->getViewSpecification($this->storeName, $viewType); + $viewSpec = $this->getConfigInstance()->getViewSpecification($this->storeName, $viewType); if($viewSpec == null) { return new \Tripod\Mongo\MongoGraph(); @@ -315,10 +312,8 @@ public function generateViews($resources,$context=null) ['store' => $this->storeName, '_id' => $resourceAlias] ); // delete any views this resource is involved in. It's type may have changed so it's not enough just to regen it with it's new type below. - foreach (Config::getInstance()->getViewSpecifications($this->storeName) as $type=>$spec) - { - if($spec['from']==$this->podName){ - + foreach ($this->getConfigInstance()->getViewSpecifications($this->storeName) as $type => $spec) { + if ($spec['from'] == $this->podName) { $this->config->getCollectionForView($this->storeName, $type, $this->readPreference) ->deleteOne(array("_id" => array("r"=>$resourceAlias,"c"=>$contextAlias,"type"=>$type))); } @@ -367,7 +362,7 @@ public function generateViewsForResourcesOfType($rdfType,$resource=null,$context $rdfType = $this->labeller->qname_to_alias($rdfType); $rdfTypeAlias = $this->labeller->uri_to_alias($rdfType); $foundSpec = false; - $viewSpecs = Config::getInstance()->getViewSpecifications($this->storeName); + $viewSpecs = $this->getConfigInstance()->getViewSpecifications($this->storeName); foreach($viewSpecs as $key=>$viewSpec) { // check for rdfType and rdfTypeAlias @@ -395,7 +390,7 @@ public function generateViewsForResourcesOfType($rdfType,$resource=null,$context */ public function deleteViewsByViewId($viewId, $timestamp = null) { - $viewSpec = Config::getInstance()->getViewSpecification($this->storeName, $viewId); + $viewSpec = $this->getConfigInstance()->getViewSpecification($this->storeName, $viewId); if ($viewSpec == null) { $this->debugLog("Could not find a view specification with viewId '$viewId'"); return; @@ -427,7 +422,7 @@ public function deleteViewsByViewId($viewId, $timestamp = null) public function generateView($viewId, $resource = null, $context = null, $queueName = null) { $contextAlias = $this->getContextAlias($context); - $viewSpec = Config::getInstance()->getViewSpecification($this->storeName, $viewId); + $viewSpec = $this->getConfigInstance()->getViewSpecification($this->storeName, $viewId); if ($viewSpec == null) { $this->debugLog("Could not find a view specification for $resource with viewId '$viewId'"); return null; @@ -461,7 +456,7 @@ public function generateView($viewId, $resource = null, $context = null, $queueN // @todo Change this to a command when we upgrade MongoDB to 1.1+ $count = $this->config->getCollectionForCBD($this->storeName, $from)->count($filter); $docs = $this->config->getCollectionForCBD($this->storeName, $from)->find($filter, array( - 'maxTimeMS' => \Tripod\Mongo\Config::getInstance()->getMongoCursorTimeout() + 'maxTimeMS' => $this->getConfigInstance()->getMongoCursorTimeout() )); @@ -591,7 +586,7 @@ protected function doJoins($source, $joins, &$dest, $from, $contextAlias, $build ); $cursor = $collection->find(array('_id'=>array('$in'=>$joinUris)), array( - 'maxTimeMS' => \Tripod\Mongo\Config::getInstance()->getMongoCursorTimeout() + 'maxTimeMS' => $this->getConfigInstance()->getMongoCursorTimeout() )); $this->addIdToImpactIndex($joinUris, $dest, $buildImpactIndex); diff --git a/src/mongo/jobs/ApplyOperation.class.php b/src/mongo/jobs/ApplyOperation.class.php index 318cfec1..2bf78665 100644 --- a/src/mongo/jobs/ApplyOperation.class.php +++ b/src/mongo/jobs/ApplyOperation.class.php @@ -4,99 +4,98 @@ use Tripod\Mongo\JobGroup; use Tripod\Mongo\Driver; - +use Tripod\ITripodConfigSerializer; /** * Class ApplyOperation * @package Tripod\Mongo\Jobs */ -class ApplyOperation extends JobBase { +class ApplyOperation extends JobBase +{ const SUBJECTS_KEY = 'subjects'; const TRACKING_KEY = 'batchId'; + protected $configRequired = true; + protected $mandatoryArgs = [self::SUBJECTS_KEY]; + /** * Run the ApplyOperation job * @throws \Exception */ public function perform() { - try { - $this->debugLog("[JOBID " . $this->job->payload['id'] . "] ApplyOperation::perform() start"); - - $timer = new \Tripod\Timer(); - $timer->start(); - - $this->validateArgs(); - - $statsConfig = array(); - if(isset($this->args['statsConfig'])) - { - $statsConfig['statsConfig'] = $this->args['statsConfig']; - } + $this->getStat()->increment( + MONGO_QUEUE_APPLY_OPERATION_JOB . '.' . SUBJECT_COUNT, + count($this->args[self::SUBJECTS_KEY]) + ); - // set the config to what is received - \Tripod\Mongo\Config::setConfig($this->args[self::TRIPOD_CONFIG_KEY]); - - $this->getStat()->increment(MONGO_QUEUE_APPLY_OPERATION_JOB . '.' . SUBJECT_COUNT, count($this->args[self::SUBJECTS_KEY])); - - foreach ($this->args[self::SUBJECTS_KEY] as $subject) { - $opTimer = new \Tripod\Timer(); - $opTimer->start(); - - $impactedSubject = $this->createImpactedSubject($subject); - $impactedSubject->update(); - - $opTimer->stop(); - // stat time taken to perform operation for the given subject - $this->getStat()->timer(MONGO_QUEUE_APPLY_OPERATION.'.'.$subject['operation'], $opTimer->result()); - - /** - * ApplyOperation jobs can either apply to a single resource (e.g. 'create composite for the given - * resource uri) or for a specification id (i.e. regenerate all of the composites defined by the - * specification). For the latter, we need to keep track of how many jobs have run so we can clean - * up any stale composite documents when completed. The TRACKING_KEY value will be the JobGroup id. - */ - if (isset($this->args[self::TRACKING_KEY])) { - $jobGroup = $this->getJobGroup($subject['storeName'], $this->args[self::TRACKING_KEY]); - $jobCount = $jobGroup->incrementJobCount(-1); - if ($jobCount <= 0) { - // @todo Replace this with ObjectId->getTimestamp() if we upgrade Mongo driver to 1.2 - $timestamp = new \MongoDB\BSON\UTCDateTime(hexdec(substr($jobGroup->getId(), 0, 8)) * 1000); - $tripod = $this->getTripod($subject['storeName'], $subject['podName']); - $count = 0; - foreach ($subject['specTypes'] as $specId) { - switch ($subject['operation']) { - case \OP_VIEWS: - $count += $tripod->getComposite(\OP_VIEWS)->deleteViewsByViewId($specId, $timestamp); - break; - case \OP_TABLES: - $count += $tripod->getComposite(\OP_TABLES)->deleteTableRowsByTableId($specId, $timestamp); - break; - case \OP_SEARCH: - $searchProvider = $this->getSearchProvider($tripod); - $count += $searchProvider->deleteSearchDocumentsByTypeId($specId, $timestamp); - break; - } + foreach ($this->args[self::SUBJECTS_KEY] as $subject) { + $opTimer = new \Tripod\Timer(); + $opTimer->start(); + + $impactedSubject = $this->createImpactedSubject($subject); + $impactedSubject->update(); + + $opTimer->stop(); + // stat time taken to perform operation for the given subject + $this->getStat()->timer(MONGO_QUEUE_APPLY_OPERATION.'.'.$subject['operation'], $opTimer->result()); + + /** + * ApplyOperation jobs can either apply to a single resource (e.g. 'create composite for the given + * resource uri) or for a specification id (i.e. regenerate all of the composites defined by the + * specification). For the latter, we need to keep track of how many jobs have run so we can clean + * up any stale composite documents when completed. The TRACKING_KEY value will be the JobGroup id. + */ + if (isset($this->args[self::TRACKING_KEY])) { + $jobGroup = $this->getJobGroup($subject['storeName'], $this->args[self::TRACKING_KEY]); + $jobCount = $jobGroup->incrementJobCount(-1); + if ($jobCount <= 0) { + // @todo Replace this with ObjectId->getTimestamp() if we upgrade Mongo driver to 1.2 + $timestamp = new \MongoDB\BSON\UTCDateTime(hexdec(substr($jobGroup->getId(), 0, 8)) * 1000); + $tripod = $this->getTripod($subject['storeName'], $subject['podName']); + $count = 0; + foreach ($subject['specTypes'] as $specId) { + switch ($subject['operation']) { + case \OP_VIEWS: + $count += $tripod->getComposite(\OP_VIEWS)->deleteViewsByViewId($specId, $timestamp); + break; + case \OP_TABLES: + $count += $tripod->getComposite(\OP_TABLES)->deleteTableRowsByTableId($specId, $timestamp); + break; + case \OP_SEARCH: + $searchProvider = $this->getSearchProvider($tripod); + $count += $searchProvider->deleteSearchDocumentsByTypeId($specId, $timestamp); + break; } - $this->infoLog( - '[JobGroupId ' . $jobGroup->getId()->__toString() . '] composite cleanup for ' . - $subject['operation'] . ' removed ' . $count . ' stale composite documents' - ); } + $this->infoLog( + '[JobGroupId ' . $jobGroup->getId()->__toString() . '] composite cleanup for ' . + $subject['operation'] . ' removed ' . $count . ' stale composite documents' + ); } } + } + } + /** + * Stat string for successful job timer + * + * @return string + */ + protected function getStatTimerSuccessKey() + { + return MONGO_QUEUE_APPLY_OPERATION_SUCCESS; + } - $timer->stop(); - // stat time taken to process job, from time it was picked up - $this->getStat()->timer(MONGO_QUEUE_APPLY_OPERATION_SUCCESS,$timer->result()); - $this->debugLog("[JOBID " . $this->job->payload['id'] . "] ApplyOperation::perform() done in {$timer->result()}ms"); - } catch (\Exception $e) { - $this->getStat()->increment(MONGO_QUEUE_APPLY_OPERATION_FAIL); - $this->errorLog("Caught exception in ".get_class($this).": ".$e->getMessage()); - throw $e; - } + /** + * Stat string for failed job increment + * + * @return string + */ + protected function getStatFailureIncrementKey() + { + return MONGO_QUEUE_APPLY_OPERATION_FAIL; } /** @@ -104,23 +103,30 @@ public function perform() * @param string|null $queueName * @param array $otherData */ - public function createJob(Array $subjects, $queueName=null,$otherData=array()) + public function createJob(array $subjects, $queueName = null, $otherData = []) { - if(!$queueName) - { - $queueName = \Tripod\Mongo\Config::getApplyQueueName(); - } - elseif(strpos($queueName, \Tripod\Mongo\Config::getApplyQueueName()) === false) - { - $queueName = \Tripod\Mongo\Config::getApplyQueueName() . '::' . $queueName; + $configInstance = $this->getConfigInstance(); + if (!$queueName) { + $queueName = $configInstance::getApplyQueueName(); + } elseif (strpos($queueName, $configInstance::getApplyQueueName()) === false) { + $queueName = $configInstance::getApplyQueueName() . '::' . $queueName; } - $data = array( - self::SUBJECTS_KEY=>array_map(function(\Tripod\Mongo\ImpactedSubject $subject) { return $subject->toArray(); }, $subjects), - self::TRIPOD_CONFIG_KEY=>\Tripod\Mongo\Config::getConfig() + $data = [ + self::SUBJECTS_KEY => array_map( + function (\Tripod\Mongo\ImpactedSubject $subject) { + return $subject->toArray(); + }, + $subjects + ), + ]; + + $data = array_merge( + $this->generateConfigJobArgs(), + $data ); - $this->submitJob($queueName,get_class($this),array_merge($otherData,$data)); + $this->submitJob($queueName, get_class($this), array_merge($otherData, $data)); } /** @@ -139,15 +145,6 @@ protected function createImpactedSubject(array $args) ); } - /** - * Validate args for ApplyOperation - * @return array - */ - protected function getMandatoryArgs() - { - return array(self::TRIPOD_CONFIG_KEY,self::SUBJECTS_KEY); - } - /** * For mocking * diff --git a/src/mongo/jobs/DiscoverImpactedSubjects.class.php b/src/mongo/jobs/DiscoverImpactedSubjects.class.php index 2f0e6c06..5c7df41c 100644 --- a/src/mongo/jobs/DiscoverImpactedSubjects.class.php +++ b/src/mongo/jobs/DiscoverImpactedSubjects.class.php @@ -2,12 +2,14 @@ namespace Tripod\Mongo\Jobs; -use \Tripod\Mongo\Config; +use \Tripod\Config; + /** * Class DiscoverImpactedSubjects * @package Tripod\Mongo\Jobs */ -class DiscoverImpactedSubjects extends JobBase { +class DiscoverImpactedSubjects extends JobBase +{ const STORE_NAME_KEY = 'storeName'; const POD_NAME_KEY = 'podName'; @@ -23,7 +25,19 @@ class DiscoverImpactedSubjects extends JobBase { /** * @var array */ - protected $subjectsGroupedByQueue = array(); + protected $subjectsGroupedByQueue = []; + + protected $configRequired = true; + + protected $subjectCount; + + protected $mandatoryArgs = [ + self::STORE_NAME_KEY, + self::POD_NAME_KEY, + self::CHANGES_KEY, + self::OPERATIONS_KEY, + self::CONTEXT_ALIAS_KEY + ]; /** * Run the DiscoverImpactedSubjects job @@ -31,153 +45,139 @@ class DiscoverImpactedSubjects extends JobBase { */ public function perform() { - try - { - - $this->debugLog("[JOBID " . $this->job->payload['id'] . "] DiscoverImpactedSubjects::perform() start"); - - $timer = new \Tripod\Timer(); - $timer->start(); - - $this->validateArgs(); - - // set the config to what is received - \Tripod\Mongo\Config::setConfig($this->args[self::TRIPOD_CONFIG_KEY]); + $tripod = $this->getTripod( + $this->args[self::STORE_NAME_KEY], + $this->args[self::POD_NAME_KEY], + $this->getTripodOptions() + ); - $statsConfig = array(); - if(isset($this->args['statsConfig'])) - { - $statsConfig['statsConfig'] = $this->args['statsConfig']; - } - $tripod = $this->getTripod($this->args[self::STORE_NAME_KEY],$this->args[self::POD_NAME_KEY],$statsConfig); - - $operations = $this->args[self::OPERATIONS_KEY]; - - $subjectsAndPredicatesOfChange = $this->args[self::CHANGES_KEY]; - - $subjectCount = 0; - foreach($operations as $op) - { - /** @var \Tripod\Mongo\Composites\IComposite $composite */ - $composite = $tripod->getComposite($op); - $modifiedSubjects = $composite->getImpactedSubjects($subjectsAndPredicatesOfChange,$this->args[self::CONTEXT_ALIAS_KEY]); - if(!empty($modifiedSubjects)){ - /* @var $subject \Tripod\Mongo\ImpactedSubject */ - foreach ($modifiedSubjects as $subject) { - $subjectCount++; - $subjectTimer = new \Tripod\Timer(); - $subjectTimer->start(); - if(isset($this->args[self::QUEUE_KEY]) || count($subject->getSpecTypes()) == 0) - { - $queueName = (isset($this->args[self::QUEUE_KEY]) ? $this->args[self::QUEUE_KEY] : Config::getApplyQueueName()); - $this->addSubjectToQueue($subject, $queueName); + $operations = $this->args[self::OPERATIONS_KEY]; + + $subjectsAndPredicatesOfChange = $this->args[self::CHANGES_KEY]; + + $this->subjectCount = 0; + foreach ($operations as $op) { + /** @var \Tripod\Mongo\Composites\IComposite $composite */ + $composite = $tripod->getComposite($op); + $modifiedSubjects = $composite->getImpactedSubjects( + $subjectsAndPredicatesOfChange, + $this->args[self::CONTEXT_ALIAS_KEY] + ); + if (!empty($modifiedSubjects)) { + $configInstance = $this->getConfigInstance(); + /* @var $subject \Tripod\Mongo\ImpactedSubject */ + foreach ($modifiedSubjects as $subject) { + $this->subjectCount++; + $subjectTimer = new \Tripod\Timer(); + $subjectTimer->start(); + if (isset($this->args[self::QUEUE_KEY]) || count($subject->getSpecTypes()) == 0) { + if (isset($this->args[self::QUEUE_KEY])) { + $queueName = $this->args[self::QUEUE_KEY]; + } else { + $queueName = $configInstance::getApplyQueueName(); } - else - { - $specsGroupedByQueue = array(); - foreach($subject->getSpecTypes() as $specType) - { - $spec = null; - switch($subject->getOperation()) - { - case OP_VIEWS: - $spec = Config::getInstance()->getViewSpecification($this->args[self::STORE_NAME_KEY], $specType); - break; - case OP_TABLES: - $spec = Config::getInstance()->getTableSpecification($this->args[self::STORE_NAME_KEY], $specType); - break; - case OP_SEARCH: - $spec = Config::getInstance()->getSearchDocumentSpecification($this->args[self::STORE_NAME_KEY], $specType); - break; - } - if(!$spec || !isset($spec['queue'])) - { - if(!$spec) - { - $spec = array(); - } - $spec['queue'] = Config::getApplyQueueName(); - } - if(!isset($specsGroupedByQueue[$spec['queue']])) - { - $specsGroupedByQueue[$spec['queue']] = array(); + $this->addSubjectToQueue($subject, $queueName); + } else { + $specsGroupedByQueue = array(); + foreach ($subject->getSpecTypes() as $specType) { + $spec = null; + switch ($subject->getOperation()) { + case OP_VIEWS: + $spec = $configInstance->getViewSpecification( + $this->args[self::STORE_NAME_KEY], + $specType + ); + break; + case OP_TABLES: + $spec = $configInstance->getTableSpecification( + $this->args[self::STORE_NAME_KEY], + $specType + ); + break; + case OP_SEARCH: + $spec = $configInstance->getSearchDocumentSpecification( + $this->args[self::STORE_NAME_KEY], + $specType + ); + break; + } + if (!$spec || !isset($spec['queue'])) { + if (!$spec) { + $spec = array(); } - $specsGroupedByQueue[$spec['queue']][] = $specType; + $spec['queue'] = $configInstance::getApplyQueueName(); } - - foreach($specsGroupedByQueue as $queueName=>$specs) - { - $queuedSubject = new \Tripod\Mongo\ImpactedSubject( - $subject->getResourceId(), - $subject->getOperation(), - $subject->getStoreName(), - $subject->getPodName(), - $specs - ); - - $this->addSubjectToQueue($queuedSubject, $queueName); + if (!isset($specsGroupedByQueue[$spec['queue']])) { + $specsGroupedByQueue[$spec['queue']] = array(); } + $specsGroupedByQueue[$spec['queue']][] = $specType; } - $subjectTimer->stop(); - // stat time taken to discover impacted subjects for the given subject of change - $this->getStat()->timer(MONGO_QUEUE_DISCOVER_SUBJECT, $subjectTimer->result()); - } - if(!empty($this->subjectsGroupedByQueue)) - { - foreach($this->subjectsGroupedByQueue as $queueName=>$subjects) - { - $this->getApplyOperation()->createJob($subjects, $queueName, $statsConfig); + + foreach ($specsGroupedByQueue as $queueName => $specs) { + $queuedSubject = new \Tripod\Mongo\ImpactedSubject( + $subject->getResourceId(), + $subject->getOperation(), + $subject->getStoreName(), + $subject->getPodName(), + $specs + ); + + $this->addSubjectToQueue($queuedSubject, $queueName); } - $this->subjectsGroupedByQueue = array(); } + $subjectTimer->stop(); + // stat time taken to discover impacted subjects for the given subject of change + $this->getStat()->timer(MONGO_QUEUE_DISCOVER_SUBJECT, $subjectTimer->result()); + } + if (!empty($this->subjectsGroupedByQueue)) { + foreach ($this->subjectsGroupedByQueue as $queueName => $subjects) { + $this->getApplyOperation()->createJob($subjects, $queueName, $this->getTripodOptions()); + } + $this->subjectsGroupedByQueue = array(); } } - - // stat time taken to process item, from time it was created (queued) - $timer->stop(); - $this->getStat()->timer(MONGO_QUEUE_DISCOVER_SUCCESS,$timer->result()); - $this->debugLog("[JOBID " . $this->job->payload['id'] . "] DiscoverImpactedSubjects::perform() done in {$timer->result()}ms"); - $this->getStat()->increment(MONGO_QUEUE_DISCOVER_JOB . '.' . SUBJECT_COUNT, $subjectCount); - } - catch(\Exception $e) - { - $this->getStat()->increment(MONGO_QUEUE_DISCOVER_FAIL); - $this->errorLog("Caught exception in ".get_class($this).": ".$e->getMessage()); - throw $e; } } + public function tearDown() + { + parent::tearDown(); + $this->getStat()->increment(MONGO_QUEUE_DISCOVER_JOB . '.' . SUBJECT_COUNT, $this->subjectCount); + } + /** - * @param array $data - * @param string|null $queueName + * Stat string for successful job timer + * + * @return string */ - public function createJob(Array $data, $queueName=null) + protected function getStatTimerSuccessKey() { - if(!$queueName) - { - $queueName = Config::getDiscoverQueueName(); - } - elseif(strpos($queueName, \Tripod\Mongo\Config::getDiscoverQueueName()) === false) - { - $queueName = \Tripod\Mongo\Config::getDiscoverQueueName() . '::' . $queueName; - } - $this->submitJob($queueName,get_class($this),$data); + return MONGO_QUEUE_DISCOVER_SUCCESS; } /** - * Validate args for DiscoverImpactedSubjects - * @return array + * Stat string for failed job increment + * + * @return string */ - protected function getMandatoryArgs() + protected function getStatFailureIncrementKey() { - return array( - self::TRIPOD_CONFIG_KEY, - self::STORE_NAME_KEY, - self::POD_NAME_KEY, - self::CHANGES_KEY, - self::OPERATIONS_KEY, - self::CONTEXT_ALIAS_KEY - ); + return MONGO_QUEUE_DISCOVER_FAIL; + } + + /** + * @param array $data + * @param string|null $queueName + */ + public function createJob(array $data, $queueName = null) + { + $configInstance = $this->getConfigInstance(); + if (!$queueName) { + $queueName = $configInstance::getDiscoverQueueName(); + } elseif (strpos($queueName, $configInstance::getDiscoverQueueName()) === false) { + $queueName = $configInstance::getDiscoverQueueName() . '::' . $queueName; + } + $this->submitJob($queueName, get_class($this), array_merge($data, $this->generateConfigJobArgs())); } /** @@ -186,8 +186,7 @@ protected function getMandatoryArgs() */ protected function addSubjectToQueue(\Tripod\Mongo\ImpactedSubject $subject, $queueName) { - if(!array_key_exists($queueName, $this->subjectsGroupedByQueue)) - { + if (!array_key_exists($queueName, $this->subjectsGroupedByQueue)) { $this->subjectsGroupedByQueue[$queueName] = array(); } $this->subjectsGroupedByQueue[$queueName][] = $subject; @@ -199,12 +198,9 @@ protected function addSubjectToQueue(\Tripod\Mongo\ImpactedSubject $subject, $qu */ protected function getApplyOperation() { - if(!isset($this->applyOperation)) - { + if (!isset($this->applyOperation)) { $this->applyOperation = new ApplyOperation(); } return $this->applyOperation; } - - } diff --git a/src/mongo/jobs/EnsureIndexes.class.php b/src/mongo/jobs/EnsureIndexes.class.php index 70eb38bd..c72e134e 100644 --- a/src/mongo/jobs/EnsureIndexes.class.php +++ b/src/mongo/jobs/EnsureIndexes.class.php @@ -6,47 +6,49 @@ * Class EnsureIndexes * @package Tripod\Mongo\Jobs */ -class EnsureIndexes extends JobBase { +class EnsureIndexes extends JobBase +{ const STORENAME_KEY = 'storeName'; const REINDEX_KEY = 'reindex'; const BACKGROUND_KEY = 'background'; + protected $mandatoryArgs = [self::STORENAME_KEY, self::REINDEX_KEY, self::BACKGROUND_KEY]; + protected $configRequired = true; + /** * Runs the EnsureIndexes Job * @throws \Exception */ public function perform() { - try - { - $this->debugLog("[JOBID " . $this->job->payload['id'] . "] EnsureIndexes::perform() start"); - - $timer = new \Tripod\Timer(); - $timer->start(); - - $this->validateArgs(); + $this->debugLog('Ensuring indexes for tenant=' . $this->args[self::STORENAME_KEY]. ', reindex=' . $this->args[self::REINDEX_KEY] . ', background=' . $this->args[self::BACKGROUND_KEY]); - \Tripod\Mongo\Config::setConfig($this->args[self::TRIPOD_CONFIG_KEY]); - $this->debugLog('Ensuring indexes for tenant=' . $this->args[self::STORENAME_KEY]. ', reindex=' . $this->args[self::REINDEX_KEY] . ', background=' . $this->args[self::BACKGROUND_KEY]); + $this->getIndexUtils()->ensureIndexes( + $this->args[self::REINDEX_KEY], + $this->args[self::STORENAME_KEY], + $this->args[self::BACKGROUND_KEY] + ); + } - $this->getIndexUtils()->ensureIndexes( - $this->args[self::REINDEX_KEY], - $this->args[self::STORENAME_KEY], - $this->args[self::BACKGROUND_KEY] - ); + /** + * Stat string for successful job timer + * + * @return string + */ + protected function getStatTimerSuccessKey() + { + return MONGO_QUEUE_ENSURE_INDEXES_SUCCESS; + } - $timer->stop(); - // stat time taken to process job, from time it was picked up - $this->getStat()->timer(MONGO_QUEUE_ENSURE_INDEXES_SUCCESS,$timer->result()); - $this->debugLog("[JOBID " . $this->job->payload['id'] . "] EnsureIndexes::perform() done in {$timer->result()}ms"); - } - catch(\Exception $e) - { - $this->getStat()->increment(MONGO_QUEUE_ENSURE_INDEXES_FAIL); - $this->errorLog("Caught exception in ".get_class($this).": ".$e->getMessage()); - throw $e; - } + /** + * Stat string for failed job increment + * + * @return string + */ + protected function getStatFailureIncrementKey() + { + return MONGO_QUEUE_ENSURE_INDEXES_FAIL; } /** @@ -56,34 +58,22 @@ public function perform() * @param booelan $reindex * @param string $queueName */ - public function createJob($storeName, $reindex, $background, $queueName=null) + public function createJob($storeName, $reindex, $background, $queueName = null) { - if(!$queueName) - { - $queueName = \Tripod\Mongo\Config::getEnsureIndexesQueueName(); - } - elseif(strpos($queueName, \Tripod\Mongo\Config::getEnsureIndexesQueueName()) === false) - { - $queueName = \Tripod\Mongo\Config::getEnsureIndexesQueueName() . '::' . $queueName; + $configInstance = $this->getConfigInstance(); + if (!$queueName) { + $queueName = $configInstance::getEnsureIndexesQueueName(); + } elseif (strpos($queueName, $configInstance::getEnsureIndexesQueueName()) === false) { + $queueName = $configInstance::getEnsureIndexesQueueName() . '::' . $queueName; } - $data = array( + $data = [ self::STORENAME_KEY => $storeName, self::REINDEX_KEY => $reindex, - self::BACKGROUND_KEY => $background, - self::TRIPOD_CONFIG_KEY => \Tripod\Mongo\Config::getConfig() - ); + self::BACKGROUND_KEY => $background + ]; - $this->submitJob($queueName,get_class($this),$data); - } - - /** - * Validate args for EnsureIndexesOperation - * @return array - */ - protected function getMandatoryArgs() - { - return array(self::TRIPOD_CONFIG_KEY, self::STORENAME_KEY, self::REINDEX_KEY, self::BACKGROUND_KEY); + $this->submitJob($queueName, get_class($this), array_merge($data, $this->generateConfigJobArgs())); } /** @@ -93,5 +83,4 @@ protected function getIndexUtils() { return new \Tripod\Mongo\IndexUtils(); } - } diff --git a/src/mongo/providers/MongoSearchProvider.class.php b/src/mongo/providers/MongoSearchProvider.class.php index d4ffef47..040ad700 100644 --- a/src/mongo/providers/MongoSearchProvider.class.php +++ b/src/mongo/providers/MongoSearchProvider.class.php @@ -2,10 +2,7 @@ namespace Tripod\Mongo; -require_once TRIPOD_DIR.'mongo/MongoTripodConstants.php'; -require_once TRIPOD_DIR . 'mongo/delegates/SearchDocuments.class.php'; -require_once TRIPOD_DIR . 'mongo/providers/ISearchProvider.php'; -require_once TRIPOD_DIR.'classes/Timer.class.php'; +require_once TRIPOD_DIR . 'mongo/MongoTripodConstants.php'; use \MongoDB\BSON\Regex; @@ -45,7 +42,7 @@ public function __construct(Driver $tripod) $this->tripod = $tripod; $this->storeName = $tripod->getStoreName(); $this->labeller = new Labeller(); - $this->config = Config::getInstance(); + $this->config = \Tripod\Config::getInstance(); } /** @@ -369,7 +366,7 @@ public function deleteSearchDocumentsByTypeId($typeId, $timestamp = null) */ protected function getSearchDocumentSpecification($typeId) { - return Config::getInstance()->getSearchDocumentSpecification($this->storeName, $typeId); + return $this->config->getSearchDocumentSpecification($this->storeName, $typeId); } /** diff --git a/src/mongo/util/IndexUtils.class.php b/src/mongo/util/IndexUtils.class.php index 8e9999f2..f6b8a644 100644 --- a/src/mongo/util/IndexUtils.class.php +++ b/src/mongo/util/IndexUtils.class.php @@ -2,8 +2,6 @@ namespace Tripod\Mongo; -require_once(TRIPOD_DIR . "mongo/Config.class.php"); - /** * Class IndexUtils * @package Tripod\Mongo @@ -164,6 +162,6 @@ public function ensureIndexes($reindex=false,$storeName=null,$background=true) */ protected function getConfig() { - return Config::getInstance(); + return \Tripod\Config::getInstance(); } } diff --git a/src/mongo/util/TriplesUtil.class.php b/src/mongo/util/TriplesUtil.class.php index b2d5e854..76f6a66b 100644 --- a/src/mongo/util/TriplesUtil.class.php +++ b/src/mongo/util/TriplesUtil.class.php @@ -12,8 +12,6 @@ * Class to help working with triples and Tripod */ -require_once(TRIPOD_DIR.'mongo/MongoGraph.class.php'); - /** * Class TriplesUtil * @package Tripod\Mongo @@ -95,15 +93,12 @@ private function is_object_literal($input) */ public function loadTriplesAbout($subject,Array $triples,$storeName,$podName,$context=null,$allowableTypes=null) { - $context = ($context==null) ? Config::getInstance()->getDefaultContextAlias() : $this->labeller->uri_to_alias($context); - if (array_key_exists($podName,$this->collections)) - { + $context = ($context==null) ? \Tripod\Config::getInstance()->getDefaultContextAlias() : $this->labeller->uri_to_alias($context); + if (array_key_exists($podName, $this->collections)) { $collection = $this->collections[$podName]; - } - else - { + } else { $m = new Client( - Config::getInstance()->getConnStr($storeName), + \Tripod\Config::getInstance()->getConnStr($storeName), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] ); @@ -162,7 +157,7 @@ public function loadTriplesAbout($subject,Array $triples,$storeName,$podName,$co */ public function bsonizeTriplesAbout($subject,Array $triples,$context=null) { - $context = ($context==null) ? Config::getInstance()->getDefaultContextAlias() : $this->labeller->uri_to_alias($context); + $context = ($context==null) ? \Tripod\Config::getInstance()->getDefaultContextAlias() : $this->labeller->uri_to_alias($context); $graph = new MongoGraph(); foreach ($triples as $triple) { diff --git a/src/tripod.inc.php b/src/tripod.inc.php index 6326c486..1a2bc66f 100644 --- a/src/tripod.inc.php +++ b/src/tripod.inc.php @@ -2,47 +2,11 @@ //todo: this file is mis-named. It has mongo specifics -if(!defined('TRIPOD_DIR')) define('TRIPOD_DIR', dirname(__FILE__) . '/' ); - -require_once TRIPOD_DIR.'classes/Timer.class.php'; -require_once TRIPOD_DIR.'ITripodStat.php'; -require_once TRIPOD_DIR . 'TripodStatFactory.class.php'; -require_once TRIPOD_DIR.'classes/StatsD.class.php'; -require_once TRIPOD_DIR . 'exceptions/Exception.class.php'; -require_once TRIPOD_DIR . 'exceptions/SearchException.class.php'; -require_once TRIPOD_DIR . 'exceptions/CardinalityException.class.php'; -require_once TRIPOD_DIR . 'exceptions/ConfigException.class.php'; -require_once TRIPOD_DIR . 'exceptions/LabellerException.class.php'; -require_once TRIPOD_DIR . 'exceptions/ViewException.class.php'; -require_once TRIPOD_DIR . 'exceptions/JobException.class.php'; -require_once TRIPOD_DIR.'mongo/MongoTripodConstants.php'; -require_once TRIPOD_DIR.'mongo/MongoGraph.class.php'; -require_once TRIPOD_DIR.'mongo/ImpactedSubject.class.php'; -require_once TRIPOD_DIR . 'mongo/base/DriverBase.class.php'; -require_once TRIPOD_DIR.'mongo/IComposite.php'; -require_once TRIPOD_DIR.'mongo/base/CompositeBase.class.php'; -require_once TRIPOD_DIR . 'mongo/delegates/TransactionLog.class.php'; -require_once TRIPOD_DIR . 'mongo/delegates/Updates.class.php'; -require_once TRIPOD_DIR . 'mongo/delegates/Views.class.php'; -require_once TRIPOD_DIR . 'mongo/delegates/Tables.class.php'; -require_once TRIPOD_DIR . 'mongo/delegates/SearchIndexer.class.php'; -require_once TRIPOD_DIR . 'IEventHook.php'; -require_once TRIPOD_DIR . 'IDriver.php'; -require_once TRIPOD_DIR.'classes/ChangeSet.class.php'; -require_once TRIPOD_DIR.'classes/Labeller.class.php'; -require_once TRIPOD_DIR . '/mongo/Driver.class.php'; -require_once TRIPOD_DIR . '/mongo/JobGroup.php'; - -require_once TRIPOD_DIR.'/mongo/base/JobBase.class.php'; -require_once TRIPOD_DIR . '/mongo/jobs/DiscoverImpactedSubjects.class.php'; -require_once TRIPOD_DIR.'/mongo/jobs/ApplyOperation.class.php'; -require_once TRIPOD_DIR.'/mongo/jobs/EnsureIndexes.class.php'; - -require_once TRIPOD_DIR . '/mongo/util/IndexUtils.class.php'; -require_once TRIPOD_DIR . '/mongo/util/TriplesUtil.class.php'; -require_once TRIPOD_DIR . '/mongo/util/DateUtil.class.php'; - -require_once TRIPOD_DIR . '/mongo/serializers/NQuadSerializer.class.php'; +if (!defined('TRIPOD_DIR')) { + define('TRIPOD_DIR', dirname(__FILE__) . '/' ); +} + +require_once TRIPOD_DIR . '/mongo/MongoTripodConstants.php'; \Resque::setBackend(\Tripod\Mongo\Config::getResqueServer()); diff --git a/test/performance/mongo/MongoTripodConfigTest.php b/test/performance/mongo/MongoTripodConfigTest.php index 1ea9d244..949d3d6b 100644 --- a/test/performance/mongo/MongoTripodConfigTest.php +++ b/test/performance/mongo/MongoTripodConfigTest.php @@ -56,8 +56,8 @@ public function testCreateMongoTripodConfigObject() //Let's try to create 1000 objects to see how much time they take. for($i =0; $i < self::BENCHMARK_OBJECT_CREATE_ITERATIONS; $i++) { - \Tripod\Mongo\Config::setConfig($this->config); - $instance = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($this->config); + $instance = \Tripod\Config::getInstance(); } $testEndTime = microtime(); diff --git a/test/performance/mongo/MongoTripodPerformanceTestBase.php b/test/performance/mongo/MongoTripodPerformanceTestBase.php index ac87869e..159b7436 100644 --- a/test/performance/mongo/MongoTripodPerformanceTestBase.php +++ b/test/performance/mongo/MongoTripodPerformanceTestBase.php @@ -1,11 +1,6 @@ get('/views/:viewId/:encodedFqUri', function($storeName, $viewSpecId, $encodedFqUri) use ($app, $tripodOptions) { - \Tripod\Mongo\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-'.$storeName .'.json'), true)); + \Tripod\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-'.$storeName .'.json'), true)); $i = 0; do { - $viewSpec = \Tripod\Mongo\Config::getInstance()->getViewSpecification($storeName, $viewSpecId); + $viewSpec = \Tripod\Config::getInstance()->getViewSpecification($storeName, $viewSpecId); $podName = isset($viewSpec['from']) ? $viewSpec['from'] : null; $tripodOptions['statsConfig'] = getStat($app, $tripodOptions); $tripod = new \Tripod\Mongo\Driver($podName, $storeName, $tripodOptions); @@ -71,7 +71,7 @@ { $app->group('/graph', function() use ($app, $tripodOptions) { $app->get('/:encodedFqUri', function($storeName, $podName, $encodedFqUri) use ($app, $tripodOptions) { - \Tripod\Mongo\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-'.$storeName .'.json'), true)); + \Tripod\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-'.$storeName .'.json'), true)); $tripodOptions['statsConfig'] = getStat($app, $tripodOptions); $contentType = $app->request()->getMediaType(); @@ -129,7 +129,7 @@ $app->group('/change', function() use ($app, $tripodOptions) { $app->post('/', function($storeName, $podName) use ($app, $tripodOptions) { - \Tripod\Mongo\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-'.$storeName .'.json'), true)); + \Tripod\Config::setConfig(json_decode(file_get_contents('./config/tripod-config-'.$storeName .'.json'), true)); $app->response()->setStatus(500); $tripodOptions['statsConfig'] = getStat($app, $tripodOptions); $tripod = new \Tripod\Mongo\Driver($podName, $storeName, $tripodOptions); diff --git a/test/unit/mongo/ApplyOperationTest.php b/test/unit/mongo/ApplyOperationTest.php index a5741a8b..b8796683 100644 --- a/test/unit/mongo/ApplyOperationTest.php +++ b/test/unit/mongo/ApplyOperationTest.php @@ -2,12 +2,12 @@ use Tripod\Mongo\Jobs\ApplyOperation; -require_once 'MongoTripodTestBase.php'; +require_once 'ResqueJobTestBase.php'; /** * Class ApplyOperationTest */ -class ApplyOperationTest extends MongoTripodTestBase +class ApplyOperationTest extends ResqueJobTestBase { protected $args = array(); @@ -18,8 +18,11 @@ public function testMandatoryArgTripodConfig() $job = new \Tripod\Mongo\Jobs\ApplyOperation(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); - $this->setExpectedException('Exception', "Argument tripodConfig was not present in supplied job args for job Tripod\Mongo\Jobs\ApplyOperation"); - $job->perform(); + $this->setExpectedException( + 'Exception', + 'Argument tripodConfig or tripodConfigGenerator was not present in supplied job args for job Tripod\Mongo\Jobs\ApplyOperation' + ); + $this->performJob($job); } public function testMandatoryArgSubject() @@ -30,7 +33,7 @@ public function testMandatoryArgSubject() $job->args = $this->args; $job->job->payload['id'] = uniqid(); $this->setExpectedException('Exception', "Argument subjects was not present in supplied job args for job Tripod\Mongo\Jobs\ApplyOperation"); - $job->perform(); + $this->performJob($job); } public function testApplyViewOperation() @@ -73,7 +76,7 @@ public function testApplyViewOperation() ->setMethods(array('update')) ->setConstructorArgs(array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisapire.com/' ))->getMock(); @@ -108,7 +111,7 @@ public function testApplyViewOperation() ->method('update') ->with($subject); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplyViewOperationDecrementsJobGroupForBatchOperations() @@ -164,7 +167,7 @@ public function testApplyViewOperationDecrementsJobGroupForBatchOperations() ->setConstructorArgs( [ 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisapire.com/' ] )->getMock(); @@ -209,7 +212,7 @@ public function testApplyViewOperationDecrementsJobGroupForBatchOperations() ->with($subject); $views->expects($this->never())->method('deleteViewsByViewId'); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplyViewOperationCleanupIfAllGroupJobsComplete() @@ -266,7 +269,7 @@ public function testApplyViewOperationCleanupIfAllGroupJobsComplete() ->setConstructorArgs( [ 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisapire.com/' ] )->getMock(); @@ -317,7 +320,7 @@ public function testApplyViewOperationCleanupIfAllGroupJobsComplete() ->with('v_foo_bar', $timestamp) ->will($this->returnValue(3)); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplyTableOperation() @@ -372,7 +375,7 @@ public function testApplyTableOperation() ->setMethods(array('update')) ->setConstructorArgs(array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisapire.com/' ))->getMock(); @@ -408,7 +411,7 @@ public function testApplyTableOperation() ->method('update') ->with($subject); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplyTableOperationDecrementsJobGroupForBatchOperations() @@ -465,7 +468,7 @@ public function testApplyTableOperationDecrementsJobGroupForBatchOperations() ->setConstructorArgs( [ 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisapire.com/' ] )->getMock(); @@ -512,7 +515,7 @@ public function testApplyTableOperationDecrementsJobGroupForBatchOperations() $tables->expects($this->never()) ->method('deleteTableRowsByTableId'); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplyTableOperationCleanupIfAllGroupJobsComplete() @@ -570,7 +573,7 @@ public function testApplyTableOperationCleanupIfAllGroupJobsComplete() ->setConstructorArgs( [ 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisapire.com/' ] )->getMock(); @@ -621,7 +624,7 @@ public function testApplyTableOperationCleanupIfAllGroupJobsComplete() ->with('t_resource', $timestamp) ->will($this->returnValue(4)); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplySearchOperation() @@ -697,7 +700,7 @@ public function testApplySearchOperation() ->method('update') ->with($subject); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplySearchOperationDecrementsJobGroupForBatchOperations() @@ -794,7 +797,7 @@ public function testApplySearchOperationDecrementsJobGroupForBatchOperations() ->method('update') ->with($subject); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testApplySearchOperationCleanupIfAllGroupJobsComplete() @@ -910,7 +913,7 @@ public function testApplySearchOperationCleanupIfAllGroupJobsComplete() ->with('i_search_resource', $timestamp) ->will($this->returnValue(8)); - $applyOperation->perform(); + $this->performJob($applyOperation); } public function testCreateJobDefaultQueue() @@ -925,7 +928,7 @@ public function testCreateJobDefaultQueue() $jobData = array( 'subjects'=>array($impactedSubject->toArray()), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), + 'tripodConfig'=>\Tripod\Config::getConfig(), ); /** @var \Tripod\Mongo\Jobs\ApplyOperation|PHPUnit_Framework_MockObject_MockObject $applyOperation */ @@ -1029,7 +1032,7 @@ public function testCreateJobSpecifyQueue() $jobData = array( 'subjects'=>array($impactedSubject->toArray()), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), + 'tripodConfig'=>\Tripod\Config::getConfig(), ); /** @var \Tripod\Mongo\Jobs\ApplyOperation|PHPUnit_Framework_MockObject_MockObject $applyOperation */ @@ -1068,7 +1071,7 @@ protected function setArgs($operation = OP_VIEWS, array $specTypes = []) ); $this->args = array( - 'tripodConfig' => \Tripod\Mongo\Config::getConfig(), + 'tripodConfig' => \Tripod\Config::getConfig(), 'subjects'=> [$subject->toArray()], 'statsConfig' => $this->getStatsDConfig() ); diff --git a/test/unit/mongo/ConfigGeneratorTest.php b/test/unit/mongo/ConfigGeneratorTest.php new file mode 100644 index 00000000..64bda4f8 --- /dev/null +++ b/test/unit/mongo/ConfigGeneratorTest.php @@ -0,0 +1,182 @@ +config = [ + 'class' => 'TestConfigGenerator', + 'filename' => dirname(__FILE__) . '/data/config.json' + ]; + \Tripod\Config::setConfig($this->config); + } + public function testCreateFromConfig() + { + /** @var TestConfigGenerator $instance */ + $instance = \Tripod\Config::getInstance(); + $this->assertInstanceOf('TestConfigGenerator', $instance); + $this->assertInstanceOf('\Tripod\Mongo\Config', $instance); + $this->assertInstanceOf('\Tripod\ITripodConfigSerializer', $instance); + $this->assertEquals( + ['CBD_testing', 'CBD_testing_2'], + $instance->getPods('tripod_php_testing') + ); + } + + public function testSerializeConfig() + { + /** @var TestConfigGenerator $instance */ + $instance = \Tripod\Config::getInstance(); + $this->assertEquals($this->config, $instance->serialize()); + } + + public function testConfigGeneratorsSerializedInDiscoverJobs() + { + $originalGraph = new \Tripod\ExtendedGraph(); + $originalGraph->add_resource_triple('http://example.com/1', RDF_TYPE, RDFS_CLASS); + + $newGraph = new \Tripod\ExtendedGraph(); + $newGraph->add_resource_triple('http://example.com/1', RDF_TYPE, OWL_CLASS); + $subjectsAndPredicatesOfChange = ['http://example.com/1' => [RDF_TYPE]]; + + /** @var \Tripod\Mongo\Driver|PHPUnit_Framework_MockObject_MockObject $tripod */ + $tripod = $this->getMockBuilder('\Tripod\Mongo\Driver') + ->setMethods(['getDataUpdater']) + ->setConstructorArgs( + ['CBD_testing', 'tripod_php_testing'] + ) + ->getMock(); + + /** @var \Tripod\Mongo\Updates|PHPUnit_Framework_MockObject_MockObject $updates */ + $updates = $this->getMockBuilder('\Tripod\Mongo\Updates') + ->setMethods( + [ + 'applyHooks', + 'storeChanges', + 'setReadPreferenceToPrimary', + 'processSyncOperations', + 'getDiscoverImpactedSubjects', + 'resetOriginalReadPreference' + ] + ) + ->setConstructorArgs([$tripod]) + ->getMock(); + + /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverJob */ + $discoverJob = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') + ->setMethods(['createJob']) + ->getMock(); + + $tripod->expects($this->once())->method('getDataUpdater')->will($this->returnValue($updates)); + $updates->expects($this->once())->method('getDiscoverImpactedSubjects')->will($this->returnValue($discoverJob)); + + $updates->expects($this->once())->method('storeChanges')->will( + $this->returnValue( + ['transaction_id' => uniqid(), 'subjectsAndPredicatesOfChange' => $subjectsAndPredicatesOfChange] + ) + ); + + $discoverJob->expects($this->once())->method('createJob') + ->with([ + 'changes' => $subjectsAndPredicatesOfChange, + 'operations' => [OP_TABLES, OP_SEARCH], + 'storeName' => 'tripod_php_testing', + 'podName' => 'CBD_testing', + 'contextAlias' => 'http://talisaspire.com/', + 'statsConfig' => [] + ]); + + $tripod->saveChanges( + $originalGraph, + $newGraph + ); + } + + public function testSerializedConfigGeneratorsSentToApplyJobs() + { + $subjectsAndPredicatesOfChange = ['http://example.com/1' => [RDF_TYPE]]; + $impactedSubjects = [ + new ImpactedSubject( + [_ID_RESOURCE => 'http://example.com/1', _ID_CONTEXT => 'http://talisaspire.com/'], + OP_VIEWS, + 'tripod_php_testing', + 'CBD_testing', + ['v_resource_full'] + ) + ]; + $jobArgs = [ + DiscoverImpactedSubjects::STORE_NAME_KEY => 'tripod_php_testing', + DiscoverImpactedSubjects::POD_NAME_KEY => 'CBD_testing', + DiscoverImpactedSubjects::CHANGES_KEY => $subjectsAndPredicatesOfChange, + DiscoverImpactedSubjects::OPERATIONS_KEY => [OP_VIEWS], + DiscoverImpactedSubjects::CONTEXT_ALIAS_KEY => 'http://talisaspire.com/', + JobBase::TRIPOD_CONFIG_GENERATOR => $this->config + ]; + + /** @var \Tripod\Mongo\Driver|PHPUnit_Framework_MockObject_MockObject $tripod */ + $tripod = $this->getMockBuilder('\Tripod\Mongo\Driver') + ->setMethods([]) + ->setConstructorArgs(['CBD_testing', 'tripod_php_testing']) + ->getMock(); + + /** @var \Tripod\Mongo\Composites\Views|PHPUnit_Framework_MockObject_MockObject $views */ + $views = $this->getMockBuilder('\Tripod\Mongo\Composites\Views') + ->setMethods(['getImpactedSubjects']) + ->disableOriginalConstructor() + ->getMock(); + + $tripod->expects($this->once())->method('getComposite') + ->with(OP_VIEWS) + ->will($this->returnValue($views)); + + $views->expects($this->once())->method('getImpactedSubjects')->will($this->returnValue($impactedSubjects)); + + /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverJob */ + $discoverJob = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') + ->setMethods(['getTripod', 'getApplyOperation']) + ->getMock(); + + /** @var \Tripod\Mongo\Jobs\ApplyOperation|PHPUnit_Framework_MockObject_MockObject $applyJob */ + $applyJob = $this->getMockBuilder('\Tripod\Mongo\Jobs\ApplyOperation') + ->setMethods(['submitJob']) + ->setMockClassName('ApplyOperation_TestConfigGenerator') + ->getMock(); + $discoverJob->args = $jobArgs; + $discoverJob->job = (object) ['payload' => ['id' => uniqid()]]; + $discoverJob->expects($this->once())->method('getTripod')->will($this->returnValue($tripod)); + $discoverJob->expects($this->once())->method('getApplyOperation')->will($this->returnValue($applyJob)); + $configInstance = \Tripod\Config::getInstance(); + $applyJob->expects($this->once())->method('submitJob') + ->with( + $configInstance::getApplyQueueName(), + 'ApplyOperation_TestConfigGenerator', + [ + ApplyOperation::SUBJECTS_KEY => [ + [ + 'resourceId' => [ + _ID_RESOURCE => 'http://example.com/1', + _ID_CONTEXT => 'http://talisaspire.com/' + ], + 'operation' => OP_VIEWS, + 'specTypes' => ['v_resource_full'], + 'storeName' => 'tripod_php_testing', + 'podName' => 'CBD_testing' + ] + ], + JobBase::TRIPOD_CONFIG_GENERATOR => $this->config + ] + ); + $discoverJob->setUp(); + $discoverJob->perform(); + } +} diff --git a/test/unit/mongo/DateUtilTest.php b/test/unit/mongo/DateUtilTest.php index 039aa728..27cbd5ee 100644 --- a/test/unit/mongo/DateUtilTest.php +++ b/test/unit/mongo/DateUtilTest.php @@ -7,7 +7,7 @@ class DateUtilTest extends MongoTripodTestBase { public function testGetMongoDateWithNoParam() { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $updatedAt = (new \Tripod\Mongo\DateUtil())->getMongoDate(); $_id = array( @@ -32,7 +32,7 @@ public function testGetMongoDateWithNoParam() } public function testGetMongoDateWithParam() { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $updatedAt = (new \Tripod\Mongo\DateUtil())->getMongoDate(); $_id = array( diff --git a/test/unit/mongo/DiscoverImpactedSubjectsTest.php b/test/unit/mongo/DiscoverImpactedSubjectsTest.php index 27700007..ddeafe2b 100644 --- a/test/unit/mongo/DiscoverImpactedSubjectsTest.php +++ b/test/unit/mongo/DiscoverImpactedSubjectsTest.php @@ -1,76 +1,82 @@ setArgs(); unset($this->args['tripodConfig']); - $job = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); + $job = new DiscoverImpactedSubjects(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); - $this->setExpectedException('Exception', "Argument tripodConfig was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects"); - $job->perform(); + $this->setExpectedException( + 'Exception', + 'Argument tripodConfig or tripodConfigGenerator was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects' + ); + $this->performJob($job); } public function testMandatoryArgStoreName() { $this->setArgs(); unset($this->args['storeName']); - $job = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); + $job = new DiscoverImpactedSubjects(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); $this->setExpectedException('Exception', "Argument storeName was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects"); - $job->perform(); + $this->performJob($job); } public function testMandatoryArgPodName() { $this->setArgs(); unset($this->args['podName']); - $job = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); + $job = new DiscoverImpactedSubjects(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); $this->setExpectedException('Exception', "Argument podName was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects"); - $job->perform(); + $this->performJob($job); } public function testMandatoryArgChanges() { $this->setArgs(); unset($this->args['changes']); - $job = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); + $job = new DiscoverImpactedSubjects(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); $this->setExpectedException('Exception', "Argument changes was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects"); - $job->perform(); + $this->performJob($job); } public function testMandatoryArgOperations() { $this->setArgs(); unset($this->args['operations']); - $job = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); + $job = new DiscoverImpactedSubjects(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); $this->setExpectedException('Exception', "Argument operations was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects"); - $job->perform(); + $this->performJob($job); } public function testMandatoryArgContextAlias() { $this->setArgs(); unset($this->args['contextAlias']); - $job = new \Tripod\Mongo\Jobs\DiscoverImpactedSubjects(); + $job = new DiscoverImpactedSubjects(); $job->args = $this->args; $job->job->payload['id'] = uniqid(); $this->setExpectedException('Exception', "Argument contextAlias was not present in supplied job args for job Tripod\Mongo\Jobs\DiscoverImpactedSubjects"); - $job->perform(); + $this->performJob($job); } public function testSubmitApplyOperationsJob() @@ -89,7 +95,7 @@ public function testSubmitApplyOperationsJob() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/' ) )->getMock(); @@ -99,7 +105,7 @@ public function testSubmitApplyOperationsJob() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/' ) )->getMock(); @@ -119,6 +125,7 @@ public function testSubmitApplyOperationsJob() ) )); + /** @var DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ $discoverImpactedSubjects = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') ->setMethods(array('getTripod', 'getApplyOperation', 'getStat')) ->getMock(); @@ -230,7 +237,7 @@ public function testSubmitApplyOperationsJob() ->method('increment') ->with(MONGO_QUEUE_DISCOVER_JOB . '.' . SUBJECT_COUNT, 3); - $discoverImpactedSubjects->perform(); + $this->performJob($discoverImpactedSubjects); } public function testCreateJobDefaultQueue() @@ -245,13 +252,13 @@ public function testCreateJobDefaultQueue() $jobData = array( 'changes'=>$subjectsAndPredicatesOfChange, 'operations'=>array(OP_SEARCH), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), + 'tripodConfig'=>\Tripod\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'contextAlias'=>'http://talisaspire.com/' ); - /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ + /** @var DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ $discoverImpactedSubjects = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') ->setMethods(array('submitJob')) ->setMockClassName('MockDiscoverImpactedSubjects') @@ -280,7 +287,7 @@ public function testCreateJobSpecifyQueue() $jobData = array( 'changes'=>$subjectsAndPredicatesOfChange, 'operations'=>array(OP_SEARCH), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), + 'tripodConfig'=>\Tripod\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'contextAlias'=>'http://talisaspire.com/' @@ -288,7 +295,7 @@ public function testCreateJobSpecifyQueue() $queueName = \Tripod\Mongo\Config::getDiscoverQueueName() . '::TRIPOD_TESTING_QUEUE_' . uniqid(); - /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ + /** @var DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ $discoverImpactedSubjects = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') ->setMethods(array('submitJob')) ->setMockClassName('MockDiscoverImpactedSubjects') @@ -307,7 +314,7 @@ public function testCreateJobSpecifyQueue() public function testManualQueueNamePersistsThroughJob() { - /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ + /** @var DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ $discoverImpactedSubjects = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') ->setMethods(array('getTripod', 'getApplyOperation')) ->getMock(); @@ -322,7 +329,7 @@ public function testManualQueueNamePersistsThroughJob() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/' ) )->getMock(); @@ -332,7 +339,7 @@ public function testManualQueueNamePersistsThroughJob() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/' ) )->getMock(); @@ -423,12 +430,12 @@ public function testManualQueueNamePersistsThroughJob() ) ); - $discoverImpactedSubjects->perform(); + $this->performJob($discoverImpactedSubjects); } public function testDiscoverOperationWillSubmitApplyOperationForDistinctQueues() { - $config = \Tripod\Mongo\Config::getConfig(); + $config = \Tripod\Config::getConfig(); // Create a bunch of specs on various queues $tableSpecs = array( @@ -508,9 +515,9 @@ public function testDiscoverOperationWillSubmitApplyOperationForDistinctQueues() $config['stores']['tripod_php_testing']['table_specifications'] = $tableSpecs; - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); - /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ + /** @var DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ $discoverImpactedSubjects = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') ->setMethods(array('getTripod', 'getApplyOperation')) ->getMock(); @@ -531,7 +538,7 @@ public function testDiscoverOperationWillSubmitApplyOperationForDistinctQueues() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/' ) )->getMock(); @@ -657,12 +664,12 @@ public function testDiscoverOperationWillSubmitApplyOperationForDistinctQueues() ) ); - $discoverImpactedSubjects->perform(); + $this->performJob($discoverImpactedSubjects); } public function testManuallySpecifiedQueueWillOverrideQueuesDefinedInConfig() { - $config = \Tripod\Mongo\Config::getConfig(); + $config = \Tripod\Config::getConfig(); // Create a bunch of specs on various queues $tableSpecs = array( @@ -742,9 +749,9 @@ public function testManuallySpecifiedQueueWillOverrideQueuesDefinedInConfig() $config['stores']['tripod_php_testing']['table_specifications'] = $tableSpecs; - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); - /** @var \Tripod\Mongo\Jobs\DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ + /** @var DiscoverImpactedSubjects|PHPUnit_Framework_MockObject_MockObject $discoverImpactedSubjects */ $discoverImpactedSubjects = $this->getMockBuilder('\Tripod\Mongo\Jobs\DiscoverImpactedSubjects') ->setMethods(array('getTripod', 'getApplyOperation')) ->getMock(); @@ -766,7 +773,7 @@ public function testManuallySpecifiedQueueWillOverrideQueuesDefinedInConfig() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/' ) )->getMock(); @@ -825,14 +832,13 @@ public function testManuallySpecifiedQueueWillOverrideQueuesDefinedInConfig() ) ); - - $discoverImpactedSubjects->perform(); + $this->performJob($discoverImpactedSubjects); } protected function setArgs() { $this->args = array( - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), + 'tripodConfig'=>\Tripod\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'changes'=>array('http://example.com/resources/foo'=>array('rdf:type','dct:title')), diff --git a/test/unit/mongo/EnsureIndexesTest.php b/test/unit/mongo/EnsureIndexesTest.php index d9ed2613..a8e36d98 100644 --- a/test/unit/mongo/EnsureIndexesTest.php +++ b/test/unit/mongo/EnsureIndexesTest.php @@ -1,11 +1,11 @@ args = $this->args; $job->job->payload['id'] = uniqid(); - unset($job->args[$argumentName]); + unset($job->args[$argument]); - $this->setExpectedException('Exception', "Argument $argumentName was not present in supplied job args for job Tripod\Mongo\Jobs\EnsureIndexes"); - $job->perform(); + $this->setExpectedException( + 'Exception', + "Argument $argumentName was not present in supplied job args for job Tripod\Mongo\Jobs\EnsureIndexes" + ); + $this->performJob($job); } /** @@ -48,12 +54,12 @@ public function testMandatoryArgs($argumentName) */ public function mandatoryArgDataProvider() { - return array( - array('tripodConfig'), - array('storeName'), - array('reindex'), - array('background') - ); + return [ + ['tripodConfig', 'tripodConfig or tripodConfigGenerator'], + ['storeName'], + ['reindex'], + ['background'] + ]; } /** @@ -66,7 +72,7 @@ public function testSuccessfullyEnsureIndexesJob() $job->args = $this->createDefaultArguments(); $this->jobSuccessfullyEnsuresIndexes($job); - $job->perform(); + $this->performJob($job); } /** @@ -80,7 +86,7 @@ public function testEnsureIndexesJobThrowsErrorWhenCreatingIndexes() $this->jobThrowsExceptionWhenEnsuringIndexes($job); $this->setExpectedException('Exception', "Ensuring index failed"); - $job->perform(); + $this->performJob($job); } /** @@ -91,7 +97,7 @@ public function testEnsureIndexesCreateJobDefaultQueue() { $jobData = array( 'storeName' => 'tripod_php_testing', - 'tripodConfig' => \Tripod\Mongo\Config::getConfig(), + 'tripodConfig' => \Tripod\Config::getConfig(), 'reindex' => false, 'background' => true ); @@ -117,7 +123,7 @@ public function testEnsureIndexesCreateJobUnreachableRedis() { $jobData = array( 'storeName' => 'tripod_php_testing', - 'tripodConfig' => \Tripod\Mongo\Config::getConfig(), + 'tripodConfig' => \Tripod\Config::getConfig(), 'reindex' => false, 'background' => true ); @@ -149,7 +155,7 @@ public function testEnsureIndexesCreateJobStatusFalse() { $jobData = array( 'storeName' => 'tripod_php_testing', - 'tripodConfig' => \Tripod\Mongo\Config::getConfig(), + 'tripodConfig' => \Tripod\Config::getConfig(), 'reindex' => false, 'background' => true ); @@ -177,7 +183,7 @@ public function testEnsureIndexesCreateJobSpecifyQueue() { $jobData = array( 'storeName' => 'tripod_php_testing', - 'tripodConfig' => \Tripod\Mongo\Config::getConfig(), + 'tripodConfig' => \Tripod\Config::getConfig(), 'reindex' => false, 'background' => true ); @@ -229,7 +235,7 @@ protected function createMockJob($methods=array()) protected function createDefaultArguments() { $arguments = array( - 'tripodConfig' => \Tripod\Mongo\Config::getConfig(), + 'tripodConfig' => \Tripod\Config::getConfig(), 'storeName' => 'tripod_php_testing', 'reindex' => false, 'background' => true diff --git a/test/unit/mongo/MongoGraphTest.php b/test/unit/mongo/MongoGraphTest.php index 1f0f484f..9a8bd389 100644 --- a/test/unit/mongo/MongoGraphTest.php +++ b/test/unit/mongo/MongoGraphTest.php @@ -46,7 +46,7 @@ public function testToNQuads() $expected = " \"some literal title\" . .\n"; - $this->assertEquals($expected, $g->to_nquads(\Tripod\Mongo\Config::getInstance()->getDefaultContextAlias())); + $this->assertEquals($expected, $g->to_nquads(\Tripod\Config::getInstance()->getDefaultContextAlias())); } public function testToNQuadsTwoGraphsWithDifferentContext() diff --git a/test/unit/mongo/MongoSearchProviderTest.php b/test/unit/mongo/MongoSearchProviderTest.php index fba4959e..ad679b1b 100644 --- a/test/unit/mongo/MongoSearchProviderTest.php +++ b/test/unit/mongo/MongoSearchProviderTest.php @@ -29,7 +29,7 @@ protected function setUp() $this->loadBaseSearchDataViaTripod(); - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch($this->tripod->getStoreName()) as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForSearch($this->tripod->getStoreName()) as $collection) { $collection->drop(); } @@ -172,7 +172,7 @@ public function testSearchIndexing() { ); // loop through every expected document and assert that it exists, and that each property matches the value we defined above. - $searchCollection = \Tripod\Mongo\Config::getInstance()->getCollectionForSearchDocument($this->tripod->getStoreName(), 'i_search_resource'); + $searchCollection = \Tripod\Config::getInstance()->getCollectionForSearchDocument($this->tripod->getStoreName(), 'i_search_resource'); foreach($expectedSearchDocs as $expectedSearchDoc){ $this->assertDocumentExists($expectedSearchDoc["_id"], $searchCollection); $this->assertDocumentHasProperty($expectedSearchDoc["_id"], "result", $expectedSearchDoc["result"], $searchCollection); @@ -196,7 +196,7 @@ public function testSearchIndexingRemovesDocWhenTypeHasNoCorrespondingSearchdocS $this->assertEquals(12, $actualSearchDocumentCount, "Should only be 12 search documents now that one of them has had its type changed with no corresponding search doc spec"); - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) { $this->assertNull( $collection->findOne(array("_id.r"=>"http://talisaspire.com/resources/doc1")), @@ -227,7 +227,7 @@ public function testSearchIndexingGeneratesNewDocForChangedTypeThatHasACorrespon $this->assertEquals(13, $actualSearchDocumentCount, "Should only be 13 search documents"); $result = array(); - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) { $result = $collection->findOne(array("_id.r"=>"http://talisaspire.com/resources/doc1")); if($result) @@ -263,7 +263,7 @@ public function testSearchIndexingGeneratesTwoDocumentsForGivenResourceTheDelete $results = array(); // We don't know where exactly these might have stored - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) { foreach($collection->find(array("_id.r"=>"http://talisaspire.com/resources/doc1")) as $result) { @@ -310,7 +310,7 @@ public function testSearchIndexingGeneratesTwoDocumentsForGivenResourceTheDelete $results = array(); // We don't know where exactly these might have stored - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForSearch('tripod_php_testing') as $collection) { foreach($collection->find(array("_id.r"=>"http://talisaspire.com/resources/doc1")) as $result) { @@ -762,12 +762,12 @@ protected function getCountForSearchSpecs(\Tripod\Mongo\Driver $tripod, $specs = $count = 0; if(empty($specs)) { - $specs = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecifications($tripod->getStoreName(), null, true); + $specs = \Tripod\Config::getInstance()->getSearchDocumentSpecifications($tripod->getStoreName(), null, true); } foreach($specs as $spec) { - $count += \Tripod\Mongo\Config::getInstance()->getCollectionForSearchDocument($tripod->getStoreName(), $spec)->count(array('_id.type'=>$spec)); + $count += \Tripod\Config::getInstance()->getCollectionForSearchDocument($tripod->getStoreName(), $spec)->count(array('_id.type'=>$spec)); } return $count; } diff --git a/test/unit/mongo/MongoTransactionLogTest.php b/test/unit/mongo/MongoTransactionLogTest.php index 1e6c87ab..7b306a46 100644 --- a/test/unit/mongo/MongoTransactionLogTest.php +++ b/test/unit/mongo/MongoTransactionLogTest.php @@ -31,7 +31,7 @@ protected function setUp() $this->getTripodCollection($this->tripod)->drop(); // Lock collection no longer available from Driver, so drop it manually - \Tripod\Mongo\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName())->drop(); + \Tripod\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName())->drop(); $this->loadResourceDataViaTripod(); diff --git a/test/unit/mongo/MongoTripodComputedFieldsTest.php b/test/unit/mongo/MongoTripodComputedFieldsTest.php index 3c6d6129..0c069e9b 100644 --- a/test/unit/mongo/MongoTripodComputedFieldsTest.php +++ b/test/unit/mongo/MongoTripodComputedFieldsTest.php @@ -12,13 +12,13 @@ class MongoTripodComputedFieldsTest extends MongoTripodTestBase public function setUp() { parent::setUp(); - $this->originalConfig = \Tripod\Mongo\Config::getConfig(); + $this->originalConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); } public function tearDown() { - \Tripod\Mongo\Config::setConfig($this->originalConfig); + \Tripod\Config::setConfig($this->originalConfig); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MIN); parent::tearDown(); } @@ -60,16 +60,16 @@ public function testConditionalComputedFieldWithDates() { ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadDatesDataViaTripod(); $this->tripod->generateTableRows('t_conditional_creators'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r' => 'baseData:foo1234')); $this->assertEquals('Updated', $tableDoc['value']['status']); @@ -77,8 +77,8 @@ public function testConditionalComputedFieldWithDates() { $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r' => 'baseData:foo12345')); $this->assertEquals('Published', $tableDoc['value']['status']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } @@ -122,15 +122,15 @@ public function testConditionalComputedField() ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadResourceDataViaTripod(); $this->tripod->generateTableRows('t_conditional_creators'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r'=>'baseData:foo1234')); @@ -141,8 +141,8 @@ public function testConditionalComputedField() $this->assertEquals(1, $tableDoc['value']['creatorCount']); $this->assertEquals(2, $tableDoc['value']['contributorCount']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } @@ -189,15 +189,15 @@ public function testNestedConditionalComputedField() ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'] = array($tableSpec); - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadResourceDataViaTripod(); $this->tripod->generateTableRows('t_conditional_creators'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r'=>'baseData:foo1234')); @@ -230,8 +230,8 @@ public function testNestedConditionalComputedField() $this->assertArrayNotHasKey('contributorCount', $tableDoc['value']); $this->assertArrayNotHasKey('creatorCount', $tableDoc['value']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } @@ -274,15 +274,15 @@ public function testReplaceComputedField() ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadResourceDataViaTripod(); $this->tripod->generateTableRows('t_replace_type'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_replace_type'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_replace_type'); $tableDoc = $collection->findOne(array('_id.type'=>'t_replace_type', '_id.r'=>'baseData:foo1234')); @@ -311,8 +311,8 @@ public function testReplaceComputedField() $this->assertEquals('Book Work', $tableDoc['value']['resourceType']); $this->assertArrayNotHasKey('rdfType', $tableDoc['value']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } @@ -366,15 +366,15 @@ public function testArithmeticComputedField() ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadResourceDataViaTripod(); $this->tripod->generateTableRows('t_creator_count'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_creator_count'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_creator_count'); $tableDoc = $collection->findOne(array('_id.type'=>'t_creator_count', '_id.r'=>'baseData:foo1234')); @@ -407,8 +407,8 @@ public function testArithmeticComputedField() $this->assertArrayNotHasKey('contributorCount', $tableDoc['value']); $this->assertArrayNotHasKey('creatorCount', $tableDoc['value']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } @@ -435,20 +435,20 @@ public function testNestArithmeticInConditionalIf() ) ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadResourceDataViaTripod(); $this->tripod->generateTableRows('t_conditional_with_nested_arithmetic'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_with_nested_arithmetic'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_with_nested_arithmetic'); $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_with_nested_arithmetic')); $this->assertEquals('b', $tableDoc['value']['foobar']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } @@ -483,20 +483,20 @@ public function testNestConditionalInArithmeticFunction() ) ) ); - $oldConfig = \Tripod\Mongo\Config::getConfig(); - $newConfig = \Tripod\Mongo\Config::getConfig(); + $oldConfig = \Tripod\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; - \Tripod\Mongo\Config::setConfig($newConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($newConfig); + \Tripod\Config::getInstance(); $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->loadResourceDataViaTripod(); $this->tripod->generateTableRows('t_arithmetic_with_nested_conditional'); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_arithmetic_with_nested_conditional'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_arithmetic_with_nested_conditional'); $tableDoc = $collection->findOne(array('_id.type'=>'t_arithmetic_with_nested_conditional')); $this->assertEquals(300, $tableDoc['value']['foobar']); - \Tripod\Mongo\Config::setConfig($oldConfig); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($oldConfig); + \Tripod\Config::getInstance(); $collection->drop(); } } \ No newline at end of file diff --git a/test/unit/mongo/MongoTripodConfigTest.php b/test/unit/mongo/MongoTripodConfigTest.php index ab88b968..4477f630 100644 --- a/test/unit/mongo/MongoTripodConfigTest.php +++ b/test/unit/mongo/MongoTripodConfigTest.php @@ -16,7 +16,7 @@ class MongoTripodConfigTest extends MongoTripodTestBase protected function setUp() { parent::setup(); - $this->tripodConfig = \Tripod\Mongo\Config::getInstance(); + $this->tripodConfig = \Tripod\Config::getInstance(); } public function testGetInstanceThrowsExceptionIfSetInstanceNotCalledFirst() @@ -27,8 +27,8 @@ public function testGetInstanceThrowsExceptionIfSetInstanceNotCalledFirst() $this->setExpectedException('\Tripod\Exceptions\ConfigException','Call Config::setConfig() first'); unset($this->tripodConfig); - \Tripod\Mongo\Config::getInstance()->destroy(); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::destroy(); + \Tripod\Config::getInstance(); } public function testNamespaces() @@ -59,8 +59,8 @@ public function testNamespaces() public function testTConfig() { - $config = \Tripod\Mongo\Config::getInstance(); - $cfg = \Tripod\Mongo\Config::getConfig(); + $config = \Tripod\Config::getInstance(); + $cfg = \Tripod\Config::getConfig(); $tConfig = $config->getTransactionLogConfig(); $this->assertEquals('tripod_php_testing',$tConfig['database']); $this->assertEquals('transaction_log',$tConfig['collection']); @@ -95,8 +95,8 @@ public function testTConfigRepSetConnStr() ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); $this->assertEquals("mongodb://tloghost:27017,tloghost:27018/admin",$mtc->getTransactionLogConnStr()); } @@ -133,8 +133,8 @@ public function testTConfigRepSetConnStrThrowsException() "data_source"=>"rs1" ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); $connStr = $mtc->getTransactionLogConnStr(); } @@ -150,7 +150,7 @@ public function testCardinality() public function testGetConnectionString() { - $this->assertEquals("mongodb://localhost:27017/",\Tripod\Mongo\Config::getInstance()->getConnStr("tripod_php_testing")); + $this->assertEquals("mongodb://localhost:27017/",\Tripod\Config::getInstance()->getConnStr("tripod_php_testing")); } public function testGetConnectionStringThrowsException() @@ -158,7 +158,7 @@ public function testGetConnectionStringThrowsException() $this->setExpectedException( '\Tripod\Exceptions\ConfigException', 'Database notexists does not exist in configuration'); - $this->assertEquals("mongodb://localhost:27017/",\Tripod\Mongo\Config::getInstance()->getConnStr("notexists")); + $this->assertEquals("mongodb://localhost:27017/",\Tripod\Config::getInstance()->getConnStr("notexists")); } public function testGetConnectionStringForReplicaSet(){ @@ -181,8 +181,8 @@ public function testGetConnectionStringForReplicaSet(){ ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); $this->assertEquals("mongodb://localhost:27017,localhost:27018/admin",$mtc->getConnStr("tripod_php_testing")); } @@ -214,8 +214,8 @@ public function testGetConnectionStringThrowsExceptionForReplicaSet(){ ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); $mtc->getConnStr("tripod_php_testing"); } @@ -253,13 +253,13 @@ public function testCompoundIndexAllArraysThrowsException() ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testSearchConfig() { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $this->assertEquals('\Tripod\Mongo\MongoSearchProvider', $config->getSearchProviderClassName('tripod_php_testing')); $this->assertEquals(3, count($config->getSearchDocumentSpecifications('tripod_php_testing'))); @@ -290,8 +290,8 @@ public function testCardinalityRuleWithNoNamespace() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testGetSearchDocumentSpecificationsByType() @@ -336,7 +336,7 @@ public function testGetSearchDocumentSpecificationsByType() ) ) ); - $actualSpec = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecifications("tripod_php_testing", "resourcelist:List"); + $actualSpec = \Tripod\Config::getInstance()->getSearchDocumentSpecifications("tripod_php_testing", "resourcelist:List"); $this->assertEquals($expectedSpec,$actualSpec); } @@ -381,7 +381,7 @@ public function testGetSearchDocumentSpecificationsById() ) ) ); - $actualSpec = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecification('tripod_php_testing', "i_search_list"); + $actualSpec = \Tripod\Config::getInstance()->getSearchDocumentSpecification('tripod_php_testing', "i_search_list"); $this->assertEquals($expectedSpec,$actualSpec); } @@ -389,7 +389,7 @@ public function testGetSearchDocumentSpecificationsById() public function testGetSearchDocumentSpecificationsWhereNoneExists() { $expectedSpec = array(); - $actualSpec = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecifications("something:doesntexist"); + $actualSpec = \Tripod\Config::getInstance()->getSearchDocumentSpecifications("something:doesntexist"); $this->assertEquals($expectedSpec,$actualSpec); } @@ -430,8 +430,8 @@ public function testViewSpecCountWithoutTTLThrowsException() "joins"=>array("dct:hasVersion"=>array()) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testViewSpecCountNestedInJoinWithoutTTLThrowsException() @@ -474,8 +474,8 @@ public function testViewSpecCountNestedInJoinWithoutTTLThrowsException() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecNestedCountWithoutPropertyThrowsException() @@ -516,8 +516,8 @@ public function testTableSpecNestedCountWithoutPropertyThrowsException() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecNested2ndLevelCountWithoutFieldNameThrowsException() @@ -561,8 +561,8 @@ public function testTableSpecNested2ndLevelCountWithoutFieldNameThrowsException( ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecFieldWithoutFieldName() @@ -600,8 +600,8 @@ public function testTableSpecFieldWithoutFieldName() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecFieldWithoutPredicates() @@ -639,8 +639,8 @@ public function testTableSpecFieldWithoutPredicates() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecCountWithoutProperty() @@ -678,8 +678,8 @@ public function testTableSpecCountWithoutProperty() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecCountWithoutFieldName() @@ -717,8 +717,8 @@ public function testTableSpecCountWithoutFieldName() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testTableSpecCountWithoutPropertyAsAString() @@ -757,8 +757,8 @@ public function testTableSpecCountWithoutPropertyAsAString() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } public function testConfigWithoutDefaultNamespaceThrowsException() @@ -799,8 +799,8 @@ public function testConfigWithoutDefaultNamespaceThrowsException() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); } /** @@ -809,7 +809,7 @@ public function testConfigWithoutDefaultNamespaceThrowsException() */ public function testGetIndexesGroupedByCollection() { - $indexSpecs = \Tripod\Mongo\Config::getInstance()->getIndexesGroupedByCollection("tripod_php_testing"); + $indexSpecs = \Tripod\Config::getInstance()->getIndexesGroupedByCollection("tripod_php_testing"); $this->assertArrayHasKey("CBD_testing", $indexSpecs); $this->assertArrayHasKey("index1", $indexSpecs["CBD_testing"]); @@ -869,8 +869,8 @@ public function testGetReplicaSetName() ) ) ); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); $this->assertEquals("myreplicaset", $mtc->getReplicaSetName($mtc->getDefaultDataSourceForStore("tripod_php_testing"))); $this->assertNull($mtc->getReplicaSetName("testing_2")); @@ -900,10 +900,10 @@ public function testGetViewSpecification(){ ) ); - $vspec = \Tripod\Mongo\Config::getInstance()->getViewSpecification('tripod_php_testing', "v_resource_full"); + $vspec = \Tripod\Config::getInstance()->getViewSpecification('tripod_php_testing', "v_resource_full"); $this->assertEquals($expectedVspec, $vspec); - $vspec = \Tripod\Mongo\Config::getInstance()->getViewSpecification('tripod_php_testing', "doesnt_exist"); + $vspec = \Tripod\Config::getInstance()->getViewSpecification('tripod_php_testing', "doesnt_exist"); $this->assertNull($vspec); } @@ -937,10 +937,10 @@ public function testGetTableSpecification() ) ); - $tspec = \Tripod\Mongo\Config::getInstance()->getTableSpecification("tripod_php_testing", "t_resource"); + $tspec = \Tripod\Config::getInstance()->getTableSpecification("tripod_php_testing", "t_resource"); $this->assertEquals($expectedTspec, $tspec); - $tspec = \Tripod\Mongo\Config::getInstance()->getTableSpecification("tripod_php_testing", "doesnt_exist"); + $tspec = \Tripod\Config::getInstance()->getTableSpecification("tripod_php_testing", "doesnt_exist"); $this->assertNull($tspec); } @@ -965,8 +965,8 @@ public function testSearchConfigNotPresent() ); $config["transaction_log"] = array("database"=>"transactions","collection"=>"transaction_log","data_source"=>"mongo1"); - \Tripod\Mongo\Config::setConfig($config); - $mtc = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($config); + $mtc = \Tripod\Config::getInstance(); $this->assertNull($mtc->getSearchProviderClassName("tripod_php_testing")); $this->assertEquals(array(), $mtc->getSearchDocumentSpecifications("tripod_php_testing")); } @@ -1085,7 +1085,7 @@ public function testDataLoadedInConfiguredDataSource() $storeName = 'tripod_php_testing'; $dataSourcesForStore = array(); - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $pods = $config->getPods($storeName); foreach($pods as $pod) @@ -1127,7 +1127,7 @@ public function testDataLoadedInConfiguredDataSource() $diff = false; - $cfg = \Tripod\Mongo\Config::getConfig(); + $cfg = \Tripod\Config::getConfig(); $defaultDataSource = $cfg["data_sources"][$config->getDefaultDataSourceForStore($storeName)]; foreach($dataSourcesForStore as $source) @@ -1184,9 +1184,9 @@ public function testDataLoadedInConfiguredDataSource() $collectionsForDataSource['rs2'] = array(VIEWS_COLLECTION, SEARCH_INDEX_COLLECTION, TABLE_ROWS_COLLECTION, 'CBD_testing_2', 'transaction_log'); $specs = array(); - $specs['views'] = \Tripod\Mongo\Config::getInstance()->getViewSpecifications($storeName); - $specs['search'] = \Tripod\Mongo\Config::getInstance()->getSearchDocumentSpecifications($storeName); - $specs['table_rows'] = \Tripod\Mongo\Config::getInstance()->getTableSpecifications($storeName); + $specs['views'] = \Tripod\Config::getInstance()->getViewSpecifications($storeName); + $specs['search'] = \Tripod\Config::getInstance()->getSearchDocumentSpecifications($storeName); + $specs['table_rows'] = \Tripod\Config::getInstance()->getTableSpecifications($storeName); $specsForDataSource = array(); foreach(array('views', 'search', 'table_rows') as $type) @@ -1279,13 +1279,13 @@ public function testDataLoadedInConfiguredDataSource() public function testTransactionLogIsWrittenToCorrectDBAndCollection() { $storeName = 'tripod_php_testing'; - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); $newConfig['transaction_log']['database'] = 'tripod_php_testing_transaction_log'; $newConfig['transaction_log']['collection'] = 'transaction_log'; - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); // Clear out any old data $tlogDB = $config->getTransactionLogDatabase(); @@ -1345,355 +1345,355 @@ public function testTransactionLogIsWrittenToCorrectDBAndCollection() public function testComputedFieldSpecValidationInvalidFunction() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $computedFieldFunction = array('fieldName'=>'fooBar', 'value'=>array('shazzbot'=>array())); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($computedFieldFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed field spec does not contain valid function"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testComputedFieldSpecValidationMultipleFunctions() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $computedFieldFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array(),'replace'=>array())); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($computedFieldFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed field spec contains more than one function"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testComputedFieldSpecValidationMustBeAtBaseLevel() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $computedFieldFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array())); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['joins']['dct:isVersionOf']['computed_fields'] = array($computedFieldFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Table spec can only contain 'computed_fields' at the base level"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationEmptyConditional() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array())); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed conditional spec does not contain an 'if' value"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationMissingThenElse() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array()))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed conditional spec must contain a then or else value"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationEmptyIf() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array(),'then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed conditional field spec 'if' value array must have 1 or 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationIfNotArray() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>'foo','then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed conditional field spec 'if' value must be an array"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationIfHasTwoValues() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('foo','*'),'then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed conditional field spec 'if' value array must have 1 or 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationIfHasMoreThanThreeValues() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('a','b','c','d'),'then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed conditional field spec 'if' value array must have 1 or 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationIfHasInvalidConditionalOperator() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('a','*','c'),'then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Invalid conditional operator '*' in conditional spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationIfHasInvalidVariableAsLeftOperand() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('$wibble','>=','c'),'then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$wibble' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationIfHasInvalidVariableAsRightOperand() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('a','contains','$wibble'),'then'=>'wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$wibble' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationThenHasInvalidVariable() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('a','<','b'),'then'=>'$wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$wibble' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testConditionalSpecValidationElseHasInvalidVariable() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $conditionalFunction = array('fieldName'=>'fooBar', 'value'=>array('conditional'=>array('if'=>array('a','<','b'),'then'=>true,'else'=>'$wibble'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($conditionalFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$wibble' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationEmptyFunction() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array())); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed replace spec does not contain 'search' value"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationMissingReplace() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'x'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed replace spec does not contain 'replace' value"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationMissingSubject() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'x', 'replace'=>'y'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed replace spec does not contain 'subject' value"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationInvalidVariableInSearch() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'$x','replace'=>'y','subject'=>'z'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$x' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationInvalidVariableInSearchArray() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>array('a','b','$x'),'replace'=>'y','subject'=>'z'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$x' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationInvalidVariableInReplace() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'x','replace'=>'$y','subject'=>'z'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$y' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationInvalidVariableInReplaceArray() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'x','replace'=>array('a','$y', 'c'),'subject'=>'z'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$y' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationInvalidVariableInSubject() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'x','replace'=>'y','subject'=>'$z'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$z' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testReplaceSpecValidationInvalidVariableInSubjectArray() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $replaceFunction = array('fieldName'=>'fooBar', 'value'=>array('replace'=>array('search'=>'x','replace'=>'y','subject'=>array('$z','b','c')))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($replaceFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$z' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationEmptyFunction() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array())); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed arithmetic spec must contain 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationOneValue() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(1))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed arithmetic spec must contain 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationTwoValues() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(1,'+'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed arithmetic spec must contain 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationFourValues() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(1,'+',3,4))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed arithmetic spec must contain 3 values"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationInvalidArithmeticOperator() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(1,'x',3))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Invalid arithmetic operator 'x' in computed arithmetic spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationInvalidVariableLeftOperand() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array('$x','*',3))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$x' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationInvalidVariableRightOperand() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(1,'*','$x'))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$x' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationInvalidNestedVariable() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(array('$x','-',100),'*',3))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Computed spec variable '\$x' is not defined in table spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testArithmeticSpecValidationInvalidNestedOperator() { - $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Config::getConfig(); \Tripod\Mongo\Config::setValidationLevel(\Tripod\Mongo\Config::VALIDATE_MAX); $arithmeticFunction = array('fieldName'=>'fooBar', 'value'=>array('arithmetic'=>array(array(101,'#',100),'*',3))); $newConfig['stores']['tripod_php_testing']['table_specifications'][0]['computed_fields'] = array($arithmeticFunction); - \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Config::setConfig($newConfig); $this->setExpectedException('\Tripod\Exceptions\ConfigException', "Invalid arithmetic operator '#' in computed arithmetic spec"); - \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::getInstance(); } public function testGetResqueServer() diff --git a/test/unit/mongo/MongoTripodDriverTest.php b/test/unit/mongo/MongoTripodDriverTest.php index 618613c5..739a7ef8 100755 --- a/test/unit/mongo/MongoTripodDriverTest.php +++ b/test/unit/mongo/MongoTripodDriverTest.php @@ -44,7 +44,7 @@ protected function setUp() $this->getTripodCollection($this->tripod)->drop(); // Lock collection no longer available from Tripod, so drop it manually - \Tripod\Mongo\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName())->drop(); + \Tripod\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName())->drop(); $this->tripod->setTransactionLog($this->tripodTransactionLog); @@ -584,21 +584,21 @@ public function testSetReadPreferenceWhenSavingChanges(){ $subjectOne = "http://talisaspire.com/works/checkReadPreferencesWrite"; /** @var $tripodMock \Tripod\Mongo\Driver **/ $tripodMock = $this->getMock( - '\Tripod\Mongo\Driver', - array('getDataUpdater'), + '\Tripod\Mongo\Driver', + array('getDataUpdater'), array( 'CBD_testing', 'tripod_php_testing', array('defaultContext'=>'http://talisaspire.com/') ) ); - + $tripodUpdate = $this->getMock( '\Tripod\Mongo\Updates', - array('addToSearchIndexQueue','setReadPreferenceToPrimary','resetOriginalReadPreference'), + array('addToSearchIndexQueue','setReadPreferenceToPrimary','resetOriginalReadPreference'), array($tripodMock) ); - + $tripodUpdate ->expects($this->once(0)) ->method('setReadPreferenceToPrimary'); @@ -606,7 +606,7 @@ public function testSetReadPreferenceWhenSavingChanges(){ $tripodUpdate ->expects($this->once()) ->method('resetOriginalReadPreference'); - + $tripodMock ->expects($this->once()) ->method('getDataUpdater') @@ -624,8 +624,8 @@ public function testReadPreferencesAreRestoredWhenErrorSavingChanges(){ $subjectOne = "http://talisaspire.com/works/checkReadPreferencesAreRestoredOnError"; /** @var $tripodMock \Tripod\Mongo\Driver|PHPUnit_Framework_MockObject_MockObject **/ $tripodMock = $this->getMock( - '\Tripod\Mongo\Driver', - array('getDataUpdater'), + '\Tripod\Mongo\Driver', + array('getDataUpdater'), array( 'CBD_testing', 'tripod_php_testing', @@ -636,7 +636,7 @@ public function testReadPreferencesAreRestoredWhenErrorSavingChanges(){ /** @var $tripodUpdate \Tripod\Mongo\Updates|PHPUnit_Framework_MockObject_MockObject **/ $tripodUpdate = $this->getMock( '\Tripod\Mongo\Updates', - array('addToSearchIndexQueue','resetOriginalReadPreference','getContextAlias'), + array('addToSearchIndexQueue','resetOriginalReadPreference','getContextAlias'), array($tripodMock) ); @@ -862,7 +862,7 @@ public function testSaveChangesWithInvalidCardinality() $config['queue'] = array("database"=>"queue","collection"=>"q_queue","data_source"=>"db"); // Override the config defined in base test class as we need specific config here. - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); $tripod = new \Tripod\Mongo\Driver('CBD_testing','tripod_php_testing',array('defaultContext'=>'http://talisaspire.com/')); @@ -943,7 +943,6 @@ public function testDiscoverImpactedSubjectsAreDoneAllOperationsASync() $jobData = array( 'changes'=>$subjectsAndPredicatesOfChange, 'operations'=>array(OP_TABLES, OP_VIEWS, OP_SEARCH), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'contextAlias'=>'http://talisaspire.com/', @@ -1019,7 +1018,7 @@ public function testDiscoverImpactedSubjectsForDeletionsSyncOpsAreDoneAsyncJobSu array('getImpactedSubjects', 'update'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), 'http://talisaspire.com/' ) ); @@ -1029,7 +1028,7 @@ public function testDiscoverImpactedSubjectsForDeletionsSyncOpsAreDoneAsyncJobSu array('getImpactedSubjects', 'update'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), 'http://talisaspire.com/' ) ); @@ -1049,7 +1048,6 @@ public function testDiscoverImpactedSubjectsForDeletionsSyncOpsAreDoneAsyncJobSu $jobData = array( 'changes'=>$subjectsAndPredicatesOfChange, 'operations'=>array(OP_SEARCH), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'contextAlias'=>'http://talisaspire.com/', @@ -1234,7 +1232,7 @@ public function testDiscoverImpactedSubjectsForDefaultOperationsSetting() array('getImpactedSubjects', 'update'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), 'http://talisaspire.com/' ) ); @@ -1284,7 +1282,6 @@ public function testDiscoverImpactedSubjectsForDefaultOperationsSetting() $jobData = array( 'changes'=>$subjectsAndPredicatesOfChange, 'operations'=>array(OP_TABLES, OP_SEARCH), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'contextAlias'=>'http://talisaspire.com/', @@ -1384,7 +1381,7 @@ public function testSpecifyQueueForAsyncOperations() array('getImpactedSubjects', 'update'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing','CBD_testing'), 'http://talisaspire.com/' ) ); @@ -1434,7 +1431,6 @@ public function testSpecifyQueueForAsyncOperations() $jobData = array( 'changes'=>$subjectsAndPredicatesOfChange, 'operations'=>array(OP_TABLES, OP_SEARCH), - 'tripodConfig'=>\Tripod\Mongo\Config::getConfig(), 'storeName'=>'tripod_php_testing', 'podName'=>'CBD_testing', 'contextAlias'=>'http://talisaspire.com/', @@ -1926,7 +1922,7 @@ public function testRemoveInertLocksUnlockAllDocumentsFailsVerifyErrorEntryInAud $tripodUpdate = $this->getMock('\Tripod\Mongo\Updates', array('unlockAllDocuments', 'generateIdForNewMongoDocument', 'getMongoDate', 'getAuditManualRollbacksCollection'), array($tripod)); - + $tripodUpdate->expects($this->once()) ->method("generateIdForNewMongoDocument") ->will($this->returnValue($mongoDocumentId)); @@ -2004,7 +2000,7 @@ public function testRemoveInertLocksUnlockSuccessfulVerifyAuditLog() $tripodUpdate = $this->getMock('\Tripod\Mongo\Updates', array('unlockAllDocuments', 'generateIdForNewMongoDocument', 'getMongoDate', 'getAuditManualRollbacksCollection'), array($tripod)); - + $tripodUpdate->expects($this->once()) ->method("generateIdForNewMongoDocument") ->will($this->returnValue($mongoDocumentId)); @@ -2181,7 +2177,7 @@ public function testPassStatConfigToTripodConstructor() public function testEtagIsMicrotimeFormat() { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $updatedAt = \Tripod\Mongo\DateUtil::getMongoDate(); $_id = array( diff --git a/test/unit/mongo/MongoTripodNQuadSerializerTest.php b/test/unit/mongo/MongoTripodNQuadSerializerTest.php index 6f67daf3..7abeb3bf 100644 --- a/test/unit/mongo/MongoTripodNQuadSerializerTest.php +++ b/test/unit/mongo/MongoTripodNQuadSerializerTest.php @@ -23,7 +23,7 @@ public function testSerializerSimple() .\n"; $serializer = new NQuadSerializer(); - $actual = $serializer->getSerializedIndex($g->_index, \Tripod\Mongo\Config::getInstance()->getDefaultContextAlias()); + $actual = $serializer->getSerializedIndex($g->_index, \Tripod\Config::getInstance()->getDefaultContextAlias()); $this->assertEquals($expected, $actual); } @@ -166,7 +166,7 @@ public function testSerializerWithMultipleSubjects() \"Doc with sequence\" ."; $serializer = new NQuadSerializer(); - $actual = $serializer->getSerializedIndex($g->_index, \Tripod\Mongo\Config::getInstance()->getDefaultContextAlias()); + $actual = $serializer->getSerializedIndex($g->_index, \Tripod\Config::getInstance()->getDefaultContextAlias()); // This test initially asserted that $expected was equal to $actual // $this->assertEquals($expected, $actual); diff --git a/test/unit/mongo/MongoTripodQueueOperationsTest.php b/test/unit/mongo/MongoTripodQueueOperationsTest.php index b1475540..d27d5a9c 100644 --- a/test/unit/mongo/MongoTripodQueueOperationsTest.php +++ b/test/unit/mongo/MongoTripodQueueOperationsTest.php @@ -11,7 +11,7 @@ * the results are as we would expect. For that reason this suite is more than just a series of unit tests, feels more like a set of integration tests since we * are testing a chained flow of events. */ -class MongoTripodQueueOperations extends MongoTripodTestBase +class MongoTripodQueueOperationsTest extends MongoTripodTestBase { /** * @var \Tripod\Mongo\Driver @@ -76,7 +76,6 @@ public function testSingleItemIsAddedToQueueForChangeToSingleSubject() $data = array( "changes" => $subjectsAndPredicatesOfChange, "operations" => array(OP_VIEWS, OP_TABLES, OP_SEARCH), - "tripodConfig" => \Tripod\Mongo\Config::getConfig(), "storeName" => 'tripod_php_testing', "podName" => 'CBD_testing', "contextAlias" => 'http://talisaspire.com/', @@ -155,7 +154,6 @@ public function testSingleItemWithViewsOpIsAddedToQueueForChangeToSingleSubject( $data = array( "changes" => $subjectsAndPredicatesOfChange, "operations" => array(OP_VIEWS), - "tripodConfig" => \Tripod\Mongo\Config::getConfig(), "storeName" => 'tripod_php_testing', "podName" => 'CBD_testing', "contextAlias" => 'http://talisaspire.com/', @@ -289,7 +287,6 @@ public function testSingleJobSubmittedToQueueForChangeToSeveralSubjects() $data = array( "changes" => $subjectsAndPredicatesOfChange, "operations" => array(OP_VIEWS, OP_TABLES, OP_SEARCH), - "tripodConfig" => \Tripod\Mongo\Config::getConfig(), "storeName" => 'tripod_php_testing', "podName" => 'CBD_testing', "contextAlias" => 'http://talisaspire.com/', @@ -324,4 +321,4 @@ public function testSingleJobSubmittedToQueueForChangeToSeveralSubjects() $tripod->saveChanges($g1, $g2); } -} \ No newline at end of file +} diff --git a/test/unit/mongo/MongoTripodSearchDocumentsTest.php b/test/unit/mongo/MongoTripodSearchDocumentsTest.php index 9bf937f5..b119e586 100644 --- a/test/unit/mongo/MongoTripodSearchDocumentsTest.php +++ b/test/unit/mongo/MongoTripodSearchDocumentsTest.php @@ -18,7 +18,7 @@ protected function setUp() $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); $this->getTripodCollection($this->tripod)->drop(); $this->loadBaseSearchDataViaTripod(); - foreach (\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch($this->tripod->getStoreName()) as $collection) + foreach (\Tripod\Config::getInstance()->getCollectionsForSearch($this->tripod->getStoreName()) as $collection) { $collection->drop(); } @@ -132,7 +132,7 @@ public function testSearchDocumentsGenerateWhenDefinedPredicateChanges() ->setConstructorArgs( array( $this->defaultStoreName, - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), + \Tripod\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), $this->defaultContext ) )->getMock(); @@ -218,7 +218,7 @@ public function testUpdateOfResourceInImpactIndexTriggersRegenerationOfSearchDoc ->setConstructorArgs( array( $this->defaultStoreName, - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), + \Tripod\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), $this->defaultContext ) )->getMock(); @@ -340,7 +340,7 @@ public function testRdfTypeTriggersGenerationOfSearchDocuments() ->setConstructorArgs( array( $this->defaultStoreName, - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), + \Tripod\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), $this->defaultContext ) )->getMock(); @@ -552,7 +552,7 @@ public function testDeleteResourceCreatesImpactedSubjects() $tripod->saveChanges(new \Tripod\ExtendedGraph(), $graph); - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForSearchDocument($this->defaultStoreName, 'i_search_resource'); + $collection = \Tripod\Config::getInstance()->getCollectionForSearchDocument($this->defaultStoreName, 'i_search_resource'); $query = array( _ID_KEY => array( diff --git a/test/unit/mongo/MongoTripodSearchIndexerTest.php b/test/unit/mongo/MongoTripodSearchIndexerTest.php index 6abdad7b..7b9c86ef 100644 --- a/test/unit/mongo/MongoTripodSearchIndexerTest.php +++ b/test/unit/mongo/MongoTripodSearchIndexerTest.php @@ -13,7 +13,7 @@ protected function setUp() parent::setUp(); $this->tripod = new \Tripod\Mongo\Driver("CBD_testing", "tripod_php_testing", array("async"=>array(OP_VIEWS=>true, OP_TABLES=>true, OP_SEARCH=>false))); - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForSearch($this->tripod->getStoreName()) as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForSearch($this->tripod->getStoreName()) as $collection) { $collection->drop(); } diff --git a/test/unit/mongo/MongoTripodTablesTest.php b/test/unit/mongo/MongoTripodTablesTest.php index 85553575..dbef6ec7 100644 --- a/test/unit/mongo/MongoTripodTablesTest.php +++ b/test/unit/mongo/MongoTripodTablesTest.php @@ -1,4 +1,6 @@ getTripodCollection($this->tripod)->drop(); $this->tripod->setTransactionLog($this->tripodTransactionLog); - $this->loadResourceDataViaTripod(); - $this->tablesConstParams = array($this->tripod->getStoreName(),$this->getTripodCollection($this->tripod),'http://talisaspire.com/'); $this->tripodTables = new \Tripod\Mongo\Composites\Tables($this->tripod->getStoreName(),$this->getTripodCollection($this->tripod),null); // pass null context, should default to http://talisaspire.com // purge tables - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForTables($this->tripod->getStoreName()) as $collection) - { + foreach (\Tripod\Config::getInstance()->getCollectionsForTables($this->tripod->getStoreName()) as $collection) { $collection->drop(); } } @@ -456,8 +455,8 @@ public function testGenerateTableRowsForUsersWithModifiersValidConfig() // Note that you need some config in order to create the Config object successfully. // Once that object has been created, we use our own table specifications to test against. - \Tripod\Mongo\Config::setConfig($this->generateMongoTripodTestConfig()); - $tripodConfig = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($this->generateMongoTripodTestConfig()); + $tripodConfig = \Tripod\Config::getInstance(); foreach($tableSpecifications['fields'] as $field) { @@ -493,8 +492,8 @@ public function testGenerateTableRowsForUsersWithModifiersInvalidConfigBadGlue() // Note that you need some config in order to create the Config object successfully. // Once that object has been created, we use our own table specifications to test against. - \Tripod\Mongo\Config::setConfig($this->generateMongoTripodTestConfig()); - $tripodConfig = \Tripod\Mongo\Config::getInstance(); + \Tripod\Config::setConfig($this->generateMongoTripodTestConfig()); + $tripodConfig = \Tripod\Config::getInstance(); $tripodConfig->checkModifierFunctions($tableSpecifications['predicates'], \Tripod\Mongo\Composites\Tables::$predicateModifiers); } @@ -838,7 +837,7 @@ public function testDistinctForFilterWithNoMatches() public function testTableRowsGenerateWhenDefinedPredicateChanges() { - foreach(\Tripod\Mongo\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId=>$spec) + foreach(\Tripod\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId=>$spec) { $this->generateTableRows($specId); } @@ -939,7 +938,7 @@ public function testTableRowsGenerateWhenDefinedPredicateChanges() // This should be 0, because we mocked the actual adding of the regenerated table. If it's zero, however, // it means we successfully deleted the views with $uri1 in the impactIndex - $collections = \Tripod\Mongo\Config::getInstance()->getCollectionsForTables($this->defaultStoreName); + $collections = \Tripod\Config::getInstance()->getCollectionsForTables($this->defaultStoreName); foreach($collections as $collection) { $query = array( @@ -952,7 +951,7 @@ public function testTableRowsGenerateWhenDefinedPredicateChanges() public function testTableRowsNotGeneratedWhenUndefinedPredicateChanges() { - foreach(\Tripod\Mongo\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId=>$spec) + foreach(\Tripod\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId=>$spec) { $this->generateTableRows($specId); } @@ -968,7 +967,7 @@ public function testTableRowsNotGeneratedWhenUndefinedPredicateChanges() $table = new \Tripod\Mongo\Composites\Tables( $this->defaultStoreName, - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), + \Tripod\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), $this->defaultContext ); @@ -1126,7 +1125,7 @@ public function testRdfTypeTriggersGenerationOfTableRows() array('generateTableRowsForType'), array( $this->defaultStoreName, - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), + \Tripod\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), $this->defaultContext ) ); @@ -1172,7 +1171,7 @@ public function testUpdateToResourceWithMatchingRdfTypeShouldOnlyRegenerateIfRdf $tables = new \Tripod\Mongo\Composites\Tables( $this->defaultStoreName, - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), + \Tripod\Config::getInstance()->getCollectionForCBD($this->defaultStoreName, $this->defaultPodName), $this->defaultContext ); @@ -1569,7 +1568,7 @@ public function testSavingMultipleNewEntitiesResultsInOneImpactedSubject() public function testRemoveTableSpecDoesNotAffectInvalidation() { - foreach(\Tripod\Mongo\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId=>$spec) + foreach(\Tripod\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId=>$spec) { $this->generateTableRows($specId); } @@ -1577,11 +1576,11 @@ public function testRemoveTableSpecDoesNotAffectInvalidation() $context = 'http://talisaspire.com/'; $uri = "http://talisaspire.com/works/4d101f63c10a6"; - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_resource'); + $collection = \Tripod\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_resource'); $this->assertGreaterThan(0, $collection->count(array('_id.type'=>'t_resource', 'value._impactIndex'=>array(_ID_RESOURCE=>$uri, _ID_CONTEXT=>$context)))); - $config = \Tripod\Mongo\Config::getConfig(); + $config = \Tripod\Config::getConfig(); unset($config['stores']['tripod_php_testing']['table_specifications'][0]); - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); /** @var PHPUnit_Framework_MockObject_MockObject|\Tripod\Mongo\Driver $mockTripod */ $mockTripod = $this->getMockBuilder('\Tripod\Mongo\Driver') @@ -1607,7 +1606,7 @@ public function testRemoveTableSpecDoesNotAffectInvalidation() ->setConstructorArgs( array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context ) ) diff --git a/test/unit/mongo/MongoTripodTestBase.php b/test/unit/mongo/MongoTripodTestBase.php index 4a41d97f..37cabc92 100644 --- a/test/unit/mongo/MongoTripodTestBase.php +++ b/test/unit/mongo/MongoTripodTestBase.php @@ -10,8 +10,6 @@ . PATH_SEPARATOR . dirname(dirname(dirname(dirname(__FILE__)))).'/src'); require_once('tripod.inc.php'); -require_once TRIPOD_DIR . 'mongo/Config.class.php'; -require_once TRIPOD_DIR . 'mongo/base/DriverBase.class.php'; /** * Mongo Config For Main DB @@ -67,13 +65,12 @@ protected function loadBaseSearchDataViaTripod() /** * @param string $filename */ - private function loadDataViaTripod($filename){ + private function loadDataViaTripod($filename) { $docs = json_decode(file_get_contents(dirname(__FILE__).$filename), true); - foreach ($docs as $d) - { + foreach ($docs as $d) { $g = new \Tripod\Mongo\MongoGraph(); $g->add_tripod_array($d); - $this->tripod->saveChanges(new \Tripod\ExtendedGraph(), $g,$d['_id'][_ID_CONTEXT]); + $this->tripod->saveChanges(new \Tripod\ExtendedGraph(), $g, $d['_id'][_ID_CONTEXT]); } } @@ -95,7 +92,7 @@ protected function setUp() { $config['data_sources']['rs2'] = json_decode(getenv('TRIPOD_DATASOURCE_RS2_CONFIG'), true); } - \Tripod\Mongo\Config::setConfig($config); + \Tripod\Config::setConfig($config); $className = get_class($this); $testName = $this->getName(); @@ -112,7 +109,7 @@ protected function setUp() protected function addDocument($doc, $toTransactionLog=false) { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); if($toTransactionLog == true) { return $this->getTlogCollection()->insertOne($doc, array("w"=>1)); @@ -129,7 +126,7 @@ protected function addDocument($doc, $toTransactionLog=false) */ protected function getTlogCollection() { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $tLogConfig = $config->getTransactionLogConfig(); return $config->getTransactionLogDatabase()->selectCollection($tLogConfig['collection']); } @@ -140,7 +137,7 @@ protected function getTlogCollection() */ protected function getTripodCollection(\Tripod\Mongo\Driver $tripod) { - $config = \Tripod\Mongo\Config::getInstance(); + $config = \Tripod\Config::getInstance(); $podName = $tripod->getPodName(); $dataSource = $config->getDataSourceForPod($tripod->getStoreName(), $podName); return $config->getDatabase( @@ -404,10 +401,10 @@ protected function assertDoesNotHaveResourceTriple(\Tripod\ExtendedGraph $graph, */ protected function lockDocument($subject, $transaction_id) { - $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForLocks('tripod_php_testing'); + $collection = \Tripod\Config::getInstance()->getCollectionForLocks('tripod_php_testing'); $labeller = new \Tripod\Mongo\Labeller(); $doc = array( - '_id' => array(_ID_RESOURCE => $labeller->uri_to_alias($subject), _ID_CONTEXT => \Tripod\Mongo\Config::getInstance()->getDefaultContextAlias()), + '_id' => array(_ID_RESOURCE => $labeller->uri_to_alias($subject), _ID_CONTEXT => \Tripod\Config::getInstance()->getDefaultContextAlias()), _LOCKED_FOR_TRANS => $transaction_id, _LOCKED_FOR_TRANS_TS => \Tripod\Mongo\DateUtil::getMongoDate() ); diff --git a/test/unit/mongo/MongoTripodTransactionRollbackTest.php b/test/unit/mongo/MongoTripodTransactionRollbackTest.php index 126e0d03..a6122042 100644 --- a/test/unit/mongo/MongoTripodTransactionRollbackTest.php +++ b/test/unit/mongo/MongoTripodTransactionRollbackTest.php @@ -40,10 +40,10 @@ protected function setUp() $tripod->expects($this->any())->method('addToSearchIndexQueue'); /** @var $tripod \Tripod\Mongo\Driver */ - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing')->drop(); + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing')->drop(); // Lock collection no longer available from Driver, so drop it manually - \Tripod\Mongo\Config::getInstance()->getCollectionForLocks('tripod_php_testing')->drop(); + \Tripod\Config::getInstance()->getCollectionForLocks('tripod_php_testing')->drop(); $tripod->setTransactionLog($this->tripodTransactionLog); @@ -429,7 +429,7 @@ public function lockSingleDocumentCauseFailureCallback($s, $transactionId, $cont */ public function lockSingleDocumentCallback($s, $transaction_id, $contextAlias) { - $lCollection = \Tripod\Mongo\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName()); + $lCollection = \Tripod\Config::getInstance()->getCollectionForLocks($this->tripod->getStoreName()); $countEntriesInLocksCollection = $lCollection->count(array('_id' => array(_ID_RESOURCE => $this->labeller->uri_to_alias($s), _ID_CONTEXT => $contextAlias))); if($countEntriesInLocksCollection > 0) //Subject is already locked diff --git a/test/unit/mongo/MongoTripodViewsTest.php b/test/unit/mongo/MongoTripodViewsTest.php index e94fcfd0..2bf3c513 100644 --- a/test/unit/mongo/MongoTripodViewsTest.php +++ b/test/unit/mongo/MongoTripodViewsTest.php @@ -45,7 +45,7 @@ protected function setUp() ); - foreach(\Tripod\Mongo\Config::getInstance()->getCollectionsForViews($this->tripod->getStoreName()) as $collection) + foreach(\Tripod\Config::getInstance()->getCollectionsForViews($this->tripod->getStoreName()) as $collection) { $collection->drop(); } @@ -103,7 +103,7 @@ public function testGenerateView() // get the view direct from mongo // $result = $this->tripod->getViewForResource("http://talisaspire.com/resources/3SplCtWGPqEyXcDiyhHQpA","v_resource_full"); $mongo = new Client( - \Tripod\Mongo\Config::getInstance()->getConnStr('tripod_php_testing'), + \Tripod\Config::getInstance()->getConnStr('tripod_php_testing'), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] ); @@ -176,7 +176,7 @@ public function testGenerateViewWithFilterRemovesFilteredDataButKeepsResourcesIn ); // get the view direct from mongo $mongo = new Client( - \Tripod\Mongo\Config::getInstance()->getConnStr('tripod_php_testing'), + \Tripod\Config::getInstance()->getConnStr('tripod_php_testing'), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] ); @@ -241,7 +241,7 @@ public function testGenerateViewWithFilterOnLiteralValue() ); // get the view direct from mongo $mongo = new Client( - \Tripod\Mongo\Config::getInstance()->getConnStr('tripod_php_testing'), + \Tripod\Config::getInstance()->getConnStr('tripod_php_testing'), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] ); @@ -316,7 +316,7 @@ public function testGenerateViewCorrectlyAfterUpdateAffectsFilter() ); // get the view direct from mongo $mongo = new Client( - \Tripod\Mongo\Config::getInstance()->getConnStr('tripod_php_testing'), + \Tripod\Config::getInstance()->getConnStr('tripod_php_testing'), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] ); @@ -462,7 +462,7 @@ public function testGenerateViewContainingRdfSequence() ); // get the view direct from mongo $mongo = new Client( - \Tripod\Mongo\Config::getInstance()->getConnStr('tripod_php_testing'), + \Tripod\Config::getInstance()->getConnStr('tripod_php_testing'), [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] ); @@ -515,7 +515,7 @@ public function testGenerateViewWithTTL() ) ); // get the view direct from mongo - $actualView = \Tripod\Mongo\Config::getInstance()->getCollectionForView('tripod_php_testing', 'v_resource_full_ttl')->findOne(array('_id'=>array("r"=>'http://talisaspire.com/resources/3SplCtWGPqEyXcDiyhHQpA',"c"=>'http://talisaspire.com/',"type"=>'v_resource_full_ttl'))); + $actualView = \Tripod\Config::getInstance()->getCollectionForView('tripod_php_testing', 'v_resource_full_ttl')->findOne(array('_id'=>array("r"=>'http://talisaspire.com/resources/3SplCtWGPqEyXcDiyhHQpA',"c"=>'http://talisaspire.com/',"type"=>'v_resource_full_ttl'))); $this->assertEquals($expectedView['_id'], $actualView['_id']); $this->assertEquals($expectedView['value'], $actualView['value']); $this->assertInstanceOf('\MongoDB\BSON\UTCDateTime', $actualView['_cts']); @@ -565,7 +565,7 @@ public function testNonExpiringViewWithNegativeTTL() ] ]; // get the view direct from mongo - $actualView = \Tripod\Mongo\Config::getInstance() + $actualView = \Tripod\Config::getInstance() ->getCollectionForView('tripod_php_testing', 'v_event_no_expiration') ->findOne( [ @@ -617,7 +617,7 @@ public function testNonExpiringViewWithNegativeTTL() ); // get the view direct from mongo, it should the same as earlier - $actualView2 = \Tripod\Mongo\Config::getInstance() + $actualView2 = \Tripod\Config::getInstance() ->getCollectionForView('tripod_php_testing', 'v_event_no_expiration') ->findOne( [ @@ -732,7 +732,7 @@ public function testGenerateViewWithCountAggregate() ) ); - $actualView = \Tripod\Mongo\Config::getInstance()->getCollectionForView('tripod_php_testing', 'v_counts')->findOne(array('_id'=>array("r"=>'http://talisaspire.com/works/4d101f63c10a6',"c"=>"http://talisaspire.com/","type"=>'v_counts'))); + $actualView = \Tripod\Config::getInstance()->getCollectionForView('tripod_php_testing', 'v_counts')->findOne(array('_id'=>array("r"=>'http://talisaspire.com/works/4d101f63c10a6',"c"=>"http://talisaspire.com/","type"=>'v_counts'))); $this->assertEquals($expectedView['_id'], $actualView['_id']); $this->assertEquals($expectedView['value'], $actualView['value']); $this->assertInstanceOf('\MongoDB\BSON\UTCDateTime', $actualView['_cts']); @@ -834,7 +834,7 @@ public function testGetViewForResourcesDoesNotInvokeViewGenerationForMissingReso ->with('tripod_php_testing', $this->anything(), $this->anything()) ->will($this->returnValue($mockViewColl)); - $mockConfig->loadConfig(\Tripod\Mongo\Config::getConfig()); + $mockConfig->loadConfig(\Tripod\Config::getConfig()); /* @var $mockTripodViews|PHPUnit_Framework_MockObject_MockObject Views */ $mockTripodViews = $this->getMock( @@ -901,7 +901,7 @@ public function testGetViewForResourcesInvokesViewGenerationForMissingResources( ->with('tripod_php_testing', $this->anything(), $this->anything()) ->will($this->returnValue($mockViewColl)); - $mockConfig->loadConfig(\Tripod\Mongo\Config::getConfig()); + $mockConfig->loadConfig(\Tripod\Config::getConfig()); /* @var $mockTripodViews \Tripod\Mongo\Composites\Views */ @@ -957,7 +957,7 @@ public function testDeletionOfResourceTriggersViewRegeneration() $tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing', ['defaultContext' => $context]); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $originalGraph); - $collections = \Tripod\Mongo\Config::getInstance()->getCollectionsForViews( + $collections = \Tripod\Config::getInstance()->getCollectionsForViews( 'tripod_php_testing', ['v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source'] ); @@ -1013,7 +1013,7 @@ public function testDeletionOfResourceTriggersViewRegeneration() ['generateViewsForResourcesOfType'], [ 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context ] ); @@ -1117,7 +1117,7 @@ public function testDeletionOfResourceInImpactIndexTriggersViewRegeneration() $tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing', array('defaultContext'=>$context)); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $originalGraph); - $collections = \Tripod\Mongo\Config::getInstance()->getCollectionsForViews('tripod_php_testing', array('v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source')); + $collections = \Tripod\Config::getInstance()->getCollectionsForViews('tripod_php_testing', array('v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source')); foreach($collections as $collection) { @@ -1172,7 +1172,7 @@ public function testDeletionOfResourceInImpactIndexTriggersViewRegeneration() array('generateView'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context ) ); @@ -1300,7 +1300,7 @@ public function testUpdateOfResourceInImpactIndexTriggersViewRegeneration() $tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing', array('defaultContext'=>$context)); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $originalGraph); - $collections = \Tripod\Mongo\Config::getInstance()->getCollectionsForViews('tripod_php_testing', array('v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source')); + $collections = \Tripod\Config::getInstance()->getCollectionsForViews('tripod_php_testing', array('v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source')); foreach($collections as $collection) { @@ -1353,7 +1353,7 @@ public function testUpdateOfResourceInImpactIndexTriggersViewRegeneration() array('generateView'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context ) ); @@ -1482,7 +1482,7 @@ public function testUpdateOfResourceTriggersViewRegeneration() $tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing', array('defaultContext'=>$context)); $tripod->saveChanges(new \Tripod\ExtendedGraph(), $originalGraph); - $collections = \Tripod\Mongo\Config::getInstance()->getCollectionsForViews('tripod_php_testing', array('v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source')); + $collections = \Tripod\Config::getInstance()->getCollectionsForViews('tripod_php_testing', array('v_resource_full', 'v_resource_full_ttl', 'v_resource_to_single_source')); foreach($collections as $collection) { @@ -1535,7 +1535,7 @@ public function testUpdateOfResourceTriggersViewRegeneration() array('generateView'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context ) ); @@ -1707,7 +1707,7 @@ public function testResourceUpdateNotCoveredBySpecStillTriggersOperations() array('generateViewsForResourcesOfType'), array( 'tripod_php_testing', - \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), + \Tripod\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context ) ); @@ -1997,7 +1997,7 @@ public function testCursorNoExceptions() ->with('tripod_php_testing', $this->anything(), $this->anything()) ->will($this->returnValue($mockViewColl)); - $mockConfig->loadConfig(\Tripod\Mongo\Config::getConfig()); + $mockConfig->loadConfig(\Tripod\Config::getConfig()); /* @var $mockTripodViews|PHPUnit_Framework_MockObject_MockObject Views */ $mockTripodViews = $this->getMock( @@ -2058,7 +2058,7 @@ public function testCursorExceptionThrown() ->with('tripod_php_testing', $this->anything(), $this->anything()) ->will($this->returnValue($mockViewColl)); - $mockConfig->loadConfig(\Tripod\Mongo\Config::getConfig()); + $mockConfig->loadConfig(\Tripod\Config::getConfig()); /* @var $mockTripodViews|PHPUnit_Framework_MockObject_MockObject Views */ $mockTripodViews = $this->getMock( @@ -2132,7 +2132,7 @@ public function testCursorNoExceptionThrownWhenCursorThrowsSomeExceptions() ->with('tripod_php_testing', $this->anything(), $this->anything()) ->will($this->returnValue($mockViewColl)); - $mockConfig->loadConfig(\Tripod\Mongo\Config::getConfig()); + $mockConfig->loadConfig(\Tripod\Config::getConfig()); /* @var $mockTripodViews|PHPUnit_Framework_MockObject_MockObject Views */ $mockTripodViews = $this->getMock( diff --git a/test/unit/mongo/ResqueJobTestBase.php b/test/unit/mongo/ResqueJobTestBase.php new file mode 100644 index 00000000..3ca663fd --- /dev/null +++ b/test/unit/mongo/ResqueJobTestBase.php @@ -0,0 +1,24 @@ +getMockBuilder('\Resque_Job') + ->setMethods(['getInstance', 'getArguments']) + ->setConstructorArgs(['test', get_class($job), $job->args]) + ->getMock(); + $mockJob->expects($this->atLeastOnce()) + ->method('getInstance') + ->will($this->returnValue($job)); + $mockJob->expects($this->any()) + ->method('getArguments') + ->will($this->returnValue($job->args)); + $mockJob->perform(); + + } +} diff --git a/test/unit/mongo/TestConfigGenerator.php b/test/unit/mongo/TestConfigGenerator.php new file mode 100644 index 00000000..1188d165 --- /dev/null +++ b/test/unit/mongo/TestConfigGenerator.php @@ -0,0 +1,24 @@ + get_class($this), 'filename' => $this->fileName]; + } + + public static function deserialize(array $config) + { + $instance = new self(); + $instance->fileName = $config['filename']; + $cfg = json_decode(file_get_contents($config['filename']), true); + $instance->loadConfig($cfg); + return $instance; + } +}