Skip to content

Commit b32d35e

Browse files
committed
Tweak unittests and added validation test for #174
1 parent 5b7f335 commit b32d35e

15 files changed

+98
-102
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"illuminate/events": "4.1.x"
1717
},
1818
"require-dev": {
19-
"illuminate/cache": "4.1.x",
20-
"illuminate/auth": "4.1.x",
19+
"orchestra/testbench": "2.1.*",
2120
"mockery/mockery": "*"
2221
},
2322
"autoload": {

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@
3939
<directory>tests/RelationsTest.php</directory>
4040
<directory>tests/MysqlRelationsTest.php</directory>
4141
</testsuite>
42+
<testsuite name="validation">
43+
<directory>tests/ValidationTest.php</directory>
44+
</testsuite>
4245
</testsuites>
4346
</phpunit>

tests/CacheTest.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
<?php
22

3-
use Illuminate\Support\Facades\DB;
4-
5-
class CacheTest extends PHPUnit_Framework_TestCase {
6-
7-
protected $cache;
8-
9-
public function setUp()
10-
{
11-
global $app;
12-
$this->cache = $app['cache'];
13-
14-
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
15-
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
16-
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
17-
}
3+
class CacheTest extends TestCase {
184

195
public function tearDown()
206
{
217
User::truncate();
22-
$this->cache->forget('db.users');
8+
Cache::forget('db.users');
239
}
2410

2511
public function testCache()
2612
{
13+
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
14+
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
15+
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
16+
2717
$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
2818
$this->assertEquals(3, count($users));
2919

@@ -33,7 +23,7 @@ public function testCache()
3323
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
3424
$this->assertEquals(3, count($users));
3525

36-
$users = $this->cache->get('db.users');
26+
$users = Cache::get('db.users');
3727
$this->assertEquals(3, count($users));
3828
}
3929

tests/ConnectionTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
<?php
2-
use Illuminate\Support\Facades\DB;
3-
use Jenssegers\Mongodb\Connection;
42

5-
class ConnectionTest extends PHPUnit_Framework_TestCase {
6-
7-
public function setUp() {}
8-
9-
public function tearDown() {}
3+
class ConnectionTest extends TestCase {
104

115
public function testConnection()
126
{
@@ -15,11 +9,11 @@ public function testConnection()
159

1610
$c1 = DB::connection('mongodb');
1711
$c2 = DB::connection('mongodb');
18-
$this->assertEquals($c1, $c2);
12+
$this->assertEquals(spl_object_hash($c1), spl_object_hash($c2));
1913

2014
$c1 = DB::connection('mongodb');
2115
$c2 = DB::reconnect('mongodb');
22-
$this->assertNotEquals($c1, $c2);
16+
$this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2));
2317
}
2418

2519
public function testDb()

tests/ModelTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

3-
class ModelTest extends PHPUnit_Framework_TestCase {
4-
5-
public function setUp() {}
3+
class ModelTest extends TestCase {
64

75
public function tearDown()
86
{

tests/MysqlRelationsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3-
class MysqlRelationsTest extends PHPUnit_Framework_TestCase {
3+
class MysqlRelationsTest extends TestCase {
44

55
public function setUp()
66
{
7+
parent::setUp();
8+
79
MysqlUser::executeSchema();
810
MysqlBook::executeSchema();
911
MysqlRole::executeSchema();

tests/QueryBuilderTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?php
22

3-
use Illuminate\Support\Facades\DB;
4-
5-
class QueryBuilderTest extends PHPUnit_Framework_TestCase {
6-
7-
public function setUp() {}
3+
class QueryBuilderTest extends TestCase {
84

95
public function tearDown()
106
{

tests/QueryTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php
22

3-
class QueryTest extends PHPUnit_Framework_TestCase {
3+
class QueryTest extends TestCase {
44

55
protected static $started = false;
66

77
public function setUp()
88
{
9+
parent::setUp();
10+
11+
// only run this stuff once
912
if (self::$started) return;
1013

1114
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));

tests/RelationsTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
<?php
22

3-
use Illuminate\Database\Eloquent\Collection;
4-
5-
class RelationsTest extends PHPUnit_Framework_TestCase {
6-
7-
public function setUp() {
8-
}
3+
class RelationsTest extends TestCase {
94

105
public function tearDown()
116
{
@@ -148,8 +143,8 @@ public function testBelongsToMany()
148143
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
149144
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
150145

151-
$users = $client->getRelation('users');
152146
$clients = $user->getRelation('clients');
147+
$users = $client->getRelation('users');
153148

154149
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
155150
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);

tests/SchemaTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
<?php
22

3-
use Illuminate\Support\Facades\DB;
4-
use Illuminate\Support\Facades\Schema;
5-
6-
class SchemaTest extends PHPUnit_Framework_TestCase {
7-
8-
public function setUp() {}
3+
class SchemaTest extends TestCase {
94

105
public function tearDown()
116
{

tests/SeederTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
<?php
22

3-
use Illuminate\Support\Facades\DB;
4-
5-
class SeederTest extends PHPUnit_Framework_TestCase {
6-
7-
public function setUp() {}
3+
class SeederTest extends TestCase {
84

95
public function tearDown()
106
{

tests/TestCase.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
class TestCase extends Orchestra\Testbench\TestCase {
4+
5+
/**
6+
* Get package providers.
7+
*
8+
* @return array
9+
*/
10+
protected function getPackageProviders()
11+
{
12+
return array('Jenssegers\Mongodb\MongodbServiceProvider');
13+
}
14+
15+
/**
16+
* Define environment setup.
17+
*
18+
* @param Illuminate\Foundation\Application $app
19+
* @return void
20+
*/
21+
protected function getEnvironmentSetUp($app)
22+
{
23+
// reset base path to point to our package's src directory
24+
//$app['path.base'] = __DIR__ . '/../src';
25+
26+
// load custom config
27+
$config = require 'config/database.php';
28+
29+
// set mongodb as default connection
30+
$app['config']->set('database.default', 'mongodb');
31+
32+
// overwrite database configuration
33+
$app['config']->set('database.connections.mysql', $config['connections']['mysql']);
34+
$app['config']->set('database.connections.mongodb', $config['connections']['mongodb']);
35+
}
36+
37+
}

tests/ValidationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class ValidationTest extends TestCase {
4+
5+
public function tearDown()
6+
{
7+
User::truncate();
8+
}
9+
10+
public function testUnique()
11+
{
12+
$validator = Validator::make(
13+
array('name' => 'John Doe'),
14+
array('name' => 'required|unique:users')
15+
);
16+
$this->assertFalse($validator->fails());
17+
18+
User::create(array('name' => 'John Doe'));
19+
20+
$validator = Validator::make(
21+
array('name' => 'John Doe'),
22+
array('name' => 'required|unique:users')
23+
);
24+
$this->assertTrue($validator->fails());
25+
}
26+
27+
}

tests/bootstrap.php

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,10 @@
11
<?php
2-
$loader = require 'vendor/autoload.php';
3-
$loader->add('', 'tests/models');
4-
$loader->add('', 'tests/seeds');
5-
6-
use Jenssegers\Mongodb\Model;
7-
use Illuminate\Support\Facades\DB;
8-
use Illuminate\Container\Container;
9-
use Illuminate\Database\DatabaseManager;
10-
use Illuminate\Database\Connectors\ConnectionFactory;
11-
use Illuminate\Events\Dispatcher;
12-
use Illuminate\Cache\CacheManager;
13-
14-
# Fake app class
15-
class App extends ArrayObject {
16-
function bound() {}
17-
}
18-
19-
# Fake app
20-
$app = new App;
212

22-
# Load database configuration
23-
$config = require 'config/database.php';
24-
foreach ($config as $key => $value)
25-
{
26-
$app['config']["database.$key"] = $value;
27-
}
28-
29-
# Event dispatcher
30-
$app['events'] = new Dispatcher;
31-
32-
# Cache driver
33-
$app['config']['cache.driver'] = 'array';
34-
$app['cache'] = new CacheManager($app);
35-
36-
# Initialize database manager
37-
$app['db.factory'] = new ConnectionFactory(new Container);
38-
$app['db'] = new DatabaseManager($app, $app['db.factory']);
3+
$loader = require 'vendor/autoload.php';
394

40-
# Extend database manager with reflection hack
41-
$reflection = new ReflectionClass('Jenssegers\Mongodb\Connection');
42-
$app['db']->extend('mongodb', array($reflection, 'newInstance'));
5+
// Could not figure out how to add this to the loader
6+
require 'TestCase.php';
437

44-
# Static setup
45-
\Jenssegers\Mongodb\Model::setConnectionResolver($app['db']);
46-
\Jenssegers\Eloquent\Model::setConnectionResolver($app['db']);
47-
DB::setFacadeApplication($app);
8+
// Add stuff to autoload
9+
$loader->add('', 'tests/models');
10+
$loader->add('', 'tests/seeds');

tests/config/database.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
return array(
44

5-
'fetch' => PDO::FETCH_CLASS,
6-
'default' => 'mongodb',
7-
85
'connections' => array(
6+
97
'mongodb' => array(
108
'name' => 'mongodb',
119
'driver' => 'mongodb',

0 commit comments

Comments
 (0)