Skip to content

Commit 1685ebc

Browse files
authored
PHPLIB-675 Add helpers to check collection types to CollectionInfo (#839)
* PHPLIB-675 Expose additional methods to access collection info * Use arrayNotHasKey over isset in tests * Add version information to deprecation messages
1 parent 903e101 commit 1685ebc

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/Model/CollectionInfo.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function __debugInfo()
6060
/**
6161
* Return the maximum number of documents to keep in the capped collection.
6262
*
63+
* @deprecated 1.0 Deprecated in favor of using getOptions
64+
*
6365
* @return integer|null
6466
*/
6567
public function getCappedMax()
@@ -71,6 +73,8 @@ public function getCappedMax()
7173
/**
7274
* Return the maximum size (in bytes) of the capped collection.
7375
*
76+
* @deprecated 1.0 Deprecated in favor of using getOptions
77+
*
7478
* @return integer|null
7579
*/
7680
public function getCappedSize()
@@ -79,9 +83,31 @@ public function getCappedSize()
7983
return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null;
8084
}
8185

86+
/**
87+
* Return information about the _id index for the collection.
88+
*
89+
* @return array
90+
*/
91+
public function getIdIndex(): array
92+
{
93+
return (array) ($this->info['idIndex'] ?? []);
94+
}
95+
96+
/**
97+
* Return the "info" property of the server response.
98+
*
99+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
100+
* @return array
101+
*/
102+
public function getInfo(): array
103+
{
104+
return (array) ($this->info['info'] ?? []);
105+
}
106+
82107
/**
83108
* Return the collection name.
84109
*
110+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
85111
* @return string
86112
*/
87113
public function getName()
@@ -92,16 +118,30 @@ public function getName()
92118
/**
93119
* Return the collection options.
94120
*
121+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
95122
* @return array
96123
*/
97124
public function getOptions()
98125
{
99-
return isset($this->info['options']) ? (array) $this->info['options'] : [];
126+
return (array) ($this->info['options'] ?? []);
127+
}
128+
129+
/**
130+
* Return the collection type.
131+
*
132+
* @see https://docs.mongodb.com/manual/reference/command/listCollections/#output
133+
* @return string
134+
*/
135+
public function getType(): string
136+
{
137+
return (string) $this->info['type'];
100138
}
101139

102140
/**
103141
* Return whether the collection is a capped collection.
104142
*
143+
* @deprecated 1.0 Deprecated in favor of using getOptions
144+
*
105145
* @return boolean
106146
*/
107147
public function isCapped()

tests/Model/CollectionInfoTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,47 @@
88

99
class CollectionInfoTest extends TestCase
1010
{
11-
public function testGetName(): void
11+
public function testGetBasicInformation(): void
1212
{
13-
$info = new CollectionInfo(['name' => 'foo']);
13+
$info = new CollectionInfo([
14+
'name' => 'foo',
15+
'type' => 'view',
16+
'options' => ['capped' => true, 'size' => 1048576],
17+
'info' => ['readOnly' => true],
18+
'idIndex' => ['idIndex' => true], // Dummy option
19+
]);
20+
1421
$this->assertSame('foo', $info->getName());
22+
$this->assertSame('foo', $info['name']);
23+
24+
$this->assertSame('view', $info->getType());
25+
$this->assertSame('view', $info['type']);
26+
27+
$this->assertSame(['capped' => true, 'size' => 1048576], $info->getOptions());
28+
$this->assertSame(['capped' => true, 'size' => 1048576], $info['options']);
29+
30+
$this->assertSame(['readOnly' => true], $info->getInfo());
31+
$this->assertSame(['readOnly' => true], $info['info']);
32+
33+
$this->assertSame(['idIndex' => true], $info->getIdIndex());
34+
$this->assertSame(['idIndex' => true], $info['idIndex']);
1535
}
1636

17-
public function testGetOptions(): void
37+
public function testMissingFields(): void
1838
{
19-
$info = new CollectionInfo(['name' => 'foo']);
39+
$info = new CollectionInfo([
40+
'name' => 'foo',
41+
'type' => 'view',
42+
]);
43+
2044
$this->assertSame([], $info->getOptions());
45+
$this->assertArrayNotHasKey('options', $info);
2146

22-
$info = new CollectionInfo(['name' => 'foo', 'options' => ['capped' => true, 'size' => 1048576]]);
23-
$this->assertSame(['capped' => true, 'size' => 1048576], $info->getOptions());
47+
$this->assertSame([], $info->getInfo());
48+
$this->assertArrayNotHasKey('info', $info);
49+
50+
$this->assertSame([], $info->getIdIndex());
51+
$this->assertArrayNotHasKey('idIndex', $info);
2452
}
2553

2654
public function testCappedCollectionMethods(): void

0 commit comments

Comments
 (0)