Skip to content

Commit

Permalink
PHPLIB-1369 Upgrade to PHPUnit 10 (#1412)
Browse files Browse the repository at this point in the history
* Migration from @annotations to #[Attributes]
* Replace hasFailed with the method onNotSuccessfulTest in FunctionalTestCase
* User createStub in getInvalidDocumentCodecValues
* Update custom constraints (TestCase::exporter() is deprecated and ComparisonFailure 5th argument was removed)
* Use xxh faster algorithm for temp collection name
  • Loading branch information
GromNaN authored Sep 27, 2024
1 parent 2186d4f commit cb33c1c
Show file tree
Hide file tree
Showing 110 changed files with 609 additions and 627 deletions.
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"
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,
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

0 comments on commit cb33c1c

Please sign in to comment.