-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review and add tests on
ResolveTargetDocumentListener
(#2660)
* Review and add tests on ResolveTargetDocumentListener doc * Get field type from prop type
- Loading branch information
Showing
6 changed files
with
204 additions
and
57 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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\ResolveTargetDocument; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; | ||
use Documentation\ResolveTargetDocument\CustomerModule\Customer as BaseCustomer; | ||
use Documentation\ResolveTargetDocument\InvoiceModule\InvoiceSubjectInterface; | ||
|
||
#[Document] | ||
class Customer extends BaseCustomer implements InvoiceSubjectInterface | ||
{ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Documentation/ResolveTargetDocument/CustomerModule/Customer.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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\ResolveTargetDocument\CustomerModule; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Field; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; | ||
|
||
#[Document] | ||
abstract class Customer | ||
{ | ||
#[Id] | ||
public string $id; | ||
|
||
#[Field] | ||
public string $name; | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Documentation/ResolveTargetDocument/InvoiceModule/Invoice.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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\ResolveTargetDocument\InvoiceModule; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceOne; | ||
|
||
#[Document] | ||
class Invoice | ||
{ | ||
#[Id] | ||
public string $id; | ||
|
||
#[ReferenceOne] | ||
public InvoiceSubjectInterface $subject; | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/Documentation/ResolveTargetDocument/InvoiceModule/InvoiceSubjectInterface.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\ResolveTargetDocument\InvoiceModule; | ||
|
||
interface InvoiceSubjectInterface | ||
{ | ||
public function getName(): string; | ||
} |
42 changes: 42 additions & 0 deletions
42
tests/Documentation/ResolveTargetDocument/ResolveTargetDocumentTest.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\ResolveTargetDocument; | ||
|
||
use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
use Doctrine\ODM\MongoDB\Tools\ResolveTargetDocumentListener; | ||
use Documentation\ResolveTargetDocument\InvoiceModule\Invoice; | ||
use Documentation\ResolveTargetDocument\InvoiceModule\InvoiceSubjectInterface; | ||
|
||
class ResolveTargetDocumentTest extends BaseTestCase | ||
{ | ||
public function testTest(): void | ||
{ | ||
$evm = $this->dm->getEventManager(); | ||
$rtdl = new ResolveTargetDocumentListener(); | ||
|
||
// Adds a target-document class | ||
$rtdl->addResolveTargetDocument( | ||
InvoiceSubjectInterface::class, | ||
Customer::class, | ||
[], | ||
); | ||
|
||
$evm->addEventSubscriber($rtdl); | ||
|
||
$customer = new Customer(); | ||
$customer->name = 'Example Customer'; | ||
$invoice = new Invoice(); | ||
$invoice->subject = $customer; | ||
|
||
$this->dm->persist($customer); | ||
$this->dm->persist($invoice); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$invoice = $this->dm->find(Invoice::class, $invoice->id); | ||
$this->assertInstanceOf(Customer::class, $invoice->subject); | ||
$this->assertSame('Example Customer', $invoice->subject->name); | ||
} | ||
} |