|
7 | 7 | use PHPUnit\Framework\TestCase; |
8 | 8 | use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\Article; |
9 | 9 | use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\Comment; |
| 10 | +use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\Country; |
10 | 11 | use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\Profile; |
11 | 12 | use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\Tag; |
12 | 13 | use Bancer\NativeQueryMapperTest\TestApp\Model\Entity\User; |
13 | 14 | use Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable; |
14 | 15 | use Bancer\NativeQueryMapperTest\TestApp\Model\Table\CommentsTable; |
| 16 | +use Bancer\NativeQueryMapperTest\TestApp\Model\Table\CountriesTable; |
15 | 17 | use Cake\ORM\Locator\LocatorAwareTrait; |
16 | 18 | use Bancer\NativeQueryMapper\ORM\UnknownAliasException; |
17 | 19 | use Bancer\NativeQueryMapperTest\TestApp\Model\Table\UsersTable; |
@@ -343,4 +345,242 @@ public function testBelongsToManyFetchJoinTable(): void |
343 | 345 | $this->assertEqualsEntities($cakeEntities, $actual); |
344 | 346 | static::assertEquals($cakeEntities, $actual);*/ |
345 | 347 | } |
| 348 | + |
| 349 | + public function testDeepAssociations(): void |
| 350 | + { |
| 351 | + /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\CountriesTable $CountriesTable */ |
| 352 | + $CountriesTable = $this->fetchTable(CountriesTable::class); |
| 353 | + $stmt = $CountriesTable->prepareSQL(" |
| 354 | + SELECT |
| 355 | + Countries.id AS Countries__id, |
| 356 | + Countries.name as Countries__name, |
| 357 | + Users.id AS Users__id, |
| 358 | + Users.username AS Users__username, |
| 359 | + Users.country_id AS Users__country_id, |
| 360 | + Profiles.id AS Profiles__id, |
| 361 | + Profiles.bio AS Profiles__bio, |
| 362 | + Articles.id AS Articles__id, |
| 363 | + Articles.title AS Articles__title, |
| 364 | + Articles.user_id AS Articles__user_id, |
| 365 | + Comments.id AS Comments__id, |
| 366 | + Comments.content AS Comments__content, |
| 367 | + Comments.article_id AS Comments__article_id |
| 368 | + FROM countries AS Countries |
| 369 | + LEFT JOIN users AS Users |
| 370 | + ON Users.country_id=Countries.id |
| 371 | + LEFT JOIN profiles AS Profiles |
| 372 | + ON Profiles.user_id=Users.id |
| 373 | + LEFT JOIN articles AS Articles |
| 374 | + ON Articles.user_id=Users.id |
| 375 | + LEFT JOIN articles_tags AS ArticlesTags |
| 376 | + ON Articles.id=ArticlesTags.article_id |
| 377 | + LEFT JOIN tags AS Tags |
| 378 | + ON Tags.id=ArticlesTags.tag_id |
| 379 | + LEFT JOIN comments AS Comments |
| 380 | + ON Comments.article_id=Articles.id |
| 381 | + "); |
| 382 | + $actual = $CountriesTable->fromNativeQuery($stmt)->all(); |
| 383 | + static::assertCount(5, $actual); |
| 384 | + static::assertInstanceOf(Country::class, $actual[0]); |
| 385 | + $actualUsers = $actual[0]->get('users'); |
| 386 | + static::assertIsArray($actualUsers); |
| 387 | + static::assertCount(1, $actualUsers); |
| 388 | + static::assertInstanceOf(User::class, $actualUsers[0]); |
| 389 | + $actualArticles = $actualUsers[0]->get('articles'); |
| 390 | + static::assertIsArray($actualArticles); |
| 391 | + static::assertCount(1, $actualArticles); |
| 392 | + static::assertInstanceOf(Article::class, $actualArticles[0]); |
| 393 | + $actualComments = $actualArticles[0]->get('comments'); |
| 394 | + static::assertIsArray($actualComments); |
| 395 | + static::assertCount(2, $actualComments); |
| 396 | + static::assertInstanceOf(Comment::class, $actualComments[0]); |
| 397 | + $expected = [ |
| 398 | + 'id' => 1, |
| 399 | + 'name' => 'USA', |
| 400 | + 'users' => [ |
| 401 | + [ |
| 402 | + 'id' => 1, |
| 403 | + 'username' => 'alice', |
| 404 | + 'country_id' => 1, |
| 405 | + 'profile' => [ |
| 406 | + 'id' => 1, |
| 407 | + 'bio' => 'Bio Alice', |
| 408 | + ], |
| 409 | + 'articles' => [ |
| 410 | + [ |
| 411 | + 'id' => 1, |
| 412 | + 'title' => 'Article 1', |
| 413 | + 'user_id' => 1, |
| 414 | + 'comments' => [ |
| 415 | + [ |
| 416 | + 'id' => 1, |
| 417 | + 'content' => 'Comment 1', |
| 418 | + 'article_id' => 1, |
| 419 | + ], |
| 420 | + [ |
| 421 | + 'id' => 2, |
| 422 | + 'content' => 'Comment 2', |
| 423 | + 'article_id' => 1, |
| 424 | + ], |
| 425 | + ], |
| 426 | + ], |
| 427 | + ], |
| 428 | + ], |
| 429 | + ], |
| 430 | + ]; |
| 431 | + static::assertSame($expected, $actual[0]->toArray()); |
| 432 | + $cakeEntities = $CountriesTable->find() |
| 433 | + ->select(['Countries.id', 'Countries.name']) |
| 434 | + ->contain([ |
| 435 | + 'Users' => [ |
| 436 | + 'fields' => ['Users.id', 'Users.username', 'Users.country_id'], |
| 437 | + 'Profiles' => [ |
| 438 | + 'fields' => ['Profiles.id', 'Profiles.bio'], |
| 439 | + ], |
| 440 | + 'Articles' => [ |
| 441 | + 'fields' => ['Articles.id', 'Articles.title', 'Articles.user_id'], |
| 442 | + 'Comments' => [ |
| 443 | + 'fields' => ['Comments.id', 'Comments.content', 'Comments.article_id'], |
| 444 | + ], |
| 445 | + ], |
| 446 | + ], |
| 447 | + ]) |
| 448 | + ->toArray(); |
| 449 | + $this->assertEqualsEntities($cakeEntities, $actual); |
| 450 | + } |
| 451 | + |
| 452 | + public function testDeepAssociationsWithBelongsToMany(): void |
| 453 | + { |
| 454 | + /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\CountriesTable $CountriesTable */ |
| 455 | + $CountriesTable = $this->fetchTable(CountriesTable::class); |
| 456 | + $stmt = $CountriesTable->prepareSQL(" |
| 457 | + SELECT |
| 458 | + Countries.id AS Countries__id, |
| 459 | + Countries.name as Countries__name, |
| 460 | + Users.id AS Users__id, |
| 461 | + Users.username AS Users__username, |
| 462 | + Profiles.id AS Profiles__id, |
| 463 | + Profiles.bio AS Profiles__bio, |
| 464 | + Articles.id AS Articles__id, |
| 465 | + Articles.title AS Articles__title, |
| 466 | + Tags.id AS Tags__id, |
| 467 | + Tags.name AS Tags__name, |
| 468 | + ArticlesTags.id AS ArticlesTags__id, |
| 469 | + ArticlesTags.article_id AS ArticlesTags__article_id, |
| 470 | + ArticlesTags.tag_id AS ArticlesTags__tag_id, |
| 471 | + Comments.id AS Comments__id, |
| 472 | + Comments.content AS Comments__content |
| 473 | + FROM countries AS Countries |
| 474 | + LEFT JOIN users AS Users |
| 475 | + ON Users.country_id=Countries.id |
| 476 | + LEFT JOIN profiles AS Profiles |
| 477 | + ON Profiles.user_id=Users.id |
| 478 | + LEFT JOIN articles AS Articles |
| 479 | + ON Articles.user_id=Users.id |
| 480 | + LEFT JOIN articles_tags AS ArticlesTags |
| 481 | + ON Articles.id=ArticlesTags.article_id |
| 482 | + LEFT JOIN tags AS Tags |
| 483 | + ON Tags.id=ArticlesTags.tag_id |
| 484 | + LEFT JOIN comments AS Comments |
| 485 | + ON Comments.article_id=Articles.id |
| 486 | + "); |
| 487 | + $actual = $CountriesTable->fromNativeQuery($stmt)->all(); |
| 488 | + static::assertCount(5, $actual); |
| 489 | + static::assertInstanceOf(Country::class, $actual[0]); |
| 490 | + $actualUsers = $actual[0]->get('users'); |
| 491 | + static::assertIsArray($actualUsers); |
| 492 | + static::assertCount(1, $actualUsers); |
| 493 | + static::assertInstanceOf(User::class, $actualUsers[0]); |
| 494 | + $actualArticles = $actualUsers[0]->get('articles'); |
| 495 | + static::assertIsArray($actualArticles); |
| 496 | + static::assertCount(1, $actualArticles); |
| 497 | + static::assertInstanceOf(Article::class, $actualArticles[0]); |
| 498 | + $actualComments = $actualArticles[0]->get('comments'); |
| 499 | + static::assertIsArray($actualComments); |
| 500 | + static::assertCount(2, $actualComments); |
| 501 | + static::assertInstanceOf(Comment::class, $actualComments[0]); |
| 502 | + $actualTags = $actualArticles[0]->get('tags'); |
| 503 | + static::assertIsArray($actualTags); |
| 504 | + static::assertCount(2, $actualTags); |
| 505 | + static::assertInstanceOf(Tag::class, $actualTags[0]); |
| 506 | + $expected = [ |
| 507 | + 'id' => 1, |
| 508 | + 'name' => 'USA', |
| 509 | + 'users' => [ |
| 510 | + [ |
| 511 | + 'id' => 1, |
| 512 | + 'username' => 'alice', |
| 513 | + 'profile' => [ |
| 514 | + 'id' => 1, |
| 515 | + 'bio' => 'Bio Alice', |
| 516 | + ], |
| 517 | + 'articles' => [ |
| 518 | + [ |
| 519 | + 'id' => 1, |
| 520 | + 'title' => 'Article 1', |
| 521 | + 'comments' => [ |
| 522 | + [ |
| 523 | + 'id' => 1, |
| 524 | + 'content' => 'Comment 1', |
| 525 | + ], |
| 526 | + [ |
| 527 | + 'id' => 2, |
| 528 | + 'content' => 'Comment 2', |
| 529 | + ], |
| 530 | + ], |
| 531 | + 'tags' => [ |
| 532 | + [ |
| 533 | + 'id' => 1, |
| 534 | + 'name' => 'Tech', |
| 535 | + 'articles_tag' => |
| 536 | + [ |
| 537 | + 'id' => 1, |
| 538 | + 'article_id' => 1, |
| 539 | + 'tag_id' => 1, |
| 540 | + ], |
| 541 | + ], |
| 542 | + [ |
| 543 | + 'id' => 2, |
| 544 | + 'name' => 'Food', |
| 545 | + 'articles_tag' => |
| 546 | + [ |
| 547 | + 'id' => 2, |
| 548 | + 'article_id' => 1, |
| 549 | + 'tag_id' => 2, |
| 550 | + ], |
| 551 | + ], |
| 552 | + ], |
| 553 | + ], |
| 554 | + ], |
| 555 | + ], |
| 556 | + ], |
| 557 | + ]; |
| 558 | + static::assertSame($expected, $actual[0]->toArray()); |
| 559 | + /*$cakeEntities = $CountriesTable->find() |
| 560 | + ->select(['Countries.id', 'Countries.name']) |
| 561 | + ->contain([ |
| 562 | + 'Users' => [ |
| 563 | + 'fields' => ['Users.id', 'Users.username', 'Users.country_id'], |
| 564 | + 'Profiles' => [ |
| 565 | + 'fields' => ['Profiles.id', 'Profiles.bio'], |
| 566 | + ], |
| 567 | + 'Articles' => [ |
| 568 | + 'fields' => ['Articles.id', 'Articles.title', 'Articles.user_id'], |
| 569 | + 'Tags' => [ |
| 570 | + 'fields' => ['Tags.id', 'Tags.name'], |
| 571 | + 'ArticlesTags' => [ |
| 572 | + 'fields' => ['ArticlesTags.id', 'ArticlesTags.article_id', 'ArticlesTags.tag_id'], |
| 573 | + ], |
| 574 | + ], |
| 575 | + 'Comments' => [ |
| 576 | + 'fields' => ['Comments.id', 'Comments.content', 'Comments.article_id'], |
| 577 | + ], |
| 578 | + ], |
| 579 | + ], |
| 580 | + ]) |
| 581 | + ->toArray(); |
| 582 | + static::assertSame($cakeEntities[0]->toArray(), $actual[0]->toArray()); |
| 583 | + $this->assertEqualsEntities($cakeEntities, $actual); |
| 584 | + static::assertEquals($cakeEntities, $actual);*/ |
| 585 | + } |
346 | 586 | } |
0 commit comments