8000 Add stubs for mongodb library classes · symfony/symfony@402ca62 · GitHub
[go: up one dir, main page]

Skip to content

Commit 402ca62

Browse files
committed
Add stubs for mongodb library classes
1 parent 50dc749 commit 402ca62

File tree

11 files changed

+141
-43
lines changed

11 files changed

+141
-43
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add `UriSigner` from the HttpKernel component
1010
* Add `partitioned` flag to `Cookie` (CHIPS Cookie)
1111
* Add argument `bool $flush = true` to `Response::send()`
12+
* `MongoDbSessionHandler` can be used with the mongodb extension only
1213

1314
6.3
1415
---

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use MongoDB\BSON\Binary;
1515
use MongoDB\BSON\UTCDateTime;
1616
use MongoDB\Client;
17-
use MongoDB\Collection;
1817
use MongoDB\Driver\BulkWrite;
1918
use MongoDB\Driver\Manager;
2019
use MongoDB\Driver\Query;
@@ -168,7 +167,7 @@ protected function doRead(#[\SensitiveParameter] string $sessionId): string
168167
'projection' => [
169168
'_id' => false,
170169
$this->options['data_field'] => true,
171-
]
170+
],
172171
]));
173172
$dbData->setTypeMap(['root' => 'object']);
174173
$dbData->rewind();
@@ -183,7 +182,7 @@ protected function doRead(#[\SensitiveParameter] string $sessionId): string
183182

184183
private function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime
185184
{
186-
$timestamp = (new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
185+
$timestamp = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
187186

188187
return new UTCDateTime(($timestamp->getTimestamp() + $additionalSeconds) * 1000);
189188
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace MongoDB;
13+
14+
use MongoDB\Driver\Manager;
15+
16+
/*
17+
* Stubs for the mongodb/mongodb library version ~1.16
18+
*/
19+
if (!class_exists(Client::class)) {
20+
abstract class Client
21+
{
22+
abstract public function getManager(): Manager;
23+
}
24+
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,17 @@ protected function setUp(): void
6262
$this->storage = new MongoDbSessionHandler($this->manager, $this->options);
6363
}
6464

65-
public function testCreateFromClient(): void
65+
public function testCreateFromClient()
6666
{
67-
if (!class_exists(Client::class)) {
68-
$this->markTestSkipped('The "mongodb/mongodb" library is required.');
69-
}
70-
71-
$client = new Client('mongodb://'.getenv('MONGODB_HOST'));
67+
$client = $this->createMock(Client::class);
68+
$client->expects($this->once())
69+
->method('getManager')
70+
->willReturn($this->manager);
7271

7372
$this->storage = new MongoDbSessionHandler($client, $this->options);
7473
$this->storage->write('foo', 'bar');
7574

76-
$this->assertSame(1, $client->selectCollection('sf-test', 'session-test')->countDocuments());
75+
$this->assertCount(1, $this->getSessions());
7776
}
7877

7978
protected function tearDown(): void
@@ -82,7 +81,7 @@ protected function tearDown(): void
8281
}
8382

8483
/** @dataProvider provideInvalidOptions */
85-
public function testConstructorShouldThrowExceptionForMissingOptions(array $options): void
84+
public function testConstructorShouldThrowExceptionForMissingOptions(array $options)
8685
{
8786
$this->expectException(\InvalidArgumentException::class);
8887
new MongoDbSessionHandler($this->manager, $options);
@@ -95,35 +94,35 @@ public function provideInvalidOptions()
9594
yield 'database missing' => [['collection' => 'foo']];
9695
}
9796

98-
public function testOpenMethodAlwaysReturnTrue(): void
97+
public function testOpenMethodAlwaysReturnTrue()
9998
{
10099
$this->assertTrue($this->storage->open('test', 'test'), 'The "open" method should always return true');
101100
}
102101

103-
public function testCloseMethodAlwaysReturnTrue(): void
102+
public function testCloseMethodAlwaysReturnTrue()
104103
{
105104
$this->assertTrue($this->storage->close(), 'The "close" method should always return true');
106105
}
107106

108-
public function testRead(): void
107+
public function testRead()
109108
{
110109
$this->insertSession('foo', 'bar', 0);
111110
$this->assertEquals('bar', $this->storage->read('foo'));
112111
}
113112

114-
public function testReadNotFound(): void
113+
public function testReadNotFound()
115114
{
116115
$this->insertSession('foo', 'bar', 0);
117116
$this->assertEquals('', $this->storage->read('foobar'));
118117
}
119118

120-
public function testReadExpired(): void
119+
public function testReadExpired()
121120
{
122121
$this->insertSession('foo', 'bar', -100_000);
123122
$this->assertEquals('', $this->storage->read('foo'));
124123
}
125124

126-
public function testWrite(): void
125+
public function testWrite()
127126
{
128127
$expectedTime = (new \DateTimeImmutable())->getTimestamp();
129128
$expectedExpiry = $expectedTime + (int) \ini_get('session.gc_maxlifetime');
@@ -140,7 +139,7 @@ public function testWrite(): void
140139
$this->assertGreaterThanOrEqual($expectedExpiry, round((string) $sessions[0]->expires_at / 1000));
141140
}
142141

143-
public function testReplaceSessionData(): void
142+
public function testReplaceSessionData()
144143
{
145144
$this->storage->write('foo', 'bar');
146145
$this->storage->write('baz', 'qux');
@@ -151,7 +150,7 @@ public function testReplaceSessionData(): void
151150
$this->assertEquals('foobar', $sessions[0]->data->getData());
152151
}
153152

154-
public function testDestroy(): void
153+
public function testDestroy()
155154
{
156155
$this->st F438 orage->write('foo', 'bar');
157156
$this->storage->write('baz', 'qux');
@@ -163,7 +162,7 @@ public function testDestroy(): void
163162
$this->assertEquals('baz', $sessions[0]->_id);
164163
}
165164

166-
public function testGc(): void
165+
public function testGc()
167166
{
168167
$this->insertSession('foo', 'bar', -100_000);
169168
$this->insertSession('bar', 'bar', -100_000);

src/Symfony/Component/HttpFoundation/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@
4040
"/Tests/"
4141
]
4242
},
43+
"autoload-dev": {
44+
"files": ["Tests/Resources/stubs/mongodb.php"]
45+
},
4346
"minimum-stability": "dev"
4447
}

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* `MongoDbStore` can be used with the mongodb extension only
8+
49
6.3
510
---
611

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
use MongoDB\BSON\UTCDateTime;
1515
use MongoDB\Client;
1616
use MongoDB\Collection;
17+
use MongoDB\Database;
1718
use MongoDB\Driver\BulkWrite;
1819
use MongoDB\Driver\Command;
1920
use MongoDB\Driver\Exception\WriteException;
2021
use MongoDB\Driver\Manager;
2122
use MongoDB\Driver\Query;
22-
use MongoDB\Driver\ReadPreference;
2323
use MongoDB\Exception\DriverRuntimeException;
2424
use MongoDB\Exception\InvalidArgumentException as MongoInvalidArgumentException;
2525
use MongoDB\Exception\UnsupportedException;
@@ -92,7 +92,7 @@ class MongoDbStore implements PersistingStoreInterface
9292
* readPreference is primary for all queries.
9393
* @see https://docs.mongodb.com/manual/applications/replication/
9494
*/
95-
public function __construct(Collection|Client|Manager|string $mongo, array $options = [], float $initialTtl = 300.0)
95+
public function __construct(Collection|Database|Client|Manager|string $mongo, array $options = [], float $initialTtl = 300.0)
9696
{
9797
if (isset($options['gcProbablity'])) {
9898
trigger_deprecation('symfony/lock', '6.3', 'The "gcProbablity" option (notice the typo in its name) is deprecated in "%s"; use the "gcProbability" option instead.', __CLASS__);
@@ -112,8 +112,11 @@ public function __construct(Collection|Client|Manager|string $mongo, array $opti
112112
$this->initialTtl = $initialTtl;
113113

114114
if ($mongo instanceof Collection) {
115-
$this->options['database'] = $mongo->getDatabaseName();
116-
$this->options['collection'] = $mongo->getCollectionName();
115+
$this->options['database'] ??= $mongo->getDatabaseName();
116+
$this->options['collection'] ??= $mongo->getCollectionName();
117+
$this->manager = $mongo->getManager();
118+
} elseif ($mongo instanceof Database) {
119+
$this->options['database'] ??= $mongo->getDatabaseName();
117120
$this->manager = $mongo->getManager();
118121
} elseif ($mongo instanceof Client) {
119122
$this->manager = $mongo->getManager();
@@ -150,7 +153,7 @@ public function __construct(Collection|Client|Manager|string $mongo, array $opti
150153
private function skimUri(string $uri): string
151154
{
152155
if (!str_starts_with($uri, 'mongodb://') && !str_starts_with($uri, 'mongodb+srv://')) {
153-
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid. Expecting "mongodb://" or "mongodb+srv://"', $uri));
156+
throw new InvalidArgumentException(sprintf('The given MongoDB Connection URI "%s" is invalid. Expecting "mongodb://" or "mongodb+srv://".', $uri));
154157
}
155158

156159
if (false === $parsedUrl = parse_url($uri)) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace MongoDB;
13+
14+
use MongoDB\Driver\Manager;
15+
16+
/*
17+
* Stubs for the mongodb/mongodb library version ~1.16
18+
*/
19+
if (!class_exists(Client::class)) {
20+
abstract class Client
21+
{
22+
abstract public function getManager(): Manager;
23+
}
24+
}
25+
26+
if (!class_exists(Database::class)) {
27+
abstract class Database
28+
{
29+
abstract public function getManager(): Manager;
30+
31+
abstract public function getDatabaseName(): string;
32+
}
33+
}
34+
35+
if (!class_exists(Collection::class)) {
36+
abstract class Collection
37+
{
38+
abstract public function getManager(): Manager;
39+
40+
abstract public function getCollectionName(): string;
41+
42+
abstract public function getDatabaseName(): string;
43+
}
44+
}

src/Symfony/Component/Lock/Tests/Store/MongoDbStoreFactoryTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class MongoDbStoreFactoryTest extends TestCase
2626
{
2727
public function testCreateMongoDbCollectionStore()
2828
{
29-
if (!class_exists(Collection::class)) {
30-
$this->markTestSkipped('The "mongodb/mongodb" library is required.');
31-
}
32-
3329
$collection = $this->createMock(Collection::class);
3430
$collection->expects($this->once())
3531
->method('getManager')

src/Symfony/Component/Lock/Tests/Store/MongoDbStoreTest.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Lock\Tests\Store;
1313

1414
use MongoDB\Client;
15+
use MongoDB\Collection;
16+
use MongoDB\Database;
1517
use MongoDB\Driver\Command;
1618
use MongoDB\Driver\Exception\ConnectionTimeoutException;
1719
use MongoDB\Driver\Manager;
@@ -93,30 +95,49 @@ public function testConstructionMethods($mongo, array $options)
9395

9496
public static function provideConstructorArgs()
9597
{
96-
$manager = self::getMongoManager();
97-
yield [$manager, ['database' => 'test', 'collection' => 'lock']];
98+
yield [self::getMongoManager(), ['database' => 'test', 'collection' => 'lock']];
9899
yield ['mongodb://localhost/test?collection=lock', []];
99100
yield ['mongodb://localhost/test', ['collection' => 'lock']];
100101
yield ['mongodb://localhost/', ['database' => 'test', 'collection' => 'lock']];
101102
}
102103

103-
/**
104-
* @dataProvider provideConstructorArgsWithLibrary
105-
*/
106-
public function testConstructionMethodsWithLibrary($mongo, array $options)
104+
public function testConstructWithClient()
107105
{
108-
$this->testConstructionMethods($mongo, $options);
106+
$client = $this->createMock(Client::class);
107+
$client->expects($this->once())
108+
->method('getManager')
109+
->willReturn(self::getMongoManager());
110+
111+
$this->testConstructionMethods($client, ['database' => 'test', 'collection' => 'lock']);
109112
}
110113

111-
public static function provideConstructorArgsWithLibrary()
114+
public function testConstructWithDatabase()
112115
{
113-
if (!class_exists(Client::class)) {
114-
self::markTestSkipped('The "mongodb/mongodb" library is required.');
115-
}
116+
$database = $this->createMock(Database::class);
117+
$database->expects($this->once())
118+
->method('getManager')
119+
->willReturn(self::getMongoManager());
120+
$database->expects($this->once())
121+
->method('getDatabaseName')
122+
->willReturn('test');
123+
124+
$this->testConstructionMethods($database, ['collection' => 'lock']);
125+
}
116126

117-
$client = new Client('mongodb://localhost');
118-
yield [$client, ['database' => 'test', 'collection' => 'lock']];
119-
yield [$client->selectCollection('test', 'lock'), []];
127+
public function testConstructWithCollection()
128+
{
129+
$collection = $this->createMock(Collection::class);
130+
$collection->expects($this->once())
131+
->method('getManager')
132+
->willReturn(self::getMongoManager());
133+
$collection->expects($this->once())
134+
->method('getDatabaseName')
135+
->willReturn('test');
136+
$collection->expects($this->once())
137+
->method('getCollectionName')
138+
->willReturn('lock');
139+
140+
$this->testConstructionMethods($collection, []);
120141
}
121142

122143
public function testUriPrecedence()

src/Symfony/Component/Lock/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
"/Tests/"
3535
]
3636
},
37+
"autoload-dev": {
38+
"files": ["Tests/Resources/stubs/mongodb.php"]
39+
},
3740
"minimum-stability": "dev"
3841
}

0 commit comments

Comments
 (0)
0