Skip to content

Commit d5f8c91

Browse files
author
Malte Riesch
committed
2 parents 8093fb7 + 0343e64 commit d5f8c91

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

README.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,81 @@ The framework itself knows which columns are not NULL-able in the table and inse
8585
Also, there might be various foreign key constraints going on in the background. Test-Db-Acle temporarily disables foreign key checks, so we do not have to worry about this, or in which order we insert the test data.
8686

8787
###What about an actual test.....?###
88+
First, if you use PHP5.4, you can use the traits version:
89+
```php
90+
class ExampleTest extends \PHPUnit_Framework_TestCase implements \TestDbAcle\PhpUnit\AbstractTestCaseInterface
91+
{
92+
use \TestDbAcle\PhpUnit\Traits\DatabaseHelperTrait;
93+
94+
/**
95+
* This method needs to be implemented, it should return the PDO object that is to be used in the database fixtures
96+
*
97+
* @return \Pdo
98+
*/
99+
public function providePdo()
100+
{
101+
return new \Pdo("mysql:dbname=my_db_tests;host=localhost",'myTestUser', 'myTestPassword');
102+
}
103+
104+
/*
105+
* An example test
106+
*/
107+
public function test_AddressService()
108+
{
109+
$this->setupTables("
110+
[address]
111+
address_id |company
112+
1 |me
113+
3 |you
114+
115+
[user]
116+
user_id |name
117+
10 |John
118+
20 |Mary
119+
120+
");
121+
122+
$this->setAutoIncrement('address', 100);
123+
124+
$this->addressService->addEntry("them");
125+
126+
$this->assertTableStateContains("
127+
[address]
128+
address_id |company
129+
1 |me
130+
3 |you
131+
100 |them
132+
133+
[user]
134+
user_id |name
135+
10 |John
136+
20 |Mary
137+
", array(), "Stuff works");
138+
}
139+
}
140+
```
141+
142+
If you don't use traits, you can use ```AbstractTestCase```, however you won't be able to use your own base test classes in that case.
88143

89144
```php
90145
class ExampleTest extends \TestDbAcle\PhpUnit\AbstractTestCase
91146
{
92-
protected $pdo;
147+
93148
protected $addressService;
94149

95150
function Setup(){
96151
parent::Setup();
97152
$this->addressService = new \Services\AddressService();
98153
}
99154

100-
function getPdo()
155+
/**
156+
* This method needs to be implemented, it should return the PDO object that is to be used in the database fixtures
157+
*
158+
* @return \Pdo
159+
*/
160+
public function providePdo()
101161
{
102-
if (!isset($this->pdo)){
103-
$config = include(__DIR__."/config.php");
104-
$this->pdo = new \Pdo("mysql:dbname={$config['db_name']};host={$config['db_host']}",$config['db_user'],$config['db_password']);
105-
}
106-
return $this->pdo;
162+
return new \Pdo("mysql:dbname=my_db_tests;host=localhost",'myTestUser', 'myTestPassword');
107163
}
108164

109165
function test_AddressService()

0 commit comments

Comments
 (0)