Skip to content

Commit d5834c1

Browse files
committed
Tweak tests to make less database connections
1 parent 2e165cc commit d5834c1

File tree

7 files changed

+32
-31
lines changed

7 files changed

+32
-31
lines changed

src/Jenssegers/Mongodb/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function getDsn(array $config)
126126
*/
127127
public function __call($method, $parameters)
128128
{
129-
return call_user_func_array(array($this->connection, $method), $parameters);
129+
return call_user_func_array(array($this->db, $method), $parameters);
130130
}
131131

132132
}

tests/CacheTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
<?php
22
require_once('vendor/autoload.php');
33
require_once('models/User.php');
4+
require_once('tests/app.php');
45

56
use Jenssegers\Mongodb\Facades\DB;
67

78
class CacheTest extends PHPUnit_Framework_TestCase {
89

9-
protected $app;
10-
1110
public function setUp()
1211
{
13-
include('tests/app.php');
14-
$this->app = $app;
15-
1612
// test data
1713
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
1814
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
@@ -37,8 +33,10 @@ public function testCache()
3733
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
3834
$this->assertEquals(3, count($users));
3935

36+
global $app;
37+
4038
# get from cache driver
41-
$cache = $this->app['cache'];
39+
$cache = $app['cache'];
4240
$users = $cache->get('db.users');
4341
$this->assertEquals(3, count($users));
4442
}

tests/ConnectionTest.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
11
<?php
22
require_once('vendor/autoload.php');
3+
require_once('tests/app.php');
34

5+
use Jenssegers\Mongodb\Facades\DB;
46
use Jenssegers\Mongodb\Connection;
57

68
class ConnectionTest extends PHPUnit_Framework_TestCase {
79

8-
private $connection;
10+
public function setUp() {}
911

10-
public function setUp()
11-
{
12-
include('tests/app.php');
13-
$this->connection = new Connection($app['config']['database.connections']['mongodb']);
14-
}
12+
public function tearDown() {}
1513

16-
public function tearDown()
14+
public function testConnection()
1715
{
16+
$connection = DB::connection('mongodb');
17+
$this->assertInstanceOf('Jenssegers\Mongodb\Connection', $connection);
18+
19+
$c1 = DB::connection('mongodb');
20+
$c2 = DB::connection('mongodb');
21+
$this->assertEquals($c1, $c2);
22+
23+
$c1 = DB::connection('mongodb');
24+
$c2 = DB::reconnect('mongodb');
25+
$this->assertNotEquals($c1, $c2);
1826
}
1927

2028
public function testDb()
2129
{
22-
$db = $this->connection->getDb();
23-
$this->assertInstanceOf('MongoDB', $db);
30+
$connection = DB::connection('mongodb');
31+
$this->assertInstanceOf('MongoDB', $connection->getDb());
2432
}
2533

2634
public function testCollection()
2735
{
28-
$collection = $this->connection->getCollection('unittest');
36+
$collection = DB::connection('mongodb')->getCollection('unittest');
2937
$this->assertInstanceOf('MongoCollection', $collection);
3038

31-
$collection = $this->connection->collection('unittests');
39+
$collection = DB::connection('mongodb')->collection('unittests');
3240
$this->assertInstanceOf('Jenssegers\Mongodb\Builder', $collection);
3341

34-
$collection = $this->connection->table('unittests');
42+
$collection = DB::connection('mongodb')->table('unittests');
3543
$this->assertInstanceOf('Jenssegers\Mongodb\Builder', $collection);
3644
}
3745

3846
public function testDynamic()
3947
{
40-
$dbs = $this->connection->listDBs();
48+
$dbs = DB::connection('mongodb')->listCollections();
4149
$this->assertTrue(is_array($dbs));
4250
}
4351

tests/ModelQueryTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require_once('vendor/autoload.php');
33
require_once('models/User.php');
4+
require_once('tests/app.php');
45

56
use Jenssegers\Mongodb\Connection;
67
use Jenssegers\Mongodb\Model;
@@ -10,8 +11,6 @@ class ModelQueryTest extends PHPUnit_Framework_TestCase {
1011

1112
public function setUp()
1213
{
13-
include('tests/app.php');
14-
1514
// test data
1615
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
1716
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));

tests/ModelTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
require_once('models/User.php');
44
require_once('models/Soft.php');
55
require_once('models/Book.php');
6+
require_once('tests/app.php');
67

78
use Jenssegers\Mongodb\Connection;
89
use Jenssegers\Mongodb\Model;
910
use Jenssegers\Mongodb\DatabaseManager;
1011

1112
class ModelTest extends PHPUnit_Framework_TestCase {
1213

13-
public function setUp()
14-
{
15-
include('tests/app.php');
16-
}
14+
public function setUp() {}
1715

1816
public function tearDown()
1917
{

tests/QueryTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22
require_once('vendor/autoload.php');
3+
require_once('tests/app.php');
34

45
use Jenssegers\Mongodb\Facades\DB;
56

67
class QueryTest extends PHPUnit_Framework_TestCase {
78

8-
public function setUp()
9-
{
10-
include('tests/app.php');
11-
}
9+
public function setUp() {}
1210

1311
public function tearDown()
1412
{

tests/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
$app['mongodb'] = new DatabaseManager($app);
2828

2929
# Static setup
30-
Model::setConnectionResolver(new DatabaseManager($app));
30+
Model::setConnectionResolver($app['mongodb']);
3131
DB::setFacadeApplication($app);

0 commit comments

Comments
 (0)