Skip to content

Commit 719710a

Browse files
authored
test: ensure Proxy::_real() always return same object (#809)
1 parent d15de0e commit 719710a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

tests/Integration/ORM/EntityRelationship/ProxyEntityFactoryRelationshipTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ public function can_use_assert_persisted_when_entity_has_changes(): void
133133
$contact->_assertPersisted();
134134
}
135135

136+
/** @test */
137+
#[Test]
138+
public function real_method_always_return_same_instance(): void
139+
{
140+
$category = static::categoryFactory()->create();
141+
142+
$this->assertSame($category->_real(), $category->_real());
143+
144+
$category->_real()->addContact($contact1 = static::contactFactory()->create()->_real());
145+
$category->_real()->addContact($contact2 = static::contactFactory()->create()->_real());
146+
147+
$category->_real()->addSecondaryContact($contact3 = static::contactFactory()->create()->_real());
148+
$category->_real()->addSecondaryContact($contact4 = static::contactFactory()->create()->_real());
149+
150+
$category->_save();
151+
152+
$this->assertSame($category->_real(), $category->_real());
153+
154+
$this->assertSame([$contact1, $contact2], $category->getContacts()->getValues());
155+
$this->assertSame([$contact1, $contact2], $category->_real()->getContacts()->getValues());
156+
157+
$this->assertSame([$contact3, $contact4], $category->getSecondaryContacts()->getValues());
158+
$this->assertSame([$contact3, $contact4], $category->_real()->getSecondaryContacts()->getValues());
159+
}
160+
136161
protected static function contactFactory(): ProxyContactFactory
137162
{
138163
return ProxyContactFactory::new();

tests/Integration/Persistence/GenericProxyFactoryTestCase.php

+15
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ public function can_get_real_object_even_if_modified(): void
248248
self::assertSame('foo', $real->getProp1());
249249
}
250250

251+
/**
252+
* @test
253+
*/
254+
#[Test]
255+
public function assert_real_method_returns_same_object_regardless_of_auto_refresh(): void
256+
{
257+
$object = static::factory()->create();
258+
259+
self::assertSame($object->_real(withAutoRefresh: false), $object->_real());
260+
261+
// check with a modified object
262+
$object->setProp1('foo');
263+
self::assertSame($object->_real(withAutoRefresh: false), $object->_real());
264+
}
265+
251266
/**
252267
* @test
253268
*/

0 commit comments

Comments
 (0)