<?php
 namespace MongoDB\Tests\Operation;
 use MongoDB\Driver\BulkWrite;
 use MongoDB\Operation\Distinct;
 use MongoDB\Tests\CommandObserver;
 use function is_scalar;
 use function json_encode;
 use function usort;
 use function version_compare;
 class DistinctFunctionalTest extends FunctionalTestCase {
 public function testDefaultReadConcernIsOmitted() {
 (new CommandObserver())->observe( function () {
 $operation = new Distinct( $this->getDatabaseName(), $this->getCollectionName(), 'x', [], ['readConcern' => $this->createDefaultReadConcern()] );
 $operation->execute($this->getPrimaryServer());
 }
, function (array $event) {
 $this->assertObjectNotHasAttribute('readConcern', $event['started']->getCommand());
 }
 );
 }
 public function testSessionOption() {
 if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
 $this->markTestSkipped('Sessions are not supported');
 }
 (new CommandObserver())->observe( function () {
 $operation = new Distinct( $this->getDatabaseName(), $this->getCollectionName(), 'x', [], ['session' => $this->createSession()] );
 $operation->execute($this->getPrimaryServer());
 }
, function (array $event) {
 $this->assertObjectHasAttribute('lsid', $event['started']->getCommand());
 }
 );
 }
 public function testTypeMapOption(array $typeMap, array $expectedDocuments) {
 $bulkWrite = new BulkWrite(['ordered' => true]);
 $bulkWrite->insert([ 'x' => (object) ['foo' => 'bar'], ]);
 $bulkWrite->insert(['x' => 4]);
 $bulkWrite->insert([ 'x' => (object) ['foo' => ['foo' => 'bar']], ]);
 $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
 $distinct = new Distinct($this->getDatabaseName(), $this->getCollectionName(), 'x', [], ['typeMap' => $typeMap]);
 $values = $distinct->execute($this->getPrimaryServer());
 $sort = function ($a, $b) {
 if (is_scalar($a) && ! is_scalar($b)) {
 return -1;
 }
 if (! is_scalar($a)) {
 if (is_scalar($b)) {
 return 1;
 }
 $a = json_encode($a);
 $b = json_encode($b);
 }
 return $a < $b ? -1 : 1;
 }
;
 usort($expectedDocuments, $sort);
 usort($values, $sort);
 $this->assertEquals($expectedDocuments, $values);
 }
 public function provideTypeMapOptionsAndExpectedDocuments() {
 return [ 'No type map' => [ ['root' => 'array', 'document' => 'array'], [ ['foo' => 'bar'], 4, ['foo' => ['foo' => 'bar']], ], ], 'array/array' => [ ['root' => 'array', 'document' => 'array'], [ ['foo' => 'bar'], 4, ['foo' => ['foo' => 'bar']], ], ], 'object/array' => [ ['root' => 'object', 'document' => 'array'], [ (object) ['foo' => 'bar'], 4, (object) ['foo' => ['foo' => 'bar']], ], ], 'array/stdClass' => [ ['root' => 'array', 'document' => 'stdClass'], [ ['foo' => 'bar'], 4, ['foo' => (object) ['foo' => 'bar']], ], ], ];
 }
 }
 