mapado /
rest-client-sdk
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Mapado\RestClientSdk\Tests\Units; |
||
| 6 | |||
| 7 | use atoum; |
||
| 8 | use Mapado\RestClientSdk\Mapping as RestMapping; |
||
| 9 | use Mapado\RestClientSdk\Mapping\Attribute; |
||
| 10 | use Mapado\RestClientSdk\Mapping\ClassMetadata; |
||
| 11 | use Mapado\RestClientSdk\Mapping\Relation; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * UnitOfWork |
||
| 15 | * |
||
| 16 | * @uses \atoum |
||
| 17 | * |
||
| 18 | * @author Julien Petit <[email protected]> |
||
| 19 | */ |
||
| 20 | class UnitOfWork extends atoum |
||
| 21 | { |
||
| 22 | private $unitOfWork; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 23 | |||
| 24 | public function testRegister() |
||
| 25 | { |
||
| 26 | $mapping = $this->getMapping(); |
||
| 27 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 28 | |||
| 29 | $ticket = (object) [ |
||
| 30 | 'firstname' => 'foo', |
||
| 31 | 'lastname' => 'bar', |
||
| 32 | 'email' => '[email protected]', |
||
| 33 | ]; |
||
| 34 | $unitOfWork->registerClean('@id1', $ticket); |
||
| 35 | $this |
||
| 36 | ->then |
||
| 37 | ->variable($unitOfWork->getDirtyEntity('@id1')) |
||
| 38 | ->isEqualTo($ticket) |
||
| 39 | ; |
||
| 40 | } |
||
| 41 | |||
| 42 | public function testSimpleEntity() |
||
| 43 | { |
||
| 44 | $mapping = $this->getMapping(); |
||
| 45 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 46 | |||
| 47 | $this |
||
| 48 | ->array($unitOfWork->getDirtyData( |
||
| 49 | [ |
||
| 50 | '@id' => '/v12/carts/1', |
||
| 51 | ], |
||
| 52 | [ |
||
| 53 | '@id' => '/v12/carts/1', |
||
| 54 | ], |
||
| 55 | $this->getCartMetadata() |
||
| 56 | )) |
||
| 57 | ->isEqualTo([]) |
||
| 58 | ->then |
||
| 59 | ->array($unitOfWork->getDirtyData( |
||
| 60 | [ |
||
| 61 | '@id' => '/v12/carts/1', |
||
| 62 | 'status' => 'payed', |
||
| 63 | 'order' => '/v12/orders/1', |
||
| 64 | ], |
||
| 65 | [ |
||
| 66 | '@id' => '/v12/carts/1', |
||
| 67 | 'status' => 'payed', |
||
| 68 | 'order' => '/v12/orders/1', |
||
| 69 | ], |
||
| 70 | $this->getCartMetadata() |
||
| 71 | )) |
||
| 72 | ->isEqualTo([]) |
||
| 73 | ->then |
||
| 74 | ->array($unitOfWork->getDirtyData( |
||
| 75 | [ |
||
| 76 | '@id' => '/v12/carts/1', |
||
| 77 | 'status' => 'payed', |
||
| 78 | ], |
||
| 79 | [ |
||
| 80 | '@id' => '/v12/carts/1', |
||
| 81 | 'status' => 'waiting', |
||
| 82 | ], |
||
| 83 | $this->getCartMetadata() |
||
| 84 | )) |
||
| 85 | ->isEqualTo(['status' => 'payed']) |
||
| 86 | ->then |
||
| 87 | ->array($unitOfWork->getDirtyData( |
||
| 88 | [ |
||
| 89 | '@id' => '/v12/carts/2', |
||
| 90 | 'status' => 'payed', |
||
| 91 | ], |
||
| 92 | [ |
||
| 93 | '@id' => '/v12/carts/1', |
||
| 94 | 'status' => 'waiting', |
||
| 95 | ], |
||
| 96 | $this->getCartMetadata() |
||
| 97 | )) |
||
| 98 | ->isEqualTo([ |
||
| 99 | '@id' => '/v12/carts/2', |
||
| 100 | 'status' => 'payed', |
||
| 101 | ]) |
||
| 102 | ->then |
||
| 103 | ->array($unitOfWork->getDirtyData( |
||
| 104 | [ |
||
| 105 | '@id' => '/v12/carts/1', |
||
| 106 | 'status' => 'payed', |
||
| 107 | 'someData' => [ |
||
| 108 | 'foo' => 'bar', |
||
| 109 | 'loo' => 'baz', |
||
| 110 | ], |
||
| 111 | ], |
||
| 112 | [ |
||
| 113 | '@id' => '/v12/carts/1', |
||
| 114 | 'status' => 'payed', |
||
| 115 | 'someData' => [ |
||
| 116 | 'foo' => 'bar', |
||
| 117 | ], |
||
| 118 | ], |
||
| 119 | $this->getCartMetadata() |
||
| 120 | )) |
||
| 121 | ->isEqualTo([ |
||
| 122 | 'someData' => [ |
||
| 123 | 'foo' => 'bar', |
||
| 124 | 'loo' => 'baz', |
||
| 125 | ], |
||
| 126 | ]) |
||
| 127 | ->then |
||
| 128 | ->array($unitOfWork->getDirtyData( |
||
| 129 | [ |
||
| 130 | '@id' => '/v12/carts/1', |
||
| 131 | 'status' => 'payed', |
||
| 132 | 'someData' => [ |
||
| 133 | 'foo' => 'bar', |
||
| 134 | 'bad' => 'baz', |
||
| 135 | ], |
||
| 136 | ], |
||
| 137 | [ |
||
| 138 | '@id' => '/v12/carts/1', |
||
| 139 | 'status' => 'payed', |
||
| 140 | 'someData' => [ |
||
| 141 | 'foo' => 'bar', |
||
| 142 | 'bad' => 'baz', |
||
| 143 | ], |
||
| 144 | ], |
||
| 145 | $this->getCartMetadata() |
||
| 146 | )) |
||
| 147 | ->isEqualTo([]) |
||
| 148 | ; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function testWithMoreData() |
||
| 152 | { |
||
| 153 | $mapping = $this->getMapping(); |
||
| 154 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 155 | |||
| 156 | $this |
||
| 157 | ->array($unitOfWork->getDirtyData( |
||
| 158 | [ |
||
| 159 | '@id' => '/v12/carts/1', |
||
| 160 | ], |
||
| 161 | [ |
||
| 162 | '@id' => '/v12/carts/1', |
||
| 163 | 'foo' => 'bar', |
||
| 164 | 'status' => 'ok', |
||
| 165 | ], |
||
| 166 | $this->getCartMetadata() |
||
| 167 | )) |
||
| 168 | ->isEqualTo([]) |
||
| 169 | ; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function testManyToOneRelation() |
||
| 173 | { |
||
| 174 | $mapping = $this->getMapping(); |
||
| 175 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 176 | |||
| 177 | $this |
||
| 178 | ->array($unitOfWork->getDirtyData( |
||
| 179 | [ |
||
| 180 | '@id' => '/v12/carts/1', |
||
| 181 | 'order' => '/v1/orders/2', |
||
| 182 | ], |
||
| 183 | [ |
||
| 184 | '@id' => '/v12/carts/1', |
||
| 185 | 'order' => ['@id' => '/v1/orders/1'], |
||
| 186 | ], |
||
| 187 | $this->getCartMetadata() |
||
| 188 | )) |
||
| 189 | ->isEqualTo([ |
||
| 190 | 'order' => '/v1/orders/2', |
||
| 191 | ]) |
||
| 192 | |||
| 193 | ->then |
||
| 194 | ->array($unitOfWork->getDirtyData( |
||
| 195 | [ |
||
| 196 | '@id' => '/v12/carts/1', |
||
| 197 | 'order' => [ |
||
| 198 | '@id' => '/v1/orders/2', |
||
| 199 | ], |
||
| 200 | ], |
||
| 201 | [ |
||
| 202 | '@id' => '/v12/carts/1', |
||
| 203 | 'order' => '/v1/orders/1', |
||
| 204 | ], |
||
| 205 | $this->getCartMetadata() |
||
| 206 | )) |
||
| 207 | ->isEqualTo([ |
||
| 208 | 'order' => [ |
||
| 209 | '@id' => '/v1/orders/2', |
||
| 210 | ], |
||
| 211 | ]) |
||
| 212 | |||
| 213 | ->then |
||
| 214 | ->array($unitOfWork->getDirtyData( |
||
| 215 | [ |
||
| 216 | '@id' => '/v12/carts/1', |
||
| 217 | 'order' => [ |
||
| 218 | '@id' => '/v1/orders/2', |
||
| 219 | 'status' => 'payed', |
||
| 220 | ], |
||
| 221 | ], |
||
| 222 | [ |
||
| 223 | '@id' => '/v12/carts/1', |
||
| 224 | 'order' => [ |
||
| 225 | '@id' => '/v1/orders/1', |
||
| 226 | 'status' => 'payed', |
||
| 227 | ], |
||
| 228 | ], |
||
| 229 | $this->getCartMetadata() |
||
| 230 | )) |
||
| 231 | ->isEqualTo([ |
||
| 232 | 'order' => [ |
||
| 233 | '@id' => '/v1/orders/2', |
||
| 234 | ], |
||
| 235 | ]) |
||
| 236 | |||
| 237 | ->then |
||
| 238 | ->array($unitOfWork->getDirtyData( |
||
| 239 | [ |
||
| 240 | '@id' => '/v12/carts/1', |
||
| 241 | 'order' => [ |
||
| 242 | '@id' => '/v1/orders/2', |
||
| 243 | 'status' => 'payed', |
||
| 244 | ], |
||
| 245 | ], |
||
| 246 | [ |
||
| 247 | '@id' => '/v12/carts/1', |
||
| 248 | 'order' => [ |
||
| 249 | '@id' => '/v1/orders/1', |
||
| 250 | 'status' => 'waiting', |
||
| 251 | ], |
||
| 252 | ], |
||
| 253 | $this->getCartMetadata() |
||
| 254 | )) |
||
| 255 | ->isEqualTo([ |
||
| 256 | 'order' => [ |
||
| 257 | '@id' => '/v1/orders/2', |
||
| 258 | 'status' => 'payed', |
||
| 259 | ], |
||
| 260 | ]) |
||
| 261 | ; |
||
| 262 | } |
||
| 263 | |||
| 264 | public function testNoChanges() |
||
| 265 | { |
||
| 266 | $mapping = $this->getMapping(); |
||
| 267 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 268 | |||
| 269 | $this |
||
| 270 | ->given($newSerializedModel = [ |
||
| 271 | '@id' => '/v12/carts/1', |
||
| 272 | 'cartItemList' => [ |
||
| 273 | '/v12/cart_items/1', |
||
| 274 | '/v12/cart_items/2', |
||
| 275 | '/v12/cart_items/3', |
||
| 276 | ], |
||
| 277 | ]) |
||
| 278 | ->then |
||
| 279 | ->variable($unitOfWork->getDirtyData( |
||
| 280 | $newSerializedModel, |
||
| 281 | [ |
||
| 282 | '@id' => '/v12/carts/1', |
||
| 283 | 'cartItemList' => [ |
||
| 284 | '/v12/cart_items/1', |
||
| 285 | '/v12/cart_items/2', |
||
| 286 | '/v12/cart_items/3', |
||
| 287 | ], |
||
| 288 | ], |
||
| 289 | $this->getCartMetadata() |
||
| 290 | )) |
||
| 291 | ->isEqualTo( |
||
| 292 | [ |
||
| 293 | ] |
||
| 294 | ) |
||
| 295 | ->then |
||
| 296 | ->variable($unitOfWork->getDirtyData( |
||
| 297 | [ |
||
| 298 | '@id' => '/v12/carts/1', |
||
| 299 | 'cartItemList' => [ |
||
| 300 | [ |
||
| 301 | '@id' => '/v12/cart_items/1', |
||
| 302 | 'amount' => 1, |
||
| 303 | ], |
||
| 304 | [ |
||
| 305 | '@id' => '/v12/cart_items/2', |
||
| 306 | 'amount' => 1, |
||
| 307 | ], |
||
| 308 | ], |
||
| 309 | ], |
||
| 310 | [ |
||
| 311 | '@id' => '/v12/carts/1', |
||
| 312 | 'cartItemList' => [ |
||
| 313 | [ |
||
| 314 | '@id' => '/v12/cart_items/1', |
||
| 315 | 'amount' => 1, |
||
| 316 | ], |
||
| 317 | [ |
||
| 318 | '@id' => '/v12/cart_items/2', |
||
| 319 | 'amount' => 1, |
||
| 320 | ], |
||
| 321 | ], |
||
| 322 | ], |
||
| 323 | $this->getCartMetadata() |
||
| 324 | )) |
||
| 325 | ->isEqualTo( |
||
| 326 | [ |
||
| 327 | ] |
||
| 328 | ) |
||
| 329 | ; |
||
| 330 | } |
||
| 331 | |||
| 332 | public function testNoMetadata() |
||
| 333 | { |
||
| 334 | $mapping = $this->getMapping(); |
||
| 335 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 336 | |||
| 337 | $this |
||
| 338 | ->given($newSerializedModel = [ |
||
| 339 | '@id' => '/v12/carts/1', |
||
| 340 | 'cartInfo' => [ |
||
| 341 | [ |
||
| 342 | 'firstname' => 'john', |
||
| 343 | 'lastname' => 'doe', |
||
| 344 | ], |
||
| 345 | ], |
||
| 346 | ]) |
||
| 347 | ->then |
||
| 348 | ->variable($unitOfWork->getDirtyData( |
||
| 349 | $newSerializedModel, |
||
| 350 | [ |
||
| 351 | '@id' => '/v12/carts/1', |
||
| 352 | 'cartInfo' => [ |
||
| 353 | [ |
||
| 354 | 'firstname' => 'john', |
||
| 355 | 'lastname' => 'doe', |
||
| 356 | ], |
||
| 357 | ], |
||
| 358 | ], |
||
| 359 | $this->getCartMetadata() |
||
| 360 | )) |
||
| 361 | ->isEqualTo( |
||
| 362 | [ |
||
| 363 | ] |
||
| 364 | ) |
||
| 365 | ->then |
||
| 366 | ->variable($unitOfWork->getDirtyData( |
||
| 367 | [ |
||
| 368 | '@id' => '/v12/carts/1', |
||
| 369 | 'cartItemList' => [ |
||
| 370 | [ |
||
| 371 | '@id' => '/v12/cart_items/1', |
||
| 372 | 'amount' => 1, |
||
| 373 | ], |
||
| 374 | [ |
||
| 375 | '@id' => '/v12/cart_items/2', |
||
| 376 | 'amount' => 1, |
||
| 377 | ], |
||
| 378 | ], |
||
| 379 | ], |
||
| 380 | [ |
||
| 381 | '@id' => '/v12/carts/1', |
||
| 382 | 'cartItemList' => [ |
||
| 383 | [ |
||
| 384 | '@id' => '/v12/cart_items/1', |
||
| 385 | 'amount' => 1, |
||
| 386 | ], |
||
| 387 | [ |
||
| 388 | '@id' => '/v12/cart_items/2', |
||
| 389 | 'amount' => 1, |
||
| 390 | ], |
||
| 391 | ], |
||
| 392 | ], |
||
| 393 | $this->getCartMetadata() |
||
| 394 | )) |
||
| 395 | ->isEqualTo( |
||
| 396 | [ |
||
| 397 | ] |
||
| 398 | ) |
||
| 399 | ; |
||
| 400 | } |
||
| 401 | |||
| 402 | public function testRemoveItem() |
||
| 403 | { |
||
| 404 | $mapping = $this->getMapping(); |
||
| 405 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 406 | |||
| 407 | $this |
||
| 408 | ->then |
||
| 409 | ->variable($unitOfWork->getDirtyData( |
||
| 410 | [ |
||
| 411 | '@id' => '/v12/carts/1', |
||
| 412 | 'cartItemList' => [ |
||
| 413 | '/v12/cart_items/1', |
||
| 414 | '/v12/cart_items/2', |
||
| 415 | ], |
||
| 416 | ], |
||
| 417 | [ |
||
| 418 | '@id' => '/v12/carts/1', |
||
| 419 | 'cartItemList' => [ |
||
| 420 | '/v12/cart_items/1', |
||
| 421 | '/v12/cart_items/2', |
||
| 422 | '/v12/cart_items/3', |
||
| 423 | ], |
||
| 424 | ], |
||
| 425 | $this->getCartMetadata() |
||
| 426 | )) |
||
| 427 | ->isEqualTo( |
||
| 428 | [ |
||
| 429 | 'cartItemList' => [ |
||
| 430 | '/v12/cart_items/1', |
||
| 431 | '/v12/cart_items/2', |
||
| 432 | ], |
||
| 433 | ] |
||
| 434 | ) |
||
| 435 | ->then |
||
| 436 | ->variable($unitOfWork->getDirtyData( |
||
| 437 | [ |
||
| 438 | '@id' => '/v12/carts/1', |
||
| 439 | 'cartItemList' => [ |
||
| 440 | '/v12/cart_items/1', |
||
| 441 | '/v12/cart_items/3', |
||
| 442 | ], |
||
| 443 | ], |
||
| 444 | [ |
||
| 445 | '@id' => '/v12/carts/1', |
||
| 446 | 'cartItemList' => [ |
||
| 447 | '/v12/cart_items/1', |
||
| 448 | '/v12/cart_items/2', |
||
| 449 | '/v12/cart_items/3', |
||
| 450 | ], |
||
| 451 | ], |
||
| 452 | $this->getCartMetadata() |
||
| 453 | )) |
||
| 454 | ->isEqualTo( |
||
| 455 | [ |
||
| 456 | 'cartItemList' => [ |
||
| 457 | '/v12/cart_items/1', |
||
| 458 | '/v12/cart_items/3', |
||
| 459 | ], |
||
| 460 | ] |
||
| 461 | ) |
||
| 462 | ->then |
||
| 463 | ->variable($unitOfWork->getDirtyData( |
||
| 464 | [ |
||
| 465 | '@id' => '/v12/carts/1', |
||
| 466 | 'cartItemList' => [ |
||
| 467 | [ |
||
| 468 | '@id' => '/v12/cart_items/1', |
||
| 469 | 'amount' => 2, |
||
| 470 | ], |
||
| 471 | ], |
||
| 472 | ], |
||
| 473 | [ |
||
| 474 | '@id' => '/v12/carts/1', |
||
| 475 | 'cartItemList' => [ |
||
| 476 | [ |
||
| 477 | '@id' => '/v12/cart_items/1', |
||
| 478 | 'amount' => 2, |
||
| 479 | ], |
||
| 480 | [ |
||
| 481 | '@id' => '/v12/cart_items/2', |
||
| 482 | 'amount' => 1, |
||
| 483 | ], |
||
| 484 | ], |
||
| 485 | ], |
||
| 486 | $this->getCartMetadata() |
||
| 487 | )) |
||
| 488 | ->isEqualTo( |
||
| 489 | [ |
||
| 490 | 'cartItemList' => [ |
||
| 491 | [ |
||
| 492 | '@id' => '/v12/cart_items/1', |
||
| 493 | ], |
||
| 494 | ], |
||
| 495 | ] |
||
| 496 | ) |
||
| 497 | ->then |
||
| 498 | ->variable($unitOfWork->getDirtyData( |
||
| 499 | [ |
||
| 500 | '@id' => '/v12/carts/1', |
||
| 501 | 'cartItemList' => [ |
||
| 502 | [ |
||
| 503 | '@id' => '/v12/cart_items/2', |
||
| 504 | 'amount' => 1, |
||
| 505 | ], |
||
| 506 | ], |
||
| 507 | ], |
||
| 508 | [ |
||
| 509 | '@id' => '/v12/carts/1', |
||
| 510 | 'cartItemList' => [ |
||
| 511 | [ |
||
| 512 | '@id' => '/v12/cart_items/1', |
||
| 513 | 'amount' => 2, |
||
| 514 | ], |
||
| 515 | [ |
||
| 516 | '@id' => '/v12/cart_items/2', |
||
| 517 | 'amount' => 1, |
||
| 518 | ], |
||
| 519 | ], |
||
| 520 | ], |
||
| 521 | $this->getCartMetadata() |
||
| 522 | )) |
||
| 523 | ->isEqualTo( |
||
| 524 | [ |
||
| 525 | 'cartItemList' => [ |
||
| 526 | [ |
||
| 527 | '@id' => '/v12/cart_items/2', |
||
| 528 | ], |
||
| 529 | ], |
||
| 530 | ] |
||
| 531 | ) |
||
| 532 | ->then |
||
| 533 | ->array($unitOfWork->getDirtyData( |
||
| 534 | [ |
||
| 535 | '@id' => '/v12/carts/1', |
||
| 536 | 'cartItemList' => [ |
||
| 537 | [ |
||
| 538 | '@id' => '/v12/cart_items/2', |
||
| 539 | 'amount' => 2, |
||
| 540 | ], |
||
| 541 | ], |
||
| 542 | ], |
||
| 543 | [ |
||
| 544 | '@id' => '/v12/carts/1', |
||
| 545 | 'cartItemList' => [ |
||
| 546 | [ |
||
| 547 | '@id' => '/v12/cart_items/1', |
||
| 548 | 'amount' => 2, |
||
| 549 | ], |
||
| 550 | [ |
||
| 551 | '@id' => '/v12/cart_items/2', |
||
| 552 | 'amount' => 1, |
||
| 553 | ], |
||
| 554 | ], |
||
| 555 | ], |
||
| 556 | $this->getCartMetadata() |
||
| 557 | )) |
||
| 558 | ->isEqualTo( |
||
| 559 | [ |
||
| 560 | 'cartItemList' => [ |
||
| 561 | [ |
||
| 562 | '@id' => '/v12/cart_items/2', |
||
| 563 | 'amount' => 2, |
||
| 564 | ], |
||
| 565 | ], |
||
| 566 | ] |
||
| 567 | ) |
||
| 568 | ; |
||
| 569 | } |
||
| 570 | |||
| 571 | public function testAddItem() |
||
| 572 | { |
||
| 573 | $mapping = $this->getMapping(); |
||
| 574 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 575 | |||
| 576 | $this |
||
| 577 | ->then |
||
| 578 | ->variable($unitOfWork->getDirtyData( |
||
| 579 | [ |
||
| 580 | '@id' => '/v12/carts/1', |
||
| 581 | 'cartItemList' => [ |
||
| 582 | '/v12/cart_items/1', |
||
| 583 | '/v12/cart_items/2', |
||
| 584 | '/v12/cart_items/3', |
||
| 585 | ], |
||
| 586 | ], |
||
| 587 | [ |
||
| 588 | '@id' => '/v12/carts/1', |
||
| 589 | 'cartItemList' => [ |
||
| 590 | '/v12/cart_items/1', |
||
| 591 | '/v12/cart_items/2', |
||
| 592 | ], |
||
| 593 | ], |
||
| 594 | $this->getCartMetadata() |
||
| 595 | )) |
||
| 596 | ->isEqualTo( |
||
| 597 | [ |
||
| 598 | 'cartItemList' => [ |
||
| 599 | '/v12/cart_items/1', |
||
| 600 | '/v12/cart_items/2', |
||
| 601 | '/v12/cart_items/3', |
||
| 602 | ], |
||
| 603 | ] |
||
| 604 | ) |
||
| 605 | ->then |
||
| 606 | ->variable($unitOfWork->getDirtyData( |
||
| 607 | [ |
||
| 608 | '@id' => '/v12/carts/1', |
||
| 609 | 'cartItemList' => [ |
||
| 610 | [ |
||
| 611 | '@id' => '/v12/cart_items/1', |
||
| 612 | 'amount' => 1, |
||
| 613 | ], |
||
| 614 | [ |
||
| 615 | '@id' => '/v12/cart_items/2', |
||
| 616 | 'amount' => 1, |
||
| 617 | ], |
||
| 618 | ], |
||
| 619 | ], |
||
| 620 | [ |
||
| 621 | '@id' => '/v12/carts/1', |
||
| 622 | 'cartItemList' => [ |
||
| 623 | [ |
||
| 624 | '@id' => '/v12/cart_items/1', |
||
| 625 | 'amount' => 1, |
||
| 626 | ], |
||
| 627 | ], |
||
| 628 | ], |
||
| 629 | $this->getCartMetadata() |
||
| 630 | )) |
||
| 631 | ->isEqualTo( |
||
| 632 | [ |
||
| 633 | 'cartItemList' => [ |
||
| 634 | [ |
||
| 635 | '@id' => '/v12/cart_items/1', |
||
| 636 | ], |
||
| 637 | [ |
||
| 638 | '@id' => '/v12/cart_items/2', |
||
| 639 | 'amount' => 1, |
||
| 640 | ], |
||
| 641 | ], |
||
| 642 | ] |
||
| 643 | ) |
||
| 644 | ; |
||
| 645 | } |
||
| 646 | |||
| 647 | public function testSendOnlyDirty() |
||
| 648 | { |
||
| 649 | $mapping = $this->getMapping(); |
||
| 650 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 651 | |||
| 652 | $this |
||
| 653 | ->then |
||
| 654 | ->variable($unitOfWork->getDirtyData( |
||
| 655 | [ |
||
| 656 | '@id' => '/v12/carts/1', |
||
| 657 | 'cartItemList' => [ |
||
| 658 | [ |
||
| 659 | '@id' => '/v12/cart_items/1', |
||
| 660 | 'amount' => 2, |
||
| 661 | ], |
||
| 662 | [ |
||
| 663 | '@id' => '/v12/cart_items/2', |
||
| 664 | 'amount' => 1, |
||
| 665 | ], |
||
| 666 | ], |
||
| 667 | ], |
||
| 668 | [ |
||
| 669 | '@id' => '/v12/carts/1', |
||
| 670 | 'cartItemList' => [ |
||
| 671 | [ |
||
| 672 | '@id' => '/v12/cart_items/1', |
||
| 673 | 'amount' => 1, |
||
| 674 | ], |
||
| 675 | [ |
||
| 676 | '@id' => '/v12/cart_items/2', |
||
| 677 | 'amount' => 1, |
||
| 678 | ], |
||
| 679 | ], |
||
| 680 | ], |
||
| 681 | $this->getCartMetadata() |
||
| 682 | )) |
||
| 683 | ->isEqualTo( |
||
| 684 | [ |
||
| 685 | 'cartItemList' => [ |
||
| 686 | [ |
||
| 687 | '@id' => '/v12/cart_items/1', |
||
| 688 | 'amount' => 2, |
||
| 689 | ], |
||
| 690 | ], |
||
| 691 | ] |
||
| 692 | ) |
||
| 693 | ->then |
||
| 694 | ->variable($unitOfWork->getDirtyData( |
||
| 695 | [ |
||
| 696 | '@id' => '/v12/carts/1', |
||
| 697 | 'status' => 'ok', |
||
| 698 | 'clientPhoneNumber' => '+33123456789', |
||
| 699 | ], |
||
| 700 | [ |
||
| 701 | '@id' => '/v12/carts/1', |
||
| 702 | 'status' => 'ko', |
||
| 703 | 'clientPhoneNumber' => '+33123456789', |
||
| 704 | ], |
||
| 705 | $this->getCartMetadata() |
||
| 706 | )) |
||
| 707 | ->isEqualTo( |
||
| 708 | [ |
||
| 709 | 'status' => 'ok', |
||
| 710 | ] |
||
| 711 | ) |
||
| 712 | ->then |
||
| 713 | ->variable($unitOfWork->getDirtyData( |
||
| 714 | [ |
||
| 715 | 'cartItemList' => [ |
||
| 716 | [ |
||
| 717 | '@id' => '/v12/cart_items/1', |
||
| 718 | 'amount' => 1, |
||
| 719 | 'cartItemDetailList' => [ |
||
| 720 | [ |
||
| 721 | '@id' => '/v12/cart_item_details/1', |
||
| 722 | 'name' => '', |
||
| 723 | ], |
||
| 724 | ], |
||
| 725 | ], |
||
| 726 | [ |
||
| 727 | '@id' => '/v12/cart_items/2', |
||
| 728 | 'amount' => 1, |
||
| 729 | ], |
||
| 730 | [ |
||
| 731 | '@id' => '/v12/cart_items/3', |
||
| 732 | 'amount' => 1, |
||
| 733 | 'cartItemDetailList' => [ |
||
| 734 | [ |
||
| 735 | '@id' => '/v12/cart_item_details/2', |
||
| 736 | 'name' => '', |
||
| 737 | ], |
||
| 738 | ], |
||
| 739 | ], |
||
| 740 | ], |
||
| 741 | ], |
||
| 742 | [ |
||
| 743 | 'cartItemList' => [ |
||
| 744 | [ |
||
| 745 | '@id' => '/v12/cart_items/1', |
||
| 746 | 'amount' => 2, |
||
| 747 | 'cartItemDetailList' => [ |
||
| 748 | '@id' => '/v12/cart_item_details/1', |
||
| 749 | 'name' => 'foo', |
||
| 750 | ], |
||
| 751 | ], |
||
| 752 | [ |
||
| 753 | '@id' => '/v12/cart_items/3', |
||
| 754 | 'amount' => 1, |
||
| 755 | ], |
||
| 756 | ], |
||
| 757 | ], |
||
| 758 | $this->getCartMetadata() |
||
| 759 | )) |
||
| 760 | ->isEqualTo( |
||
| 761 | [ |
||
| 762 | 'cartItemList' => [ |
||
| 763 | [ |
||
| 764 | 'amount' => 1, |
||
| 765 | 'cartItemDetailList' => [ |
||
| 766 | [ |
||
| 767 | '@id' => '/v12/cart_item_details/1', |
||
| 768 | 'name' => '', |
||
| 769 | ], |
||
| 770 | ], |
||
| 771 | '@id' => '/v12/cart_items/1', |
||
| 772 | ], |
||
| 773 | [ |
||
| 774 | '@id' => '/v12/cart_items/2', |
||
| 775 | 'amount' => 1, |
||
| 776 | ], |
||
| 777 | [ |
||
| 778 | '@id' => '/v12/cart_items/3', |
||
| 779 | 'cartItemDetailList' => [ |
||
| 780 | [ |
||
| 781 | '@id' => '/v12/cart_item_details/2', |
||
| 782 | 'name' => '', |
||
| 783 | ], |
||
| 784 | ], |
||
| 785 | ], |
||
| 786 | ], |
||
| 787 | ] |
||
| 788 | ) |
||
| 789 | ; |
||
| 790 | } |
||
| 791 | |||
| 792 | public function testNoMetadataChangeArray() |
||
| 793 | { |
||
| 794 | $mapping = $this->getMapping(); |
||
| 795 | $unitOfWork = $this->newTestedInstance($mapping); |
||
| 796 | |||
| 797 | $this |
||
| 798 | ->given($newSerializedModel = [ |
||
| 799 | '@id' => '/v12/carts/1', |
||
| 800 | 'cartInfo' => [ |
||
| 801 | [ |
||
| 802 | 'firstname' => 'jane', |
||
| 803 | 'lastname' => 'doe', |
||
| 804 | 'children' => ['rusty', 'john-john-junior'], |
||
| 805 | ], |
||
| 806 | ], |
||
| 807 | ]) |
||
| 808 | ->then |
||
| 809 | ->variable($unitOfWork->getDirtyData( |
||
| 810 | $newSerializedModel, |
||
| 811 | [ |
||
| 812 | '@id' => '/v12/carts/1', |
||
| 813 | 'cartInfo' => [ |
||
| 814 | [ |
||
| 815 | 'firstname' => 'john', |
||
| 816 | 'lastname' => 'doe', |
||
| 817 | 'children' => ['rusty', 'john-john-junior'], |
||
| 818 | ], |
||
| 819 | ], |
||
| 820 | ], |
||
| 821 | $this->getCartMetadata() |
||
| 822 | )) |
||
| 823 | ->isEqualTo( |
||
| 824 | [ |
||
| 825 | 'cartInfo' => [ |
||
| 826 | [ |
||
| 827 | 'firstname' => 'jane', |
||
| 828 | 'lastname' => 'doe', |
||
| 829 | 'children' => ['rusty', 'john-john-junior'], |
||
| 830 | ], |
||
| 831 | ], |
||
| 832 | ] |
||
| 833 | ) |
||
| 834 | |||
| 835 | ->then |
||
| 836 | ->variable($unitOfWork->getDirtyData( |
||
| 837 | [ |
||
| 838 | '@id' => '/v12/carts/1', |
||
| 839 | 'order' => [ |
||
| 840 | '@id' => '/v12/orders/1', |
||
| 841 | 'customerPaidAmount' => 1500, |
||
| 842 | 'status' => 'awaiting_payment', |
||
| 843 | ], |
||
| 844 | ], |
||
| 845 | [ |
||
| 846 | '@id' => '/v12/carts/1', |
||
| 847 | 'order' => [ |
||
| 848 | '@id' => '/v12/orders/1', |
||
| 849 | 'customerPaidAmount' => 1000, |
||
| 850 | 'status' => 'awaiting_payment', |
||
| 851 | ], |
||
| 852 | ], |
||
| 853 | $this->getCartMetadata() |
||
| 854 | )) |
||
| 855 | ->isEqualTo( |
||
| 856 | [ |
||
| 857 | 'order' => [ |
||
| 858 | '@id' => '/v12/orders/1', |
||
| 859 | 'customerPaidAmount' => 1500, |
||
| 860 | ], |
||
| 861 | ] |
||
| 862 | ) |
||
| 863 | |||
| 864 | ; |
||
| 865 | } |
||
| 866 | |||
| 867 | private function getMapping() |
||
| 868 | { |
||
| 869 | $mapping = $mapping = new RestMapping(); |
||
|
0 ignored issues
–
show
|
|||
| 870 | $mapping->setMapping([ |
||
| 871 | $this->getOrderMetadata(), |
||
| 872 | $this->getCartMetadata(), |
||
| 873 | $this->getCartItemMetadata(), |
||
| 874 | $this->getCartItemDetailMetadata(), |
||
| 875 | ]); |
||
| 876 | |||
| 877 | return $mapping; |
||
| 878 | } |
||
| 879 | |||
| 880 | private function getOrderMetadata() |
||
| 881 | { |
||
| 882 | $orderMetadata = new ClassMetadata( |
||
| 883 | 'orders', |
||
| 884 | 'Mapado\RestClientSdk\Tests\Model\JsonLd\Order', |
||
| 885 | '' |
||
| 886 | ); |
||
| 887 | |||
| 888 | $orderMetadata->setAttributeList([ |
||
| 889 | new Attribute('@id', 'id', 'string', true), |
||
| 890 | new Attribute('customerPaidAmount', 'customerPaidAmount', 'int'), |
||
| 891 | new Attribute('status'), |
||
| 892 | ]); |
||
| 893 | |||
| 894 | $orderMetadata->setRelationList([ |
||
| 895 | ]); |
||
| 896 | |||
| 897 | return $orderMetadata; |
||
| 898 | } |
||
| 899 | |||
| 900 | private function getCartMetadata() |
||
| 901 | { |
||
| 902 | $cartMetadata = new ClassMetadata( |
||
| 903 | 'carts', |
||
| 904 | 'Mapado\RestClientSdk\Tests\Model\JsonLd\Cart', |
||
| 905 | '' |
||
| 906 | ); |
||
| 907 | |||
| 908 | $cartMetadata->setAttributeList([ |
||
| 909 | new Attribute('@id', 'id', 'string', true), |
||
| 910 | new Attribute('status'), |
||
| 911 | new Attribute('clientPhoneNumber', 'clientPhoneNumber', 'phone_number'), |
||
| 912 | new Attribute('createdAt', 'createdAt', 'datetime'), |
||
| 913 | new Attribute('cart_items', 'cartItemList'), |
||
| 914 | new Attribute('order'), |
||
| 915 | ]); |
||
| 916 | |||
| 917 | $cartMetadata->setRelationList([ |
||
| 918 | new Relation('cartItemList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem'), |
||
| 919 | new Relation('order', Relation::MANY_TO_ONE, 'Mapado\RestClientSdk\Tests\Model\JsonLd\Order'), |
||
| 920 | ]); |
||
| 921 | |||
| 922 | return $cartMetadata; |
||
| 923 | } |
||
| 924 | |||
| 925 | private function getCartItemMetadata() |
||
| 926 | { |
||
| 927 | $cartItemMetadata = new ClassMetadata( |
||
| 928 | 'cart_items', |
||
| 929 | 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItem', |
||
| 930 | '' |
||
| 931 | ); |
||
| 932 | |||
| 933 | $cartItemMetadata->setAttributeList([ |
||
| 934 | new Attribute('@id', 'id', 'string', true), |
||
| 935 | new Attribute('amount'), |
||
| 936 | new Attribute('cartItemDetailList'), |
||
| 937 | ]); |
||
| 938 | |||
| 939 | $cartItemMetadata->setRelationList([ |
||
| 940 | new Relation('cartItemDetailList', Relation::ONE_TO_MANY, 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail'), |
||
| 941 | ]); |
||
| 942 | |||
| 943 | return $cartItemMetadata; |
||
| 944 | } |
||
| 945 | |||
| 946 | private function getCartItemDetailMetadata() |
||
| 947 | { |
||
| 948 | $cartItemDetailMetadata = new ClassMetadata( |
||
| 949 | 'cart_item_details', |
||
| 950 | 'Mapado\RestClientSdk\Tests\Model\JsonLd\CartItemDetail', |
||
| 951 | '' |
||
| 952 | ); |
||
| 953 | |||
| 954 | $cartItemDetailMetadata->setAttributeList([ |
||
| 955 | new Attribute('@id', 'id', 'string', true), |
||
| 956 | new Attribute('name'), |
||
| 957 | ]); |
||
| 958 | |||
| 959 | return $cartItemDetailMetadata; |
||
| 960 | } |
||
| 961 | } |
||
| 962 |