1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
|
|
|
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
7
|
|
|
use Zenstruck\Assert; |
8
|
|
|
use Zenstruck\Foundry\Proxy; |
9
|
|
|
use Zenstruck\Foundry\Test\Factories; |
10
|
|
|
use Zenstruck\Foundry\Test\ResetDatabase; |
11
|
|
|
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class ProxyTest extends KernelTestCase |
17
|
|
|
{ |
18
|
|
|
use ContainerBC, Factories, ResetDatabase; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
*/ |
23
|
|
|
public function can_assert_persisted(): void |
24
|
|
|
{ |
25
|
|
|
$this->postFactoryClass()::createOne()->assertPersisted(); |
26
|
|
|
|
27
|
|
|
Assert::that(function() { $this->postFactoryClass()::new()->withoutPersisting()->create()->assertPersisted(); }) |
28
|
|
|
->throws(AssertionFailedError::class, \sprintf('%s is not persisted.', Post::class)) |
29
|
|
|
; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @test |
34
|
|
|
*/ |
35
|
|
|
public function can_assert_not_persisted(): void |
36
|
|
|
{ |
37
|
|
|
$this->postFactoryClass()::new()->withoutPersisting()->create()->assertNotPersisted(); |
38
|
|
|
|
39
|
|
|
Assert::that(function() { $this->postFactoryClass()::createOne()->assertNotPersisted(); }) |
40
|
|
|
->throws(AssertionFailedError::class, \sprintf('%s is persisted but it should not be.', Post::class)) |
41
|
|
|
; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @test |
46
|
|
|
*/ |
47
|
|
|
public function can_remove_and_assert_not_persisted(): void |
48
|
|
|
{ |
49
|
|
|
$post = $this->postFactoryClass()::createOne(); |
50
|
|
|
|
51
|
|
|
$post->remove(); |
52
|
|
|
|
53
|
|
|
$post->assertNotPersisted(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
|
public function functions_are_passed_to_wrapped_object(): void |
60
|
|
|
{ |
61
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'my title']); |
62
|
|
|
|
63
|
|
|
$this->assertSame('my title', $post->getTitle()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function can_convert_to_string_if_wrapped_object_can(): void |
70
|
|
|
{ |
71
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'my title']); |
72
|
|
|
|
73
|
|
|
$this->assertSame('my title', (string) $post); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
*/ |
79
|
|
|
public function can_refetch_object_if_object_manager_has_been_cleared(): void |
80
|
|
|
{ |
81
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'my title']); |
82
|
|
|
|
83
|
|
|
self::container()->get($this->registryServiceId())->getManager()->clear(); |
84
|
|
|
|
85
|
|
|
$this->assertSame('my title', $post->refresh()->getTitle()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @test |
90
|
|
|
*/ |
91
|
|
|
public function exception_thrown_if_trying_to_refresh_deleted_object(): void |
92
|
|
|
{ |
93
|
|
|
$postFactoryClass = $this->postFactoryClass(); |
94
|
|
|
|
95
|
|
|
$post = $postFactoryClass::createOne(); |
96
|
|
|
|
97
|
|
|
self::container()->get($this->registryServiceId())->getManager()->clear(); |
98
|
|
|
|
99
|
|
|
$postFactoryClass::repository()->truncate(); |
100
|
|
|
|
101
|
|
|
$this->expectException(\RuntimeException::class); |
102
|
|
|
$this->expectExceptionMessage('The object no longer exists.'); |
103
|
|
|
|
104
|
|
|
$post->refresh(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
*/ |
110
|
|
|
public function can_force_set_and_save(): void |
111
|
|
|
{ |
112
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title']); |
113
|
|
|
|
114
|
|
|
$post->repository()->assert()->notExists(['title' => 'new title']); |
115
|
|
|
|
116
|
|
|
$post->forceSet('title', 'new title')->save(); |
117
|
|
|
|
118
|
|
|
$post->repository()->assert()->exists(['title' => 'new title']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @test |
123
|
|
|
*/ |
124
|
|
|
public function can_force_set_multiple_fields(): void |
125
|
|
|
{ |
126
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); |
127
|
|
|
|
128
|
|
|
$this->assertSame('old title', $post->getTitle()); |
129
|
|
|
$this->assertSame('old body', $post->getBody()); |
130
|
|
|
|
131
|
|
|
$post |
132
|
|
|
->forceSet('title', 'new title') |
133
|
|
|
->forceSet('body', 'new body') |
134
|
|
|
->save() |
135
|
|
|
; |
136
|
|
|
|
137
|
|
|
$this->assertSame('new title', $post->getTitle()); |
138
|
|
|
$this->assertSame('new body', $post->getBody()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @test |
143
|
|
|
*/ |
144
|
|
|
public function exception_thrown_if_trying_to_autorefresh_object_with_unsaved_changes(): void |
145
|
|
|
{ |
146
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']) |
147
|
|
|
->enableAutoRefresh() |
148
|
|
|
; |
149
|
|
|
|
150
|
|
|
$this->assertSame('old title', $post->getTitle()); |
151
|
|
|
$this->assertSame('old body', $post->getBody()); |
152
|
|
|
|
153
|
|
|
$post |
154
|
|
|
->enableAutoRefresh() |
155
|
|
|
->forceSet('title', 'new title') |
156
|
|
|
; |
157
|
|
|
|
158
|
|
|
$this->expectException(\RuntimeException::class); |
159
|
|
|
|
160
|
|
|
// exception thrown because of "unsaved changes" to $post from above |
161
|
|
|
$post->forceSet('body', 'new body'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @test |
166
|
|
|
*/ |
167
|
|
|
public function can_autorefresh_between_kernel_boots(): void |
168
|
|
|
{ |
169
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']) |
170
|
|
|
->enableAutoRefresh() |
171
|
|
|
; |
172
|
|
|
|
173
|
|
|
$this->assertSame('old title', $post->getTitle()); |
174
|
|
|
$this->assertSame('old body', $post->getBody()); |
175
|
|
|
|
176
|
|
|
// reboot kernel |
177
|
|
|
self::ensureKernelShutdown(); |
178
|
|
|
self::bootKernel(); |
179
|
|
|
|
180
|
|
|
$this->assertSame('old title', $post->getTitle()); |
181
|
|
|
$this->assertSame('old body', $post->getBody()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @test |
186
|
|
|
*/ |
187
|
|
|
public function force_set_all_solves_the_auto_refresh_problem(): void |
188
|
|
|
{ |
189
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); |
190
|
|
|
|
191
|
|
|
$this->assertSame('old title', $post->getTitle()); |
192
|
|
|
$this->assertSame('old body', $post->getBody()); |
193
|
|
|
|
194
|
|
|
$post |
195
|
|
|
->enableAutoRefresh() |
196
|
|
|
->forceSetAll([ |
197
|
|
|
'title' => 'new title', |
198
|
|
|
'body' => 'new body', |
199
|
|
|
]) |
200
|
|
|
->save() |
201
|
|
|
; |
202
|
|
|
|
203
|
|
|
$this->assertSame('new title', $post->getTitle()); |
204
|
|
|
$this->assertSame('new body', $post->getBody()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @test |
209
|
|
|
*/ |
210
|
|
|
public function without_auto_refresh_solves_the_auto_refresh_problem(): void |
211
|
|
|
{ |
212
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); |
213
|
|
|
|
214
|
|
|
$this->assertSame('old title', $post->getTitle()); |
215
|
|
|
$this->assertSame('old body', $post->getBody()); |
216
|
|
|
|
217
|
|
|
$post |
218
|
|
|
->enableAutoRefresh() |
219
|
|
|
->withoutAutoRefresh(static function(Proxy $proxy) { |
220
|
|
|
$proxy |
221
|
|
|
->forceSet('title', 'new title') |
222
|
|
|
->forceSet('body', 'new body') |
223
|
|
|
; |
224
|
|
|
}) |
225
|
|
|
->save() |
226
|
|
|
; |
227
|
|
|
|
228
|
|
|
$this->assertSame('new title', $post->getTitle()); |
229
|
|
|
$this->assertSame('new body', $post->getBody()); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @test |
234
|
|
|
*/ |
235
|
|
|
public function without_auto_refresh_does_not_enable_auto_refresh_if_it_was_disabled_originally(): void |
236
|
|
|
{ |
237
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); |
238
|
|
|
|
239
|
|
|
$this->assertSame('old title', $post->getTitle()); |
240
|
|
|
$this->assertSame('old body', $post->getBody()); |
241
|
|
|
|
242
|
|
|
$post |
243
|
|
|
->withoutAutoRefresh(static function(Proxy $proxy) { |
244
|
|
|
$proxy |
245
|
|
|
->forceSet('title', 'new title') |
246
|
|
|
->forceSet('body', 'new body') |
247
|
|
|
; |
248
|
|
|
}) |
249
|
|
|
->forceSet('title', 'another new title') |
250
|
|
|
->forceSet('body', 'another new body') |
251
|
|
|
->save() |
252
|
|
|
; |
253
|
|
|
|
254
|
|
|
$this->assertSame('another new title', $post->getTitle()); |
255
|
|
|
$this->assertSame('another new body', $post->getBody()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @test |
260
|
|
|
*/ |
261
|
|
|
public function without_auto_refresh_keeps_disabled_if_originally_disabled(): void |
262
|
|
|
{ |
263
|
|
|
$post = $this->postFactoryClass()::createOne(['title' => 'old title', 'body' => 'old body']); |
264
|
|
|
|
265
|
|
|
$this->assertSame('old title', $post->getTitle()); |
266
|
|
|
$this->assertSame('old body', $post->getBody()); |
267
|
|
|
|
268
|
|
|
$post |
269
|
|
|
->withoutAutoRefresh(static function(Proxy $proxy) { |
270
|
|
|
$proxy |
271
|
|
|
->forceSet('title', 'new title') |
272
|
|
|
->forceSet('body', 'new body') |
273
|
|
|
; |
274
|
|
|
}) |
275
|
|
|
->save() |
276
|
|
|
->forceSet('title', 'another new title') |
277
|
|
|
->forceSet('body', 'another new body') |
278
|
|
|
->save() |
279
|
|
|
; |
280
|
|
|
|
281
|
|
|
$this->assertSame('another new title', $post->getTitle()); |
282
|
|
|
$this->assertSame('another new body', $post->getBody()); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
abstract protected function postFactoryClass(): string; |
286
|
|
|
|
287
|
|
|
abstract protected function registryServiceId(): string; |
288
|
|
|
} |
289
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths