|
783 | 783 | expect($model->createdAt)->toBeInstanceOf(Date::class); |
784 | 784 | }); |
785 | 785 |
|
| 786 | +it('creates model with manually assigned string ID when insertGetId returns zero', function () { |
| 787 | + $insertResult = new Result([['Query OK']]); |
| 788 | + $insertResult->setLastInsertedId(0); |
| 789 | + |
| 790 | + $connection = $this->getMockBuilder(MysqlConnectionPool::class)->getMock(); |
| 791 | + |
| 792 | + $connection->expects($this->exactly(1)) |
| 793 | + ->method('prepare') |
| 794 | + ->willReturnOnConsecutiveCalls( |
| 795 | + new Statement($insertResult), |
| 796 | + ); |
| 797 | + |
| 798 | + $this->app->swap(Connection::default(), $connection); |
| 799 | + |
| 800 | + $uuid = Str::uuid()->toString(); |
| 801 | + |
| 802 | + $model = UserWithUuid::create([ |
| 803 | + 'id' => $uuid, |
| 804 | + 'name' => 'John Doe', |
| 805 | + 'email' => faker()->email(), |
| 806 | + 'created_at' => Date::now(), |
| 807 | + ]); |
| 808 | + |
| 809 | + expect($model->isExisting())->toBeTrue(); |
| 810 | + expect($model->id)->toBe($uuid); |
| 811 | + expect($model->createdAt)->toBeInstanceOf(Date::class); |
| 812 | +}); |
| 813 | + |
786 | 814 | it('throws an exception when column in invalid on create instance', function () { |
787 | 815 | expect(function () { |
788 | 816 | $connection = $this->getMockBuilder(MysqlConnectionPool::class)->getMock(); |
|
895 | 923 | expect($model->createdAt)->toBeInstanceOf(Date::class); |
896 | 924 | }); |
897 | 925 |
|
| 926 | +it('saves a new model with manually assigned string ID when insertGetId returns zero', function () { |
| 927 | + $insertResult = new Result([['Query OK']]); |
| 928 | + $insertResult->setLastInsertedId(0); |
| 929 | + |
| 930 | + $connection = $this->getMockBuilder(MysqlConnectionPool::class)->getMock(); |
| 931 | + |
| 932 | + $connection->expects($this->exactly(1)) |
| 933 | + ->method('prepare') |
| 934 | + ->willReturnOnConsecutiveCalls( |
| 935 | + new Statement($insertResult), |
| 936 | + ); |
| 937 | + |
| 938 | + $this->app->swap(Connection::default(), $connection); |
| 939 | + |
| 940 | + $uuid = Str::uuid()->toString(); |
| 941 | + $model = new UserWithUuid(); |
| 942 | + $model->id = $uuid; |
| 943 | + $model->name = 'John Doe'; |
| 944 | + $model->email = faker()->email(); |
| 945 | + |
| 946 | + expect($model->isExisting())->toBeFalse(); |
| 947 | + expect($model->save())->toBeTrue(); |
| 948 | + expect($model->isExisting())->toBeTrue(); |
| 949 | + expect($model->id)->toBe($uuid); |
| 950 | + expect($model->createdAt)->toBeInstanceOf(Date::class); |
| 951 | +}); |
| 952 | + |
898 | 953 | it('updates an existing model with string ID correctly', function () { |
899 | 954 | $uuid = Str::uuid()->toString(); |
900 | 955 | $data = [ |
|
0 commit comments