Sokil\Mongo\CursorTest::testFindRandom PHP Method

testFindRandom() public method

public testFindRandom ( )
    public function testFindRandom()
    {
        // create new document
        $document1 = $this->collection->createDocument(array('p1' => 'v', 'p2' => 'doc1'));
        $document1->save();
        $document2 = $this->collection->createDocument(array('p1' => 'v', 'p2' => 'doc2'));
        $document2->save();
        $document3 = $this->collection->createDocument(array('p1' => 'other_v', 'p2' => 'doc3'));
        $document3->save();
        // find unexisted random document
        $document = $this->collection->find()->where('pZZZ', 'v')->findRandom();
        $this->assertEmpty($document);
        // find random documents if only one document match query
        $document = $this->collection->find()->where('p1', 'other_v')->findRandom();
        $this->assertEquals($document->getId(), $document3->getId());
        // find random document among two existed documents
        $document = $this->collection->find()->where('p1', 'v')->findRandom();
        $this->assertTrue(in_array($document->getId(), array($document1->getId(), $document2->getId())));
    }