-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
17 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
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
75 changes: 75 additions & 0 deletions
75
tests/Doctrine/ODM/MongoDB/Tests/Functional/TypedPropertiesTest.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 Doctrine\ODM\MongoDB\Tests\Functional; | ||
|
||
use Doctrine\ODM\MongoDB\Tests\BaseTest; | ||
use Documents74\TypedDocument; | ||
use Documents74\TypedEmbeddedDocument; | ||
use MongoDB\BSON\ObjectId; | ||
use function phpversion; | ||
use function version_compare; | ||
|
||
class TypedPropertiesTest extends BaseTest | ||
{ | ||
public function setUp() : void | ||
{ | ||
if (version_compare((string) phpversion(), '7.4.0', '<')) { | ||
$this->markTestSkipped('PHP 7.4 is required to run this test'); | ||
} | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testPersistNew() : void | ||
{ | ||
$doc = new TypedDocument(); | ||
$doc->setName('Maciej'); | ||
$doc->setEmbedOne(new TypedEmbeddedDocument('The answer', 42)); | ||
$doc->getEmbedMany()->add(new TypedEmbeddedDocument('Lucky number', 7)); | ||
$this->dm->persist($doc); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
/** @var TypedDocument $saved */ | ||
$saved = $this->dm->find(TypedDocument::class, $doc->getId()); | ||
$this->assertEquals($doc->getId(), $saved->getId()); | ||
$this->assertSame($doc->getName(), $saved->getName()); | ||
$this->assertEquals($doc->getEmbedOne(), $saved->getEmbedOne()); | ||
$this->assertEquals($doc->getEmbedMany()->getValues(), $saved->getEmbedMany()->getValues()); | ||
} | ||
|
||
public function testMerge() : void | ||
{ | ||
$doc = new TypedDocument(); | ||
$doc->setId((string) new ObjectId()); | ||
$doc->setName('Maciej'); | ||
$doc->setEmbedOne(new TypedEmbeddedDocument('The answer', 42)); | ||
$doc->getEmbedMany()->add(new TypedEmbeddedDocument('Lucky number', 7)); | ||
|
||
$merged = $this->dm->merge($doc); | ||
$this->assertEquals($doc->getId(), $merged->getId()); | ||
$this->assertSame($doc->getName(), $merged->getName()); | ||
$this->assertEquals($doc->getEmbedOne(), $merged->getEmbedOne()); | ||
$this->assertEquals($doc->getEmbedMany()->getValues(), $merged->getEmbedMany()->getValues()); | ||
} | ||
|
||
public function testProxying() : void | ||
{ | ||
$doc = new TypedDocument(); | ||
$doc->setName('Maciej'); | ||
$doc->setEmbedOne(new TypedEmbeddedDocument('The answer', 42)); | ||
$doc->getEmbedMany()->add(new TypedEmbeddedDocument('Lucky number', 7)); | ||
$this->dm->persist($doc); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
/** @var TypedDocument $proxy */ | ||
$proxy = $this->dm->getReference(TypedDocument::class, $doc->getId()); | ||
$this->assertEquals($doc->getId(), $proxy->getId()); | ||
$this->assertSame($doc->getName(), $proxy->getName()); | ||
$this->assertEquals($doc->getEmbedOne(), $proxy->getEmbedOne()); | ||
$this->assertEquals($doc->getEmbedMany()->getValues(), $proxy->getEmbedMany()->getValues()); | ||
} | ||
} |
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 Documents74; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
/** | ||
* @ODM\Document() | ||
*/ | ||
class TypedDocument | ||
{ | ||
/** | ||
* @ODM\Id() | ||
*/ | ||
private string $id; | ||
|
||
/** | ||
* @ODM\Field(type="string") | ||
*/ | ||
private string $name; | ||
|
||
/** | ||
* @ODM\EmbedOne(targetDocument=TypedEmbeddedDocument::class) | ||
*/ | ||
private TypedEmbeddedDocument $embedOne; | ||
|
||
/** | ||
* @ODM\EmbedMany(targetDocument=TypedEmbeddedDocument::class) | ||
*/ | ||
private Collection $embedMany; | ||
|
||
public function __construct() | ||
{ | ||
$this->embedMany = new ArrayCollection(); | ||
} | ||
|
||
public function getId() : string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function setId(string $id) : void | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function getName() : string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name) : void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getEmbedOne() : TypedEmbeddedDocument | ||
{ | ||
return $this->embedOne; | ||
} | ||
|
||
public function setEmbedOne(TypedEmbeddedDocument $embedOne) : void | ||
{ | ||
$this->embedOne = $embedOne; | ||
} | ||
|
||
public function getEmbedMany() : Collection | ||
{ | ||
return $this->embedMany; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documents74; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
/** | ||
* @ODM\EmbeddedDocument() | ||
*/ | ||
class TypedEmbeddedDocument | ||
{ | ||
/** | ||
* @ODM\Field(type="string") | ||
*/ | ||
private string $name; | ||
|
||
/** | ||
* @ODM\Field(type="int") | ||
*/ | ||
private int $number; | ||
|
||
public function __construct(string $name, int $number) | ||
{ | ||
$this->name = $name; | ||
$this->number = $number; | ||
} | ||
|
||
public function getName() : string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getNumber() : int | ||
{ | ||
return $this->number; | ||
} | ||
} |