|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Unit; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\Persistence\ObjectManager; |
|
7
|
|
|
use Zenstruck\Foundry\Proxy; |
|
8
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category; |
|
9
|
|
|
use Zenstruck\Foundry\Tests\UnitTestCase; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Kevin Bond <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ProxyTest extends UnitTestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @test |
|
18
|
|
|
*/ |
|
19
|
|
|
public function can_force_get_and_set_non_public_properties(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$proxy = new Proxy(new Category()); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertNull($proxy->forceGet('name')); |
|
24
|
|
|
|
|
25
|
|
|
$proxy->forceSet('name', 'foo'); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertSame('foo', $proxy->forceGet('name')); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
*/ |
|
33
|
|
|
public function can_access_wrapped_objects_properties(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$proxy = new Proxy(new class() { |
|
36
|
|
|
public $property; |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertFalse(isset($proxy->property)); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$proxy->property = 'foo'; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$this->assertSame('foo', $proxy->property); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertTrue(isset($proxy->property)); |
|
46
|
|
|
|
|
47
|
|
|
unset($proxy->property); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertFalse(isset($proxy->property)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @test |
|
54
|
|
|
*/ |
|
55
|
|
|
public function cannot_refresh_unpersisted_proxy(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->expectException(\RuntimeException::class); |
|
58
|
|
|
$this->expectExceptionMessage('Cannot refresh unpersisted object (Zenstruck\Foundry\Tests\Fixtures\Entity\Category).'); |
|
59
|
|
|
|
|
60
|
|
|
(new Proxy(new Category()))->refresh(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
*/ |
|
66
|
|
|
public function saving_unpersisted_proxy_changes_it_to_a_persisted_proxy(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$registry = $this->createMock(ManagerRegistry::class); |
|
69
|
|
|
$registry |
|
70
|
|
|
->expects($this->exactly(2)) |
|
71
|
|
|
->method('getManagerForClass') |
|
72
|
|
|
->with(Category::class) |
|
73
|
|
|
->willReturn($this->createMock(ObjectManager::class)) |
|
74
|
|
|
; |
|
75
|
|
|
|
|
76
|
|
|
$this->configuration->setManagerRegistry($registry); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$category = new Proxy(new Category()); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertFalse($category->isPersisted()); |
|
81
|
|
|
|
|
82
|
|
|
$category->save(); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTrue($category->isPersisted()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @test |
|
89
|
|
|
*/ |
|
90
|
|
|
public function can_use_without_auto_refresh_with_proxy_or_object_typehint(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$proxy = new Proxy(new Category()); |
|
93
|
|
|
$calls = 0; |
|
94
|
|
|
|
|
95
|
|
|
$proxy |
|
96
|
|
|
->withoutAutoRefresh(static function(Proxy $proxy) use (&$calls) { |
|
|
|
|
|
|
97
|
|
|
++$calls; |
|
98
|
|
|
}) |
|
99
|
|
|
->withoutAutoRefresh(static function(Category $category) use (&$calls) { |
|
|
|
|
|
|
100
|
|
|
++$calls; |
|
101
|
|
|
}) |
|
102
|
|
|
->withoutAutoRefresh(function($proxy) use (&$calls) { |
|
103
|
|
|
$this->assertInstanceOf(Proxy::class, $proxy); |
|
104
|
|
|
++$calls; |
|
105
|
|
|
}) |
|
106
|
|
|
->withoutAutoRefresh(static function() use (&$calls) { |
|
107
|
|
|
++$calls; |
|
108
|
|
|
}) |
|
109
|
|
|
->withoutAutoRefresh([$this, 'functionWithProxy']) |
|
110
|
|
|
->withoutAutoRefresh([$this, 'functionWithObject']) |
|
111
|
|
|
->withoutAutoRefresh([$this, 'functionWithNoTypeHint']) |
|
112
|
|
|
->withoutAutoRefresh([self::class, 'staticFunctionWithProxy']) |
|
113
|
|
|
->withoutAutoRefresh([self::class, 'staticFunctionWithObject']) |
|
114
|
|
|
->withoutAutoRefresh([self::class, 'staticFunctionWithNoTypeHint']) |
|
115
|
|
|
; |
|
116
|
|
|
|
|
117
|
|
|
$this->assertSame(4, $calls); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function functionWithProxy(Proxy $proxy) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->assertInstanceOf(Category::class, $proxy->object()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function functionWithObject(Category $category) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->assertInstanceOf(Category::class, $category); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function functionWithNoTypeHint($proxy) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->assertInstanceOf(Proxy::class, $proxy); |
|
133
|
|
|
$this->assertInstanceOf(Category::class, $proxy->object()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public static function staticFunctionWithProxy(Proxy $proxy) |
|
137
|
|
|
{ |
|
138
|
|
|
self::assertInstanceOf(Category::class, $proxy->object()); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function staticFunctionWithObject(Category $category) |
|
142
|
|
|
{ |
|
143
|
|
|
self::assertInstanceOf(Category::class, $category); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public static function staticFunctionWithNoTypeHint($proxy) |
|
147
|
|
|
{ |
|
148
|
|
|
self::assertInstanceOf(Proxy::class, $proxy); |
|
149
|
|
|
self::assertInstanceOf(Category::class, $proxy->object()); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|