Skip to content

Commit 12adb39

Browse files
authored
Merge pull request #1972 from divine/refactor_connection_tests
[refactor] tests
2 parents 22a9e3b + cf6326e commit 12adb39

7 files changed

+36
-76
lines changed

Diff for: tests/AuthTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function tearDown(): void
1515

1616
public function testAuthAttempt()
1717
{
18-
$user = User::create([
18+
User::create([
1919
'name' => 'John Doe',
2020
'email' => '[email protected]',
2121
'password' => Hash::make('foobar'),

Diff for: tests/ConnectionTest.php

+15-34
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
declare(strict_types=1);
33

44
use Illuminate\Support\Facades\DB;
5+
use Jenssegers\Mongodb\Collection;
6+
use Jenssegers\Mongodb\Connection;
7+
use Jenssegers\Mongodb\Query\Builder;
8+
use Jenssegers\Mongodb\Schema\Builder as SchemaBuilder;
9+
use MongoDB\Client;
10+
use MongoDB\Database;
511

612
class ConnectionTest extends TestCase
713
{
814
public function testConnection()
915
{
1016
$connection = DB::connection('mongodb');
11-
$this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $connection);
17+
$this->assertInstanceOf(Connection::class, $connection);
1218
}
1319

1420
public function testReconnect()
@@ -26,54 +32,29 @@ public function testReconnect()
2632
public function testDb()
2733
{
2834
$connection = DB::connection('mongodb');
29-
$this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB());
30-
31-
$connection = DB::connection('mongodb');
32-
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
35+
$this->assertInstanceOf(Database::class, $connection->getMongoDB());
36+
$this->assertInstanceOf(Client::class, $connection->getMongoClient());
3337
}
3438

3539
public function testDsnDb()
3640
{
3741
$connection = DB::connection('dsn_mongodb_db');
38-
$this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB());
39-
40-
$connection = DB::connection('dsn_mongodb_db');
41-
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
42+
$this->assertInstanceOf(Database::class, $connection->getMongoDB());
43+
$this->assertInstanceOf(Client::class, $connection->getMongoClient());
4244
}
4345

4446
public function testCollection()
4547
{
4648
$collection = DB::connection('mongodb')->getCollection('unittest');
47-
$this->assertInstanceOf(Jenssegers\Mongodb\Collection::class, $collection);
49+
$this->assertInstanceOf(Collection::class, $collection);
4850

4951
$collection = DB::connection('mongodb')->collection('unittests');
50-
$this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection);
52+
$this->assertInstanceOf(Builder::class, $collection);
5153

5254
$collection = DB::connection('mongodb')->table('unittests');
53-
$this->assertInstanceOf(Jenssegers\Mongodb\Query\Builder::class, $collection);
55+
$this->assertInstanceOf(Builder::class, $collection);
5456
}
5557

56-
// public function testDynamic()
57-
// {
58-
// $dbs = DB::connection('mongodb')->listCollections();
59-
// $this->assertIsArray($dbs);
60-
// }
61-
62-
// public function testMultipleConnections()
63-
// {
64-
// global $app;
65-
66-
// # Add fake host
67-
// $db = $app['config']['database.connections']['mongodb'];
68-
// $db['host'] = array($db['host'], '1.2.3.4');
69-
70-
// $connection = new Connection($db);
71-
// $mongoclient = $connection->getMongoClient();
72-
73-
// $hosts = $mongoclient->getHosts();
74-
// $this->assertCount(1, $hosts);
75-
// }
76-
7758
public function testQueryLog()
7859
{
7960
DB::enableQueryLog();
@@ -99,7 +80,7 @@ public function testQueryLog()
9980
public function testSchemaBuilder()
10081
{
10182
$schema = DB::connection('mongodb')->getSchemaBuilder();
102-
$this->assertInstanceOf(\Jenssegers\Mongodb\Schema\Builder::class, $schema);
83+
$this->assertInstanceOf(SchemaBuilder::class, $schema);
10384
}
10485

10586
public function testDriverName()

Diff for: tests/EmbeddedRelationsTest.php

+1-24
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testEmbedsManySave()
4242
$address->unsetEventDispatcher();
4343

4444
$this->assertNotNull($user->addresses);
45-
$this->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $user->addresses);
45+
$this->assertInstanceOf(Collection::class, $user->addresses);
4646
$this->assertEquals(['London'], $user->addresses->pluck('city')->all());
4747
$this->assertInstanceOf(DateTime::class, $address->created_at);
4848
$this->assertInstanceOf(DateTime::class, $address->updated_at);
@@ -103,29 +103,6 @@ public function testEmbedsManySave()
103103
$this->assertEquals(['London', 'Manhattan', 'Bruxelles'], $freshUser->addresses->pluck('city')->all());
104104
}
105105

106-
// public function testEmbedsManySaveModel()
107-
// {
108-
// $user = User::create(['name' => 'John Doe']);
109-
// $address = new Address(['city' => 'London']);
110-
111-
// $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class));
112-
// $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
113-
// $events->shouldReceive('until')->once()->with('eloquent.creating: ' . get_class($address), $address)->andReturn(true);
114-
// $events->shouldReceive('dispatch')->once()->with('eloquent.created: ' . get_class($address), $address);
115-
// $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address);
116-
117-
// $address->save();
118-
119-
// $address->setEventDispatcher($events = Mockery::mock(\Illuminate\Events\Dispatcher::class));
120-
// $events->shouldReceive('until')->once()->with('eloquent.saving: ' . get_class($address), $address)->andReturn(true);
121-
// $events->shouldReceive('until')->once()->with('eloquent.updating: ' . get_class($address), $address)->andReturn(true);
122-
// $events->shouldReceive('dispatch')->once()->with('eloquent.updated: ' . get_class($address), $address);
123-
// $events->shouldReceive('dispatch')->once()->with('eloquent.saved: ' . get_class($address), $address);
124-
125-
// $address->city = 'Paris';
126-
// $address->save();
127-
// }
128-
129106
public function testEmbedsToArray()
130107
{
131108
$user = User::create(['name' => 'John Doe']);

Diff for: tests/ModelTest.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
declare(strict_types=1);
33

44
use Carbon\Carbon;
5-
use Illuminate\Database\Eloquent\Collection;
5+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
66
use Illuminate\Database\Eloquent\ModelNotFoundException;
7+
use Jenssegers\Mongodb\Collection;
8+
use Jenssegers\Mongodb\Connection;
79
use Jenssegers\Mongodb\Eloquent\Model;
810
use MongoDB\BSON\ObjectID;
911
use MongoDB\BSON\UTCDateTime;
@@ -22,7 +24,7 @@ public function testNewModel(): void
2224
{
2325
$user = new User;
2426
$this->assertInstanceOf(Model::class, $user);
25-
$this->assertInstanceOf(\Jenssegers\Mongodb\Connection::class, $user->getConnection());
27+
$this->assertInstanceOf(Connection::class, $user->getConnection());
2628
$this->assertFalse($user->exists);
2729
$this->assertEquals('users', $user->getTable());
2830
$this->assertEquals('_id', $user->getKeyName());
@@ -196,7 +198,7 @@ public function testGet(): void
196198

197199
$users = User::get();
198200
$this->assertCount(2, $users);
199-
$this->assertInstanceOf(Collection::class, $users);
201+
$this->assertInstanceOf(EloquentCollection::class, $users);
200202
$this->assertInstanceOf(Model::class, $users[0]);
201203
}
202204

@@ -216,7 +218,7 @@ public function testFirst(): void
216218
public function testNoDocument(): void
217219
{
218220
$items = Item::where('name', 'nothing')->get();
219-
$this->assertInstanceOf(Collection::class, $items);
221+
$this->assertInstanceOf(EloquentCollection::class, $items);
220222
$this->assertEquals(0, $items->count());
221223

222224
$item = Item::where('name', 'nothing')->first();
@@ -436,11 +438,11 @@ public function testDates(): void
436438

437439
public function testCarbonDateMockingWorks()
438440
{
439-
$fakeDate = \Carbon\Carbon::createFromDate(2000, 01, 01);
441+
$fakeDate = Carbon::createFromDate(2000, 01, 01);
440442

441443
Carbon::setTestNow($fakeDate);
442444
$item = Item::create(['name' => 'sword']);
443-
445+
444446
$this->assertLessThan(1, $fakeDate->diffInSeconds($item->created_at));
445447
}
446448

@@ -487,24 +489,24 @@ public function testRaw(): void
487489
User::create(['name' => 'Jane Doe', 'age' => 35]);
488490
User::create(['name' => 'Harry Hoe', 'age' => 15]);
489491

490-
$users = User::raw(function (\Jenssegers\Mongodb\Collection $collection) {
492+
$users = User::raw(function (Collection $collection) {
491493
return $collection->find(['age' => 35]);
492494
});
493-
$this->assertInstanceOf(Collection::class, $users);
495+
$this->assertInstanceOf(EloquentCollection::class, $users);
494496
$this->assertInstanceOf(Model::class, $users[0]);
495497

496-
$user = User::raw(function (\Jenssegers\Mongodb\Collection $collection) {
498+
$user = User::raw(function (Collection $collection) {
497499
return $collection->findOne(['age' => 35]);
498500
});
499501

500502
$this->assertInstanceOf(Model::class, $user);
501503

502-
$count = User::raw(function (\Jenssegers\Mongodb\Collection $collection) {
504+
$count = User::raw(function (Collection $collection) {
503505
return $collection->count();
504506
});
505507
$this->assertEquals(3, $count);
506508

507-
$result = User::raw(function (\Jenssegers\Mongodb\Collection $collection) {
509+
$result = User::raw(function (Collection $collection) {
508510
return $collection->insertOne(['name' => 'Yvonne Yoe', 'age' => 35]);
509511
});
510512
$this->assertNotNull($result);
@@ -566,7 +568,7 @@ public function testChunkById(): void
566568
User::create(['name' => 'spoon', 'tags' => ['round', 'bowl']]);
567569

568570
$count = 0;
569-
User::chunkById(2, function (\Illuminate\Database\Eloquent\Collection $items) use (&$count) {
571+
User::chunkById(2, function (EloquentCollection $items) use (&$count) {
570572
$count += count($items);
571573
});
572574

Diff for: tests/QueryBuilderTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public function testUpdate()
151151
]);
152152

153153
DB::collection('users')->where('name', 'John Doe')->update(['age' => 100]);
154-
$users = DB::collection('users')->get();
155154

156155
$john = DB::collection('users')->where('name', 'John Doe')->first();
157156
$jane = DB::collection('users')->where('name', 'Jane Doe')->first();

Diff for: tests/QueueTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4+
use Carbon\Carbon;
45
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
56

67
class QueueTest extends TestCase
@@ -43,7 +44,7 @@ public function testQueueJobExpired(): void
4344
$this->assertNotNull($id);
4445

4546
// Expire the test job
46-
$expiry = \Carbon\Carbon::now()->subSeconds(Config::get('queue.connections.database.expire'))->getTimestamp();
47+
$expiry = Carbon::now()->subSeconds(Config::get('queue.connections.database.expire'))->getTimestamp();
4748
Queue::getDatabase()
4849
->table(Config::get('queue.connections.database.table'))
4950
->where('_id', $id)

Diff for: tests/RelationsTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ public function testBelongsToManyAttachArray(): void
296296

297297
public function testBelongsToManyAttachEloquentCollection(): void
298298
{
299-
$user = User::create(['name' => 'John Doe']);
299+
User::create(['name' => 'John Doe']);
300300
$client1 = Client::create(['name' => 'Test 1']);
301301
$client2 = Client::create(['name' => 'Test 2']);
302-
$collection = new \Illuminate\Database\Eloquent\Collection([$client1, $client2]);
302+
$collection = new Collection([$client1, $client2]);
303303

304304
$user = User::where('name', '=', 'John Doe')->first();
305305
$user->clients()->attach($collection);
@@ -467,7 +467,7 @@ public function testNestedKeys(): void
467467
],
468468
]);
469469

470-
$address = $client->addresses()->create([
470+
$client->addresses()->create([
471471
'data' => [
472472
'address_id' => 1432,
473473
'city' => 'Paris',

0 commit comments

Comments
 (0)