1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Zenstruck\Foundry\AnonymousFactory; |
6
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
7
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Contact; |
8
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory; |
9
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Kevin Bond <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class ORMProxyTest extends ProxyTest |
15
|
|
|
{ |
16
|
|
|
protected function setUp(): void |
17
|
|
|
{ |
18
|
|
|
if (false === \getenv('DATABASE_URL')) { |
19
|
|
|
self::markTestSkipped('doctrine/orm not enabled.'); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
* @requires PHP >= 7.4 |
26
|
|
|
*/ |
27
|
|
|
public function cannot_convert_to_string_if_underlying_object_cant(): void |
28
|
|
|
{ |
29
|
|
|
$this->expectException(\RuntimeException::class); |
30
|
|
|
$this->expectExceptionMessage(\sprintf('Proxied object "%s" cannot be converted to a string.', Category::class)); |
31
|
|
|
|
32
|
|
|
(string) CategoryFactory::createOne(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
* @requires PHP < 7.4 |
38
|
|
|
*/ |
39
|
|
|
public function on_php_versions_less_than_7_4_if_underlying_object_is_missing_to_string_proxy_to_string_returns_note(): void |
40
|
|
|
{ |
41
|
|
|
$this->assertSame('(no __toString)', (string) CategoryFactory::createOne()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
*/ |
47
|
|
|
public function can_autorefresh_entity_with_embedded_object(): void |
48
|
|
|
{ |
49
|
|
|
$contact = AnonymousFactory::new(Contact::class)->create(['name' => 'john']) |
50
|
|
|
->enableAutoRefresh() |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
$this->assertSame('john', $contact->getName()); |
|
|
|
|
54
|
|
|
|
55
|
|
|
// I discovered when autorefreshing the second time, the embedded |
56
|
|
|
// object is included in the changeset when using UOW::recomputeSingleEntityChangeSet(). |
57
|
|
|
// Changing to UOW::computeChangeSet() fixes this. |
58
|
|
|
$this->assertSame('john', $contact->getName()); |
59
|
|
|
$this->assertNull($contact->getAddress()->getValue()); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$contact->getAddress()->setValue('address'); |
62
|
|
|
$contact->save(); |
63
|
|
|
|
64
|
|
|
$this->assertSame('address', $contact->getAddress()->getValue()); |
65
|
|
|
|
66
|
|
|
self::ensureKernelShutdown(); |
67
|
|
|
self::bootKernel(); |
68
|
|
|
|
69
|
|
|
$this->assertSame('address', $contact->getAddress()->getValue()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function postFactoryClass(): string |
73
|
|
|
{ |
74
|
|
|
return PostFactory::class; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function registryServiceId(): string |
78
|
|
|
{ |
79
|
|
|
return 'doctrine'; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|