@@ -31,7 +31,9 @@ protected function tearDown(): void
31
31
public function testFindMethod ()
32
32
{
33
33
$ builder = m::mock (Builder::class.'[first] ' , [$ this ->getMockQueryBuilder ()]);
34
- $ builder ->setModel ($ this ->getMockModel ());
34
+ $ model = $ this ->getMockModel ();
35
+ $ builder ->setModel ($ model );
36
+ $ model ->shouldReceive ('getKeyType ' )->once ()->andReturn ('int ' );
35
37
$ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ('foo_table.foo ' , '= ' , 'bar ' );
36
38
$ builder ->shouldReceive ('first ' )->with (['column ' ])->andReturn ('baz ' );
37
39
@@ -76,6 +78,7 @@ public function testFindManyMethod()
76
78
public function testFindOrNewMethodModelFound ()
77
79
{
78
80
$ model = $ this ->getMockModel ();
81
+ $ model ->shouldReceive ('getKeyType ' )->once ()->andReturn ('int ' );
79
82
$ model ->shouldReceive ('findOrNew ' )->once ()->andReturn ('baz ' );
80
83
81
84
$ builder = m::mock (Builder::class.'[first] ' , [$ this ->getMockQueryBuilder ()]);
@@ -91,6 +94,7 @@ public function testFindOrNewMethodModelFound()
91
94
public function testFindOrNewMethodModelNotFound ()
92
95
{
93
96
$ model = $ this ->getMockModel ();
97
+ $ model ->shouldReceive ('getKeyType ' )->once ()->andReturn ('int ' );
94
98
$ model ->shouldReceive ('findOrNew ' )->once ()->andReturn (m::mock (Model::class));
95
99
96
100
$ builder = m::mock (Builder::class.'[first] ' , [$ this ->getMockQueryBuilder ()]);
@@ -109,7 +113,9 @@ public function testFindOrFailMethodThrowsModelNotFoundException()
109
113
$ this ->expectException (ModelNotFoundException::class);
110
114
111
115
$ builder = m::mock (Builder::class.'[first] ' , [$ this ->getMockQueryBuilder ()]);
112
- $ builder ->setModel ($ this ->getMockModel ());
116
+ $ model = $ this ->getMockModel ();
117
+ $ model ->shouldReceive ('getKeyType ' )->once ()->andReturn ('int ' );
118
+ $ builder ->setModel ($ model );
113
119
$ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ('foo_table.foo ' , '= ' , 'bar ' );
114
120
$ builder ->shouldReceive ('first ' )->with (['column ' ])->andReturn (null );
115
121
$ builder ->findOrFail ('bar ' , ['column ' ]);
@@ -1017,11 +1023,39 @@ public function testWhereKeyMethodWithInt()
1017
1023
1018
1024
$ int = 1 ;
1019
1025
1026
+ $ model ->shouldReceive ('getKeyType ' )->once ()->andReturn ('int ' );
1020
1027
$ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ($ keyName , '= ' , $ int );
1021
1028
1022
1029
$ builder ->whereKey ($ int );
1023
1030
}
1024
1031
1032
+ public function testWhereKeyMethodWithStringZero ()
1033
+ {
1034
+ $ model = new EloquentBuilderTestStubStringPrimaryKey ();
1035
+ $ builder = $ this ->getBuilder ()->setModel ($ model );
1036
+ $ keyName = $ model ->getQualifiedKeyName ();
1037
+
1038
+ $ int = 0 ;
1039
+
1040
+ $ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ($ keyName , '= ' , (string ) $ int );
1041
+
1042
+ $ builder ->whereKey ($ int );
1043
+ }
1044
+
1045
+ /** @group Foo */
1046
+ public function testWhereKeyMethodWithStringNull ()
1047
+ {
1048
+ $ model = new EloquentBuilderTestStubStringPrimaryKey ();
1049
+ $ builder = $ this ->getBuilder ()->setModel ($ model );
1050
+ $ keyName = $ model ->getQualifiedKeyName ();
1051
+
1052
+ $ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ($ keyName , '= ' , m::on (function ($ argument ) {
1053
+ return $ argument === null ;
1054
+ }));
1055
+
1056
+ $ builder ->whereKey (null );
1057
+ }
1058
+
1025
1059
public function testWhereKeyMethodWithArray ()
1026
1060
{
1027
1061
$ model = $ this ->getMockModel ();
@@ -1048,6 +1082,33 @@ public function testWhereKeyMethodWithCollection()
1048
1082
$ builder ->whereKey ($ collection );
1049
1083
}
1050
1084
1085
+ public function testWhereKeyNotMethodWithStringZero ()
1086
+ {
1087
+ $ model = new EloquentBuilderTestStubStringPrimaryKey ();
1088
+ $ builder = $ this ->getBuilder ()->setModel ($ model );
1089
+ $ keyName = $ model ->getQualifiedKeyName ();
1090
+
1091
+ $ int = 0 ;
1092
+
1093
+ $ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ($ keyName , '!= ' , (string ) $ int );
1094
+
1095
+ $ builder ->whereKeyNot ($ int );
1096
+ }
1097
+
1098
+ /** @group Foo */
1099
+ public function testWhereKeyNotMethodWithStringNull ()
1100
+ {
1101
+ $ model = new EloquentBuilderTestStubStringPrimaryKey ();
1102
+ $ builder = $ this ->getBuilder ()->setModel ($ model );
1103
+ $ keyName = $ model ->getQualifiedKeyName ();
1104
+
1105
+ $ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ($ keyName , '!= ' , m::on (function ($ argument ) {
1106
+ return $ argument === null ;
1107
+ }));
1108
+
1109
+ $ builder ->whereKeyNot (null );
1110
+ }
1111
+
1051
1112
public function testWhereKeyNotMethodWithInt ()
1052
1113
{
1053
1114
$ model = $ this ->getMockModel ();
@@ -1056,6 +1117,7 @@ public function testWhereKeyNotMethodWithInt()
1056
1117
1057
1118
$ int = 1 ;
1058
1119
1120
+ $ model ->shouldReceive ('getKeyType ' )->once ()->andReturn ('int ' );
1059
1121
$ builder ->getQuery ()->shouldReceive ('where ' )->once ()->with ($ keyName , '!= ' , $ int );
1060
1122
1061
1123
$ builder ->whereKeyNot ($ int );
@@ -1414,3 +1476,12 @@ class EloquentBuilderTestStubWithoutTimestamp extends Model
1414
1476
1415
1477
protected $ table = 'table ' ;
1416
1478
}
1479
+
1480
+ class EloquentBuilderTestStubStringPrimaryKey extends Model
1481
+ {
1482
+ public $ incrementing = false ;
1483
+
1484
+ protected $ table = 'foo_table ' ;
1485
+
1486
+ protected $ keyType = 'string ' ;
1487
+ }
0 commit comments