Skip to content

Refactoring tests #1385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testCollection()
// public function testDynamic()
// {
// $dbs = DB::connection('mongodb')->listCollections();
// $this->assertTrue(is_array($dbs));
// $this->assertInternalType('array', $dbs);
// }

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

// $hosts = $mongoclient->getHosts();
// $this->assertEquals(1, count($hosts));
// $this->assertCount(1, $hosts);
// }

public function testQueryLog()
{
DB::enableQueryLog();

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

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

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

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

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

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

public function testSchemaBuilder()
Expand Down
26 changes: 13 additions & 13 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testEmbedsManySave()
$this->assertInstanceOf('DateTime', $address->created_at);
$this->assertInstanceOf('DateTime', $address->updated_at);
$this->assertNotNull($address->_id);
$this->assertTrue(is_string($address->_id));
$this->assertInternalType('string', $address->_id);

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

$this->assertEquals(2, count($user->addresses));
$this->assertEquals(2, count($user->addresses()->get()));
$this->assertCount(2, $user->addresses);
$this->assertCount(2, $user->addresses()->get());
$this->assertEquals(2, $user->addresses->count());
$this->assertEquals(2, $user->addresses()->count());
$this->assertEquals(['London', 'New York'], $user->addresses->pluck('city')->all());
Expand Down Expand Up @@ -115,8 +115,8 @@ public function testEmbedsToArray()
$user->addresses()->saveMany([new Address(['city' => 'London']), new Address(['city' => 'Bristol'])]);

$array = $user->toArray();
$this->assertFalse(array_key_exists('_addresses', $array));
$this->assertTrue(array_key_exists('addresses', $array));
$this->assertArrayNotHasKey('_addresses', $array);
$this->assertArrayHasKey('addresses', $array);
}

public function testEmbedsManyAssociate()
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testEmbedsManyCreate()
$user = User::create([]);
$address = $user->addresses()->create(['city' => 'Bruxelles']);
$this->assertInstanceOf('Address', $address);
$this->assertTrue(is_string($address->_id));
$this->assertInternalType('string', $address->_id);
$this->assertEquals(['Bruxelles'], $user->addresses->pluck('city')->all());

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

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

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

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

$user = User::with('addresses')->get()->first();
$relations = $user->getRelations();
$this->assertTrue(array_key_exists('addresses', $relations));
$this->assertArrayHasKey('addresses', $relations);
$this->assertEquals(2, $relations['addresses']->count());
$this->assertArrayHasKey('addresses', $user->toArray());
$this->assertTrue(is_array($user->toArray()['addresses']));
$this->assertInternalType('array', $user->toArray()['addresses']);
}

public function testEmbedsManyDeleteAll()
Expand Down Expand Up @@ -466,7 +466,7 @@ public function testEmbedsOne()
$this->assertInstanceOf('DateTime', $father->created_at);
$this->assertInstanceOf('DateTime', $father->updated_at);
$this->assertNotNull($father->_id);
$this->assertTrue(is_string($father->_id));
$this->assertInternalType('string', $father->_id);

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

$array = $user->toArray();
$this->assertArrayHasKey('addresses', $array);
$this->assertTrue(is_array($array['addresses']));
$this->assertInternalType('array', $array['addresses']);
}

public function testEmbeddedSave()
Expand Down
14 changes: 7 additions & 7 deletions tests/HybridRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public function testMysqlRelations()
// Mysql User
$user->name = "John Doe";
$user->save();
$this->assertTrue(is_int($user->id));
$this->assertInternalType('int', $user->id);

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

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

// SQL belongs to
$book = $user->mysqlBooks()->first(); // refetch
Expand Down Expand Up @@ -93,8 +93,8 @@ public function testHybridWhereHas()
$otherUser->id = 3;
$otherUser->save();
// Make sure they are created
$this->assertTrue(is_int($user->id));
$this->assertTrue(is_int($otherUser->id));
$this->assertInternalType('int', $user->id);
$this->assertInternalType('int', $otherUser->id);
// Clear to start
$user->books()->truncate();
$otherUser->books()->truncate();
Expand Down Expand Up @@ -147,8 +147,8 @@ public function testHybridWith()
$otherUser->id = 3;
$otherUser->save();
// Make sure they are created
$this->assertTrue(is_int($user->id));
$this->assertTrue(is_int($otherUser->id));
$this->assertInternalType('int', $user->id);
$this->assertInternalType('int', $otherUser->id);
// Clear to start
Book::truncate();
MysqlBook::truncate();
Expand Down
52 changes: 26 additions & 26 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testNewModel()
$user = new User;
$this->assertInstanceOf(Model::class, $user);
$this->assertInstanceOf('Jenssegers\Mongodb\Connection', $user->getConnection());
$this->assertEquals(false, $user->exists);
$this->assertFalse($user->exists);
$this->assertEquals('users', $user->getTable());
$this->assertEquals('_id', $user->getKeyName());
}
Expand All @@ -35,11 +35,11 @@ public function testInsert()

$user->save();

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

$this->assertTrue(isset($user->_id));
$this->assertTrue(is_string($user->_id));
$this->assertInternalType('string', $user->_id);
$this->assertNotEquals('', (string) $user->_id);
$this->assertNotEquals(0, strlen((string) $user->_id));
$this->assertInstanceOf(Carbon::class, $user->created_at);
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testUpdate()
$check->age = 36;
$check->save();

$this->assertEquals(true, $check->exists);
$this->assertTrue($check->exists);
$this->assertInstanceOf(Carbon::class, $check->created_at);
$this->assertInstanceOf(Carbon::class, $check->updated_at);
$this->assertEquals(1, User::count());
Expand All @@ -93,7 +93,7 @@ public function testManualStringId()
$user->age = 35;
$user->save();

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

$raw = $user->getAttributes();
Expand All @@ -106,7 +106,7 @@ public function testManualStringId()
$user->age = 35;
$user->save();

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

$raw = $user->getAttributes();
Expand All @@ -122,7 +122,7 @@ public function testManualIntId()
$user->age = 35;
$user->save();

$this->assertEquals(true, $user->exists);
$this->assertTrue($user->exists);
$this->assertEquals(1, $user->_id);

$raw = $user->getAttributes();
Expand All @@ -137,7 +137,7 @@ public function testDelete()
$user->age = 35;
$user->save();

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

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

$all = User::all();

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

$this->assertInstanceOf(Model::class, $check);
$this->assertEquals(true, $check->exists);
$this->assertTrue($check->exists);
$this->assertEquals($user->_id, $check->_id);

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

$users = User::get();
$this->assertEquals(2, count($users));
$this->assertCount(2, $users);
$this->assertInstanceOf(Collection::class, $users);
$this->assertInstanceOf(Model::class, $users[0]);
}
Expand All @@ -216,10 +216,10 @@ public function testNoDocument()
$this->assertEquals(0, $items->count());

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

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

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

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

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

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

$user->delete();
$this->assertEquals(true, $user->trashed());
$this->assertTrue($user->trashed());
$this->assertNotNull($user->deleted_at);

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

$user->restore();
$this->assertEquals(2, Soft::count());
Expand Down Expand Up @@ -340,9 +340,9 @@ public function testToArray()
$keys = array_keys($array);
sort($keys);
$this->assertEquals(['_id', 'created_at', 'name', 'type', 'updated_at'], $keys);
$this->assertTrue(is_string($array['created_at']));
$this->assertTrue(is_string($array['updated_at']));
$this->assertTrue(is_string($array['_id']));
$this->assertInternalType('string', $array['created_at']);
$this->assertInternalType('string', $array['updated_at']);
$this->assertInternalType('string', $array['_id']);
}

public function testUnset()
Expand All @@ -352,7 +352,7 @@ public function testUnset()

$user1->unset('note1');

$this->assertFalse(isset($user1->note1));
$this->assertObjectNotHasAttribute('note1', $user1);
$this->assertTrue(isset($user1->note2));
$this->assertTrue(isset($user2->note1));
$this->assertTrue(isset($user2->note2));
Expand All @@ -361,15 +361,15 @@ public function testUnset()
$user1 = User::find($user1->_id);
$user2 = User::find($user2->_id);

$this->assertFalse(isset($user1->note1));
$this->assertObjectNotHasAttribute('note1', $user1);
$this->assertTrue(isset($user1->note2));
$this->assertTrue(isset($user2->note1));
$this->assertTrue(isset($user2->note2));

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

$this->assertFalse(isset($user2->note1));
$this->assertFalse(isset($user2->note2));
$this->assertObjectNotHasAttribute('note1', $user2);
$this->assertObjectNotHasAttribute('note2', $user2);
}

public function testDates()
Expand All @@ -396,7 +396,7 @@ public function testDates()
$this->assertEquals($item->getOriginal('created_at')
->toDateTime()
->getTimestamp(), $item->created_at->getTimestamp());
$this->assertTrue(abs(time() - $item->created_at->getTimestamp()) < 2);
$this->assertLessThan(2, abs(time() - $item->created_at->getTimestamp()));

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