|
10 | 10 | use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat; |
11 | 11 | use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; |
12 | 12 | use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; |
| 13 | +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; |
13 | 14 | use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias; |
14 | 15 | use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog; |
15 | 16 | use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item; |
@@ -763,4 +764,58 @@ public function testToArrayWithClosure(): void |
763 | 764 | $customer->toArray(), |
764 | 765 | ); |
765 | 766 | } |
| 767 | + |
| 768 | + public function testToArrayForArrayable(): void |
| 769 | + { |
| 770 | + $this->checkFixture($this->db, 'customer', true); |
| 771 | + |
| 772 | + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); |
| 773 | + |
| 774 | + /** @var CustomerForArrayable $customer */ |
| 775 | + $customer = $customerQuery->findOne(1); |
| 776 | + /** @var CustomerForArrayable $customer2 */ |
| 777 | + $customer2 = $customerQuery->findOne(2); |
| 778 | + /** @var CustomerForArrayable $customer3 */ |
| 779 | + $customer3 = $customerQuery->findOne(3); |
| 780 | + |
| 781 | + $customer->setItem($customer2); |
| 782 | + $customer->setItems($customer3); |
| 783 | + |
| 784 | + $this->assertSame( |
| 785 | + [ |
| 786 | + 'id' => 1, |
| 787 | + 'email' => 'user1@example.com', |
| 788 | + 'name' => 'user1', |
| 789 | + 'address' => 'address1', |
| 790 | + 'status' => 'active', |
| 791 | + 'item' => [ |
| 792 | + 'id' => 2, |
| 793 | + 'email' => 'user2@example.com', |
| 794 | + 'name' => 'user2', |
| 795 | + 'status' => 'active', |
| 796 | + ], |
| 797 | + 'items' => [ |
| 798 | + [ |
| 799 | + 'id' => 3, |
| 800 | + 'email' => 'user3@example.com', |
| 801 | + 'name' => 'user3', |
| 802 | + 'status' => 'inactive', |
| 803 | + ], |
| 804 | + ], |
| 805 | + ], |
| 806 | + $customer->toArray([ |
| 807 | + 'id', |
| 808 | + 'name', |
| 809 | + 'email', |
| 810 | + 'address', |
| 811 | + 'status', |
| 812 | + 'item.id', |
| 813 | + 'item.name', |
| 814 | + 'item.email', |
| 815 | + 'items.0.id', |
| 816 | + 'items.0.name', |
| 817 | + 'items.0.email', |
| 818 | + ]), |
| 819 | + ); |
| 820 | + } |
766 | 821 | } |
0 commit comments