Skip to content

Commit

Permalink
Use attributes in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Dec 5, 2023
1 parent 69a5b08 commit 9d65757
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\Document(repositoryClass=TestCustomClassRepoRepository::class) */
#[ODM\Document(repositoryClass: TestCustomClassRepoRepository::class)]
class TestCustomClassRepoDocument
{
/** @ODM\Id */
#[ODM\Id]
private string $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\Document(repositoryClass=TestCustomServiceRepoDocumentRepository::class) */
#[ODM\Document(repositoryClass: TestCustomServiceRepoDocumentRepository::class)]
class TestCustomServiceRepoDocument
{
/** @ODM\Id */
#[ODM\Id]
private string $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\File(repositoryClass=TestCustomServiceRepoGridFSRepository::class) */
#[ODM\File(repositoryClass: TestCustomServiceRepoGridFSRepository::class)]
class TestCustomServiceRepoFile
{
/** @ODM\Id */
#[ODM\Id]
private string $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\Document */
#[ODM\Document]
class TestDefaultRepoDocument
{
/** @ODM\Id */
#[ODM\Id]
private string $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\File */
#[ODM\File]
class TestDefaultRepoFile
{
/** @ODM\Id */
#[ODM\Id]
private string $id;
}
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/Fixtures/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
'default' => [
'mappings' => [
'RepositoryServiceBundle' => [
'type' => 'annotation',
'type' => 'attribute',
'dir' => __DIR__ . '/Bundles/RepositoryServiceBundle/Document',
'prefix' => 'Fixtures\Bundles\RepositoryServiceBundle\Document',
],
Expand Down
5 changes: 5 additions & 0 deletions Tests/Fixtures/Cache/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@
use MongoDB\BSON\ObjectId;

/** @ODM\Document */
#[ODM\Document]
class Collections
{
/** @ODM\Id */
#[ODM\Id]
public ?ObjectId $id = null;

/** @ODM\EmbedMany(collectionClass=SomeCollection::class) */
#[ODM\EmbedMany(collectionClass: SomeCollection::class)]
public SomeCollection $coll;

/** @ODM\ReferenceMany(collectionClass=SomeCollection::class) */
#[ODM\ReferenceMany(collectionClass: SomeCollection::class)]
public SomeCollection $refs;

/** @ODM\EmbedMany(collectionClass=AnotherCollection::class) */
#[ODM\EmbedMany(collectionClass: AnotherCollection::class)]
public AnotherCollection $another;
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/CommandBundle/Document/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\Document */
#[ODM\Document]
class User
{
/** @ODM\Id */
#[ODM\Id]
private ?string $id = null;
}
4 changes: 4 additions & 0 deletions Tests/Fixtures/DataCollector/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\DataCollector;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\ObjectId;

/** @ODM\Document */
#[ODM\Document]
class Category
{
/** @ODM\Id */
#[ODM\Id]
protected ?ObjectId $id = null;

/** @ODM\Field(type="string") */
#[ODM\Field(type: Type::STRING)]
public string $name;

public function __construct(string $name)
Expand Down
8 changes: 8 additions & 0 deletions Tests/Fixtures/Form/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\ObjectId;

/** @ODM\Document */
#[ODM\Document]
class Category
{
/** @ODM\Id */
#[ODM\Id]
protected ObjectId|string|null $id;

/** @ODM\Field(type="string") */
#[ODM\Field(type: Type::STRING)]
public string $name;

/**
Expand All @@ -26,6 +30,10 @@ class Category
*
* @var Collection<int, Document>
*/
#[ODM\ReferenceMany(
targetDocument: Document::class,
mappedBy: 'categories',
)]
public Collection $documents;

public function __construct(string $name)
Expand Down
10 changes: 10 additions & 0 deletions Tests/Fixtures/Form/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\ObjectId;

/** @ODM\Document */
#[ODM\Document]
class Document
{
/** @ODM\Id(strategy="none") */
#[ODM\Id(strategy: 'none')]
protected ObjectId $id;

/** @ODM\Field(type="string") */
#[ODM\Field(type: Type::STRING)]
public string $name;

/**
Expand All @@ -27,6 +32,11 @@ class Document
*
* @var Collection<int, Category>
*/
#[ODM\ReferenceMany(
targetDocument: Category::class,
inversedBy: 'documents',
strategy: ClassMetadata::STORAGE_STRATEGY_ATOMIC_SET_ARRAY,
)]
public Collection $categories;

public function __construct(ObjectId $id, string $name)
Expand Down
16 changes: 16 additions & 0 deletions Tests/Fixtures/Form/Guesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
use DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\ObjectId;

/** @ODM\Document */
#[ODM\Document]
class Guesser
{
/** @ODM\Id(strategy="none") */
#[ODM\Id(strategy: 'none')]
protected ?ObjectId $id = null;

/** @ODM\Field() */
#[ODM\Field]
public ?string $name = null;

/** @ODM\Field(type="date") */
#[ODM\Field(type: Type::DATE)]
public ?DateTime $date = null;

/** @ODM\Field(type="timestamp") */
#[ODM\Field(type: Type::TIMESTAMP)]
public DateTime $ts;

/**
Expand All @@ -33,22 +40,31 @@ class Guesser
*
* @var Collection<int, Category>
*/
#[ODM\ReferenceMany(
targetDocument: Category::class,
inversedBy: 'documents',
strategy: ClassMetadata::STORAGE_STRATEGY_ATOMIC_SET_ARRAY,
)]
public Collection $categories;

/** @ODM\Field(type="bool") */
#[ODM\Field(type: Type::BOOL)]
public ?bool $boolField = null;

/** @ODM\Field(type="float") */
#[ODM\Field(type: Type::FLOAT)]
public ?float $floatField = null;

/** @ODM\Field(type="int") */
#[ODM\Field(type: Type::INT)]
public ?int $intField = null;

/**
* @ODM\Field(type="collection")
*
* @var array
*/
#[ODM\Field(type: Type::COLLECTION)]
public array $collectionField;

public mixed $nonMappedField;
Expand Down
4 changes: 4 additions & 0 deletions Tests/Fixtures/Security/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Security;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\ObjectId;
use Symfony\Component\Security\Core\User\UserInterface;

/** @ODM\Document */
#[ODM\Document]
class User implements UserInterface
{
/** @ODM\Id(strategy="none") */
#[ODM\Id(strategy: 'none')]
protected ObjectId $id;

/** @ODM\Field(type="string") */
#[ODM\Field(type: Type::STRING)]
public string $name;

public function __construct(ObjectId $id, string $name)
Expand Down
11 changes: 11 additions & 0 deletions Tests/Fixtures/Validator/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,51 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Validator;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;
use MongoDB\BSON\ObjectId;

/** @ODM\Document(collection="DoctrineMongoDBBundle_Tests_Validator_Document") */
#[ODM\Document(collection: 'DoctrineMongoDBBundle_Tests_Validator_Document')]
class Document
{
/** @ODM\Id(strategy="none") */
#[ODM\Id(strategy: 'none')]
protected ObjectId $id;

/** @ODM\Field(type="string") */
#[ODM\Field(type: Type::STRING)]
public string $name;

/**
* @ODM\Field(type="hash")
*
* @var array
*/
#[ODM\Field(type: Type::HASH)]
public array $hash;

/**
* @ODM\Field(type="collection")
*
* @var array
*/
#[ODM\Field(type: Type::COLLECTION)]
public array $collection;

/** @ODM\ReferenceOne(targetDocument="Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Validator\Document") */
#[ODM\ReferenceOne(targetDocument: self::class)]
public ?Document $referenceOne = null;

/** @ODM\EmbedOne(targetDocument="Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Validator\EmbeddedDocument") */
#[ODM\EmbedOne(targetDocument: EmbeddedDocument::class)]
public ?EmbeddedDocument $embedOne = null;

/**
* @ODM\EmbedMany(targetDocument="Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\Validator\EmbeddedDocument")
*
* @var EmbeddedDocument[]
*/
#[ODM\EmbedMany(targetDocument: EmbeddedDocument::class)]
public array $embedMany = [];

public function __construct(ObjectId $id)
Expand All @@ -50,8 +59,10 @@ public function __construct(ObjectId $id)
}

/** @ODM\EmbeddedDocument */
#[ODM\EmbeddedDocument]
class EmbeddedDocument
{
/** @ODM\Field(type="string") */
#[ODM\Field(type: Type::STRING)]
public string $name;
}
2 changes: 1 addition & 1 deletion Tests/ServiceRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function setUp(): void
'default' => [
'mappings' => [
'RepositoryServiceBundle' => [
'type' => 'annotation',
'type' => 'attribute',
'dir' => __DIR__ . '/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Document',
'prefix' => 'Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\RepositoryServiceBundle\Document',
],
Expand Down
5 changes: 2 additions & 3 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Doctrine\Bundle\MongoDBBundle\Tests;

use Doctrine\Common\Annotations\AnnotationReader;
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 PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

Expand All @@ -24,7 +23,7 @@ public static function createTestDocumentManager(array $paths = []): DocumentMan
$config->setHydratorDir(sys_get_temp_dir());
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setHydratorNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), $paths));
$config->setMetadataDriverImpl(new AttributeDriver($paths));
$config->setMetadataCache(new ArrayAdapter());

return DocumentManager::create(null, $config);
Expand Down

0 comments on commit 9d65757

Please sign in to comment.