-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Review mapping ORM and ODM cookbook (#2658)
* Review mapping ORM and ODM cookbook * Add tests on mapping ORM and ODM * ORM test requires pdo_sqlite
- Loading branch information
Showing
6 changed files
with
192 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Documentation/MappingOrmAndOdm/BlogPostRepositoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/Documentation/MappingOrmAndOdm/MappingOrmAndOdmTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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; | ||
use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
|
||
#[RequiresPhpExtension('pdo_sqlite')] | ||
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 | ||
$ormBlogPostRepository = $em->getRepository(BlogPost::class); | ||
$this->assertInstanceOf(OrmBlogPostRepository::class, $ormBlogPostRepository); | ||
$ormBlogPost = $ormBlogPostRepository->findPostById($blogPost->id); | ||
|
||
$odmBlogPostRepository = $dm->getRepository(BlogPost::class); | ||
$this->assertInstanceOf(OdmBlogPostRepository::class, $odmBlogPostRepository); | ||
$odmBlogPost = $odmBlogPostRepository->findPostById($blogPost->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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Documentation/MappingOrmAndOdm/OdmBlogPostRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
tests/Documentation/MappingOrmAndOdm/OrmBlogPostRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |