Skip to content

Commit

Permalink
Add tests on mapping ORM and ODM
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jun 27, 2024
1 parent 98a1963 commit b378961
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"ext-bcmath": "*",
"doctrine/annotations": "^1.12 || ^2.0",
"doctrine/coding-standard": "^12.0",
"doctrine/orm": "^3.2",
"jmikola/geojson": "^1.0",
"phpbench/phpbench": "^1.0.0",
"phpstan/phpstan": "~1.10.67",
Expand All @@ -63,8 +64,8 @@
"psr-4": {
"Doctrine\\ODM\\MongoDB\\Benchmark\\": "benchmark",
"Doctrine\\ODM\\MongoDB\\Tests\\": "tests/Doctrine/ODM/MongoDB/Tests",
"Documentation\\": "tests/Documentation",
"Documents\\": "tests/Documents",
"Documents81\\": "tests/Documents81",
"Stubs\\": "tests/Stubs",
"TestDocuments\\" :"tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures"
}
Expand Down
35 changes: 25 additions & 10 deletions docs/en/cookbook/mapping-classes-to-orm-and-odm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ for multiple Doctrine persistence layers:
class BlogPost
{
public string $id;
public int $id;
public string $title;
public string $body;
}
Expand Down Expand Up @@ -52,8 +52,9 @@ First define the mapping for the ORM:
class BlogPost
{
#[ORM\Id]
#[ORM\Column(type: 'int')]
public string $id;
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public int $id;
#[ORM\Column(type: 'string')]
public string $title;
Expand Down Expand Up @@ -85,7 +86,7 @@ Now you are able to persist the ``Documents\Blog\BlogPost`` with an instance of
<?php
$blogPost = new BlogPost();
$blogPost->title = 'test';
$blogPost->title = 'Hello World!';
$em->persist($blogPost);
$em->flush();
Expand All @@ -96,7 +97,7 @@ You can find the blog post:
<?php
$blogPost = $em->getRepository(BlogPost::class)->findOneBy(['title' => 'test']);
$blogPost = $em->getRepository(BlogPost::class)->findOneBy(['title' => 'Hello World!']);
MongoDB ODM
~~~~~~~~~~~
Expand All @@ -117,8 +118,8 @@ Now map the same class to the Doctrine MongoDB ODM:
#[ODM\Document(repositoryClass: BlogPostRepository::class)]
class BlogPost
{
#[ODM\Id]
public string $id;
#[ODM\Id(type: 'int', strategy: 'INCREMENT')]
public int $id;
#[ODM\Field]
public string $title;
Expand All @@ -142,14 +143,21 @@ Now map the same class to the Doctrine MongoDB ODM:
</document>
</doctrine-mongo-mapping>
Now the same class is able to be persisted in the same way using an instance of ``DocumentManager``:
.. note::

We use the ``INCREMENT`` strategy for the MongoDB ODM for compatibility with
the ORM mapping. But you can also use the default ``AUTO`` strategy
and store a generated MongoDB ObjectId as a string in the SQL database.

Now the same class is able to be persisted in the same way using an instance of
``DocumentManager``:

.. code-block:: php
<?php
$blogPost = new BlogPost();
$blogPost->title = 'test';
$blogPost->title = 'Hello World!';
$dm->persist($blogPost);
$dm->flush();
Expand All @@ -160,7 +168,7 @@ You can find the blog post:
<?php
$blogPost = $dm->getRepository(BlogPost::class)->findOneBy(['title' => 'test']);
$blogPost = $dm->getRepository(BlogPost::class)->findOneBy(['title' => 'Hello World!']);
Repository Classes
------------------
Expand Down Expand Up @@ -225,4 +233,11 @@ PHP objects. The data is transparently injected to the objects for you automatic
are not forced to extend some base class or shape your domain in any certain way for it to work
with the Doctrine persistence layers.

.. note::

If the same class is mapped to both the ORM and ODM, and you persist the
instance in both, you will have two separate instances in memory. This is
because the ORM and ODM are separate libraries and do not share the same
object manager.

.. _persistence: https://github.com/doctrine/persistence
28 changes: 28 additions & 0 deletions tests/Documentation/MappingOrmAndOdm/BlogPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Documentation\MappingOrmAndOdm;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: OrmBlogPostRepository::class)]
#[ORM\Table(name: 'blog_posts')]
#[ODM\Document(repositoryClass: OdmBlogPostRepository::class)]
class BlogPost
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ODM\Id(type: 'int', strategy: 'INCREMENT')]
public int $id;

#[ORM\Column(type: 'string')]
#[ODM\Field]
public string $title;

#[ORM\Column(type: 'text')]
#[ODM\Field]
public string $body;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Documentation\MappingOrmAndOdm;

interface BlogPostRepositoryInterface
{
public function findPostById(int $id): ?BlogPost;
}
68 changes: 68 additions & 0 deletions tests/Documentation/MappingOrmAndOdm/MappingOrmAndOdmTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Documentation\MappingOrmAndOdm;

use Doctrine\DBAL\DriverManager;
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;

class MappingOrmAndOdmTest extends BaseTestCase
{
public function testTest(): void
{
// Init ORM
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: [__DIR__],
isDevMode: true,
);
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'path' => ':memory:',
], $config);
$connection->executeQuery('CREATE TABLE blog_posts (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, body TEXT)');
$em = new EntityManager($connection, $config);

// Init ODM
$dm = $this->dm;

// Create and persist a BlogPost in both ORM and ODM
$blogPost = new BlogPost();
$blogPost->title = 'Hello World!';

$em->persist($blogPost);
$em->flush();
$em->clear();

$dm->persist($blogPost);
$dm->flush();
$dm->clear();

// Load the BlogPost from both ORM and ODM
$ormBlogPost = $em->find(BlogPost::class, $blogPost->id);
$odmBlogPost = $dm->find(BlogPost::class, $blogPost->id);

$this->assertSame($blogPost->id, $ormBlogPost->id);
$this->assertSame($blogPost->id, $odmBlogPost->id);
$this->assertSame($blogPost->title, $ormBlogPost->title);
$this->assertSame($blogPost->title, $odmBlogPost->title);

// Different Object Managers are used, so the instances are different
$this->assertNotSame($odmBlogPost, $ormBlogPost);

$dm->clear();
$em->clear();

// Remove the BlogPost from both ORM and ODM using the repository
$ormBlogPost = $em->getRepository(BlogPost::class)->findPostById($blogPost->id);

Check failure on line 59 in tests/Documentation/MappingOrmAndOdm/MappingOrmAndOdmTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Call to an undefined method Doctrine\ORM\EntityRepository<Documentation\MappingOrmAndOdm\BlogPost>::findPostById().
$odmBlogPost = $dm->getRepository(BlogPost::class)->findPostById($blogPost->id);

Check failure on line 60 in tests/Documentation/MappingOrmAndOdm/MappingOrmAndOdmTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Call to an undefined method Doctrine\ODM\MongoDB\Repository\DocumentRepository<Documentation\MappingOrmAndOdm\BlogPost>|Doctrine\ODM\MongoDB\Repository\GridFSRepository<Documentation\MappingOrmAndOdm\BlogPost>|Doctrine\ODM\MongoDB\Repository\ViewRepository<Documentation\MappingOrmAndOdm\BlogPost>::findPostById().

$this->assertSame($blogPost->title, $ormBlogPost->title);
$this->assertSame($blogPost->title, $odmBlogPost->title);

// Different Object Managers are used, so the instances are different
$this->assertNotSame($odmBlogPost, $ormBlogPost);
}
}
15 changes: 15 additions & 0 deletions tests/Documentation/MappingOrmAndOdm/OdmBlogPostRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Documentation\MappingOrmAndOdm;

use Doctrine\ODM\MongoDB\Repository\DocumentRepository;

final class OdmBlogPostRepository extends DocumentRepository implements BlogPostRepositoryInterface
{
public function findPostById(int $id): ?BlogPost
{
return $this->findOneBy(['id' => $id]);
}
}
15 changes: 15 additions & 0 deletions tests/Documentation/MappingOrmAndOdm/OrmBlogPostRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Documentation\MappingOrmAndOdm;

use Doctrine\ORM\EntityRepository;

final class OrmBlogPostRepository extends EntityRepository implements BlogPostRepositoryInterface
{
public function findPostById(int $id): ?BlogPost
{
return $this->findOneBy(['id' => $id]);
}
}

0 comments on commit b378961

Please sign in to comment.