Skip to content

Commit 9925bbd

Browse files
committed
Trigger warning when calling CodecCursor::setTypeMap
1 parent 82a68e8 commit 9925bbd

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Model/CodecCursor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
use function assert;
2929
use function iterator_to_array;
30+
use function sprintf;
31+
use function trigger_error;
32+
33+
use const E_USER_WARNING;
3034

3135
/**
3236
* @template TValue of object
@@ -104,6 +108,7 @@ public function rewind(): void
104108
public function setTypeMap(array $typemap): void
105109
{
106110
// Not supported
111+
trigger_error(sprintf('Discarding type map for %s', __METHOD__), E_USER_WARNING);
107112
}
108113

109114
/** @return array<int, TValue> */
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Model;
4+
5+
use MongoDB\BSON\Document;
6+
use MongoDB\Codec\DocumentCodec;
7+
use MongoDB\Model\CodecCursor;
8+
use MongoDB\Tests\FunctionalTestCase;
9+
10+
class CodecCursorFunctionalTest extends FunctionalTestCase
11+
{
12+
public function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->dropCollection($this->getDatabaseName(), $this->getCollectionName());
17+
}
18+
19+
public function testSetTypeMap(): void
20+
{
21+
$collection = self::createTestClient()->selectCollection($this->getDatabaseName(), $this->getCollectionName());
22+
$cursor = $collection->find();
23+
24+
$codecCursor = CodecCursor::fromCursor($cursor, $this->createCodec());
25+
26+
$this->expectWarning();
27+
$this->expectWarningMessage('Discarding type map for MongoDB\Model\CodecCursor::setTypeMap');
28+
29+
$codecCursor->setTypeMap(['root' => 'array']);
30+
}
31+
32+
private function createCodec(): DocumentCodec
33+
{
34+
return new class implements DocumentCodec {
35+
public function canDecode($value): bool
36+
{
37+
return false;
38+
}
39+
40+
public function canEncode($value): bool
41+
{
42+
return false;
43+
}
44+
45+
public function decode($value): object
46+
{
47+
return (object) [];
48+
}
49+
50+
public function decodeIfSupported($value): object
51+
{
52+
return (object) [];
53+
}
54+
55+
public function encode($value): Document
56+
{
57+
return Document::fromPHP([]);
58+
}
59+
60+
public function encodeIfSupported($value): Document
61+
{
62+
return Document::fromPHP([]);
63+
}
64+
};
65+
}
66+
}

0 commit comments

Comments
 (0)