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

Analyze tests directory with psalm #2311

Merged
merged 18 commits into from
May 31, 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
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public function getIdentifier(): array
* Since MongoDB only allows exactly one identifier field
* this will always return an array with only one value
*
* return (string|null)[]
* @return array<string|null>
*/
public function getIdentifierFieldNames(): array
{
Expand Down
16 changes: 16 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
findUnusedPsalmSuppress="true"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="lib/Doctrine/ODM/MongoDB" />
<directory name="tests/Doctrine" />
<directory name="tests/Documents" />
<directory name="tests/Documents74" />
<ignoreFiles>
<file name="lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup/Match.php" />
<file name="lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Match.php" />
<file name="tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/User.php" />
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<UndefinedConstant>
<!-- DOCTRINE_MONGODB_DATABASE constant defined in phpunit.xml.dist -->
<errorLevel type="suppress">
<file name="tests/Doctrine/ODM/MongoDB/Tests/BaseTest.php"/>
<file name="tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php"/>
<file name="tests/Doctrine/ODM/MongoDB/Tests/Id/IncrementGeneratorTest.php"/>
<file name="tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php"/>
</errorLevel>
</UndefinedConstant>
</issueHandlers>
</psalm>
Original file line number Diff line number Diff line change
Expand Up @@ -510,22 +510,22 @@ public function testPrepareQueryOrNewObjWithComplexRefToTargetDocumentFieldWithO
public static function queryProviderForComplexRefWithObjectValue(): Generator
{
yield 'Direct comparison' => [
'expected' => ['complexRef.date' => new UTCDateTime('1590710400000')],
'expected' => ['complexRef.date' => new UTCDateTime(1590710400000)],
'query' => ['complexRef.date' => DateTime::createFromFormat('U', '1590710400')],
];

yield 'Operator with single value' => [
'expected' => ['complexRef.date' => ['$ne' => new UTCDateTime('1590710400000')]],
'expected' => ['complexRef.date' => ['$ne' => new UTCDateTime(1590710400000)]],
'query' => ['complexRef.date' => ['$ne' => DateTime::createFromFormat('U', '1590710400')]],
];

yield 'Operator with multiple values' => [
'expected' => ['complexRef.date' => ['$in' => [new UTCDateTime('1590710400000'), new UTCDateTime('1590796800000')]]],
'expected' => ['complexRef.date' => ['$in' => [new UTCDateTime(1590710400000), new UTCDateTime(1590796800000)]]],
'query' => ['complexRef.date' => ['$in' => [DateTime::createFromFormat('U', '1590710400'), DateTime::createFromFormat('U', '1590796800')]]],
];

yield 'Nested operator' => [
'expected' => ['complexRef.date' => ['$not' => ['$in' => [new UTCDateTime('1590710400000'), new UTCDateTime('1590796800000')]]]],
'expected' => ['complexRef.date' => ['$not' => ['$in' => [new UTCDateTime(1590710400000), new UTCDateTime(1590796800000)]]]],
'query' => ['complexRef.date' => ['$not' => ['$in' => [DateTime::createFromFormat('U', '1590710400'), DateTime::createFromFormat('U', '1590796800')]]]],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function testRemoveOption()
$this->dm->flush();
$this->dm->detach($product);
unset($product);
$this->assertFalse(isset($product));

$product = $this->getProduct();
$this->assertCount(2, $product->getOptions());
Expand Down
22 changes: 17 additions & 5 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Documents\User;
use MongoDB\BSON\ObjectId;

use function assert;
use function get_class;

class EmbeddedTest extends BaseTest
Expand Down Expand Up @@ -112,6 +113,7 @@ public function testOneEmbedded()
->field('id')->equals($user->getId())
->getQuery()
->getSingleResult();
assert($user instanceof User);
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
$this->assertNotNull($user);
$this->assertEquals($addressClone, $user->getAddress());

Expand Down Expand Up @@ -148,6 +150,7 @@ public function testRemoveOneEmbedded()
->field('id')->equals($user->getId())
->getQuery()
->getSingleResult();
assert($user instanceof User);
$this->assertNotNull($user);
$this->assertNull($user->getAddress());
}
Expand All @@ -166,6 +169,7 @@ public function testManyEmbedded()
->field('id')->equals($user->getId())
->getQuery()
->getSingleResult();
assert($user2 instanceof User);
$this->assertNotNull($user2);
$this->assertEquals($user->getPhonenumbers()->toArray(), $user2->getPhonenumbers()->toArray());
}
Expand All @@ -187,10 +191,11 @@ public function testPostRemoveEventOnEmbeddedManyDocument()
$this->dm->clear();

// retrieve test
$test = $this->dm->createQueryBuilder(get_class($test))
$test = $this->dm->createQueryBuilder(get_class($test))
->field('id')->equals($test->id)
->getQuery()
->getSingleResult();
assert($test instanceof EmbeddedTestLevel0b);
$level1 = $test->level1[0];

// $test->level1[0] is available
Expand Down Expand Up @@ -227,6 +232,7 @@ public function testRemoveEmbeddedManyDocument()
->getQuery()
->getSingleResult();

assert($test instanceof EmbeddedTestLevel0b);
// $test->level1[0] is available
$this->assertEquals('test level1 #1', $test->level1[0]->name);

Expand All @@ -244,6 +250,8 @@ public function testRemoveEmbeddedManyDocument()
->getQuery()
->getSingleResult();

assert($test instanceof EmbeddedTestLevel0b);

$this->assertInstanceOf(PersistentCollection::class, $test->level1);

// verify that test has no more level1
Expand Down Expand Up @@ -272,10 +280,11 @@ public function testRemoveDeepEmbeddedManyDocument()
$this->dm->clear();

// retrieve test
$test = $this->dm->createQueryBuilder(get_class($test))
$test = $this->dm->createQueryBuilder(get_class($test))
->field('id')->equals($test->id)
->getQuery()
->getSingleResult();
assert($test instanceof EmbeddedTestLevel0b);
$level1 = $test->oneLevel1;
$level2 = $level1->level2[0];

Expand All @@ -291,10 +300,11 @@ public function testRemoveDeepEmbeddedManyDocument()
$this->assertEquals(0, $level1->level2->count());

// retrieve test
$test = $this->dm->createQueryBuilder(get_class($test))
$test = $this->dm->createQueryBuilder(get_class($test))
->field('id')->equals($test->id)
->getQuery()
->getSingleResult();
assert($test instanceof EmbeddedTestLevel0b);
$level1 = $test->oneLevel1;

// verify that level1 has no more level2
Expand Down Expand Up @@ -323,10 +333,11 @@ public function testPostRemoveEventOnDeepEmbeddedManyDocument()
$this->dm->clear();

// retrieve test
$test = $this->dm->createQueryBuilder(get_class($test))
$test = $this->dm->createQueryBuilder(get_class($test))
->field('id')->equals($test->id)
->getQuery()
->getSingleResult();
assert($test instanceof EmbeddedTestLevel0b);
$level1 = $test->oneLevel1;
$level2 = $level1->level2[0];

Expand Down Expand Up @@ -360,10 +371,11 @@ public function testEmbeddedLoadEvents()
$this->dm->flush();
$this->dm->clear();

$test = $this->dm->createQueryBuilder(get_class($test))
$test = $this->dm->createQueryBuilder(get_class($test))
->field('id')->equals($test->id)
->getQuery()
->getSingleResult();
assert($test instanceof EmbeddedTestLevel0b);
$level1 = $test->oneLevel1;
$level2 = $level1->level2[0];

Expand Down
7 changes: 7 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\ODM\MongoDB\DocumentNotFoundException;
use Doctrine\ODM\MongoDB\Query\FilterCollection;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Documents\Group;
use Documents\Profile;
Expand All @@ -14,6 +15,12 @@

class FilterTest extends BaseTest
{
/** @var array<string, string> */
private $ids;

/** @var FilterCollection */
private $fc;

public function setUp(): void
{
parent::setUp();
Expand Down
8 changes: 7 additions & 1 deletion tests/Doctrine/ODM/MongoDB/Tests/Functional/IdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ public function testStrategyNoneAndNoIdThrowsException()

public function testStrategyAutoWithNotValidIdThrowsException()
{
$this->createIdTestClass('id', 'auto');
$user = new TestIdTypesIdAutoUser();
$user->id = 1;
$this->expectException(InvalidArgumentException::class);
Expand Down Expand Up @@ -515,3 +514,10 @@ public function __construct($name)
$this->name = $name;
}
}

/** @ODM\Document */
class TestIdTypesIdAutoUser
{
/** @ODM\Id(strategy="auto", options={"type"="id"}) **/
public $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Documents\Event;
use Documents\User;

use function assert;
use function get_class;

class IdentifiersTest extends BaseTest
Expand Down Expand Up @@ -66,18 +67,21 @@ public function testIdentityMap()
->field('id')->equals($user->getId());

$user = $qb->getQuery()->getSingleResult();
assert($user instanceof User);
$this->assertSame($user, $user);

$this->dm->clear();

$user2 = $qb->getQuery()->getSingleResult();
assert($user2 instanceof User);
$this->assertNotSame($user, $user2);

$user2->setUsername('changed');

$qb->refresh();

$user3 = $qb->getQuery()->getSingleResult();
assert($user3 instanceof User);
$this->assertSame($user2, $user3);
$this->assertEquals('jwage', $user3->getUsername());

Expand All @@ -86,6 +90,7 @@ public function testIdentityMap()
$qb->refresh(false);

$user4 = $qb->getQuery()->getSingleResult();
assert($user4 instanceof User);
$this->assertEquals('changed', $user4->getUsername());

$qb = $this->dm->createQueryBuilder(User::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Documents\Product;
use Documents\Tag;

use function assert;
use function get_class;
use function strtotime;

Expand Down Expand Up @@ -79,9 +80,10 @@ public function testOneToManyBiDirectional()
$check = $this->dm->getDocumentCollection(Feature::class)->findOne();
$this->assertArrayHasKey('product', $check);

$product = $this->dm->createQueryBuilder(get_class($product))
$product = $this->dm->createQueryBuilder(get_class($product))
->getQuery()
->getSingleResult();
assert($product instanceof Product);
$features = $product->features;
$this->assertCount(2, $features);
$this->assertEquals('Pages', $features[0]->name);
Expand All @@ -106,6 +108,7 @@ public function testOneToManySelfReferencing()
->field('children')->exists(false)
->getQuery()
->getSingleResult();
assert($root instanceof BrowseNode);
$this->assertInstanceOf(BrowseNode::class, $root);
$this->assertCount(2, $root->children);

Expand Down Expand Up @@ -138,6 +141,7 @@ public function testManyToMany()
$blogPost = $this->dm->createQueryBuilder(BlogPost::class)
->getQuery()
->getSingleResult();
assert($blogPost instanceof BlogPost);
$this->assertCount(1, $blogPost->tags);

$this->dm->clear();
Expand Down Expand Up @@ -177,7 +181,7 @@ public function testManyToManySelfReferencing()
->field('name')->equals('fabpot')
->getQuery()
->getSingleResult();

assert($user instanceof FriendUser);
$this->assertCount(1, $user->friendsWithMe);
$this->assertEquals('jwage', $user->friendsWithMe[0]->name);

Expand All @@ -187,7 +191,7 @@ public function testManyToManySelfReferencing()
->field('name')->equals('romanb')
->getQuery()
->getSingleResult();

assert($user instanceof FriendUser);
$this->assertCount(1, $user->friendsWithMe);
$this->assertEquals('jwage', $user->friendsWithMe[0]->name);

Expand All @@ -197,7 +201,7 @@ public function testManyToManySelfReferencing()
->field('name')->equals('jwage')
->getQuery()
->getSingleResult();

assert($user instanceof FriendUser);
$this->assertCount(2, $user->myFriends);
$this->assertEquals('fabpot', $user->myFriends[0]->name);
$this->assertEquals('romanb', $user->myFriends[1]->name);
Expand Down Expand Up @@ -228,6 +232,7 @@ public function testSortLimitAndSkipReferences()
$blogPost = $this->dm->createQueryBuilder(BlogPost::class)
->getQuery()
->getSingleResult();
assert($blogPost instanceof BlogPost);
$this->assertEquals('Comment 1', $blogPost->comments[0]->text);
$this->assertEquals('Comment 2', $blogPost->comments[1]->text);
$this->assertEquals('Test', $blogPost->comments[0]->parent->name);
Expand All @@ -238,13 +243,15 @@ public function testSortLimitAndSkipReferences()
$comment = $this->dm->createQueryBuilder(Comment::class)
->getQuery()
->getSingleResult();
assert($comment instanceof Comment);
$this->assertEquals('Test', $comment->parent->getName());

$this->dm->clear();

$blogPost = $this->dm->createQueryBuilder(BlogPost::class)
->getQuery()
->getSingleResult();
assert($blogPost instanceof BlogPost);
$this->assertEquals('Comment 1', $blogPost->firstComment->getText());
$this->assertEquals('Comment 2', $blogPost->latestComment->getText());
$this->assertCount(2, $blogPost->last5Comments);
Expand All @@ -258,6 +265,7 @@ public function testSortLimitAndSkipReferences()
->getQuery()
->getSingleResult();

assert($blogPost instanceof BlogPost);
$blogPost->addComment(new Comment('Comment 3 by admin', $date1, true));
$blogPost->addComment(new Comment('Comment 4 by admin', $date2, true));
$this->dm->flush();
Expand All @@ -266,6 +274,7 @@ public function testSortLimitAndSkipReferences()
$blogPost = $this->dm->createQueryBuilder(BlogPost::class)
->getQuery()
->getSingleResult();
assert($blogPost instanceof BlogPost);
$this->assertCount(2, $blogPost->adminComments);
$this->assertEquals('Comment 4 by admin', $blogPost->adminComments[0]->getText());
$this->assertEquals('Comment 3 by admin', $blogPost->adminComments[1]->getText());
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

class QueryTest extends BaseTest
{
/** @var User */
private $user;

public function setUp(): void
{
parent::setUp();
Expand Down
Loading