Skip to content

Commit

Permalink
Add some generics to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Nov 19, 2021
1 parent 240fc93 commit a457e72
Show file tree
Hide file tree
Showing 35 changed files with 259 additions and 64 deletions.
13 changes: 7 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,6 @@ parameters:
count: 1
path: lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php

# See https://github.com/phpstan/phpstan/issues/5512
-
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\Functional\\\\My.*Collection\\:\\:.*\\(\\) should return Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\Functional\\\\My.*Collection but returns Doctrine\\\\Common\\\\Collections\\\\ArrayCollection\\<\\(int\\|string\\), mixed\\>\\.$#"
count: 3
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomCollectionsTest.php

# These classes are not final
-
message: "#^Unsafe call to private method Doctrine\\\\ODM\\\\MongoDB\\\\Query\\\\Expr\\:\\:convertExpression\\(\\) through static::\\.$#"
Expand Down Expand Up @@ -253,6 +247,12 @@ parameters:
count: 1
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/DiscriminatorsDefaultValueTest.php

# Collection elements cannot be covariant, see https://github.com/doctrine/collections/pull/220
-
message: "#^Parameter \\#2 \\$projects of class Documents\\\\Developer constructor expects Doctrine\\\\Common\\\\Collections\\\\Collection\\<int, Documents\\\\Project\\>\\|null, Doctrine\\\\Common\\\\Collections\\\\ArrayCollection\\<int, Documents\\\\SubProject\\> given\\.$#"
count: 1
path: tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php

# See https://github.com/phpstan/phpstan/issues/5860
-
message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\UnitOfWork\\:\\:getOrCreateDocument\\(\\) should return T of object but returns \\(Doctrine\\\\Persistence\\\\NotifyPropertyChanged&ProxyManager\\\\Proxy\\\\GhostObjectInterface&T of object\\)\\|\\(ProxyManager\\\\Proxy\\\\GhostObjectInterface&T of object\\)\\|T of object\\.$#"
Expand Down Expand Up @@ -284,6 +284,7 @@ parameters:
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php

-
message: "#Property .+ is never written, only read.#"
path: tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ public function __construct(TestCase $phpunit)
$this->phpunit = $phpunit;
}

/**
* @param PostCollectionLoadEventArgs<int, TestEmbeddedDocument> $e
*/
public function postCollectionLoad(PostCollectionLoadEventArgs $e): void
{
switch ($this->at++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testAtomicUpsert(): void
}

/**
* @param array|ArrayCollection|null $clearWith
* @param mixed[]|ArrayCollection<int, mixed>|null $clearWith
*
* @dataProvider provideAtomicCollectionUnset
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class DocumentWithCustomCollection
* targetDocument=EmbeddedDocumentInCustomCollection::class
* )
*
* @var MyEmbedsCollection
* @var MyEmbedsCollection<int, EmbeddedDocumentInCustomCollection>
*/
public $coll;

Expand All @@ -225,7 +225,7 @@ class DocumentWithCustomCollection
* targetDocument=DocumentWithCustomCollection::class
* )
*
* @var MyDocumentsCollection
* @var MyDocumentsCollection<int, DocumentWithCustomCollection>
*/
public $refMany;

Expand All @@ -236,7 +236,7 @@ class DocumentWithCustomCollection
* targetDocument=DocumentWithCustomCollection::class
* )
*
* @var MyDocumentsCollection
* @var MyDocumentsCollection<int, DocumentWithCustomCollection>
*/
public $inverseRefMany;

Expand Down Expand Up @@ -275,15 +275,26 @@ public function __construct(string $name, bool $enabled)
}
}

/**
* @template TKey of array-key
* @template TElement
* @template-extends ArrayCollection<TKey, TElement>
*/
class MyEmbedsCollection extends ArrayCollection
{
/**
* @return MyEmbedsCollection<TKey, TElement>
*/
public function getByName(string $name): MyEmbedsCollection
{
return $this->filter(static function ($item) use ($name) {
return $item->name === $name;
});
}

/**
* @return MyEmbedsCollection<TKey, TElement>
*/
public function getEnabled(): MyEmbedsCollection
{
return $this->filter(static function ($item) {
Expand All @@ -303,8 +314,16 @@ public function nothingReally(): void
}
}

/**
* @template TKey of array-key
* @template TElement
* @template-extends ArrayCollection<TKey, TElement>
*/
class MyDocumentsCollection extends ArrayCollection
{
/**
* @return MyDocumentsCollection<TKey, TElement>
*/
public function havingEmbeds(): MyDocumentsCollection
{
return $this->filter(static function ($item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DocumentPersisterTest extends BaseTest
/** @var class-string<DocumentPersisterTestDocument> */
private $class;

/** @var DocumentPersister */
/** @var DocumentPersister<DocumentPersisterTestDocument> */
private $documentPersister;

public function setUp(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public function testClearAndAddEmbedMany(): void
$this->assertNotNull($this->getAddressRepository()->find($address3->id), 'Should have added address 3');
}

/**
* @return DocumentRepository<OrphanRemovalCascadeUser>
*/
private function getUserRepository(): DocumentRepository
{
$repository = $this->dm->getRepository(OrphanRemovalCascadeUser::class);
Expand All @@ -150,6 +153,9 @@ private function getUserRepository(): DocumentRepository
return $repository;
}

/**
* @return DocumentRepository<OrphanRemovalCascadeAddress>
*/
private function getAddressRepository(): DocumentRepository
{
$repository = $this->dm->getRepository(OrphanRemovalCascadeAddress::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ public function testOrphanRemovalReferenceManyOnRemoveWithoutCascade(): void
$this->assertNull($this->getProfileRepository()->find($profile2->id), 'Profile 2 should have been removed');
}

/**
* @return DocumentRepository<OrphanRemovalUser>
*/
private function getUserRepository(): DocumentRepository
{
$repository = $this->dm->getRepository(OrphanRemovalUser::class);
Expand All @@ -291,6 +294,9 @@ private function getUserRepository(): DocumentRepository
return $repository;
}

/**
* @return DocumentRepository<OrphanRemovalProfile>
*/
private function getProfileRepository(): DocumentRepository
{
$repository = $this->dm->getRepository(OrphanRemovalProfile::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RepositoriesTest extends BaseTest
/** @var User */
private $user;

/** @var DocumentRepository */
/** @var DocumentRepository<User> */
private $repository;

public function setUp(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class GH1232Comment
public $post;
}

/**
* @template-extends DocumentRepository<GH1232Comment>
*/
class GH1232CommentRepository extends DocumentRepository
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ public function __construct(GH1572Blog $blog)
}
}

/**
* @template-extends DocumentRepository<GH1572Blog>
*/
class GH1572PostRepository extends DocumentRepository
{
/**
* @return Iterator<GH1572Blog>
*/
public function getPostsForBlog(GH1572Blog $blog): Iterator
{
return $this->createQueryBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,17 @@ public function getId(): ?string
return $this->id;
}

/**
* @return Collection<int, GH597Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}

/**
* @return Collection<int, GH597ReferenceMany>
*/
public function getReferenceMany(): Collection
{
return $this->referenceMany;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class MODM29Doc
*/
protected $collection;

/**
* @param Collection<int, MODM29Embedded> $c
*/
public function __construct(Collection $c)
{
$this->set($c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public function setName(string $name): void
$this->name = $name;
}

/**
* @return Collection<int, MODM81TestEmbeddedDocument>
*/
public function getEmbeddedDocuments(): Collection
{
return $this->embeddedDocuments;
Expand Down
Loading

0 comments on commit a457e72

Please sign in to comment.