| Server IP : 54.94.228.101 / Your IP : 172.28.20.13 Web Server : Apache System : Linux ip-172-28-29-189 6.5.0-1014-aws #14~22.04.1-Ubuntu SMP Thu Feb 15 15:27:06 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.2.34-43+ubuntu22.04.1+deb.sury.org+1 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/vinumday2_0/vendor/mongodb/mongodb/tests/Collection/ |
Upload File : |
<?php
namespace MongoDB\Tests\Collection;
use MongoDB\BSON\Javascript;
use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\MapReduce;
/**
* Functional tests for the Collection class.
*/
class CollectionFunctionalTest extends FunctionalTestCase
{
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDatabaseAndCollectionNames
*/
public function testConstructorDatabaseNameArgument($databaseName)
{
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
new Collection($this->manager, $databaseName, $this->getCollectionName());
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidDatabaseAndCollectionNames
*/
public function testConstructorCollectionNameArgument($collectionName)
{
// TODO: Move to unit test once ManagerInterface can be mocked (PHPC-378)
new Collection($this->manager, $this->getDatabaseName(), $collectionName);
}
public function provideInvalidDatabaseAndCollectionNames()
{
return [
[null],
[''],
];
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options)
{
new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $options);
}
public function provideInvalidConstructorOptions()
{
$options = [];
foreach ($this->getInvalidReadConcernValues() as $value) {
$options[][] = ['readConcern' => $value];
}
foreach ($this->getInvalidReadPreferenceValues() as $value) {
$options[][] = ['readPreference' => $value];
}
foreach ($this->getInvalidArrayValues() as $value) {
$options[][] = ['typeMap' => $value];
}
foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
return $options;
}
public function testGetManager()
{
$this->assertSame($this->manager, $this->collection->getManager());
}
public function testToString()
{
$this->assertEquals($this->getNamespace(), (string) $this->collection);
}
public function getGetCollectionName()
{
$this->assertEquals($this->getCollectionName(), $this->collection->getCollectionName());
}
public function getGetDatabaseName()
{
$this->assertEquals($this->getDatabaseName(), $this->collection->getDatabaseName());
}
public function testGetNamespace()
{
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
}
public function testDrop()
{
$writeResult = $this->collection->insertOne(['x' => 1]);
$this->assertEquals(1, $writeResult->getInsertedCount());
$commandResult = $this->collection->drop();
$this->assertCommandSucceeded($commandResult);
$this->assertCollectionCount($this->getNamespace(), 0);
}
/**
* @expectedException MongoDB\Exception\InvalidArgumentException
* @todo Move this to a unit test once Manager can be mocked
*/
public function testDropIndexShouldNotAllowWildcardCharacter()
{
$this->collection->dropIndex('*');
}
public function testFindOne()
{
$this->createFixtures(5);
$filter = ['_id' => ['$lt' => 5]];
$options = [
'skip' => 1,
'sort' => ['x' => -1],
];
$expected = ['_id' => 3, 'x' => 33];
$this->assertSameDocument($expected, $this->collection->findOne($filter, $options));
}
public function testWithOptionsInheritsOptions()
{
$collectionOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'typeMap' => ['root' => 'array'],
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName(), $collectionOptions);
$clone = $collection->withOptions();
$debug = $clone->__debugInfo();
$this->assertSame($this->manager, $debug['manager']);
$this->assertSame($this->getDatabaseName(), $debug['databaseName']);
$this->assertSame($this->getCollectionName(), $debug['collectionName']);
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInternalType('array', $debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testWithOptionsPassesOptions()
{
$collectionOptions = [
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
'typeMap' => ['root' => 'array'],
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
];
$clone = $this->collection->withOptions($collectionOptions);
$debug = $clone->__debugInfo();
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
$this->assertInternalType('array', $debug['typeMap']);
$this->assertSame(['root' => 'array'], $debug['typeMap']);
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
}
public function testMapReduce()
{
$this->createFixtures(3);
$map = new Javascript('function() { emit(1, this.x); }');
$reduce = new Javascript('function(key, values) { return Array.sum(values); }');
$out = ['inline' => 1];
$result = $this->collection->mapReduce($map, $reduce, $out);
$this->assertInstanceOf('MongoDB\MapReduceResult', $result);
$expected = [
[ '_id' => 1.0, 'value' => 66.0 ],
];
$this->assertSameDocuments($expected, $result);
$this->assertGreaterThanOrEqual(0, $result->getExecutionTimeMS());
$this->assertNotEmpty($result->getCounts());
$this->assertNotEmpty($result->getTiming());
}
/**
* Create data fixtures.
*
* @param integer $n
*/
private function createFixtures($n)
{
$bulkWrite = new BulkWrite(['ordered' => true]);
for ($i = 1; $i <= $n; $i++) {
$bulkWrite->insert([
'_id' => $i,
'x' => (integer) ($i . $i),
]);
}
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
$this->assertEquals($n, $result->getInsertedCount());
}
}