1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Zenstruck\Foundry\AnonymousFactory; |
6
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Contact; |
7
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; |
8
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithProxyGenerator; |
9
|
|
|
use function Zenstruck\Foundry\disable_autorefresh; |
10
|
|
|
use function Zenstruck\Foundry\without_autorefresh; |
11
|
|
|
use function Zenstruck\Foundry\force_set; |
12
|
|
|
use function Zenstruck\Foundry\force_set_all; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Wouter de Jong <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class ProxyGeneratorTest extends ProxyTest |
18
|
|
|
{ |
19
|
|
|
protected static $POST_FACTORY = PostFactoryWithProxyGenerator::class; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @test |
23
|
|
|
*/ |
24
|
|
|
public function can_assert_persisted(): void |
25
|
|
|
{ |
26
|
|
|
$this->markTestSkipped('assertPersisted() is not supported'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public function can_remove_and_assert_not_persisted(): void |
33
|
|
|
{ |
34
|
|
|
$this->markTestSkipped('assertNotPersisted() is not supported'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
*/ |
40
|
|
|
public function can_refetch_object_if_object_manager_has_been_cleared(): void |
41
|
|
|
{ |
42
|
|
|
$this->markTestSkipped('refresh() is not supported'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
*/ |
48
|
|
|
public function exception_thrown_if_trying_to_refresh_deleted_object(): void |
49
|
|
|
{ |
50
|
|
|
$this->markTestSkipped('refresh() is not supported'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function can_force_set_and_save(): void |
57
|
|
|
{ |
58
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title']); |
59
|
|
|
|
60
|
|
|
static::$POST_FACTORY::assert()->notExists(['title' => 'new title']); |
61
|
|
|
|
62
|
|
|
force_set($post, 'title', 'new title'); |
63
|
|
|
static::$POST_FACTORY::save($post); |
64
|
|
|
|
65
|
|
|
static::$POST_FACTORY::assert()->exists(['title' => 'new title']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function can_force_set_multiple_fields(): void |
72
|
|
|
{ |
73
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
74
|
|
|
|
75
|
|
|
$this->assertSame('old title', $post->getTitle()); |
76
|
|
|
$this->assertSame('old body', $post->getBody()); |
77
|
|
|
|
78
|
|
|
force_set($post, 'title', 'new title'); |
79
|
|
|
force_set($post, 'body', 'new body'); |
80
|
|
|
static::$POST_FACTORY::save($post); |
81
|
|
|
|
82
|
|
|
$this->assertSame('new title', $post->getTitle()); |
83
|
|
|
$this->assertSame('new body', $post->getBody()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
*/ |
89
|
|
|
public function exception_thrown_if_trying_to_autorefresh_object_with_unsaved_changes(): void |
90
|
|
|
{ |
91
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
92
|
|
|
|
93
|
|
|
$this->assertSame('old title', $post->getTitle()); |
94
|
|
|
$this->assertSame('old body', $post->getBody()); |
95
|
|
|
|
96
|
|
|
$post->setTitle('new title'); |
97
|
|
|
|
98
|
|
|
$this->expectException(\RuntimeException::class); |
99
|
|
|
|
100
|
|
|
$post->setBody('new body'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @test |
105
|
|
|
*/ |
106
|
|
|
public function can_autorefresh_between_kernel_boots(): void |
107
|
|
|
{ |
108
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
109
|
|
|
|
110
|
|
|
$this->assertSame('old title', $post->getTitle()); |
111
|
|
|
$this->assertSame('old body', $post->getBody()); |
112
|
|
|
|
113
|
|
|
// reboot kernel |
114
|
|
|
static::ensureKernelShutdown(); |
115
|
|
|
static::bootKernel(); |
116
|
|
|
|
117
|
|
|
$this->assertSame('old title', $post->getTitle()); |
118
|
|
|
$this->assertSame('old body', $post->getBody()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
*/ |
124
|
|
|
public function can_autorefresh_entity_with_embedded_object(): void |
125
|
|
|
{ |
126
|
|
|
$contactFactory = AnonymousFactory::new(Contact::class)->withProxyGenerator(); |
127
|
|
|
$contact = $contactFactory->create(['name' => 'john']); |
128
|
|
|
|
129
|
|
|
$this->assertSame('john', $contact->getName()); |
130
|
|
|
|
131
|
|
|
// I discovered when autorefreshing the second time, the embedded |
132
|
|
|
// object is included in the changeset when using UOW::recomputeSingleEntityChangeSet(). |
133
|
|
|
// Changing to UOW::computeChangeSet() fixes this. |
134
|
|
|
$this->assertSame('john', $contact->getName()); |
135
|
|
|
$this->assertNull($contact->getAddress()->getValue()); |
136
|
|
|
|
137
|
|
|
$contact->getAddress()->setValue('address'); |
138
|
|
|
$contactFactory->save($contact); |
139
|
|
|
|
140
|
|
|
$this->assertSame('address', $contact->getAddress()->getValue()); |
141
|
|
|
|
142
|
|
|
static::ensureKernelShutdown(); |
143
|
|
|
static::bootKernel(); |
144
|
|
|
|
145
|
|
|
$this->assertSame('address', $contact->getAddress()->getValue()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @test |
150
|
|
|
*/ |
151
|
|
|
public function force_set_all_solves_the_auto_refresh_problem(): void |
152
|
|
|
{ |
153
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
154
|
|
|
|
155
|
|
|
$this->assertSame('old title', $post->getTitle()); |
156
|
|
|
$this->assertSame('old body', $post->getBody()); |
157
|
|
|
|
158
|
|
|
force_set_all($post, [ |
159
|
|
|
'title' => 'new title', |
160
|
|
|
'body' => 'new body', |
161
|
|
|
]); |
162
|
|
|
static::$POST_FACTORY::save($post); |
163
|
|
|
|
164
|
|
|
$this->assertSame('new title', $post->getTitle()); |
165
|
|
|
$this->assertSame('new body', $post->getBody()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @test |
170
|
|
|
*/ |
171
|
|
|
public function without_auto_refresh_solves_the_auto_refresh_problem(): void |
172
|
|
|
{ |
173
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
174
|
|
|
|
175
|
|
|
$this->assertSame('old title', $post->getTitle()); |
176
|
|
|
$this->assertSame('old body', $post->getBody()); |
177
|
|
|
|
178
|
|
|
without_autorefresh($post, static function(Post $post) { |
179
|
|
|
$post->setTitle('new title'); |
180
|
|
|
$post->setBody('new body'); |
181
|
|
|
}); |
182
|
|
|
static::$POST_FACTORY::save($post); |
183
|
|
|
|
184
|
|
|
$this->assertSame('new title', $post->getTitle()); |
185
|
|
|
$this->assertSame('new body', $post->getBody()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @test |
190
|
|
|
*/ |
191
|
|
|
public function without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disabled_originally(): void |
192
|
|
|
{ |
193
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
194
|
|
|
|
195
|
|
|
disable_autorefresh($post); |
196
|
|
|
|
197
|
|
|
$this->assertSame('old title', $post->getTitle()); |
198
|
|
|
$this->assertSame('old body', $post->getBody()); |
199
|
|
|
|
200
|
|
|
without_autorefresh($post, static function(Post $post) { |
201
|
|
|
$post->setTitle('new title'); |
202
|
|
|
$post->setBody('new body'); |
203
|
|
|
}); |
204
|
|
|
|
205
|
|
|
$post->setTitle('another new title'); |
206
|
|
|
$post->setBody('another new body'); |
207
|
|
|
|
208
|
|
|
static::$POST_FACTORY::save($post); |
209
|
|
|
|
210
|
|
|
$this->assertSame('another new title', $post->getTitle()); |
211
|
|
|
$this->assertSame('another new body', $post->getBody()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
*/ |
217
|
|
|
public function without_auto_refresh_keeps_disabled_if_originally_disabled(): void |
218
|
|
|
{ |
219
|
|
|
$post = static::$POST_FACTORY::createOne(['title' => 'old title', 'body' => 'old body']); |
220
|
|
|
|
221
|
|
|
disable_autorefresh($post); |
222
|
|
|
|
223
|
|
|
$this->assertSame('old title', $post->getTitle()); |
224
|
|
|
$this->assertSame('old body', $post->getBody()); |
225
|
|
|
|
226
|
|
|
without_autorefresh($post, static function(Post $post) { |
227
|
|
|
$post->setTitle('new title'); |
228
|
|
|
$post->setBody('new body'); |
229
|
|
|
}); |
230
|
|
|
|
231
|
|
|
static::$POST_FACTORY::save($post); |
232
|
|
|
|
233
|
|
|
$post->setTitle('another new title'); |
234
|
|
|
$post->setBody('another new body'); |
235
|
|
|
|
236
|
|
|
static::$POST_FACTORY::save($post); |
237
|
|
|
|
238
|
|
|
$this->assertSame('another new title', $post->getTitle()); |
239
|
|
|
$this->assertSame('another new body', $post->getBody()); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|