Factory method to get not stored Document instance from array
public createDocument ( array $data = null ) : |
||
$data | array | |
리턴 |
/** * Abstract test * * @param mixed $value1 first value to push * @param mixed $value2 second value to push * @param array $expectedList expected list, stored in db * @param int $fieldName name of field, where values pushed: one of self::FIELD_NAME_* * @param bool $isDocumentSaved is document already stored to db before push */ private function doPushTest($value1, $value2, $expectedList, $fieldName, $isDocumentSaved) { // create document $doc = $this->collection->createDocument($this->initialDocument); // test push to saved or new document if ($isDocumentSaved) { $doc->save(); } // prepare expected value switch ($fieldName) { case self::FIELD_NAME_SCALAR: $expectedList = array_merge(array($this->initialDocument[$fieldName]), $expectedList); break; case self::FIELD_NAME_LIST: $expectedList = array_merge($this->initialDocument[$fieldName], $expectedList); break; } // push single to empty $doc->push($fieldName, $value1)->push($fieldName, $value2); // test document in identity map after push before save $this->assertEquals($expectedList, $doc->get($fieldName)); $doc->save(); // test document in identity map after push after save $this->assertEquals($expectedList, $this->collection->getDocument($doc->getId())->get($fieldName)); // test document in db $this->assertEquals($expectedList, $this->collection->getDocumentDirectly($doc->getId())->get($fieldName)); }
public function testDetach() { $document = $this->collection->createDocument(array('param' => 'value')); $this->assertFalse($this->persistence->contains($document)); // attach document $this->persistence->persist($document); // check if document in persistence $this->assertTrue($this->persistence->contains($document)); // detach document $this->persistence->detach($document); // check if document in persistence $this->assertFalse($this->persistence->contains($document)); }
public function testClearTriggeredErrors() { $document = $this->collection->createDocument()->triggerError('someField', 'someRule', 'someMessage'); $this->assertTrue($document->hasErrors()); $document->clearTriggeredErrors(); $this->assertFalse($document->hasErrors()); }
public function testExpressionWithinPolygon() { $this->collection->ensure2dIndex('point'); $point1Id = $this->collection->createDocument()->setLegacyPoint('point', 5, 4)->save()->getId(); $point2Id = $this->collection->createDocument()->setLegacyPoint('point', 50, 40)->save()->getId(); $point = $this->collection->find()->withinPolygon('point', array(array(0, 0), array(0, 10), array(10, 10), array(10, 0)))->findOne(); $this->assertNotEmpty($point); $this->assertEquals($point1Id, $point->getId()); }
public function testCancelOperation_BeforeValidate() { $testCase = $this; $this->collection->createDocument()->onBeforeValidate(function (\Sokil\Mongo\Event $event, $eventName, $dispatcher) { $event->cancel(); })->onAfterValidate(function (\Sokil\Mongo\Event $event, $eventName, $dispatcher) use($testCase) { $testCase->fail('Validation must be cancelled'); })->validate(); }
public function testAddValidatorNamespace() { $document = $this->collection->createDocument()->addValidatorNamespace('\\Vendor\\Mongo\\Validator\\'); $reflectionClass = new \ReflectionClass($document); $property = $reflectionClass->getProperty('validatorNamespaces'); $property->setAccessible(true); $namespaces = $property->getValue($document); $this->assertNotEquals(false, array_search('\\Vendor\\Mongo\\Validator', $namespaces)); }
public function testGetRelatedDocumentList() { $this->collection->disableDocumentPool(); $relatedDocument = $this->collection->createDocument(array('param' => 'value'))->save(); $document = $this->collection->createDocument()->pushReference('related', $relatedDocument)->save(); $foundRelatedDocumentList = $document->getReferencedDocumentList('related'); $this->assertSame(1, count($foundRelatedDocumentList)); $foundRelatedDocument = current($foundRelatedDocumentList); $this->assertEquals($relatedDocument->getId(), $foundRelatedDocument->getId()); }
public function testPaginatorIteratorInterface() { $d11 = $this->collection->createDocument(array('param1' => 1, 'param2' => 1))->save(); $d12 = $this->collection->createDocument(array('param1' => 1, 'param2' => 2))->save(); $d21 = $this->collection->createDocument(array('param1' => 2, 'param2' => 1))->save(); $d22 = $this->collection->createDocument(array('param1' => 2, 'param2' => 2))->save(); $pager = $this->collection->find()->paginate(20, 2); $this->assertEquals($d21->getId(), $pager->current()->getId()); $this->assertEquals((string) $d21->getId(), $pager->key()); $this->assertTrue($pager->valid()); $pager->next(); $this->assertEquals($d22->getId(), $pager->current()->getId()); $this->assertEquals((string) $d22->getId(), $pager->key()); $this->assertTrue($pager->valid()); $pager->next(); $this->assertFalse($pager->valid()); $pager->rewind(); $this->assertEquals($d21->getId(), $pager->current()->getId()); $this->assertEquals((string) $d21->getId(), $pager->key()); }
public function testGetResultSet() { $document1Id = $this->collection->createDocument(array('param' => 1))->save()->getId(); $document2Id = $this->collection->createDocument(array('param' => 2))->save()->getId(); $document3Id = $this->collection->createDocument(array('param' => 3))->save()->getId(); $document4Id = $this->collection->createDocument(array('param' => 4))->save()->getId(); $resultSet = $this->collection->find()->getResultSet(); $this->assertInstanceOf('\\Sokil\\Mongo\\ResultSet', $resultSet); $filtered = $resultSet->filter(function ($item) { return $item->param % 2; })->keys(); $this->assertEquals(array($document1Id, $document3Id), $filtered); }
public function testExplainAggregate() { $this->collection->createDocument(array('param' => 1))->save(); $this->collection->createDocument(array('param' => 2))->save(); $this->collection->createDocument(array('param' => 3))->save(); $this->collection->createDocument(array('param' => 4))->save(); $pipeline = $this->collection->createAggregator()->match(array('param' => array('$gte' => 2)))->group(array('_id' => 0, 'sum' => array('$sum' => '$param'))); try { $explain = $this->collection->explainAggregate($pipeline); $this->assertArrayHasKey('stages', $explain); } catch (\Exception $e) { $this->assertEquals('Explain of aggregation implemented only from 2.6.0', $e->getMessage()); } }
public function testMoveToCollection() { $targetCollectionName = 'targetMoveCollection'; $targetCollection = $this->collection->getDatabase()->getCollection($targetCollectionName)->delete(); // fill collection with documents for ($i = 0; $i < 200; $i++) { $this->collection->createDocument(array('i' => $i))->save(); } $this->collection->find()->whereMod('i', 2, 0)->moveToCollection($targetCollectionName); // check source collection $this->assertEquals(100, $this->collection->count()); foreach ($this->collection->find() as $document) { $this->assertEquals(1, $document->i % 2); } // check target collection $this->assertEquals(100, $targetCollection->count()); foreach ($targetCollection->find() as $document) { $this->assertEquals(0, $document->i % 2); } // clear $targetCollection->delete(); }
/** * @expectedException \Sokil\Mongo\Exception * @expectedExceptionMessage Behavior class not specified */ public function testAttachBehaviors_AsArray_ClassNotSpecified() { $document = $this->collection->createDocument(array('param' => 0)); $document->attachBehaviors(array('get42' => array('param' => 'value'))); }
/** * Add item to queue * * @param mixed $payload data to send * @param int $priority more priority num give quicker getting from queue * @return \Sokil\Mongo\Queue */ public function enqueue($payload, $priority = 0) { $this->collection->createDocument(array('payload' => $payload, 'priority' => (int) $priority, 'datetime' => new \MongoDate()))->save(); return $this; }
public function testJsonSerializable() { $document = $this->collection->createDocument(array('k1' => 'v1')); $this->assertEquals(array('k1' => 'v1'), $document->jsonSerialize()); }
/** * @expectedException \Sokil\Mongo\Exception * @expectedExceptionMessage Update error: some_strange_error: Some strange error */ public function testSave_UpdateError() { $mongoCollectionMock = $this->getMock('\\MongoCollection', array('update'), array($this->collection->getDatabase()->getMongoDb(), 'phpmongo_test_collection')); $mongoCollectionMock->expects($this->once())->method('update')->will($this->returnValue(array('ok' => (double) 0, 'err' => 'some_strange_error', 'errmsg' => 'Some strange error'))); $collection = new Collection($this->collection->getDatabase(), $mongoCollectionMock); // create document $document = $collection->createDocument(array('p' => 'v'))->save(); // update document with error $document->set('p', 'v1')->save(); }