|
49 | 49 | use Tests\Tempest\Fixtures\Modules\Books\Models\Book; |
50 | 50 | use Tests\Tempest\Fixtures\Modules\Books\Models\Isbn; |
51 | 51 | use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 52 | +use Tempest\Database\Builder\QueryBuilders\QueryBuilder; |
52 | 53 |
|
53 | 54 | use function Tempest\Database\query; |
54 | 55 | use function Tempest\Mapper\map; |
@@ -694,6 +695,100 @@ public function test_nullable_relation_save(): void |
694 | 695 | $this->assertNotNull($a->b); |
695 | 696 | $this->assertSame('b', $a->b->name); |
696 | 697 | } |
| 698 | + |
| 699 | + public function test_on_returns_query_builder_with_database_tag(): void |
| 700 | + { |
| 701 | + $builder = Foo::on('analytics'); |
| 702 | + |
| 703 | + $this->assertInstanceOf(QueryBuilder::class, $builder); |
| 704 | + $this->assertSame('analytics', $builder->onDatabase); |
| 705 | + } |
| 706 | + |
| 707 | + public function test_on_propagates_tag_to_select_builder(): void |
| 708 | + { |
| 709 | + $selectBuilder = Foo::on('analytics')->select(); |
| 710 | + |
| 711 | + $this->assertSame('analytics', $selectBuilder->onDatabase); |
| 712 | + } |
| 713 | + |
| 714 | + public function test_on_propagates_tag_to_insert_builder(): void |
| 715 | + { |
| 716 | + $insertBuilder = Foo::on('analytics')->insert(); |
| 717 | + |
| 718 | + $this->assertSame('analytics', $insertBuilder->onDatabase); |
| 719 | + } |
| 720 | + |
| 721 | + public function test_on_propagates_tag_to_count_builder(): void |
| 722 | + { |
| 723 | + $countBuilder = Foo::on('analytics')->count(); |
| 724 | + |
| 725 | + $this->assertSame('analytics', $countBuilder->onDatabase); |
| 726 | + } |
| 727 | + |
| 728 | + public function test_on_propagates_tag_to_delete_builder(): void |
| 729 | + { |
| 730 | + $deleteBuilder = Foo::on('analytics')->delete(); |
| 731 | + |
| 732 | + $this->assertSame('analytics', $deleteBuilder->onDatabase); |
| 733 | + } |
| 734 | + |
| 735 | + public function test_on_with_null_sets_null_tag(): void |
| 736 | + { |
| 737 | + $builder = Foo::on(null); |
| 738 | + |
| 739 | + $this->assertNull($builder->onDatabase); |
| 740 | + } |
| 741 | + |
| 742 | + public function test_on_with_enum_tag(): void |
| 743 | + { |
| 744 | + $builder = Foo::on(TestDatabaseTag::Analytics); |
| 745 | + |
| 746 | + $this->assertSame(TestDatabaseTag::Analytics, $builder->onDatabase); |
| 747 | + } |
| 748 | + |
| 749 | + public function test_on_database_returns_clone(): void |
| 750 | + { |
| 751 | + $this->database->migrate( |
| 752 | + CreateMigrationsTable::class, |
| 753 | + FooDatabaseMigration::class, |
| 754 | + ); |
| 755 | + |
| 756 | + $foo = Foo::create(bar: 'baz'); |
| 757 | + $clone = $foo->onDatabase('analytics'); |
| 758 | + |
| 759 | + $this->assertNotSame($foo, $clone); |
| 760 | + } |
| 761 | + |
| 762 | + public function test_on_database_does_not_mutate_original(): void |
| 763 | + { |
| 764 | + $this->database->migrate( |
| 765 | + CreateMigrationsTable::class, |
| 766 | + FooDatabaseMigration::class, |
| 767 | + ); |
| 768 | + |
| 769 | + $foo = Foo::create(bar: 'baz'); |
| 770 | + $foo->onDatabase('analytics'); |
| 771 | + |
| 772 | + // Original still works against default database |
| 773 | + $foo->update(bar: 'updated'); |
| 774 | + $refreshed = Foo::get($foo->id); |
| 775 | + |
| 776 | + $this->assertSame('updated', $refreshed->bar); |
| 777 | + } |
| 778 | + |
| 779 | + public function test_on_database_preserves_model_data(): void |
| 780 | + { |
| 781 | + $this->database->migrate( |
| 782 | + CreateMigrationsTable::class, |
| 783 | + FooDatabaseMigration::class, |
| 784 | + ); |
| 785 | + |
| 786 | + $foo = Foo::create(bar: 'baz'); |
| 787 | + $clone = $foo->onDatabase('analytics'); |
| 788 | + |
| 789 | + $this->assertSame('baz', $clone->bar); |
| 790 | + $this->assertEquals($foo->id, $clone->id); |
| 791 | + } |
697 | 792 | } |
698 | 793 |
|
699 | 794 | final class Foo |
@@ -1117,3 +1212,9 @@ final class ModelWithHookedVirtualProperty |
1117 | 1212 | get => strtoupper($this->name); |
1118 | 1213 | } |
1119 | 1214 | } |
| 1215 | + |
| 1216 | +enum TestDatabaseTag |
| 1217 | +{ |
| 1218 | + case Analytics; |
| 1219 | + case Reporting; |
| 1220 | +} |
0 commit comments