Skip to content

Commit

Permalink
Add BankAccount example to TheCure. Also add unit tests for the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorton committed May 1, 2012
1 parent f5ccf9d commit 4543151
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
100 changes: 94 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,98 @@ $object->accessor('name', 'Jim');

``` php
<?php
var_dump($object->name);
var_dump($object->get('name'));
var_dump($object->get(array('name', 'age')));
var_dump($object->accessor('name'));
var_dump($object->as_array());
var_dump($object->name); // => 'Jim'
var_dump($object->get('name')); // => 'Jim'
var_dump($object->get(array('name', 'age'))); // => array('name' => 'Jim', 'age' => 26)
var_dump($object->accessor('name')); // => 'Jim'
var_dump($object->as_array()); // => array('name' => 'Jim', 'age' => 26)
?>
```
```

## Unit testing your domain logic

The Cure imposes minimal logic on it's base domain objects.
Even `Gignite\TheCure\Models\Magic` is fairly minimal in the
logic it exposes to your own domains. This isn't by accident,
we have made this decision by design. Our mapper logic, or
your own mapper logic should never be tested along side your
applications domain logic. They are unrelated and coupling
them together will add headaches in unit testing and
elsewhere.

### A banking example

So in this example we're going to design, test and then build
a very simple bank account object.

We only expect this bank account to be able to transfer money
to another account. This is what I came up with:

``` php
<?php
$lukes_bank_account = new Models\BankAccount;
$bobs_bank_account = new Models\BankAccount;
$lukes_bank_account->transfer_money($bobs_bank_account, 100);
?>
```

So we have one object, one method and two parameters. Pretty
simple design, let's test it.

``` php
<?php

class ModelBankAccountTest extends PHPUnit_Framework_TestCase {

public function provideBankAccounts()
{
return array(
array(new Models\BankAccount, new Models\BankAccount),
);
}

/**
* @dataProvider provideBankAccounts
*/
public function testItShouldTransferMoneyFromOneAccountToAnother(
$lukesAccount,
$bobsAccount)
{
$lukesAccount->__object()->balance = 100;
$bobsAccount->__object()->balance = 0;

$lukesAccount->transfer_money($bobsAccount, 100);

$this->assertSame(0, $lukesAccount->__object()->balance);
$this->assertSame(100, $bobsAccount->__object()->balance);
}

}
?>
```

First run your tests, they should fail... we haven't written
the BankAccount model yet! We have a test to prove the balance
transfers to another account.

Let's implement the bare minimum:

``` php
<?php

namespace Models;

class BankAccount extends \Gignite\TheCure\Models\Model {

public function transfer_money(BankAccount $account, $amount)
{
$this->__object()->balance -= $amount;
$account->__object()->balance += $amount;
}

}
?>
```

Running the unit test will show this code passes.

2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<file>test/RelationshipOneToOneTest.php</file>
<file>test/RelationshipOneToManyTest.php</file>
<file>test/ObjectTest.php</file>

<file>test/ModelBankAccountTest.php</file>
</testsuite>
</testsuites>
<filter>
Expand Down
28 changes: 28 additions & 0 deletions test/ModelBankAccountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

class ModelBankAccountTest extends PHPUnit_Framework_TestCase {

public function provideBankAccounts()
{
return array(
array(new Models\BankAccount, new Models\BankAccount),
);
}

/**
* @dataProvider provideBankAccounts
*/
public function testItShouldTransferMoneyFromOneAccountToAnother(
$lukesAccount,
$bobsAccount)
{
$lukesAccount->__object()->balance = 100;
$bobsAccount->__object()->balance = 0;

$lukesAccount->transfer_money($bobsAccount, 100);

$this->assertSame(0, $lukesAccount->__object()->balance);
$this->assertSame(100, $bobsAccount->__object()->balance);
}

}
13 changes: 13 additions & 0 deletions test/classes/Models/BankAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Models;

class BankAccount extends \Gignite\TheCure\Models\Model {

public function transfer_money(BankAccount $account, $amount)
{
$this->__object()->balance -= $amount;
$account->__object()->balance += $amount;
}

}

0 comments on commit 4543151

Please sign in to comment.