/** * @param \Tripod\Mongo\ImpactedSubject[] $subjects * @param string|null $queueName * @param array $otherData */ public function createJob(array $subjects, $queueName = null, $otherData = array()) { if (!$queueName) { $queueName = \Tripod\Mongo\Config::getApplyQueueName(); } elseif (strpos($queueName, \Tripod\Mongo\Config::getApplyQueueName()) === false) { $queueName = \Tripod\Mongo\Config::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()); $this->submitJob($queueName, get_class($this), array_merge($otherData, $data)); }
/** * Sets job arguments */ protected function setArgs() { $subject = new \Tripod\Mongo\ImpactedSubject(array(_ID_RESOURCE => 'http://example.com/resources/foo', _ID_CONTEXT => 'http://talisaspire.com/'), OP_VIEWS, 'tripod_php_testing', 'CBD_testing'); $this->args = array('tripodConfig' => \Tripod\Mongo\Config::getConfig(), 'subjects' => array($subject->toArray()), 'statsConfig' => $this->getStatsDConfig()); }
public function testCursorNoExceptionThrownWhenCursorThrowsSomeExceptions() { $uri1 = "http://uri1"; $viewType = "someView"; $context = "http://someContext"; $returnedGraph = new \Tripod\ExtendedGraph(); $returnedGraph->add_literal_triple($uri1, 'http://somepred', 'someval'); $mockDb = $this->getMockBuilder('\\MongoDB\\Database')->disableOriginalConstructor()->setMethods(['selectCollection'])->getMock(); $mockColl = $this->getMockBuilder('\\MongoDB\\Collection')->disableOriginalConstructor()->setMethods(['findOne'])->getMock(); $mockViewColl = $this->getMockBuilder('\\MongoDB\\Collection')->disableOriginalConstructor()->setMethods(['find', 'findOne'])->getMock(); $mockCursor = $this->getMock('\\ArrayIterator', array('rewind')); $mockCursor->expects($this->exactly(5))->method('rewind')->will($this->onConsecutiveCalls($this->throwException(new \Exception('Exception thrown when cursoring to Mongo')), $this->throwException(new \Exception('Exception thrown when cursoring to Mongo')), $this->throwException(new \Exception('Exception thrown when cursoring to Mongo')), $this->throwException(new \Exception('Exception thrown when cursoring to Mongo')), $this->returnValue($mockCursor))); $mockViewColl->expects($this->once())->method('find')->will($this->returnValue($mockCursor)); $mockDb->expects($this->any())->method("selectCollection")->will($this->returnValue($mockColl)); $mockColl->expects($this->once())->method("findOne")->will($this->returnValue(null)); /** @var PHPUnit_Framework_MockObject_MockObject|TripodTestConfig $mockConfig */ $mockConfig = $this->getMock('TripodTestConfig', array('getCollectionForCBD', 'getCollectionForView')); $mockConfig->expects($this->atLeastOnce())->method('getCollectionForCBD')->with('tripod_php_testing', $this->anything(), $this->anything())->will($this->returnValue($mockColl)); $mockConfig->expects($this->atLeastOnce())->method('getCollectionForView')->with('tripod_php_testing', $this->anything(), $this->anything())->will($this->returnValue($mockViewColl)); $mockConfig->loadConfig(\Tripod\Mongo\Config::getConfig()); /* @var $mockTripodViews|PHPUnit_Framework_MockObject_MockObject Views */ $mockTripodViews = $this->getMock('\\Tripod\\Mongo\\Composites\\Views', array('generateView', 'getConfigInstance'), array('tripod_php_testing', $mockColl, $context)); $mockTripodViews->expects($this->never())->method('generateView'); $mockTripodViews->expects($this->atLeastOnce())->method('getConfigInstance')->will($this->returnValue($mockConfig)); $mockTripodViews->getViewForResources(array($uri1), $viewType, $context); }
/** * Saving a change to an entity that appears in the impact index for view/table_rows/search docs of 3 other entities should result in * 4 items being placed on the queue, with the operations for each relevant to the configured operations based on the specifications * todo: new test in composite for one subject that impacts another */ public function testSingleJobSubmittedToQueueForChangeToSeveralSubjects() { $tripod = $this->getMockBuilder('\\Tripod\\Mongo\\Driver')->setMethods(array('getDataUpdater', 'getComposite'))->setConstructorArgs(array('CBD_testing', 'tripod_php_testing', array('defaultContext' => 'http://talisaspire.com/', OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => true, OP_SEARCH => true))))->getMock(); $tripodUpdates = $this->getMockBuilder('\\Tripod\\Mongo\\Updates')->setMethods(array('processSyncOperations', 'getDiscoverImpactedSubjects'))->setConstructorArgs(array($tripod, array('defaultContext' => 'http://talisaspire.com/', OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => true, OP_SEARCH => true))))->getMock(); $discoverImpactedSubjects = $this->getMockBuilder('\\Tripod\\Mongo\\Jobs\\DiscoverImpactedSubjects')->setMethods(array('createJob'))->getMock(); $tripod->expects($this->once())->method('getDataUpdater')->will($this->returnValue($tripodUpdates)); $subjectsAndPredicatesOfChange = array("http://talisaspire.com/resources/doc1" => array('dct:date'), "http://talisaspire.com/resources/doc2" => array('dct:date'), "http://talisaspire.com/resources/doc3" => array('dct:date')); $tripod->expects($this->never())->method('getComposite'); $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/', "statsConfig" => array()); $tripodUpdates->expects($this->once())->method('getDiscoverImpactedSubjects')->will($this->returnValue($discoverImpactedSubjects)); $discoverImpactedSubjects->expects($this->once())->method('createJob')->with($data, \Tripod\Mongo\Config::getDiscoverQueueName()); $g1 = $tripod->describeResources(array("http://talisaspire.com/resources/doc1", "http://talisaspire.com/resources/doc2", "http://talisaspire.com/resources/doc3")); $g2 = $tripod->describeResources(array("http://talisaspire.com/resources/doc1", "http://talisaspire.com/resources/doc2", "http://talisaspire.com/resources/doc3")); $g2->add_literal_triple("http://talisaspire.com/resources/doc1", $g2->qname_to_uri("dct:date"), "01-01-1970"); $g2->add_literal_triple("http://talisaspire.com/resources/doc2", $g2->qname_to_uri("dct:date"), "01-01-1970"); $g2->add_literal_triple("http://talisaspire.com/resources/doc3", $g2->qname_to_uri("dct:date"), "01-01-1970"); $tripod->saveChanges($g1, $g2); }
protected function setArgs() { $this->args = array('tripodConfig' => \Tripod\Mongo\Config::getConfig(), 'storeName' => 'tripod_php_testing', 'podName' => 'CBD_testing', 'changes' => array('http://example.com/resources/foo' => array('rdf:type', 'dct:title')), 'operations' => array(OP_VIEWS, OP_TABLES, OP_SEARCH), 'contextAlias' => 'http://talisaspire.com/'); }
public function testNestConditionalInArithmeticFunction() { $tableSpec = array("_id" => "t_arithmetic_with_nested_conditional", "type" => array("bibo:Book", "bibo:Document"), "from" => "CBD_testing", "fields" => array(array("fieldName" => "x", "predicates" => array("foo:wibble"))), "computed_fields" => array(array("fieldName" => "foobar", "value" => array("arithmetic" => array(array("conditional" => array("if" => array('$x'), "then" => '$x', "else" => 100)), "*", 3))))); $oldConfig = \Tripod\Mongo\Config::getConfig(); $newConfig = \Tripod\Mongo\Config::getConfig(); $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; \Tripod\Mongo\Config::setConfig($newConfig); \Tripod\Mongo\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'); $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(); $collection->drop(); }
public function testRemoveTableSpecDoesNotAffectInvalidation() { foreach (\Tripod\Mongo\Config::getInstance()->getTableSpecifications($this->tripod->getStoreName()) as $specId => $spec) { $this->generateTableRows($specId); } $context = 'http://talisaspire.com/'; $uri = "http://talisaspire.com/works/4d101f63c10a6"; $collection = \Tripod\Mongo\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(); unset($config['stores']['tripod_php_testing']['table_specifications'][0]); \Tripod\Mongo\Config::setConfig($config); /** @var PHPUnit_Framework_MockObject_MockObject|\Tripod\Mongo\Driver $mockTripod */ $mockTripod = $this->getMockBuilder('\\Tripod\\Mongo\\Driver')->setMethods(array('getComposite'))->setConstructorArgs(array('CBD_testing', 'tripod_php_testing', array('defaultContext' => $context, OP_ASYNC => array(OP_VIEWS => true, OP_TABLES => false, OP_SEARCH => true))))->getMock(); $mockTables = $this->getMockBuilder('\\Tripod\\Mongo\\Composites\\Tables')->setMethods(array('update'))->setConstructorArgs(array('tripod_php_testing', \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), $context))->getMock(); $labeller = new \Tripod\Mongo\Labeller(); $mockTripod->expects($this->once())->method('getComposite')->with(OP_TABLES)->will($this->returnValue($mockTables)); $mockTables->expects($this->never())->method('update'); $originalGraph = $mockTripod->describeResource($uri); $updatedGraph = $originalGraph->get_subject_subgraph($uri); $updatedGraph->add_literal_triple($uri, $labeller->qname_to_uri('dct:description'), 'Physics textbook'); $mockTripod->saveChanges($originalGraph, $updatedGraph); // The table row should still be there, even if the tablespec no longer exists $this->assertGreaterThan(0, $collection->count(array('_id.type' => 't_resource', 'value._impactIndex' => array(_ID_RESOURCE => $uri, _ID_CONTEXT => $context)))); }
public function testSpecifyQueueForAsyncOperations() { $uri_1 = "http://example.com/1"; $uri_2 = "http://example.com/2"; $oG = new \Tripod\Mongo\MongoGraph(); $oG->add_resource_triple($uri_1, $oG->qname_to_uri("rdf:type"), $oG->qname_to_uri("acorn:Resource")); $oG->add_resource_triple($uri_2, $oG->qname_to_uri("rdf:type"), $oG->qname_to_uri("acorn:Resource")); // a delete and an update $nG = new \Tripod\Mongo\MongoGraph(); $nG->add_graph($oG); $nG->add_literal_triple($uri_1, $nG->qname_to_uri("searchterms:title"), "wibble"); $nG->remove_resource_triple($uri_2, $oG->qname_to_uri("rdf:type"), "http://foo/bar#Class2"); /** @var \Tripod\Mongo\Driver|PHPUnit_Framework_MockObject_MockObject $mockTripod */ $mockTripod = $this->getMock('\\Tripod\\Mongo\\Driver', array('getComposite', 'getDataUpdater'), array('CBD_testing', 'tripod_php_testing', array('defaultContext' => 'http://talisaspire.com/'))); $queueName = 'TRIPOD_TESTING_QUEUE_' . uniqid(); $mockTripodUpdates = $this->getMock('\\Tripod\\Mongo\\Updates', array('storeChanges', 'getDiscoverImpactedSubjects'), array($mockTripod, array(OP_ASYNC => array(OP_TABLES => true, OP_VIEWS => false, OP_SEARCH => true, 'queue' => $queueName)))); $mockViews = $this->getMock('\\Tripod\\Mongo\\Composites\\Views', array('getImpactedSubjects', 'update'), array('tripod_php_testing', \Tripod\Mongo\Config::getInstance()->getCollectionForCBD('tripod_php_testing', 'CBD_testing'), 'http://talisaspire.com/')); $mockDiscoverImpactedSubjects = $this->getMockBuilder('\\Tripod\\Mongo\\Jobs\\DiscoverImpactedSubjects')->setMethods(array('createJob'))->getMock(); $impactedViewSubjects = array($this->getMockBuilder('\\Tripod\\Mongo\\ImpactedSubject')->setConstructorArgs(array(array(_ID_RESOURCE => $uri_1, _ID_CONTEXT => 'http://talisaspire.com'), OP_VIEWS, 'tripod_php_testing', 'CBD_testing'))->setMethods(array('update'))->getMock(), $this->getMockBuilder('\\Tripod\\Mongo\\ImpactedSubject')->setConstructorArgs(array(array(_ID_RESOURCE => $uri_2, _ID_CONTEXT => 'http://talisaspire.com'), OP_VIEWS, 'tripod_php_testing', 'CBD_testing'))->setMethods(array('update'))->getMock()); $labeller = new \Tripod\Mongo\Labeller(); $subjectsAndPredicatesOfChange = array($labeller->uri_to_alias($uri_1) => array('searchterms:title'), $labeller->uri_to_alias($uri_2) => array('rdf:type')); $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/', 'queue' => $queueName, 'statsConfig' => array()); // getComposite() should only be called if there are synchronous operations $mockTripod->expects($this->once())->method('getComposite')->with(OP_VIEWS)->will($this->returnValue($mockViews)); $mockTripodUpdates->expects($this->once())->method('getDiscoverImpactedSubjects')->will($this->returnValue($mockDiscoverImpactedSubjects)); $mockTripodUpdates->expects($this->once())->method('storeChanges')->will($this->returnValue(array("subjectsAndPredicatesOfChange" => $subjectsAndPredicatesOfChange, "transaction_id" => "t1234"))); $mockTripod->expects($this->once())->method('getDataUpdater')->will($this->returnValue($mockTripodUpdates)); $mockViews->expects($this->once())->method('getImpactedSubjects')->will($this->returnValue($impactedViewSubjects)); // This shouldn't be called because ImpactedSubject->update has been mocked and isn't doing anything $mockViews->expects($this->never())->method('update'); $impactedViewSubjects[0]->expects($this->once())->method('update'); $impactedViewSubjects[1]->expects($this->once())->method('update'); $mockDiscoverImpactedSubjects->expects($this->once())->method('createJob')->with($jobData, $queueName); $mockTripod->saveChanges($oG, $nG, "http://talisaspire.com/"); }
public function testArithmeticSpecValidationInvalidNestedOperator() { $newConfig = \Tripod\Mongo\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); $this->setExpectedException('\\Tripod\\Exceptions\\ConfigException', "Invalid arithmetic operator '#' in computed arithmetic spec"); \Tripod\Mongo\Config::getInstance(); }
/** * Adds the operations to the queue to be performed asynchronously * @param array $subjectsAndPredicatesOfChange * @param string $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[OP_QUEUE] = $this->queueName; $queueName = $this->queueName; } else { $queueName = Config::getDiscoverQueueName(); } $this->getDiscoverImpactedSubjects()->createJob($data, $queueName); } }