Skip to content

Commit

Permalink
PHPC-1489: Deprecate integer readPreference constants (#1604)
Browse files Browse the repository at this point in the history
* PHPC-1489: Deprecate integer readPreference constants

* Add tests for creating ReadPreference with integer values
  • Loading branch information
alcaeus authored Jul 8, 2024
1 parent 0daf917 commit 164a56d
Show file tree
Hide file tree
Showing 74 changed files with 377 additions and 185 deletions.
2 changes: 2 additions & 0 deletions src/MongoDB/ReadPreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, __construct)
PHONGO_PARSE_PARAMETERS_END();

if (Z_TYPE_P(mode) == IS_LONG) {
php_error_docref(NULL, E_DEPRECATED, "Passing an integer mode to \"MongoDB\\Driver\\ReadPreference::__construct\" is deprecated and will be removed in a future release.");

switch (Z_LVAL_P(mode)) {
case MONGOC_READ_PRIMARY:
case MONGOC_READ_SECONDARY:
Expand Down
6 changes: 6 additions & 0 deletions src/MongoDB/ReadPreference.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@ final class ReadPreference implements \MongoDB\BSON\Serializable, \Serializable
/**
* @var int
* @cvalue MONGOC_READ_PRIMARY
* @deprecated
*/
public const RP_PRIMARY = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_PRIMARY_PREFERRED
* @deprecated
*/
public const RP_PRIMARY_PREFERRED = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_SECONDARY
* @deprecated
*/
public const RP_SECONDARY = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_SECONDARY_PREFERRED
* @deprecated
*/
public const RP_SECONDARY_PREFERRED = UNKNOWN;

/**
* @var int
* @cvalue MONGOC_READ_NEAREST
* @deprecated
*/
public const RP_NEAREST = UNKNOWN;

Expand Down Expand Up @@ -92,6 +97,7 @@ final public function getHedge(): ?object {}

final public function getMaxStalenessSeconds(): int {}

/** @deprecated */
final public function getMode(): int {}

final public function getModeString(): string {}
Expand Down
14 changes: 7 additions & 7 deletions src/MongoDB/ReadPreference_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/connect/bug1015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PHPC-1015: Initial DNS Seedlist test
require_once __DIR__ . "/../utils/basic.inc";

$m = create_test_manager("mongodb+srv://test1.test.build.10gen.cc/");
$s = $m->selectServer( new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_NEAREST ) );
$s = $m->selectServer( new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::NEAREST ) );
$servers = $m->getServers();

foreach ( $servers as $server )
Expand Down
4 changes: 2 additions & 2 deletions tests/manager/manager-ctor-duplicate-option-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ MongoDB\Driver\Manager::__construct() with duplicate read preference option

$manager = new MongoDB\Driver\Manager(null, ['readPreference' => 'primary', 'readpreference' => 'secondary']);

echo $manager->getReadPreference()->getMode(), "\n";
echo $manager->getReadPreference()->getModeString(), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
2
secondary
===DONE===
4 changes: 2 additions & 2 deletions tests/manager/manager-executeCommand-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require_once __DIR__ . "/../utils/basic.inc";

$manager = create_test_manager();

$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY);

echo "Testing primary:\n";
$command = new MongoDB\Driver\Command(['ping' => 1]);
Expand Down
4 changes: 2 additions & 2 deletions tests/manager/manager-executeCommand-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require_once __DIR__ . "/../utils/basic.inc";

$manager = create_test_manager();

$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY);

echo "Testing primary:\n";
$command = new MongoDB\Driver\Command(['ping' => 1]);
Expand Down
2 changes: 1 addition & 1 deletion tests/manager/manager-executeCommand-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $manager = create_test_manager();
DATABASE_NAME,
$command,
[
'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_SECONDARY),
'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::SECONDARY),
'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::LOCAL),
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY),
]
Expand Down
4 changes: 2 additions & 2 deletions tests/manager/manager-executeQuery-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ $bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(['_id' => 1, 'x' => 2, 'y' => 3]);
$manager->executeBulkWrite(NS, $bulk);

$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY);

echo "Testing primary:\n";
$query = new MongoDB\Driver\Query(['x' => 3], ['projection' => ['y' => 1]]);
Expand Down
4 changes: 2 additions & 2 deletions tests/manager/manager-executeQuery-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ $bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(['_id' => 1, 'x' => 2, 'y' => 3]);
$manager->executeBulkWrite(NS, $bulk);

$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
$primary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);
$secondary = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY);

echo "Testing primary:\n";
$query = new MongoDB\Driver\Query(['x' => 3], ['projection' => ['y' => 1]]);
Expand Down
2 changes: 1 addition & 1 deletion tests/manager/manager-executeReadCommand-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $manager = create_test_manager();
DATABASE_NAME,
$command,
[
'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_SECONDARY),
'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::SECONDARY),
'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::MAJORITY),
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/manager/manager-selectserver_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MongoDB\Driver\Manager::selectServer() should not issue warning before exception
<?php
require_once __DIR__ . "/../utils/basic.inc";

$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY);

// Invalid host cannot be resolved
$manager = create_test_manager('mongodb://example.invalid:27017', ['serverSelectionTimeoutMS' => 1]);
Expand Down
8 changes: 4 additions & 4 deletions tests/manager/manager-startSession_error-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ $options = [
[ 'readPreference' => new \MongoDB\Driver\ReadConcern( \MongoDB\Driver\ReadConcern::LOCAL ) ],
[ 'writeConcern' => 42 ],
[ 'writeConcern' => new stdClass ],
[ 'writeConcern' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::RP_SECONDARY ) ],
[ 'writeConcern' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::SECONDARY ) ],

[
'readConcern' => new \MongoDB\Driver\ReadConcern( \MongoDB\Driver\ReadConcern::LOCAL ),
'readPreference' => new \MongoDB\Driver\ReadConcern( \MongoDB\Driver\ReadConcern::LOCAL ),
],
[
'readConcern' => new \MongoDB\Driver\ReadConcern( \MongoDB\Driver\ReadConcern::LOCAL ),
'writeConcern' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::RP_SECONDARY ),
'writeConcern' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::SECONDARY ),
],
[
'readPreference' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::RP_SECONDARY ),
'writeConcern' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::RP_SECONDARY ),
'readPreference' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::SECONDARY ),
'writeConcern' => new \MongoDB\Driver\ReadPreference( \MongoDB\Driver\ReadPreference::SECONDARY ),
],

42,
Expand Down
14 changes: 7 additions & 7 deletions tests/readPreference/bug0146-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ $bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(array('my' => 'document'));
$manager->executeBulkWrite(NS, $bulk);

$rps = array(
MongoDB\Driver\ReadPreference::RP_PRIMARY,
MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED,
MongoDB\Driver\ReadPreference::RP_SECONDARY,
MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED,
MongoDB\Driver\ReadPreference::RP_NEAREST,
);
$rps = [
MongoDB\Driver\ReadPreference::PRIMARY,
MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED,
MongoDB\Driver\ReadPreference::SECONDARY,
MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED,
MongoDB\Driver\ReadPreference::NEAREST,
];

foreach($rps as $r) {
$rp = new MongoDB\Driver\ReadPreference($r);
Expand Down
2 changes: 1 addition & 1 deletion tests/readPreference/bug0851-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $tagSets = [
[],
];

$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED, $tagSets);
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED, $tagSets);
var_dump($tagSets);

/* Dump the Manager's ReadPreference to ensure that each element in the $tagSets
Expand Down
18 changes: 9 additions & 9 deletions tests/readPreference/readpreference-bsonserialize-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ MongoDB\Driver\ReadPreference::bsonSerialize()
require_once __DIR__ . '/../utils/basic.inc';

$tests = [
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_NEAREST),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY, []),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, [['dc' => 'ny']]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, [['dc' => 'ny'], ['dc' => 'sf', 'use' => 'reporting'], []]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, null, ['maxStalenessSeconds' => 1000]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::NEAREST),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY, []),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, [['dc' => 'ny']]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, [['dc' => 'ny'], ['dc' => 'sf', 'use' => 'reporting'], []]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, null, ['maxStalenessSeconds' => 1000]),
];

foreach ($tests as $test) {
Expand Down
18 changes: 9 additions & 9 deletions tests/readPreference/readpreference-bsonserialize-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ MongoDB\Driver\ReadPreference::bsonSerialize() returns an object
require_once __DIR__ . '/../utils/basic.inc';

$tests = [
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_NEAREST),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY, []),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, [['dc' => 'ny']]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, [['dc' => 'ny'], ['dc' => 'sf', 'use' => 'reporting'], []]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY, null, ['maxStalenessSeconds' => 1000]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::NEAREST),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::PRIMARY, []),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, [['dc' => 'ny']]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, [['dc' => 'ny'], ['dc' => 'sf', 'use' => 'reporting'], []]),
new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::SECONDARY, null, ['maxStalenessSeconds' => 1000]),
];

foreach ($tests as $test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,24 @@ MongoDB\Driver\ReadPreference constants
--FILE--
<?php

var_dump(MongoDB\Driver\ReadPreference::RP_PRIMARY);
var_dump(MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED);
var_dump(MongoDB\Driver\ReadPreference::RP_SECONDARY);
var_dump(MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED);
var_dump(MongoDB\Driver\ReadPreference::RP_NEAREST);
var_dump(MongoDB\Driver\ReadPreference::NO_MAX_STALENESS);
var_dump(MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS);

var_dump(MongoDB\Driver\ReadPreference::PRIMARY);
var_dump(MongoDB\Driver\ReadPreference::PRIMARY_PREFERRED);
var_dump(MongoDB\Driver\ReadPreference::SECONDARY);
var_dump(MongoDB\Driver\ReadPreference::SECONDARY_PREFERRED);
var_dump(MongoDB\Driver\ReadPreference::NEAREST);

var_dump(MongoDB\Driver\ReadPreference::NO_MAX_STALENESS);
var_dump(MongoDB\Driver\ReadPreference::SMALLEST_MAX_STALENESS_SECONDS);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
int(1)
int(5)
int(2)
int(6)
int(10)
int(-1)
int(90)
string(7) "primary"
string(16) "primaryPreferred"
string(9) "secondary"
string(18) "secondaryPreferred"
string(7) "nearest"
int(-1)
int(90)
===DONE===
33 changes: 33 additions & 0 deletions tests/readPreference/readpreference-constants-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
MongoDB\Driver\ReadPreference deprecated constants
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_php_version('<', '8.3.0'); ?>
--FILE--
<?php

var_dump(MongoDB\Driver\ReadPreference::RP_PRIMARY);
var_dump(MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED);
var_dump(MongoDB\Driver\ReadPreference::RP_SECONDARY);
var_dump(MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED);
var_dump(MongoDB\Driver\ReadPreference::RP_NEAREST);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: Constant MongoDB\Driver\ReadPreference::RP_PRIMARY is deprecated in %s
int(1)

Deprecated: Constant MongoDB\Driver\ReadPreference::RP_PRIMARY_PREFERRED is deprecated in %s
int(5)

Deprecated: Constant MongoDB\Driver\ReadPreference::RP_SECONDARY is deprecated in %s
int(2)

Deprecated: Constant MongoDB\Driver\ReadPreference::RP_SECONDARY_PREFERRED is deprecated in %s
int(6)

Deprecated: Constant MongoDB\Driver\ReadPreference::RP_NEAREST is deprecated in %s
int(10)
===DONE===
Loading

0 comments on commit 164a56d

Please sign in to comment.