Skip to content

Commit 0f63c9b

Browse files
committed
Add mock Cursor implementation
1 parent 7dc9d50 commit 0f63c9b

8 files changed

Lines changed: 85 additions & 6 deletions

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@
4141
->ignoreVCSIgnored(true)
4242
->ignoreDotFiles(false)
4343
->notPath('rector.php')
44+
->notPath('test/unit/mongo/mocks')
4445
);

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ parameters:
88
paths:
99
- src
1010
- test
11+
excludePaths:
12+
- test/unit/mongo/mocks

test/unit/mongo/MongoTripodSearchIndexerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Tripod\Mongo\MongoGraph;
1515
use Tripod\Mongo\MongoSearchProvider;
1616
use Tripod\Mongo\Updates;
17+
use Tripod\Test\Mongo\Mocks\Cursor as FakeCursor;
1718

1819
class MongoTripodSearchIndexerTest extends MongoTripodTestBase
1920
{
@@ -508,7 +509,7 @@ public function testBatchSearchDocumentsGeneration(): void
508509
$docs[] = ['_id' => ['r' => 'tenantLists:batch' . $i, 'c' => 'tenantContexts:DefaultGraph']];
509510
}
510511

511-
$fakeCursor = new ArrayIterator($docs);
512+
$fakeCursor = new FakeCursor($docs);
512513
$configInstance = $this->getMockBuilder(TripodTestConfig::class)
513514
->onlyMethods(['getCollectionForCBD'])
514515
->disableOriginalConstructor()

test/unit/mongo/MongoTripodTablesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Tripod\Mongo\MongoGraph;
1919
use Tripod\Mongo\TransactionLog;
2020
use Tripod\Mongo\Updates;
21+
use Tripod\Test\Mongo\Mocks\Cursor as FakeCursor;
2122

2223
class MongoTripodTablesTest extends MongoTripodTestBase
2324
{
@@ -290,7 +291,7 @@ public function testBatchTableRowGeneration(): void
290291
$docs[] = ['_id' => ['r' => 'tenantLists:batch' . $i, 'c' => 'tenantContexts:DefaultGraph']];
291292
}
292293

293-
$fakeCursor = new ArrayIterator($docs);
294+
$fakeCursor = new FakeCursor($docs);
294295
$configInstance = $this->getMockBuilder(TripodTestConfig::class)
295296
->onlyMethods(['getCollectionForTable', 'getCollectionForCBD'])
296297
->disableOriginalConstructor()

test/unit/mongo/MongoTripodViewsTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Tripod\Mongo\MongoGraph;
1919
use Tripod\Mongo\TransactionLog;
2020
use Tripod\Mongo\Updates;
21+
use Tripod\Test\Mongo\Mocks\Cursor as FakeCursor;
2122

2223
class MongoTripodViewsTest extends MongoTripodTestBase
2324
{
@@ -1970,7 +1971,8 @@ public function testCursorNoExceptions(): void
19701971
->setConstructorArgs([new Manager(), 'db', 'view'])
19711972
->onlyMethods(['find'])
19721973
->getMock();
1973-
$mockCursor = $this->getMockBuilder(ArrayIterator::class)
1974+
$mockCursor = $this->getMockBuilder(FakeCursor::class)
1975+
->setConstructorArgs([[]])
19741976
->onlyMethods(['rewind'])
19751977
->getMock();
19761978

@@ -2029,7 +2031,8 @@ public function testCursorExceptionThrown(): void
20292031
->setConstructorArgs([new Manager(), 'db', 'view'])
20302032
->onlyMethods(['findOne', 'find'])
20312033
->getMock();
2032-
$mockCursor = $this->getMockBuilder(ArrayIterator::class)
2034+
$mockCursor = $this->getMockBuilder(FakeCursor::class)
2035+
->setConstructorArgs([[]])
20332036
->onlyMethods(['rewind'])
20342037
->getMock();
20352038

@@ -2089,7 +2092,8 @@ public function testCursorNoExceptionThrownWhenCursorThrowsSomeExceptions(): voi
20892092
->setConstructorArgs([new Manager(), 'db', 'view'])
20902093
->onlyMethods(['find', 'findOne'])
20912094
->getMock();
2092-
$mockCursor = $this->getMockBuilder(ArrayIterator::class)
2095+
$mockCursor = $this->getMockBuilder(FakeCursor::class)
2096+
->setConstructorArgs([[]])
20932097
->onlyMethods(['rewind'])
20942098
->getMock();
20952099

@@ -2276,7 +2280,7 @@ public function testBatchViewGeneration(): void
22762280
$docs[] = ['_id' => ['r' => 'tenantLists:batch' . $i, 'c' => 'tenantContexts:DefaultGraph']];
22772281
}
22782282

2279-
$fakeCursor = new ArrayIterator($docs);
2283+
$fakeCursor = new FakeCursor($docs);
22802284
$configInstance = $this->getMockBuilder(TripodTestConfig::class)
22812285
->onlyMethods(['getCollectionForView', 'getCollectionForCBD'])
22822286
->disableOriginalConstructor()

test/unit/mongo/mocks/Cursor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tripod\Test\Mongo\Mocks;
4+
5+
if (PHP_VERSION_ID >= 80000) {
6+
require_once __DIR__ . '/CursorPhp80.php';
7+
class Cursor extends CursorPhp80 {}
8+
} else {
9+
require_once __DIR__ . '/CursorPhp74.php';
10+
class Cursor extends CursorPhp74 {}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Tripod\Test\Mongo\Mocks;
4+
5+
class CursorPhp74 extends \ArrayIterator {}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tripod\Test\Mongo\Mocks;
4+
5+
use MongoDB\BSON\Int64;
6+
use MongoDB\Driver\CursorInterface;
7+
use MongoDB\Driver\Manager;
8+
use MongoDB\Driver\Server;
9+
10+
class CursorPhp80 extends \ArrayIterator implements CursorInterface
11+
{
12+
private array $array;
13+
14+
public function __construct(array $array)
15+
{
16+
parent::__construct($array);
17+
$this->array = $array;
18+
}
19+
20+
public function getId(): Int64
21+
{
22+
return new Int64(0);
23+
}
24+
25+
public function getServer(): Server
26+
{
27+
return (new Manager())->getServers()[0];
28+
}
29+
30+
public function isDead(): bool
31+
{
32+
return false;
33+
}
34+
35+
public function setTypeMap(array $typemap): void
36+
{
37+
// noop
38+
}
39+
40+
public function toArray(): array
41+
{
42+
return $this->array;
43+
}
44+
45+
public function key(): ?int
46+
{
47+
return parent::key();
48+
}
49+
50+
public function current(): array|object|null
51+
{
52+
return parent::current();
53+
}
54+
}

0 commit comments

Comments
 (0)