|
7 | 7 | use DateTimeImmutable; |
8 | 8 | use DateTimeZone; |
9 | 9 | use DivisionByZeroError; |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use Yiisoft\ActiveRecord\ActiveQueryInterface; |
10 | 12 | use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Alpha; |
11 | 13 | use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Animal; |
12 | 14 | use Yiisoft\ActiveRecord\Tests\Stubs\MagicActiveRecord\Cat; |
@@ -849,4 +851,138 @@ public function testIsChanged(): void |
849 | 851 |
|
850 | 852 | $this->assertTrue($newItem->isChanged()); |
851 | 853 | } |
| 854 | + |
| 855 | + public function testGettingWriteOnlyProperty(): void |
| 856 | + { |
| 857 | + $customer = new Customer(); |
| 858 | + |
| 859 | + $this->expectException(InvalidCallException::class); |
| 860 | + $this->expectExceptionMessage( |
| 861 | + 'Getting write-only property: ' . Customer::class . '::ordersReadOnly' |
| 862 | + ); |
| 863 | + $customer->ordersReadOnly; |
| 864 | + } |
| 865 | + |
| 866 | + public function testUnsetPropertyWithDependentRelations(): void |
| 867 | + { |
| 868 | + $orderItem = new OrderItem(); |
| 869 | + $orderItem->order_id = 1; |
| 870 | + $orderItem->item_id = 2; |
| 871 | + |
| 872 | + $order = $orderItem->order; |
| 873 | + $this->assertNotNull($order); |
| 874 | + $this->assertEquals(1, $order->id); |
| 875 | + $this->assertTrue($orderItem->isRelationPopulated('order')); |
| 876 | + |
| 877 | + unset($orderItem->order_id); |
| 878 | + |
| 879 | + $this->assertFalse($orderItem->isRelationPopulated('order')); |
| 880 | + $this->assertNull($orderItem->order_id); |
| 881 | + } |
| 882 | + |
| 883 | + public function testUnsetPopulatedRelation(): void |
| 884 | + { |
| 885 | + $customerQuery = Customer::query(); |
| 886 | + $customer = $customerQuery->findByPk(1); |
| 887 | + |
| 888 | + $orders = $customer->orders; |
| 889 | + $this->assertNotEmpty($orders); |
| 890 | + $this->assertTrue($customer->isRelationPopulated('orders')); |
| 891 | + |
| 892 | + unset($customer->orders); |
| 893 | + |
| 894 | + $this->assertFalse($customer->isRelationPopulated('orders')); |
| 895 | + } |
| 896 | + |
| 897 | + public function testSettingUnknownProperty(): void |
| 898 | + { |
| 899 | + $customer = new Customer(); |
| 900 | + |
| 901 | + $this->expectException(UnknownPropertyException::class); |
| 902 | + $this->expectExceptionMessage( |
| 903 | + 'Setting unknown property: ' . Customer::class . '::nonExistentProperty' |
| 904 | + ); |
| 905 | + $customer->nonExistentProperty = 'value'; |
| 906 | + } |
| 907 | + |
| 908 | + public static function dataIsProperty(): array |
| 909 | + { |
| 910 | + return [ |
| 911 | + 'table column property' => [true, 'name'], |
| 912 | + 'another table column' => [true, 'email'], |
| 913 | + 'relation query' => [true, 'profile'], |
| 914 | + 'setter only' => [true, 'ordersReadOnly'], |
| 915 | + 'public class property' => [true, 'status2'], |
| 916 | + 'another public class property' => [true, 'sumTotal'], |
| 917 | + 'non-existent property' => [false, 'nonExistent'], |
| 918 | + ]; |
| 919 | + } |
| 920 | + |
| 921 | + #[DataProvider('dataIsProperty')] |
| 922 | + public function testIsProperty(bool $expected, string $name): void |
| 923 | + { |
| 924 | + $customer = new Customer(); |
| 925 | + |
| 926 | + $this->assertSame($expected, $customer->isProperty($name)); |
| 927 | + } |
| 928 | + |
| 929 | + public static function dataIsPropertyWithoutCheckVars(): array |
| 930 | + { |
| 931 | + return [ |
| 932 | + 'table column property' => [true, 'name'], |
| 933 | + 'another table column' => [true, 'email'], |
| 934 | + 'relation query' => [true, 'profile'], |
| 935 | + 'setter only' => [true, 'ordersReadOnly'], |
| 936 | + 'public class property' => [false, 'status2'], |
| 937 | + 'another public class property' => [false, 'sumTotal'], |
| 938 | + 'non-existent property' => [false, 'nonExistent'], |
| 939 | + ]; |
| 940 | + } |
| 941 | + |
| 942 | + #[DataProvider('dataIsPropertyWithoutCheckVars')] |
| 943 | + public function testIsPropertyWithoutCheckVars(bool $expected, string $name): void |
| 944 | + { |
| 945 | + $customer = new Customer(); |
| 946 | + |
| 947 | + $this->assertSame($expected, $customer->isProperty($name, false)); |
| 948 | + } |
| 949 | + |
| 950 | + public function testRelationQueryNonExistentRelation(): void |
| 951 | + { |
| 952 | + $customer = new Customer(); |
| 953 | + |
| 954 | + $this->expectException(InvalidArgumentException::class); |
| 955 | + $this->expectExceptionMessage( |
| 956 | + Customer::class . ' has no relation named "nonExistentRelation".' |
| 957 | + ); |
| 958 | + $customer->relationQuery('nonExistentRelation'); |
| 959 | + } |
| 960 | + |
| 961 | + public function testRelationQueryInvalidReturnType(): void |
| 962 | + { |
| 963 | + $customer = new Customer(); |
| 964 | + |
| 965 | + $this->expectException(InvalidArgumentException::class); |
| 966 | + $this->expectExceptionMessage( |
| 967 | + 'Relation query method "' |
| 968 | + . Customer::class |
| 969 | + . '::getItemQuery()" should return type "' |
| 970 | + . ActiveQueryInterface::class |
| 971 | + . '", but returns "void" type.' |
| 972 | + ); |
| 973 | + $customer->relationQuery('item'); |
| 974 | + } |
| 975 | + |
| 976 | + public function testRelationQueryCaseSensitive(): void |
| 977 | + { |
| 978 | + $customer = new Customer(); |
| 979 | + |
| 980 | + $this->expectException(InvalidArgumentException::class); |
| 981 | + $this->expectExceptionMessage( |
| 982 | + 'Relation names are case sensitive. ' |
| 983 | + . Customer::class |
| 984 | + . ' has a relation named "profile" instead of "Profile".' |
| 985 | + ); |
| 986 | + $customer->relationQuery('Profile'); |
| 987 | + } |
852 | 988 | } |
0 commit comments