Skip to content

Commit

Permalink
Add a test to check proxy initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Dec 26, 2021
1 parent 47642e7 commit 4a1a004
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2349Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Documents74\GH2349Customer;
use Documents74\GH2349Order;

/**
* @requires PHP 7.4
*/
class GH2349Test extends BaseTest
{
/**
* @doesNotPerformAssertions
*/
public function testAccessingUnitializedProperties(): void
{
$customer = new GH2349Customer('Some Place');
$order = new GH2349Order($customer);

$this->dm->persist($customer);
$this->dm->persist($order);
$this->dm->flush();
$this->dm->clear();

$orderId = GH2349Order::ID;

$order = $this->dm->getRepository(GH2349Order::class)->find($orderId);

// Fetch a list of Customers from the DB. Any customer object that was referenced in the above order is still
// a proxy object, however it will not have the defaults set for the un-managed $domainEvents property.
$customers = $this->dm->getRepository(GH2349Customer::class)->findAll();

foreach ($customers as $customer) {
$customer->doSomeUpdate(); // This will trigger an error as we try to append to the non-existing $domainEvents property
}
}
}
39 changes: 39 additions & 0 deletions tests/Documents74/GH2349Customer.php
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\Document()
*/
class GH2349Customer
{
public const ID = '610419a277d50f7609139542';

/** @ODM\Id() */
private string $id;

/** @ODM\Field() */
private string $name;

private array $domainEvents = [];

public function __construct(string $name)
{
$this->id = self::ID;
$this->name = $name;
}

public function doSomeUpdate(): void
{
$this->domainEvents[] = 'a new event!';
}

public function getEvents(): array
{
return $this->domainEvents;
}
}
28 changes: 28 additions & 0 deletions tests/Documents74/GH2349Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Documents74;

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

/**
* @ODM\Document()
*/
class GH2349Order
{
public const ID = '610419a277d50f7609139543';

/** @ODM\Id() */
private string $id;

/** @ODM\ReferenceOne(targetDocument=GH2349Customer::class, storeAs="ref") */
private GH2349Customer $customer;

public function __construct(GH2349Customer $customer)
{
$this->id = (string) new ObjectId(self::ID);
$this->customer = $customer;
}
}

0 comments on commit 4a1a004

Please sign in to comment.