Skip to content

[refactor] tests #1972

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 4 commits into from
Feb 26, 2020
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
2 changes: 1 addition & 1 deletion tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function tearDown(): void

public function testAuthAttempt()
{
$user = User::create([
User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => Hash::make('foobar'),
Expand Down
49 changes: 15 additions & 34 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -26,54 +32,29 @@ public function testReconnect()
public function testDb()
{
$connection = DB::connection('mongodb');
$this->assertInstanceOf(\MongoDB\Database::class, $connection->getMongoDB());

$connection = DB::connection('mongodb');
$this->assertInstanceOf(\MongoDB\Client::class, $connection->getMongoClient());
$this->assertInstanceOf(Database::class, $connection->getMongoDB());
$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();
Expand All @@ -99,7 +80,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()
Expand Down
25 changes: 1 addition & 24 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
Expand Down
26 changes: 14 additions & 12 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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]);
}

Expand All @@ -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();
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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(EloquentCollection::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);
Expand Down Expand Up @@ -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);
});

Expand Down
1 change: 0 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion tests/QueueTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
declare(strict_types=1);

use Carbon\Carbon;
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;

class QueueTest extends TestCase
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testQueueJobExpired(): void
$this->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)
Expand Down
6 changes: 3 additions & 3 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -467,7 +467,7 @@ public function testNestedKeys(): void
],
]);

$address = $client->addresses()->create([
$client->addresses()->create([
'data' => [
'address_id' => 1432,
'city' => 'Paris',
Expand Down