From 52c9faeb154f529aa359dc8f5d81290759decb2d Mon Sep 17 00:00:00 2001 From: Ditty Date: Wed, 26 Feb 2020 11:02:45 +0300 Subject: [PATCH 1/4] Refactor tests as suggested by @Smolevich --- tests/AuthTest.php | 2 +- tests/ConnectionTest.php | 47 +++++++++++---------------------- tests/EmbeddedRelationsTest.php | 23 ---------------- tests/ModelTest.php | 24 +++++++++-------- tests/QueryBuilderTest.php | 1 - tests/QueueTest.php | 3 ++- tests/RelationsTest.php | 6 ++--- 7 files changed, 34 insertions(+), 72 deletions(-) diff --git a/tests/AuthTest.php b/tests/AuthTest.php index 226dd9c28..84212b84b 100644 --- a/tests/AuthTest.php +++ b/tests/AuthTest.php @@ -15,7 +15,7 @@ public function tearDown(): void public function testAuthAttempt() { - $user = User::create([ + User::create([ 'name' => 'John Doe', 'email' => 'john@doe.com', 'password' => Hash::make('foobar'), diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 9a77b5d81..3283c2846 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -2,13 +2,19 @@ declare(strict_types=1); use Illuminate\Support\Facades\DB; +use Jenssegers\Mongodb\Collection; +use Jenssegers\Mongodb\Connection; +use Jenssegers\Mongodb\Query\Builder; +use Jenssegers\Mongodb\Schema\Builder as SchemaBuilder; +use MongoDB\Client; +use MongoDB\Database; class ConnectionTest extends TestCase { public function testConnection() { $connection = DB::connection('mongodb'); - $this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $connection); + $this->assertInstanceOf(Connection::class, $connection); } public function testReconnect() @@ -26,54 +32,31 @@ public function testReconnect() public function testDb() { $connection = DB::connection('mongodb'); - $this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB()); + $this->assertInstanceOf(Database::class, $connection->getMongoDB()); $connection = DB::connection('mongodb'); - $this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient()); + $this->assertInstanceOf(Client::class, $connection->getMongoClient()); } public function testDsnDb() { $connection = DB::connection('dsn_mongodb_db'); - $this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB()); - - $connection = DB::connection('dsn_mongodb_db'); - $this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient()); + $this->assertInstanceOf(Database::class, $connection->getMongoDB()); + $this->assertInstanceOf(Client::class, $connection->getMongoClient()); } public function testCollection() { $collection = DB::connection('mongodb')->getCollection('unittest'); - $this->assertInstanceOf(Jenssegers\Mongodb\Collection::class, $collection); + $this->assertInstanceOf(Collection::class, $collection); $collection = DB::connection('mongodb')->collection('unittests'); - $this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection); + $this->assertInstanceOf(Builder::class, $collection); $collection = DB::connection('mongodb')->table('unittests'); - $this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection); + $this->assertInstanceOf(Builder::class, $collection); } - // public function testDynamic() - // { - // $dbs = DB::connection('mongodb')->listCollections(); - // $this->assertIsArray($dbs); - // } - - // public function testMultipleConnections() - // { - // global $app; - - // # Add fake host - // $db = $app['config']['database.connections']['mongodb']; - // $db['host'] = array($db['host'], '1.2.3.4'); - - // $connection = new Connection($db); - // $mongoclient = $connection->getMongoClient(); - - // $hosts = $mongoclient->getHosts(); - // $this->assertCount(1, $hosts); - // } - public function testQueryLog() { DB::enableQueryLog(); @@ -99,7 +82,7 @@ public function testQueryLog() public function testSchemaBuilder() { $schema = DB::connection('mongodb')->getSchemaBuilder(); - $this->assertInstanceOf(\Jenssegers\Mongodb\Schema\Builder::class, $schema); + $this->assertInstanceOf(SchemaBuilder::class, $schema); } public function testDriverName() diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index dbc1e6758..c91b8ac14 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -103,29 +103,6 @@ public function testEmbedsManySave() $this->assertEquals(['London', 'Manhattan', 'Bruxelles'], $freshUser->addresses->pluck('city')->all()); } - // public function testEmbedsManySaveModel() - // { - // $user = User::create(['name' => 'John Doe']); - // $address = new Address(['city' => 'London']); - - // $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); - // $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); - // $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true); - // $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($address), $address); - // $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); - - // $address->save(); - - // $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class)); - // $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true); - // $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true); - // $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($address), $address); - // $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address); - - // $address->city = 'Paris'; - // $address->save(); - // } - public function testEmbedsToArray() { $user = User::create(['name' => 'John Doe']); diff --git a/tests/ModelTest.php b/tests/ModelTest.php index e998cade6..869035192 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -2,8 +2,10 @@ declare(strict_types=1); use Carbon\Carbon; -use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Jenssegers\Mongodb\Collection; +use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Eloquent\Model; use MongoDB\BSON\ObjectID; use MongoDB\BSON\UTCDateTime; @@ -22,7 +24,7 @@ public function testNewModel(): void { $user = new User; $this->assertInstanceOf(Model::class, $user); - $this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $user->getConnection()); + $this->assertInstanceOf(Connection::class, $user->getConnection()); $this->assertFalse($user->exists); $this->assertEquals('users', $user->getTable()); $this->assertEquals('_id', $user->getKeyName()); @@ -196,7 +198,7 @@ public function testGet(): void $users = User::get(); $this->assertCount(2, $users); - $this->assertInstanceOf(Collection::class, $users); + $this->assertInstanceOf(EloquentCollection::class, $users); $this->assertInstanceOf(Model::class, $users[0]); } @@ -216,7 +218,7 @@ public function testFirst(): void public function testNoDocument(): void { $items = Item::where('name', 'nothing')->get(); - $this->assertInstanceOf(Collection::class, $items); + $this->assertInstanceOf(EloquentCollection::class, $items); $this->assertEquals(0, $items->count()); $item = Item::where('name', 'nothing')->first(); @@ -436,11 +438,11 @@ public function testDates(): void public function testCarbonDateMockingWorks() { - $fakeDate = \Carbon\Carbon::createFromDate(2000, 01, 01); + $fakeDate = Carbon::createFromDate(2000, 01, 01); Carbon::setTestNow($fakeDate); $item = Item::create(['name' => 'sword']); - + $this->assertLessThan(1, $fakeDate->diffInSeconds($item->created_at)); } @@ -487,24 +489,24 @@ public function testRaw(): void User::create(['name' => 'Jane Doe', 'age' => 35]); User::create(['name' => 'Harry Hoe', 'age' => 15]); - $users = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { + $users = User::raw(function (Collection $collection) { return $collection->find(['age' => 35]); }); $this->assertInstanceOf(Collection::class, $users); $this->assertInstanceOf(Model::class, $users[0]); - $user = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { + $user = User::raw(function (Collection $collection) { return $collection->findOne(['age' => 35]); }); $this->assertInstanceOf(Model::class, $user); - $count = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { + $count = User::raw(function (Collection $collection) { return $collection->count(); }); $this->assertEquals(3, $count); - $result = User::raw(function (\Jenssegers\Mongodb\Collection $collection) { + $result = User::raw(function (Collection $collection) { return $collection->insertOne(['name' => 'Yvonne Yoe', 'age' => 35]); }); $this->assertNotNull($result); @@ -566,7 +568,7 @@ public function testChunkById(): void User::create(['name' => 'spoon', 'tags' => ['round', 'bowl']]); $count = 0; - User::chunkById(2, function (\Illuminate\Database\Eloquent\Collection $items) use (&$count) { + User::chunkById(2, function (EloquentCollection $items) use (&$count) { $count += count($items); }); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 40b1febc2..6588be159 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -151,7 +151,6 @@ public function testUpdate() ]); DB::collection('users')->where('name', 'John Doe')->update(['age' => 100]); - $users = DB::collection('users')->get(); $john = DB::collection('users')->where('name', 'John Doe')->first(); $jane = DB::collection('users')->where('name', 'Jane Doe')->first(); diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 94af36f56..9f3ad8887 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -1,6 +1,7 @@ assertNotNull($id); // Expire the test job - $expiry = \Carbon\Carbon::now()->subSeconds(Config::get('queue.connections.database.expire'))->getTimestamp(); + $expiry = Carbon::now()->subSeconds(Config::get('queue.connections.database.expire'))->getTimestamp(); Queue::getDatabase() ->table(Config::get('queue.connections.database.table')) ->where('_id', $id) diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index a50329682..86065aac3 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -296,10 +296,10 @@ public function testBelongsToManyAttachArray(): void public function testBelongsToManyAttachEloquentCollection(): void { - $user = User::create(['name' => 'John Doe']); + User::create(['name' => 'John Doe']); $client1 = Client::create(['name' => 'Test 1']); $client2 = Client::create(['name' => 'Test 2']); - $collection = new \Illuminate\Database\Eloquent\Collection([$client1, $client2]); + $collection = new Collection([$client1, $client2]); $user = User::where('name', '=', 'John Doe')->first(); $user->clients()->attach($collection); @@ -467,7 +467,7 @@ public function testNestedKeys(): void ], ]); - $address = $client->addresses()->create([ + $client->addresses()->create([ 'data' => [ 'address_id' => 1432, 'city' => 'Paris', From 87dfecf8db99cb5c8c908007c8756d4f61300360 Mon Sep 17 00:00:00 2001 From: Ditty Date: Wed, 26 Feb 2020 11:08:52 +0300 Subject: [PATCH 2/4] Fix incorrect collection --- tests/ModelTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 869035192..ce7177fe6 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -492,7 +492,7 @@ public function testRaw(): void $users = User::raw(function (Collection $collection) { return $collection->find(['age' => 35]); }); - $this->assertInstanceOf(Collection::class, $users); + $this->assertInstanceOf(EloquentCollection::class, $users); $this->assertInstanceOf(Model::class, $users[0]); $user = User::raw(function (Collection $collection) { From c6d36d4d490e7dd6ccfbae225aeac4387b97ca3f Mon Sep 17 00:00:00 2001 From: Ditty Date: Wed, 26 Feb 2020 11:20:04 +0300 Subject: [PATCH 3/4] Remove unused call --- tests/ConnectionTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 3283c2846..46de29fa8 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -33,8 +33,6 @@ public function testDb() { $connection = DB::connection('mongodb'); $this->assertInstanceOf(Database::class, $connection->getMongoDB()); - - $connection = DB::connection('mongodb'); $this->assertInstanceOf(Client::class, $connection->getMongoClient()); } From cf6326ec1be8f43c18fdb04554fd5184e0abd47e Mon Sep 17 00:00:00 2001 From: Divine <48183131+divine@users.noreply.github.com> Date: Wed, 26 Feb 2020 20:29:57 +0300 Subject: [PATCH 4/4] Remove unnecessary class name --- tests/EmbeddedRelationsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index c91b8ac14..23e2fae03 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -42,7 +42,7 @@ public function testEmbedsManySave() $address->unsetEventDispatcher(); $this->assertNotNull($user->addresses); - $this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $user->addresses); + $this->assertInstanceOf(Collection::class, $user->addresses); $this->assertEquals(['London'], $user->addresses->pluck('city')->all()); $this->assertInstanceOf(DateTime::class, $address->created_at); $this->assertInstanceOf(DateTime::class, $address->updated_at);