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

Use attributes in tests #2596

Merged
merged 5 commits into from Mar 6, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmark/BaseBench.php
Expand Up @@ -6,7 +6,7 @@

use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use MongoDB\Client;
use MongoDB\Model\DatabaseInfo;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
Expand Down Expand Up @@ -80,8 +80,8 @@ public function clearDatabase(): void
}
}

protected static function createMetadataDriverImpl(): AnnotationDriver
protected static function createMetadataDriverImpl(): AttributeDriver
{
return AnnotationDriver::create(__DIR__ . '/../tests/Documents');
return AttributeDriver::create(__DIR__ . '/../tests/Documents');
}
}
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -790,6 +790,16 @@ parameters:
count: 1
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/NestedDocumentsTest.php

-
message: "#^Parameter \\$discriminatorMap of attribute class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\Annotations\\\\ReferenceOne constructor expects array\\<string, class\\-string\\>\\|null, array\\<string, string\\> given\\.$#"
count: 1
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/TargetDocumentTest.php

-
message: "#^Parameter \\$targetDocument of attribute class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\Annotations\\\\ReferenceOne constructor expects class\\-string\\|null, string given\\.$#"
count: 1
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/TargetDocumentTest.php

-
message: "#^Property Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\Functional\\\\Ticket\\\\GH1058PersistDocument\\:\\:\\$id is never written, only read\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -391,6 +391,11 @@
<code><![CDATA[['upsert' => true]]]></code>
</InvalidArgument>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Functional/TargetDocumentTest.php">
<UndefinedClass>
<code><![CDATA[['Foo' => 'Doctrine\ODM\MongoDB\Tests\Functional\SomeInvalidClass']]]></code>
</UndefinedClass>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH1011Test.php">
<InvalidArgument>
<code><![CDATA[$doc->embeds]]></code>
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/BaseTestCase.php
Expand Up @@ -6,7 +6,7 @@

use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use Doctrine\ODM\MongoDB\Tests\Query\Filter\Filter;
use Doctrine\ODM\MongoDB\UnitOfWork;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
Expand Down Expand Up @@ -116,7 +116,7 @@ public static function assertArraySubset(array $subset, array $array, bool $chec

protected static function createMetadataDriverImpl(): MappingDriver
{
return AnnotationDriver::create(__DIR__ . '/../../../../Documents');
return AttributeDriver::create(__DIR__ . '/../../../../Documents');
}

protected static function createTestDocumentManager(): DocumentManager
Expand Down
53 changes: 16 additions & 37 deletions tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php
Expand Up @@ -231,59 +231,38 @@ public function testDifferentStoreAsDbReferences(): void
}
}

/** @ODM\Document */
#[ODM\Document]
class WrongSimpleRefDocument
{
/**
* @ODM\Id
*
* @var string|null
*/
/** @var string|null */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for later, we can use typed properties.

#[ODM\Id]
public $id;

/**
* @ODM\ReferenceOne(targetDocument=Documents\Tournament\Participant::class, storeAs="id")
*
* @var Participant|null
*/
/** @var Participant|null */
#[ODM\ReferenceOne(targetDocument: Participant::class, storeAs: 'id')]
public $ref;
}

/** @ODM\Document */
#[ODM\Document]
class ReferenceStoreAsDocument
{
/**
* @ODM\Id
*
* @var string|null
*/
/** @var string|null */
#[ODM\Id]
public $id;

/**
* @ODM\ReferenceOne(targetDocument=User::class, storeAs="id")
*
* @var User|null
*/
/** @var User|null */
#[ODM\ReferenceOne(targetDocument: User::class, storeAs: 'id')]
public $ref1;

/**
* @ODM\ReferenceOne(targetDocument=User::class, storeAs="dbRef")
*
* @var Collection<int, User>
*/
/** @var Collection<int, User> */
#[ODM\ReferenceOne(targetDocument: User::class, storeAs: 'dbRef')]
public $ref2;

/**
* @ODM\ReferenceOne(targetDocument=User::class, storeAs="dbRefWithDb")
*
* @var Collection<int, User>
*/
/** @var Collection<int, User> */
#[ODM\ReferenceOne(targetDocument: User::class, storeAs: 'dbRefWithDb')]
public $ref3;

/**
* @ODM\ReferenceOne(targetDocument=User::class, storeAs="ref")
*
* @var Collection<int, User>
*/
/** @var Collection<int, User> */
#[ODM\ReferenceOne(targetDocument: User::class, storeAs: 'ref')]
public $ref4;
}
116 changes: 39 additions & 77 deletions tests/Doctrine/ODM/MongoDB/Tests/Events/LifecycleCallbacksTest.php
Expand Up @@ -246,110 +246,72 @@ public function testEventsNotFiredForInverseSide(): void
}
}

/** @ODM\Document */
#[ODM\Document]
class User extends BaseDocument
{
/**
* @ODM\Id
*
* @var string|null
*/
/** @var string|null */
#[ODM\Id]
public $id;

/**
* @ODM\EmbedOne(targetDocument=Profile::class)
*
* @var Profile|null
*/
/** @var Profile|null */
#[ODM\EmbedOne(targetDocument: Profile::class)]
public $profile;

/**
* @ODM\EmbedMany(targetDocument=Profile::class)
*
* @var Collection<int, Profile>|array<Profile>
*/
/** @var Collection<int, Profile>|array<Profile> */
#[ODM\EmbedMany(targetDocument: Profile::class)]
public $profiles = [];

/**
* @ODM\ReferenceMany(targetDocument=User::class)
*
* @var Collection<int, User>|array<User>
*/
/** @var Collection<int, User>|array<User> */
#[ODM\ReferenceMany(targetDocument: self::class)]
public $friends = [];
}

/** @ODM\Document */
#[ODM\Document]
class Cart extends BaseDocument
{
/**
* @ODM\Id
*
* @var string|null
*/
/** @var string|null */
#[ODM\Id]
public $id;

/**
* @ODM\ReferenceOne(targetDocument=Customer::class, inversedBy="cart")
*
* @var Customer|null
*/
/** @var Customer|null */
#[ODM\ReferenceOne(targetDocument: Customer::class, inversedBy: 'cart')]
public $customer;
}

/** @ODM\Document */
#[ODM\Document]
class Customer extends BaseDocument
{
/**
* @ODM\Id
*
* @var string|null
*/
/** @var string|null */
#[ODM\Id]
public $id;

/**
* @ODM\ReferenceOne(targetDocument=Cart::class, mappedBy="customer")
*
* @var Cart|null
*/
/** @var Cart|null */
#[ODM\ReferenceOne(targetDocument: Cart::class, mappedBy: 'customer')]
public $cart;
}

/** @ODM\EmbeddedDocument */
#[ODM\EmbeddedDocument]
class Profile extends BaseDocument
{
/**
* @ODM\EmbedOne(targetDocument=Profile::class)
*
* @var Profile|null
*/
/** @var Profile|null */
#[ODM\EmbedOne(targetDocument: self::class)]
public $profile;
}

/**
* @ODM\MappedSuperclass
* @ODM\HasLifecycleCallbacks
*/
#[ODM\MappedSuperclass]
#[ODM\HasLifecycleCallbacks]
abstract class BaseDocument
{
/**
* @ODM\Field(type="string")
*
* @var string|null
*/
/** @var string|null */
#[ODM\Field(type: 'string')]
public $name;

/**
* @ODM\Field(type="date")
*
* @var DateTime
*/
/** @var DateTime */
#[ODM\Field(type: 'date')]
public $createdAt;

/**
* @ODM\Field(type="date")
*
* @var DateTime|null
*/
/** @var DateTime|null */
#[ODM\Field(type: 'date')]
public $updatedAt;

/** @var bool */
Expand Down Expand Up @@ -379,57 +341,57 @@ abstract class BaseDocument
/** @var bool */
public $preFlush = false;

/** @ODM\PrePersist */
#[ODM\PrePersist]
public function prePersist(Event\LifecycleEventArgs $e): void
{
$this->prePersist = true;
$this->createdAt = new DateTime();
}

/** @ODM\PostPersist */
#[ODM\PostPersist]
public function postPersist(Event\LifecycleEventArgs $e): void
{
$this->postPersist = true;
}

/** @ODM\PreUpdate */
#[ODM\PreUpdate]
public function preUpdate(Event\PreUpdateEventArgs $e): void
{
$this->preUpdate = true;
$this->updatedAt = new DateTime();
}

/** @ODM\PostUpdate */
#[ODM\PostUpdate]
public function postUpdate(Event\LifecycleEventArgs $e): void
{
$this->postUpdate = true;
}

/** @ODM\PreRemove */
#[ODM\PreRemove]
public function preRemove(Event\LifecycleEventArgs $e): void
{
$this->preRemove = true;
}

/** @ODM\PostRemove */
#[ODM\PostRemove]
public function postRemove(Event\LifecycleEventArgs $e): void
{
$this->postRemove = true;
}

/** @ODM\PreLoad */
#[ODM\PreLoad]
public function preLoad(Event\PreLoadEventArgs $e): void
{
$this->preLoad = true;
}

/** @ODM\PostLoad */
#[ODM\PostLoad]
public function postLoad(Event\LifecycleEventArgs $e): void
{
$this->postLoad = true;
}

/** @ODM\PreFlush */
#[ODM\PreFlush]
public function preFlush(Event\PreFlushEventArgs $e): void
{
$this->preFlush = true;
Expand Down