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