8
8
use Illuminate \Database \Query \Builder ;
9
9
use Illuminate \Foundation \Testing \Concerns \InteractsWithDatabase ;
10
10
use Illuminate \Foundation \Testing \TestCase as TestingTestCase ;
11
+ use Illuminate \Support \Arr ;
11
12
use Illuminate \Support \Facades \DB ;
12
13
use Mockery as m ;
13
14
use Orchestra \Testbench \Concerns \CreatesApplication ;
@@ -39,14 +40,14 @@ protected function tearDown(): void
39
40
40
41
public function testSeeInDatabaseFindsResults ()
41
42
{
42
- $ this ->mockCountBuilder (1 );
43
+ $ this ->mockCountBuilder (true );
43
44
44
45
$ this ->assertDatabaseHas ($ this ->table , $ this ->data );
45
46
}
46
47
47
48
public function testAssertDatabaseHasSupportsModelClass ()
48
49
{
49
- $ this ->mockCountBuilder (1 );
50
+ $ this ->mockCountBuilder (true );
50
51
51
52
$ this ->assertDatabaseHas (ProductStub::class, $ this ->data );
52
53
}
@@ -60,7 +61,7 @@ public function testAssertDatabaseHasConstrainsToModel()
60
61
...$ this ->data ,
61
62
];
62
63
63
- $ this ->mockCountBuilder (1 );
64
+ $ this ->mockCountBuilder (true );
64
65
65
66
$ this ->assertDatabaseHas (new ProductStub (['id ' => 1 ]), $ data );
66
67
}
@@ -70,7 +71,7 @@ public function testSeeInDatabaseDoesNotFindResults()
70
71
$ this ->expectException (ExpectationFailedException::class);
71
72
$ this ->expectExceptionMessage ('The table is empty. ' );
72
73
73
- $ builder = $ this ->mockCountBuilder (0 );
74
+ $ builder = $ this ->mockCountBuilder (false );
74
75
75
76
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
76
77
@@ -83,7 +84,7 @@ public function testSeeInDatabaseFindsNotMatchingResults()
83
84
84
85
$ this ->expectExceptionMessage ('Found similar results: ' .json_encode ([['title ' => 'Forge ' ]], JSON_PRETTY_PRINT ));
85
86
86
- $ builder = $ this ->mockCountBuilder (0 );
87
+ $ builder = $ this ->mockCountBuilder (false );
87
88
88
89
$ builder ->shouldReceive ('take ' )->andReturnSelf ();
89
90
$ builder ->shouldReceive ('get ' )->andReturn (collect ([['title ' => 'Forge ' ]]));
@@ -97,8 +98,7 @@ public function testSeeInDatabaseFindsManyNotMatchingResults()
97
98
98
99
$ this ->expectExceptionMessage ('Found similar results: ' .json_encode (['data ' , 'data ' , 'data ' ], JSON_PRETTY_PRINT ).' and 2 others. ' );
99
100
100
- $ builder = $ this ->mockCountBuilder (0 );
101
- $ builder ->shouldReceive ('count ' )->andReturn (0 , 5 );
101
+ $ builder = $ this ->mockCountBuilder (false , countResult: [5 , 5 ]);
102
102
103
103
$ builder ->shouldReceive ('take ' )->andReturnSelf ();
104
104
$ builder ->shouldReceive ('get ' )->andReturn (
@@ -110,14 +110,14 @@ public function testSeeInDatabaseFindsManyNotMatchingResults()
110
110
111
111
public function testDontSeeInDatabaseDoesNotFindResults ()
112
112
{
113
- $ this ->mockCountBuilder (0 );
113
+ $ this ->mockCountBuilder (false );
114
114
115
115
$ this ->assertDatabaseMissing ($ this ->table , $ this ->data );
116
116
}
117
117
118
118
public function testAssertDatabaseMissingSupportsModelClass ()
119
119
{
120
- $ this ->mockCountBuilder (0 );
120
+ $ this ->mockCountBuilder (false );
121
121
122
122
$ this ->assertDatabaseMissing (ProductStub::class, $ this ->data );
123
123
}
@@ -131,7 +131,7 @@ public function testAssertDatabaseMissingConstrainsToModel()
131
131
...$ this ->data ,
132
132
];
133
133
134
- $ this ->mockCountBuilder (0 );
134
+ $ this ->mockCountBuilder (false );
135
135
136
136
$ this ->assertDatabaseMissing (new ProductStub (['id ' => 1 ]), $ data );
137
137
}
@@ -140,7 +140,7 @@ public function testDontSeeInDatabaseFindsResults()
140
140
{
141
141
$ this ->expectException (ExpectationFailedException::class);
142
142
143
- $ builder = $ this ->mockCountBuilder (1 );
143
+ $ builder = $ this ->mockCountBuilder (true );
144
144
145
145
$ builder ->shouldReceive ('take ' )->andReturnSelf ();
146
146
$ builder ->shouldReceive ('get ' )->andReturn (collect ([$ this ->data ]));
@@ -150,22 +150,22 @@ public function testDontSeeInDatabaseFindsResults()
150
150
151
151
public function testAssertTableEntriesCount ()
152
152
{
153
- $ this ->mockCountBuilder (1 );
153
+ $ this ->mockCountBuilder (true );
154
154
155
155
$ this ->assertDatabaseCount ($ this ->table , 1 );
156
156
}
157
157
158
158
public function testAssertDatabaseCountSupportModels ()
159
159
{
160
- $ this ->mockCountBuilder (1 );
160
+ $ this ->mockCountBuilder (true );
161
161
162
162
$ this ->assertDatabaseCount (ProductStub::class, 1 );
163
163
$ this ->assertDatabaseCount (new ProductStub , 1 );
164
164
}
165
165
166
166
public function testAssertDatabaseEmpty ()
167
167
{
168
- $ this ->mockCountBuilder (0 );
168
+ $ this ->mockCountBuilder (false );
169
169
170
170
$ this ->assertDatabaseEmpty (ProductStub::class);
171
171
$ this ->assertDatabaseEmpty (new ProductStub );
@@ -175,14 +175,14 @@ public function testAssertTableEntriesCountWrong()
175
175
{
176
176
$ this ->expectException (ExpectationFailedException::class);
177
177
$ this ->expectExceptionMessage ('Failed asserting that table [products] matches expected entries count of 3. Entries found: 1. ' );
178
- $ this ->mockCountBuilder (1 );
178
+ $ this ->mockCountBuilder (true );
179
179
180
180
$ this ->assertDatabaseCount ($ this ->table , 3 );
181
181
}
182
182
183
183
public function testAssertDatabaseMissingPassesWhenDoesNotFindResults ()
184
184
{
185
- $ this ->mockCountBuilder (0 );
185
+ $ this ->mockCountBuilder (false );
186
186
187
187
$ this ->assertDatabaseMissing ($ this ->table , $ this ->data );
188
188
}
@@ -191,7 +191,7 @@ public function testAssertDatabaseMissingFailsWhenFindsResults()
191
191
{
192
192
$ this ->expectException (ExpectationFailedException::class);
193
193
194
- $ builder = $ this ->mockCountBuilder (1 );
194
+ $ builder = $ this ->mockCountBuilder (true );
195
195
196
196
$ builder ->shouldReceive ('get ' )->andReturn (collect ([$ this ->data ]));
197
197
@@ -202,7 +202,7 @@ public function testAssertModelMissingPassesWhenDoesNotFindModelResults()
202
202
{
203
203
$ this ->data = ['id ' => 1 ];
204
204
205
- $ builder = $ this ->mockCountBuilder (0 );
205
+ $ builder = $ this ->mockCountBuilder (false );
206
206
207
207
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
208
208
@@ -211,14 +211,14 @@ public function testAssertModelMissingPassesWhenDoesNotFindModelResults()
211
211
212
212
public function testAssertSoftDeletedInDatabaseFindsResults ()
213
213
{
214
- $ this ->mockCountBuilder (1 );
214
+ $ this ->mockCountBuilder (true );
215
215
216
216
$ this ->assertSoftDeleted ($ this ->table , $ this ->data );
217
217
}
218
218
219
219
public function testAssertSoftDeletedSupportModelStrings ()
220
220
{
221
- $ this ->mockCountBuilder (1 );
221
+ $ this ->mockCountBuilder (true );
222
222
223
223
$ this ->assertSoftDeleted (ProductStub::class, $ this ->data );
224
224
}
@@ -228,7 +228,7 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindResults()
228
228
$ this ->expectException (ExpectationFailedException::class);
229
229
$ this ->expectExceptionMessage ('The table is empty. ' );
230
230
231
- $ builder = $ this ->mockCountBuilder (0 );
231
+ $ builder = $ this ->mockCountBuilder (false );
232
232
233
233
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
234
234
@@ -242,7 +242,7 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelResults()
242
242
243
243
$ this ->data = ['id ' => 1 ];
244
244
245
- $ builder = $ this ->mockCountBuilder (0 );
245
+ $ builder = $ this ->mockCountBuilder (false );
246
246
247
247
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
248
248
@@ -257,7 +257,7 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnR
257
257
$ model = new CustomProductStub (['id ' => 1 , 'name ' => 'Laravel ' ]);
258
258
$ this ->data = ['id ' => 1 , 'name ' => 'Tailwind ' ];
259
259
260
- $ builder = $ this ->mockCountBuilder (0 , 'trashed_at ' );
260
+ $ builder = $ this ->mockCountBuilder (false , 'trashed_at ' );
261
261
262
262
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
263
263
@@ -272,7 +272,7 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModePassedViaFcnWithCu
272
272
$ model = new CustomProductStub (['id ' => 1 , 'name ' => 'Laravel ' ]);
273
273
$ this ->data = ['id ' => 1 ];
274
274
275
- $ builder = $ this ->mockCountBuilder (0 , 'trashed_at ' );
275
+ $ builder = $ this ->mockCountBuilder (false , 'trashed_at ' );
276
276
277
277
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
278
278
@@ -281,14 +281,14 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModePassedViaFcnWithCu
281
281
282
282
public function testAssertNotSoftDeletedInDatabaseFindsResults ()
283
283
{
284
- $ this ->mockCountBuilder (1 );
284
+ $ this ->mockCountBuilder (true );
285
285
286
286
$ this ->assertNotSoftDeleted ($ this ->table , $ this ->data );
287
287
}
288
288
289
289
public function testAssertNotSoftDeletedSupportModelStrings ()
290
290
{
291
- $ this ->mockCountBuilder (1 );
291
+ $ this ->mockCountBuilder (true );
292
292
293
293
$ this ->assertNotSoftDeleted (ProductStub::class, $ this ->data );
294
294
}
@@ -298,7 +298,7 @@ public function testAssertNotSoftDeletedOnlyFindsMatchingModels()
298
298
$ this ->expectException (ExpectationFailedException::class);
299
299
$ this ->expectExceptionMessage ('Failed asserting that any existing row ' );
300
300
301
- $ builder = $ this ->mockCountBuilder (0 );
301
+ $ builder = $ this ->mockCountBuilder (false );
302
302
303
303
$ builder ->shouldReceive ('get ' )->andReturn (collect (), collect (1 ));
304
304
@@ -310,7 +310,7 @@ public function testAssertNotSoftDeletedInDatabaseDoesNotFindResults()
310
310
$ this ->expectException (ExpectationFailedException::class);
311
311
$ this ->expectExceptionMessage ('The table is empty. ' );
312
312
313
- $ builder = $ this ->mockCountBuilder (0 );
313
+ $ builder = $ this ->mockCountBuilder (false );
314
314
315
315
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
316
316
@@ -324,7 +324,7 @@ public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelResults()
324
324
325
325
$ this ->data = ['id ' => 1 ];
326
326
327
- $ builder = $ this ->mockCountBuilder (0 );
327
+ $ builder = $ this ->mockCountBuilder (false );
328
328
329
329
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
330
330
@@ -339,7 +339,7 @@ public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelWithCustomColu
339
339
$ model = new CustomProductStub (['id ' => 1 , 'name ' => 'Laravel ' ]);
340
340
$ this ->data = ['id ' => 1 , 'name ' => 'Tailwind ' ];
341
341
342
- $ builder = $ this ->mockCountBuilder (0 , 'trashed_at ' );
342
+ $ builder = $ this ->mockCountBuilder (false , 'trashed_at ' );
343
343
344
344
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
345
345
@@ -354,7 +354,7 @@ public function testAssertNotSoftDeletedInDatabaseDoesNotFindModelPassedViaFcnWi
354
354
$ model = new CustomProductStub (['id ' => 1 , 'name ' => 'Laravel ' ]);
355
355
$ this ->data = ['id ' => 1 ];
356
356
357
- $ builder = $ this ->mockCountBuilder (0 , 'trashed_at ' );
357
+ $ builder = $ this ->mockCountBuilder (false , 'trashed_at ' );
358
358
359
359
$ builder ->shouldReceive ('get ' )->andReturn (collect ());
360
360
@@ -365,7 +365,7 @@ public function testAssertExistsPassesWhenFindsResults()
365
365
{
366
366
$ this ->data = ['id ' => 1 ];
367
367
368
- $ builder = $ this ->mockCountBuilder (1 );
368
+ $ builder = $ this ->mockCountBuilder (true );
369
369
370
370
$ builder ->shouldReceive ('get ' )->andReturn (collect ($ this ->data ));
371
371
@@ -482,10 +482,13 @@ public function testExpectsDatabaseQueryCount()
482
482
$ case ->tearDown ();
483
483
}
484
484
485
- protected function mockCountBuilder ($ countResult , $ deletedAtColumn = 'deleted_at ' )
485
+ protected function mockCountBuilder ($ existsResult , $ deletedAtColumn = 'deleted_at ' , $ countResult = null )
486
486
{
487
487
$ builder = m::mock (Builder::class);
488
488
489
+ $ countResult = Arr::wrap ($ countResult );
490
+ $ countResult = ! empty ($ countResult ) ? $ countResult : [$ existsResult ? 1 : 0 ];
491
+
489
492
$ key = array_key_first ($ this ->data );
490
493
$ value = $ this ->data [$ key ];
491
494
@@ -501,7 +504,9 @@ protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at
501
504
502
505
$ builder ->shouldReceive ('whereNull ' )->with ($ deletedAtColumn )->andReturnSelf ();
503
506
504
- $ builder ->shouldReceive ('count ' )->andReturn ($ countResult )->byDefault ();
507
+ $ builder ->shouldReceive ('exists ' )->andReturn ($ existsResult )->byDefault ();
508
+
509
+ $ builder ->shouldReceive ('count ' )->andReturn (...$ countResult )->byDefault ();
505
510
506
511
$ this ->connection ->shouldReceive ('table ' )
507
512
->with ($ this ->table )
0 commit comments