Skip to content

Commit 07b5d15

Browse files
Merge pull request #24 from fotografde/dbunit
Support for DbUnit
2 parents 0e4eb80 + 0325d35 commit 07b5d15

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ $app->post('/user', function() use ($app){
4545
});
4646
```
4747

48+
### Example with DbUnit
49+
50+
If you with to use Database fixture, use class `WebDbTestCase`. Caution: make sure the names you use for you fixture models won't conflict with your actual DB tables.
51+
52+
```php
53+
class LocalDbWebTestCase extends \There4\Slim\Test\WebDbTestCase {
54+
/**
55+
* You must implement this method
56+
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
57+
*/
58+
public function getDataSet() {
59+
return $this->createFlatXMLDataSet(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fixture.xml');
60+
}
61+
}
62+
```
63+
4864
## Setup
4965

5066
You'll need a bootstrap file for phpunit that can instantiate your Slim

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"minimum-stability": "dev",
2222
"require": {
2323
"slim/slim": "2.6.*",
24-
"phpunit/phpunit": "4.*"
24+
"phpunit/phpunit": "4.*",
25+
"phpunit/dbunit": "1.*",
26+
"illuminate/database": ">=4.0"
2527
},
2628
"require-dev": {
2729
"squizlabs/php_codesniffer": "1.*"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace There4\Slim\Test;
4+
5+
use Illuminate\Database\Eloquent\Model as Eloquent;
6+
use \Slim\Slim;
7+
8+
class WebDbTestCase extends \PHPUnit_Extensions_Database_TestCase
9+
{
10+
/** @var \Slim\Slim */
11+
protected $app;
12+
13+
/** @var WebTestClient */
14+
protected $client;
15+
16+
// Run for each unit test to setup our slim app environment
17+
public function setup()
18+
{
19+
parent::setUp();
20+
// Establish a local reference to the Slim app object
21+
$this->app = $this->getSlimInstance();
22+
$this->client = new WebTestClient($this->app);
23+
}
24+
25+
// Instantiate a Slim application for use in our testing environment. You
26+
// will most likely override this for your own application.
27+
public function getSlimInstance()
28+
{
29+
// @todo Find a way not to duplicate code from WebTestCase. Using trait file?
30+
$slim = new Slim(array(
31+
'version' => '0.0.0',
32+
'debug' => false,
33+
'mode' => 'testing'
34+
));
35+
// force to overwrite the App singleton, so that \Slim\Slim::getInstance()
36+
// returns the correct instance.
37+
$slim->setName('default');
38+
39+
// make sure we don't use a caching router
40+
$slim->router = new NoCacheRouter($slim->router);
41+
return $slim;
42+
}
43+
44+
public function getConnection()
45+
{
46+
$pdo = Eloquent::getConnectionResolver()->connection()->getPdo();
47+
return $this->createDefaultDBConnection($pdo, ':memory:');
48+
}
49+
50+
public function getDataSet()
51+
{
52+
throw new \Exception('Method getDataSet() not implemented!');
53+
}
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace There4Test\Slim\Test;
4+
5+
use There4\Slim\Test\WebDbTestCase;
6+
7+
class WebDbTestCaseTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testExtendsDbUnit()
10+
{
11+
$testCase = new WebDbTestCase();
12+
$this->assertInstanceOf('\PHPUnit_Extensions_Database_TestCase', $testCase);
13+
}
14+
15+
public function testGetSlimInstance()
16+
{
17+
$expectedConfig = array(
18+
'version' => '0.0.0',
19+
'debug' => false,
20+
'mode' => 'testing'
21+
);
22+
$testCase = new WebDbTestCase();
23+
$slim = $testCase->getSlimInstance();
24+
$this->assertInstanceOf('\Slim\Slim', $slim);
25+
foreach ($expectedConfig as $key => $value) {
26+
$this->assertSame($expectedConfig[$key], $slim->config($key));
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)