Skip to content

PHPLIB-1369 Upgrade to PHPUnit 10 #1412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if [ "${IS_MATRIX_TESTING}" = "true" ]; then
fi

# Enable verbose output to see skipped and incomplete tests
PHPUNIT_OPTS="${PHPUNIT_OPTS} -v --configuration phpunit.evergreen.xml"
PHPUNIT_OPTS="${PHPUNIT_OPTS} --configuration phpunit.evergreen.xml"

if [ "$SSL" = "yes" ]; then
SSL_OPTS="ssl=true&sslallowinvalidcertificates=true"
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jobs:
php-ini-values: "zend.assertions=1"

- name: "Run PHPUnit"
run: "vendor/bin/phpunit -v"
run: "vendor/bin/phpunit"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about removing the output of skipped tests, so I agree with this change 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also, the -v option isn't available on PHPUnit 10

env:
SYMFONY_DEPRECATIONS_HELPER: 999999
MONGODB_URI: ${{ steps.setup-mongodb.outputs.cluster-uri }}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
},
"require-dev": {
"doctrine/coding-standard": "^12.0",
"phpunit/phpunit": "^9.6.11",
"rector/rector": "^1.1",
"phpunit/phpunit": "^10.5.35",
"rector/rector": "^1.2",
"squizlabs/php_codesniffer": "^3.7",
"vimeo/psalm": "^5.13"
},
Expand Down
8 changes: 7 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassLike\RemoveAnnotationRector;
use Rector\Php70\Rector\StmtsAwareInterface\IfIssetToCoalescingRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\StaticDataProviderClassMethodRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -16,13 +18,17 @@
]);

// Modernize code
$rectorConfig->sets([LevelSetList::UP_TO_PHP_74]);
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have been bumped to PHP 8.1 in 24214b6?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PHP 8.1 rules have to be tuned. Some like ReadOnlyPropertyRector and ClassPropertyAssignToConstructorPromotionRector need to be challenged.
https://jira.mongodb.org/browse/PHPLIB-1536

PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->rule(ChangeSwitchToMatchRector::class);
$rectorConfig->rule(StaticDataProviderClassMethodRector::class);

// phpcs:disable Squiz.Arrays.ArrayDeclaration.KeySpecified
$rectorConfig->skip([
RemoveExtraParametersRector::class,
// Do not use ternaries extensively
IfIssetToCoalescingRector::class,
ChangeSwitchToMatchRector::class => [
Expand Down
4 changes: 2 additions & 2 deletions tests/Builder/BuilderEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\Sort;
use MongoDB\Builder\Variable;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function array_merge;
Expand Down Expand Up @@ -153,9 +154,8 @@ public function testPerformCount(): void
*
* @param list<int> $limit
* @param array<string, int> $expectedLimit
*
* @dataProvider provideExpressionFilterLimit
*/
#[DataProvider('provideExpressionFilterLimit')]
public function testExpressionFilter(array $limit, array $expectedLimit): void
{
$pipeline = new Pipeline(
Expand Down
5 changes: 3 additions & 2 deletions tests/Builder/FieldPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use MongoDB\Builder\Expression;
use MongoDB\Builder\Type\FieldPathInterface;
use MongoDB\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function is_subclass_of;
use function sprintf;

class FieldPathTest extends TestCase
{
/** @dataProvider provideFieldPath */
#[DataProvider('provideFieldPath')]
public function testFieldPath(string $fieldPathClass, string $resolveClass): void
{
$fieldPath = Expression::{$fieldPathClass}('foo');
Expand All @@ -27,7 +28,7 @@ public function testFieldPath(string $fieldPathClass, string $resolveClass): voi
$this->assertTrue(is_subclass_of(Expression\FieldPath::class, $resolveClass), sprintf('%s instanceof %s', Expression\FieldPath::class, $resolveClass));
}

/** @dataProvider provideFieldPath */
#[DataProvider('provideFieldPath')]
public function testRejectDollarPrefix(string $fieldPathClass): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
10 changes: 4 additions & 6 deletions tests/Builder/Type/CombinedFieldQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MongoDB\Builder\Query\GtOperator;
use MongoDB\Builder\Type\CombinedFieldQuery;
use MongoDB\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class CombinedFieldQueryTest extends TestCase
Expand Down Expand Up @@ -47,7 +48,7 @@ public function testFlattenCombinedFieldQueries(): void
$this->assertCount(3, $fieldQueries->fieldQueries);
}

/** @dataProvider provideInvalidFieldQuery */
#[DataProvider('provideInvalidFieldQuery')]
public function testRejectInvalidFieldQueries(mixed $invalidQuery, string $message = '-'): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -71,11 +72,8 @@ public static function provideInvalidFieldQuery(): Generator
yield 'object key without $' => [(object) ['eq' => 1], 'Operator must contain exactly one key starting with $, "eq" given'];
}

/**
* @param array<mixed> $fieldQueries
*
* @dataProvider provideDuplicateOperator
*/
/** @param array<mixed> $fieldQueries */
#[DataProvider('provideDuplicateOperator')]
public function testRejectDuplicateOperator(array $fieldQueries): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
15 changes: 5 additions & 10 deletions tests/Builder/Type/OutputWindowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use MongoDB\Builder\Type\TimeUnit;
use MongoDB\Builder\Type\WindowInterface;
use MongoDB\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class OutputWindowTest extends TestCase
Expand Down Expand Up @@ -57,11 +58,8 @@ public function testWithUnit(): void
$this->assertEquals((object) ['unit' => TimeUnit::Day], $outputWindow->window);
}

/**
* @param array<mixed> $documents
*
* @dataProvider provideInvalidDocuments
*/
/** @param array<mixed> $documents */
#[DataProvider('provideInvalidDocuments')]
public function testRejectInvalidDocuments(array $documents): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -82,11 +80,8 @@ public static function provideInvalidDocuments(): Generator
yield 'not a list' => [['foo' => 1, 'bar' => 2]];
}

/**
* @param array<mixed> $range
*
* @dataProvider provideInvalidRange
*/
/** @param array<mixed> $range */
#[DataProvider('provideInvalidRange')]
public function testRejectInvalidRange(array $range): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
15 changes: 5 additions & 10 deletions tests/Builder/Type/QueryObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use MongoDB\Builder\Type\CombinedFieldQuery;
use MongoDB\Builder\Type\QueryInterface;
use MongoDB\Builder\Type\QueryObject;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class QueryObjectTest extends TestCase
Expand All @@ -32,23 +33,17 @@ public function testShortCutQueryObject(): void
$this->assertSame($query, $queryObject);
}

/**
* @param array<array-key, mixed> $value
*
* @dataProvider provideQueryObjectValue
*/
/** @param array<array-key, mixed> $value */
#[DataProvider('provideQueryObjectValue')]
public function testCreateQueryObject(array $value, int $expectedCount = 1): void
{
$queryObject = QueryObject::create($value);

$this->assertCount($expectedCount, $queryObject->queries);
}

/**
* @param array<array-key, mixed> $value
*
* @dataProvider provideQueryObjectValue
*/
/** @param array<array-key, mixed> $value */
#[DataProvider('provideQueryObjectValue')]
public function testCreateQueryObjectFromArray(array $value, int $expectedCount = 1): void
{
// $value is wrapped in an array as if the user used an array instead of variadic arguments
Expand Down
3 changes: 2 additions & 1 deletion tests/Builder/VariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\Builder\Expression;
use MongoDB\Builder\Variable;
use MongoDB\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class VariableTest extends TestCase
Expand All @@ -27,7 +28,7 @@ public function testVariableRejectDollarPrefix(): void
new Expression\Variable('$$foo');
}

/** @dataProvider provideVariableBuilders */
#[DataProvider('provideVariableBuilders')]
public function testSystemVariables($factory): void
{
$variable = $factory();
Expand Down
6 changes: 4 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;

/**
* Unit tests for the Client class.
Expand All @@ -23,7 +25,7 @@ public function testConstructorDefaultUri(): void
$this->assertEquals('mongodb://127.0.0.1/', (string) $client);
}

/** @doesNotPerformAssertions */
#[DoesNotPerformAssertions]
public function testConstructorAutoEncryptionOpts(): void
{
$autoEncryptionOpts = [
Expand All @@ -35,7 +37,7 @@ public function testConstructorAutoEncryptionOpts(): void
new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
}

/** @dataProvider provideInvalidConstructorDriverOptions */
#[DataProvider('provideInvalidConstructorDriverOptions')]
public function testConstructorDriverOptionTypeChecks(array $driverOptions, string $exception = InvalidArgumentException::class): void
{
$this->expectException($exception);
Expand Down
21 changes: 11 additions & 10 deletions tests/Collection/CodecCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Tests\Fixtures\Codec\TestDocumentCodec;
use MongoDB\Tests\Fixtures\Document\TestObject;
use PHPUnit\Framework\Attributes\DataProvider;

class CodecCollectionFunctionalTest extends FunctionalTestCase
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public static function provideAggregateOptions(): Generator
];
}

/** @dataProvider provideAggregateOptions */
#[DataProvider('provideAggregateOptions')]
public function testAggregate($expected, $options): void
{
$this->createFixtures(3);
Expand Down Expand Up @@ -114,7 +115,7 @@ public static function provideBulkWriteOptions(): Generator
];
}

/** @dataProvider provideBulkWriteOptions */
#[DataProvider('provideBulkWriteOptions')]
public function testBulkWrite($expected, $options): void
{
$this->createFixtures(3);
Expand Down Expand Up @@ -169,7 +170,7 @@ public static function provideFindOneAndModifyOptions(): Generator
];
}

/** @dataProvider provideFindOneAndModifyOptions */
#[DataProvider('provideFindOneAndModifyOptions')]
public function testFindOneAndDelete($expected, $options): void
{
$this->createFixtures(1);
Expand All @@ -190,7 +191,7 @@ public function testFindOneAndDeleteWithCodecAndTypemap(): void
$this->collection->findOneAndDelete(['_id' => 1], $options);
}

/** @dataProvider provideFindOneAndModifyOptions */
#[DataProvider('provideFindOneAndModifyOptions')]
public function testFindOneAndUpdate($expected, $options): void
{
$this->createFixtures(1);
Expand Down Expand Up @@ -235,7 +236,7 @@ public static function provideFindOneAndReplaceOptions(): Generator
];
}

/** @dataProvider provideFindOneAndReplaceOptions */
#[DataProvider('provideFindOneAndReplaceOptions')]
public function testFindOneAndReplace($expected, $options): void
{
$this->createFixtures(1);
Expand Down Expand Up @@ -293,7 +294,7 @@ public static function provideFindOptions(): Generator
];
}

/** @dataProvider provideFindOptions */
#[DataProvider('provideFindOptions')]
public function testFind($expected, $options): void
{
$this->createFixtures(3);
Expand Down Expand Up @@ -332,7 +333,7 @@ public static function provideFindOneOptions(): Generator
];
}

/** @dataProvider provideFindOneOptions */
#[DataProvider('provideFindOneOptions')]
public function testFindOne($expected, $options): void
{
$this->createFixtures(1);
Expand Down Expand Up @@ -383,7 +384,7 @@ public static function provideInsertManyOptions(): Generator
];
}

/** @dataProvider provideInsertManyOptions */
#[DataProvider('provideInsertManyOptions')]
public function testInsertMany($expected, $options): void
{
$documents = [
Expand Down Expand Up @@ -430,7 +431,7 @@ public static function provideInsertOneOptions(): Generator
];
}

/** @dataProvider provideInsertOneOptions */
#[DataProvider('provideInsertOneOptions')]
public function testInsertOne($expected, $options): void
{
$result = $this->collection->insertOne(TestObject::createForFixture(1), $options);
Expand Down Expand Up @@ -475,7 +476,7 @@ public static function provideReplaceOneOptions(): Generator
];
}

/** @dataProvider provideReplaceOneOptions */
#[DataProvider('provideReplaceOneOptions')]
public function testReplaceOne($expected, $options): void
{
$this->createFixtures(1);
Expand Down
Loading