Skip to content

Commit

Permalink
Add parameter type hints to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Sep 14, 2021
1 parent 38fc494 commit fba6112
Show file tree
Hide file tree
Showing 99 changed files with 439 additions and 216 deletions.
5 changes: 4 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />

<!-- Will cause BC breaks to method signatures - disabled for now -->
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint" />

Expand Down Expand Up @@ -70,6 +69,10 @@
<exclude-pattern>*/lib/*</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint">
<exclude-pattern>*/lib/*</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
<!-- We do want to test generating collections without return types -->
<exclude-pattern>*/tests/Doctrine/ODM/MongoDB/Tests/PersistentCollection/Coll*</exclude-pattern>
Expand Down
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 @@ -4,6 +4,7 @@

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

use Closure;
use DateTime;
use Doctrine\ODM\MongoDB\Aggregation\Stage\MatchStage;
use Doctrine\ODM\MongoDB\Query\Expr;
Expand Down Expand Up @@ -40,9 +41,11 @@ public function testMatchFromBuilder(): void
}

/**
* @param Closure|array $args
*
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods($method, array $args = []): void
public function testProxiedExprMethods(string $method, $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 @@ -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\Project;
use Doctrine\ODM\MongoDB\Tests\Aggregation\AggregationTestTrait;
Expand Down Expand Up @@ -44,7 +45,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 @@ -64,9 +65,11 @@ public function provideAccumulators(): array
}

/**
* @param Closure|array $args
*
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods($method, $args = []): void
public function testProxiedExprMethods(string $method, $args = []): void
{
$expr = $this->getMockAggregationExpr();
$expr
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
5 changes: 5 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 Expand Up @@ -119,6 +121,9 @@ public function testAtomicCollectionUnset($clearWith): void
$this->assertCount(0, $user->phonenumbers);
}

/**
* @return array<array{array|ArrayCollection|null}>
*/
public function provideAtomicCollectionUnset(): array
{
return [
Expand Down
5 changes: 4 additions & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/Functional/BinDataTest.php
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 All @@ -28,6 +28,9 @@ public function testBinData($field, $data, $type): void
$this->assertEquals($data, $check[$field]->getData());
}

/**
* @return array<array{string, string, int}>
*/
public function provideData(): array
{
return [
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
7 changes: 7 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 All @@ -55,6 +58,9 @@ public function testDateInstanceChangeDoesNotCauseUpdateIfValueIsTheSame($oldVal
$this->assertEmpty($changeset);
}

/**
* @return array<array{DateTime|UTCDateTime, DateTime|UTCDateTime}>
*/
public function provideEquivalentDates(): array
{
return [
Expand All @@ -76,6 +82,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
Loading

0 comments on commit fba6112

Please sign in to comment.