Skip to content

Commit 8895014

Browse files
authored
Merge pull request #1385 from carusogabriel/refactoring-tests
Refactoring tests
2 parents a1c69ee + 13ba9a3 commit 8895014

File tree

7 files changed

+181
-181
lines changed

7 files changed

+181
-181
lines changed

tests/ConnectionTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testCollection()
4444
// public function testDynamic()
4545
// {
4646
// $dbs = DB::connection('mongodb')->listCollections();
47-
// $this->assertTrue(is_array($dbs));
47+
// $this->assertInternalType('array', $dbs);
4848
// }
4949

5050
// public function testMultipleConnections()
@@ -59,29 +59,29 @@ public function testCollection()
5959
// $mongoclient = $connection->getMongoClient();
6060

6161
// $hosts = $mongoclient->getHosts();
62-
// $this->assertEquals(1, count($hosts));
62+
// $this->assertCount(1, $hosts);
6363
// }
6464

6565
public function testQueryLog()
6666
{
6767
DB::enableQueryLog();
6868

69-
$this->assertEquals(0, count(DB::getQueryLog()));
69+
$this->assertCount(0, DB::getQueryLog());
7070

7171
DB::collection('items')->get();
72-
$this->assertEquals(1, count(DB::getQueryLog()));
72+
$this->assertCount(1, DB::getQueryLog());
7373

7474
DB::collection('items')->insert(['name' => 'test']);
75-
$this->assertEquals(2, count(DB::getQueryLog()));
75+
$this->assertCount(2, DB::getQueryLog());
7676

7777
DB::collection('items')->count();
78-
$this->assertEquals(3, count(DB::getQueryLog()));
78+
$this->assertCount(3, DB::getQueryLog());
7979

8080
DB::collection('items')->where('name', 'test')->update(['name' => 'test']);
81-
$this->assertEquals(4, count(DB::getQueryLog()));
81+
$this->assertCount(4, DB::getQueryLog());
8282

8383
DB::collection('items')->where('name', 'test')->delete();
84-
$this->assertEquals(5, count(DB::getQueryLog()));
84+
$this->assertCount(5, DB::getQueryLog());
8585
}
8686

8787
public function testSchemaBuilder()

tests/EmbeddedRelationsTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testEmbedsManySave()
3636
$this->assertInstanceOf('DateTime', $address->created_at);
3737
$this->assertInstanceOf('DateTime', $address->updated_at);
3838
$this->assertNotNull($address->_id);
39-
$this->assertTrue(is_string($address->_id));
39+
$this->assertInternalType('string', $address->_id);
4040

4141
$raw = $address->getAttributes();
4242
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
@@ -57,8 +57,8 @@ public function testEmbedsManySave()
5757
$user->addresses()->save($address);
5858
$address->unsetEventDispatcher();
5959

60-
$this->assertEquals(2, count($user->addresses));
61-
$this->assertEquals(2, count($user->addresses()->get()));
60+
$this->assertCount(2, $user->addresses);
61+
$this->assertCount(2, $user->addresses()->get());
6262
$this->assertEquals(2, $user->addresses->count());
6363
$this->assertEquals(2, $user->addresses()->count());
6464
$this->assertEquals(['London', 'New York'], $user->addresses->pluck('city')->all());
@@ -115,8 +115,8 @@ public function testEmbedsToArray()
115115
$user->addresses()->saveMany([new Address(['city' => 'London']), new Address(['city' => 'Bristol'])]);
116116

117117
$array = $user->toArray();
118-
$this->assertFalse(array_key_exists('_addresses', $array));
119-
$this->assertTrue(array_key_exists('addresses', $array));
118+
$this->assertArrayNotHasKey('_addresses', $array);
119+
$this->assertArrayHasKey('addresses', $array);
120120
}
121121

122122
public function testEmbedsManyAssociate()
@@ -176,7 +176,7 @@ public function testEmbedsManyCreate()
176176
$user = User::create([]);
177177
$address = $user->addresses()->create(['city' => 'Bruxelles']);
178178
$this->assertInstanceOf('Address', $address);
179-
$this->assertTrue(is_string($address->_id));
179+
$this->assertInternalType('string', $address->_id);
180180
$this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all());
181181

182182
$raw = $address->getAttributes();
@@ -187,7 +187,7 @@ public function testEmbedsManyCreate()
187187

188188
$user = User::create([]);
189189
$address = $user->addresses()->create(['_id' => '', 'city' => 'Bruxelles']);
190-
$this->assertTrue(is_string($address->_id));
190+
$this->assertInternalType('string', $address->_id);
191191

192192
$raw = $address->getAttributes();
193193
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
@@ -385,16 +385,16 @@ public function testEmbedsManyEagerLoading()
385385

386386
$user = User::find($user1->id);
387387
$relations = $user->getRelations();
388-
$this->assertFalse(array_key_exists('addresses', $relations));
388+
$this->assertArrayNotHasKey('addresses', $relations);
389389
$this->assertArrayHasKey('addresses', $user->toArray());
390-
$this->assertTrue(is_array($user->toArray()['addresses']));
390+
$this->assertInternalType('array', $user->toArray()['addresses']);
391391

392392
$user = User::with('addresses')->get()->first();
393393
$relations = $user->getRelations();
394-
$this->assertTrue(array_key_exists('addresses', $relations));
394+
$this->assertArrayHasKey('addresses', $relations);
395395
$this->assertEquals(2, $relations['addresses']->count());
396396
$this->assertArrayHasKey('addresses', $user->toArray());
397-
$this->assertTrue(is_array($user->toArray()['addresses']));
397+
$this->assertInternalType('array', $user->toArray()['addresses']);
398398
}
399399

400400
public function testEmbedsManyDeleteAll()
@@ -466,7 +466,7 @@ public function testEmbedsOne()
466466
$this->assertInstanceOf('DateTime', $father->created_at);
467467
$this->assertInstanceOf('DateTime', $father->updated_at);
468468
$this->assertNotNull($father->_id);
469-
$this->assertTrue(is_string($father->_id));
469+
$this->assertInternalType('string', $father->_id);
470470

471471
$raw = $father->getAttributes();
472472
$this->assertInstanceOf('MongoDB\BSON\ObjectID', $raw['_id']);
@@ -541,7 +541,7 @@ public function testEmbedsManyToArray()
541541

542542
$array = $user->toArray();
543543
$this->assertArrayHasKey('addresses', $array);
544-
$this->assertTrue(is_array($array['addresses']));
544+
$this->assertInternalType('array', $array['addresses']);
545545
}
546546

547547
public function testEmbeddedSave()

tests/HybridRelationsTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public function testMysqlRelations()
2727
// Mysql User
2828
$user->name = "John Doe";
2929
$user->save();
30-
$this->assertTrue(is_int($user->id));
30+
$this->assertInternalType('int', $user->id);
3131

3232
// SQL has many
3333
$book = new Book(['title' => 'Game of Thrones']);
3434
$user->books()->save($book);
3535
$user = MysqlUser::find($user->id); // refetch
36-
$this->assertEquals(1, count($user->books));
36+
$this->assertCount(1, $user->books);
3737

3838
// MongoDB belongs to
3939
$book = $user->books()->first(); // refetch
@@ -58,7 +58,7 @@ public function testMysqlRelations()
5858
$book = new MysqlBook(['title' => 'Game of Thrones']);
5959
$user->mysqlBooks()->save($book);
6060
$user = User::find($user->_id); // refetch
61-
$this->assertEquals(1, count($user->mysqlBooks));
61+
$this->assertCount(1, $user->mysqlBooks);
6262

6363
// SQL belongs to
6464
$book = $user->mysqlBooks()->first(); // refetch
@@ -93,8 +93,8 @@ public function testHybridWhereHas()
9393
$otherUser->id = 3;
9494
$otherUser->save();
9595
// Make sure they are created
96-
$this->assertTrue(is_int($user->id));
97-
$this->assertTrue(is_int($otherUser->id));
96+
$this->assertInternalType('int', $user->id);
97+
$this->assertInternalType('int', $otherUser->id);
9898
// Clear to start
9999
$user->books()->truncate();
100100
$otherUser->books()->truncate();
@@ -147,8 +147,8 @@ public function testHybridWith()
147147
$otherUser->id = 3;
148148
$otherUser->save();
149149
// Make sure they are created
150-
$this->assertTrue(is_int($user->id));
151-
$this->assertTrue(is_int($otherUser->id));
150+
$this->assertInternalType('int', $user->id);
151+
$this->assertInternalType('int', $otherUser->id);
152152
// Clear to start
153153
Book::truncate();
154154
MysqlBook::truncate();

tests/ModelTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testNewModel()
2121
$user = new User;
2222
$this->assertInstanceOf(Model::class, $user);
2323
$this->assertInstanceOf('Jenssegers\Mongodb\Connection', $user->getConnection());
24-
$this->assertEquals(false, $user->exists);
24+
$this->assertFalse($user->exists);
2525
$this->assertEquals('users', $user->getTable());
2626
$this->assertEquals('_id', $user->getKeyName());
2727
}
@@ -35,11 +35,11 @@ public function testInsert()
3535

3636
$user->save();
3737

38-
$this->assertEquals(true, $user->exists);
38+
$this->assertTrue($user->exists);
3939
$this->assertEquals(1, User::count());
4040

4141
$this->assertTrue(isset($user->_id));
42-
$this->assertTrue(is_string($user->_id));
42+
$this->assertInternalType('string', $user->_id);
4343
$this->assertNotEquals('', (string) $user->_id);
4444
$this->assertNotEquals(0, strlen((string) $user->_id));
4545
$this->assertInstanceOf(Carbon::class, $user->created_at);
@@ -67,7 +67,7 @@ public function testUpdate()
6767
$check->age = 36;
6868
$check->save();
6969

70-
$this->assertEquals(true, $check->exists);
70+
$this->assertTrue($check->exists);
7171
$this->assertInstanceOf(Carbon::class, $check->created_at);
7272
$this->assertInstanceOf(Carbon::class, $check->updated_at);
7373
$this->assertEquals(1, User::count());
@@ -93,7 +93,7 @@ public function testManualStringId()
9393
$user->age = 35;
9494
$user->save();
9595

96-
$this->assertEquals(true, $user->exists);
96+
$this->assertTrue($user->exists);
9797
$this->assertEquals('4af9f23d8ead0e1d32000000', $user->_id);
9898

9999
$raw = $user->getAttributes();
@@ -106,7 +106,7 @@ public function testManualStringId()
106106
$user->age = 35;
107107
$user->save();
108108

109-
$this->assertEquals(true, $user->exists);
109+
$this->assertTrue($user->exists);
110110
$this->assertEquals('customId', $user->_id);
111111

112112
$raw = $user->getAttributes();
@@ -122,7 +122,7 @@ public function testManualIntId()
122122
$user->age = 35;
123123
$user->save();
124124

125-
$this->assertEquals(true, $user->exists);
125+
$this->assertTrue($user->exists);
126126
$this->assertEquals(1, $user->_id);
127127

128128
$raw = $user->getAttributes();
@@ -137,7 +137,7 @@ public function testDelete()
137137
$user->age = 35;
138138
$user->save();
139139

140-
$this->assertEquals(true, $user->exists);
140+
$this->assertTrue($user->exists);
141141
$this->assertEquals(1, User::count());
142142

143143
$user->delete();
@@ -161,7 +161,7 @@ public function testAll()
161161

162162
$all = User::all();
163163

164-
$this->assertEquals(2, count($all));
164+
$this->assertCount(2, $all);
165165
$this->assertContains('John Doe', $all->pluck('name'));
166166
$this->assertContains('Jane Doe', $all->pluck('name'));
167167
}
@@ -177,7 +177,7 @@ public function testFind()
177177
$check = User::find($user->_id);
178178

179179
$this->assertInstanceOf(Model::class, $check);
180-
$this->assertEquals(true, $check->exists);
180+
$this->assertTrue($check->exists);
181181
$this->assertEquals($user->_id, $check->_id);
182182

183183
$this->assertEquals('John Doe', $check->name);
@@ -192,7 +192,7 @@ public function testGet()
192192
]);
193193

194194
$users = User::get();
195-
$this->assertEquals(2, count($users));
195+
$this->assertCount(2, $users);
196196
$this->assertInstanceOf(Collection::class, $users);
197197
$this->assertInstanceOf(Model::class, $users[0]);
198198
}
@@ -216,10 +216,10 @@ public function testNoDocument()
216216
$this->assertEquals(0, $items->count());
217217

218218
$item = Item::where('name', 'nothing')->first();
219-
$this->assertEquals(null, $item);
219+
$this->assertNull($item);
220220

221221
$item = Item::find('51c33d8981fec6813e00000a');
222-
$this->assertEquals(null, $item);
222+
$this->assertNull($item);
223223
}
224224

225225
public function testFindOrfail()
@@ -233,7 +233,7 @@ public function testCreate()
233233
$user = User::create(['name' => 'Jane Poe']);
234234

235235
$this->assertInstanceOf(Model::class, $user);
236-
$this->assertEquals(true, $user->exists);
236+
$this->assertTrue($user->exists);
237237
$this->assertEquals('Jane Poe', $user->name);
238238

239239
$check = User::where('name', 'Jane Poe')->first();
@@ -278,12 +278,12 @@ public function testSoftDelete()
278278
$this->assertEquals(2, Soft::count());
279279

280280
$user = Soft::where('name', 'John Doe')->first();
281-
$this->assertEquals(true, $user->exists);
282-
$this->assertEquals(false, $user->trashed());
281+
$this->assertTrue($user->exists);
282+
$this->assertFalse($user->trashed());
283283
$this->assertNull($user->deleted_at);
284284

285285
$user->delete();
286-
$this->assertEquals(true, $user->trashed());
286+
$this->assertTrue($user->trashed());
287287
$this->assertNotNull($user->deleted_at);
288288

289289
$user = Soft::where('name', 'John Doe')->first();
@@ -295,7 +295,7 @@ public function testSoftDelete()
295295
$user = Soft::withTrashed()->where('name', 'John Doe')->first();
296296
$this->assertNotNull($user);
297297
$this->assertInstanceOf(Carbon::class, $user->deleted_at);
298-
$this->assertEquals(true, $user->trashed());
298+
$this->assertTrue($user->trashed());
299299

300300
$user->restore();
301301
$this->assertEquals(2, Soft::count());
@@ -340,9 +340,9 @@ public function testToArray()
340340
$keys = array_keys($array);
341341
sort($keys);
342342
$this->assertEquals(['_id', 'created_at', 'name', 'type', 'updated_at'], $keys);
343-
$this->assertTrue(is_string($array['created_at']));
344-
$this->assertTrue(is_string($array['updated_at']));
345-
$this->assertTrue(is_string($array['_id']));
343+
$this->assertInternalType('string', $array['created_at']);
344+
$this->assertInternalType('string', $array['updated_at']);
345+
$this->assertInternalType('string', $array['_id']);
346346
}
347347

348348
public function testUnset()
@@ -352,7 +352,7 @@ public function testUnset()
352352

353353
$user1->unset('note1');
354354

355-
$this->assertFalse(isset($user1->note1));
355+
$this->assertObjectNotHasAttribute('note1', $user1);
356356
$this->assertTrue(isset($user1->note2));
357357
$this->assertTrue(isset($user2->note1));
358358
$this->assertTrue(isset($user2->note2));
@@ -361,15 +361,15 @@ public function testUnset()
361361
$user1 = User::find($user1->_id);
362362
$user2 = User::find($user2->_id);
363363

364-
$this->assertFalse(isset($user1->note1));
364+
$this->assertObjectNotHasAttribute('note1', $user1);
365365
$this->assertTrue(isset($user1->note2));
366366
$this->assertTrue(isset($user2->note1));
367367
$this->assertTrue(isset($user2->note2));
368368

369369
$user2->unset(['note1', 'note2']);
370370

371-
$this->assertFalse(isset($user2->note1));
372-
$this->assertFalse(isset($user2->note2));
371+
$this->assertObjectNotHasAttribute('note1', $user2);
372+
$this->assertObjectNotHasAttribute('note2', $user2);
373373
}
374374

375375
public function testDates()
@@ -396,7 +396,7 @@ public function testDates()
396396
$this->assertEquals($item->getOriginal('created_at')
397397
->toDateTime()
398398
->getTimestamp(), $item->created_at->getTimestamp());
399-
$this->assertTrue(abs(time() - $item->created_at->getTimestamp()) < 2);
399+
$this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp()));
400400

401401
// test default date format for json output
402402
$item = Item::create(['name' => 'sword']);

0 commit comments

Comments
 (0)