Skip to content

Commit

Permalink
Trigger warning when calling CodecCursor::setTypeMap
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jul 31, 2023
1 parent 82a68e8 commit 9925bbd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Model/CodecCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

use function assert;
use function iterator_to_array;
use function sprintf;
use function trigger_error;

use const E_USER_WARNING;

/**
* @template TValue of object
Expand Down Expand Up @@ -104,6 +108,7 @@ public function rewind(): void
public function setTypeMap(array $typemap): void
{
// Not supported
trigger_error(sprintf('Discarding type map for %s', __METHOD__), E_USER_WARNING);
}

/** @return array<int, TValue> */
Expand Down
66 changes: 66 additions & 0 deletions tests/Model/CodecCursorFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace MongoDB\Tests\Model;

use MongoDB\BSON\Document;
use MongoDB\Codec\DocumentCodec;
use MongoDB\Model\CodecCursor;
use MongoDB\Tests\FunctionalTestCase;

class CodecCursorFunctionalTest extends FunctionalTestCase
{
public function setUp(): void
{
parent::setUp();

$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
}

public function testSetTypeMap(): void
{
$collection = self::createTestClient()->selectCollection($this->getDatabaseName(), $this->getCollectionName());
$cursor = $collection->find();

$codecCursor = CodecCursor::fromCursor($cursor, $this->createCodec());

$this->expectWarning();
$this->expectWarningMessage('Discarding type map for MongoDB\Model\CodecCursor::setTypeMap');

$codecCursor->setTypeMap(['root' => 'array']);
}

private function createCodec(): DocumentCodec
{
return new class implements DocumentCodec {
public function canDecode($value): bool
{
return false;
}

public function canEncode($value): bool
{
return false;
}

public function decode($value): object
{
return (object) [];
}

public function decodeIfSupported($value): object
{
return (object) [];
}

public function encode($value): Document
{
return Document::fromPHP([]);
}

public function encodeIfSupported($value): Document
{
return Document::fromPHP([]);
}
};
}
}

0 comments on commit 9925bbd

Please sign in to comment.