Skip to content
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

Add parameter type hints to tests #2369

Merged
merged 2 commits into from
Oct 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ protected function createExpr(): Expr
return new Expr($this->dm, new ClassMetadata(User::class));
}

/**
* @param mixed $args
*/
protected function resolveArgs($args): array
{
if (is_array($args)) {
Expand Down
9 changes: 7 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/ExprTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Aggregation;

use BadMethodCallException;
use Closure;
use Doctrine\ODM\MongoDB\Aggregation\Expr;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use LogicException;
Expand All @@ -14,9 +15,11 @@ class ExprTest extends BaseTest
use AggregationOperatorsProviderTrait;

/**
* @param array|Closure $args
*
* @dataProvider provideAllOperators
*/
public function testGenericOperator($expected, $operator, $args): void
public function testGenericOperator(array $expected, string $operator, $args): void
{
$expr = $this->createExpr();
$args = $this->resolveArgs($args);
Expand All @@ -26,9 +29,11 @@ public function testGenericOperator($expected, $operator, $args): void
}

/**
* @param array|Closure $args
*
* @dataProvider provideAllOperators
*/
public function testGenericOperatorWithField($expected, $operator, $args): void
public function testGenericOperatorWithField(array $expected, string $operator, $args): void
{
$expr = $this->createExpr();
$args = $this->resolveArgs($args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public function testGeoNearFromBuilder(): void
}

/**
* @param mixed $value
*
* @dataProvider provideOptionalSettings
*/
public function testOptionalSettings($field, $value): void
public function testOptionalSettings(string $field, $value): void
{
$geoNearStage = new GeoNear($this->getTestAggregationBuilder(), 0, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB\Tests\Aggregation\Stage;

use Closure;
use Doctrine\ODM\MongoDB\Aggregation\Expr;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Group;
use Doctrine\ODM\MongoDB\Tests\Aggregation\AggregationOperatorsProviderTrait;
Expand All @@ -16,9 +17,11 @@ class GroupTest extends BaseTest
use AggregationOperatorsProviderTrait;

/**
* @param Closure|array $args
*
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods($method, $args = []): void
public function testProxiedExprMethods(string $method, $args = []): void
{
$args = $this->resolveArgs($args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testMatchFromBuilder(): void
/**
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods($method, array $args = []): void
public function testProxiedExprMethods(string $method, array $args = []): void
{
$expr = $this->getMockQueryExpr();
$expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Aggregation\Stage;

use BadMethodCallException;
use Closure;
use Doctrine\ODM\MongoDB\Aggregation\Expr;
use Doctrine\ODM\MongoDB\Aggregation\Stage\Operator;
use Doctrine\ODM\MongoDB\Tests\Aggregation\AggregationOperatorsProviderTrait;
Expand All @@ -17,9 +18,11 @@ class OperatorTest extends BaseTest
use AggregationOperatorsProviderTrait;

/**
* @param Closure|array $args
*
* @dataProvider provideExpressionOperators
*/
public function testProxiedExpressionOperators($expected, $operator, $args): void
public function testProxiedExpressionOperators(array $expected, string $operator, $args): void
{
$stage = $this->getStubStage();
$args = $this->resolveArgs($args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testProjectFromBuilder(): void
/**
* @dataProvider provideAccumulators
*/
public function testAccumulatorsWithMultipleArguments($operator): void
public function testAccumulatorsWithMultipleArguments(string $operator): void
{
$projectStage = new Project($this->getTestAggregationBuilder());
$projectStage
Expand All @@ -66,7 +66,7 @@ public function provideAccumulators(): array
/**
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods($method, $args = []): void
public function testProxiedExprMethods(string $method, array $args = []): void
{
$expr = $this->getMockAggregationExpr();
$expr
Expand All @@ -85,6 +85,9 @@ public function setExpr(Expr $expr): void
$this->assertSame($stage, $stage->$method(...$args));
}

/**
* @return array<array{string, string[]}>
*/
public static function provideProxiedExprMethods(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ class SortTest extends BaseTest
use AggregationTestTrait;

/**
* @param string|array<string, string> $field
*
* @dataProvider provideSortOptions
*/
public function testSortStage($expectedSort, $field, $order = null): void
public function testSortStage(array $expectedSort, $field, ?string $order = null): void
{
$sortStage = new Sort($this->getTestAggregationBuilder(), $field, $order);

$this->assertSame(['$sort' => $expectedSort], $sortStage->getExpression());
}

/**
* @param string|array<string, string> $field
*
* @dataProvider provideSortOptions
*/
public function testSortFromBuilder($expectedSort, $field, $order = null): void
public function testSortFromBuilder(array $expectedSort, $field, ?string $order = null): void
{
$builder = $this->getTestAggregationBuilder();
$builder->sort($field, $order);
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function getConfiguration(): Configuration
*
* @deprecated
*/
public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void
public static function assertArraySubset(array $subset, array $array, bool $checkForObjectIdentity = false, string $message = ''): void
{
foreach ($subset as $key => $value) {
self::assertArrayHasKey($key, $array, $message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class LifecycleCallbacksTest extends BaseTest
{
private function createUser($name = 'jon', $fullName = 'Jonathan H. Wage'): User
private function createUser(string $name = 'jon', string $fullName = 'Jonathan H. Wage'): User
{
$user = new User();
$user->name = $name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class MyEventListener
/** @psalm-var array<string, list<class-string>> */
public $called = [];

public function __call($method, $args)
public function __call(string $method, array $args)
{
$document = $args[0]->getDocument();
$className = get_class($document);
Expand All @@ -245,7 +245,7 @@ class PostCollectionLoadEventListener
/** @var TestCase */
private $phpunit;

public function __construct($phpunit)
public function __construct(TestCase $phpunit)
{
$this->phpunit = $phpunit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class AlsoLoadDocument
public $testOlder;

/** @ODM\AlsoLoad({"name", "fullName"}) */
public function populateFirstAndLastName($name): void
public function populateFirstAndLastName(string $name): void
{
[$this->firstName, $this->lastName] = explode(' ', $name);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public function testAtomicUpsert(): void
}

/**
* @param array|ArrayCollection|null $clearWith
*
* @dataProvider provideAtomicCollectionUnset
*/
public function testAtomicCollectionUnset($clearWith): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BinDataTest extends BaseTest
/**
* @dataProvider provideData
*/
public function testBinData($field, $data, $type): void
public function testBinData(string $field, string $data, int $type): void
{
$test = new BinDataTestUser();
$test->$field = $data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function getSubscribedEvents(): array
];
}

public function __call($eventName, $args)
public function __call(string $eventName, array $args)
{
$document = $args[0]->getDocument();
if (! ($document instanceof User)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function getEnabled(): MyEmbedsCollection
});
}

public function move($i, $j): void
public function move(int $i, int $j): void
{
$tmp = $this->get($i);
$this->set($i, $this->get($j));
Expand Down
4 changes: 4 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function testDates(): void
}

/**
* @param DateTime|UTCDateTime $oldValue
* @param DateTime|UTCDateTime $newValue
*
* @dataProvider provideEquivalentDates
*/
public function testDateInstanceChangeDoesNotCauseUpdateIfValueIsTheSame($oldValue, $newValue): void
Expand Down Expand Up @@ -76,6 +79,7 @@ public function testDateInstanceValueChangeDoesCauseUpdateIfValueIsTheSame(): vo
$this->dm->clear();

$user = $this->dm->getRepository(get_class($user))->findOneBy([]);
$this->assertInstanceOf(DateTime::class, $user->getCreatedAt());
$user->getCreatedAt()->setTimestamp(time() - 3600);

$this->dm->getUnitOfWork()->computeChangeSets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testLoadAllWithSortLimitAndSkip(): void
/**
* @dataProvider getTestPrepareFieldNameData
*/
public function testPrepareFieldName($fieldName, $expected): void
public function testPrepareFieldName(string $fieldName, string $expected): void
{
$this->assertEquals($expected, $this->documentPersister->prepareFieldName($fieldName));
}
Expand Down Expand Up @@ -172,9 +172,11 @@ public function testExistsInQuery(): void
}

/**
* @param array<array-key, string> $hashId
*
* @dataProvider provideHashIdentifiers
*/
public function testPrepareQueryOrNewObjWithHashId($hashId): void
public function testPrepareQueryOrNewObjWithHashId(array $hashId): void
{
$class = DocumentPersisterTestHashIdDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);
Expand All @@ -186,9 +188,11 @@ public function testPrepareQueryOrNewObjWithHashId($hashId): void
}

/**
* @param array<array-key, string> $hashId
*
* @dataProvider provideHashIdentifiers
*/
public function testPrepareQueryOrNewObjWithHashIdAndInOperators($hashId): void
public function testPrepareQueryOrNewObjWithHashIdAndInOperators(array $hashId): void
{
$class = DocumentPersisterTestHashIdDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);
Expand Down Expand Up @@ -379,9 +383,11 @@ static function (DocumentManager $dm) use ($getReference): array {
}

/**
* @param array<array-key, string> $hashId
*
* @dataProvider provideHashIdentifiers
*/
public function testPrepareQueryOrNewObjWithSimpleReferenceToTargetDocumentWithHashIdType($hashId): void
public function testPrepareQueryOrNewObjWithSimpleReferenceToTargetDocumentWithHashIdType(array $hashId): void
{
$class = DocumentPersisterTestDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);
Expand Down Expand Up @@ -456,9 +462,11 @@ public function testPrepareQueryOrNewObjWithDBRefReferenceToTargetDocumentWithNo
}

/**
* @param array<array-key, string> $hashId
*
* @dataProvider provideHashIdentifiers
*/
public function testPrepareQueryOrNewObjWithDBRefReferenceToTargetDocumentWithHashIdType($hashId): void
public function testPrepareQueryOrNewObjWithDBRefReferenceToTargetDocumentWithHashIdType(array $hashId): void
{
$class = DocumentPersisterTestDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);
Expand Down Expand Up @@ -570,9 +578,11 @@ public function testPrepareQueryOrNewObjWithEmbeddedReferenceToTargetDocumentWit
}

/**
* @param array<array-key, string> $hashId
*
* @dataProvider provideHashIdentifiers
*/
public function testPrepareQueryOrNewObjWithEmbeddedReferenceToTargetDocumentWithHashIdType($hashId): void
public function testPrepareQueryOrNewObjWithEmbeddedReferenceToTargetDocumentWithHashIdType(array $hashId): void
{
$class = DocumentPersisterTestDocument::class;
$documentPersister = $this->uow->getDocumentPersister($class);
Expand Down Expand Up @@ -1067,6 +1077,9 @@ public function convertToPHPValue($value)
throw self::createException($value);
}

/**
* @param mixed $value
*/
private static function createException($value): InvalidArgumentException
{
return new InvalidArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ class ReferencedDocument
/**
* @ODM\Field(type="string")
*
* @var string|null
* @var string
*/
public $name;

public function __construct($name)
public function __construct(string $name)
{
$this->name = $name;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/Functional/FlushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testFlush(): void
$this->assertSize(1);
}

protected function assertSize($size): void
protected function assertSize(int $size): void
{
$this->assertEquals($size, $this->dm->getUnitOfWork()->size());
}
Expand Down
Loading