Refactored example of Proxy pattern

This commit is contained in:
Dominik Liebler
2018-06-14 22:17:14 +02:00
parent a494c07255
commit ea85485b85
10 changed files with 197 additions and 413 deletions

View File

@@ -2,24 +2,23 @@
namespace DesignPatterns\Structural\Proxy\Tests;
use DesignPatterns\Structural\Proxy\RecordProxy;
use DesignPatterns\Structural\Proxy\BankAccountProxy;
use PHPUnit\Framework\TestCase;
class ProxyTest extends TestCase
{
public function testWillSetDirtyFlagInProxy()
public function testProxyWillOnlyExecuteExpensiveGetBalanceOnce()
{
$recordProxy = new RecordProxy([]);
$recordProxy->username = 'baz';
$bankAccount = new BankAccountProxy();
$bankAccount->deposit(30);
$this->assertTrue($recordProxy->isDirty());
}
// this time balance is being calculated
$this->assertEquals(0, $bankAccount->getBalance());
public function testProxyIsInstanceOfRecord()
{
$recordProxy = new RecordProxy([]);
$recordProxy->username = 'baz';
// inheritance allows for BankAccountProxy to behave to an outsider exactly like ServerBankAccount
$bankAccount->deposit(50);
$this->assertInstanceOf(RecordProxy::class, $recordProxy);
// this time the previously calculated balance is returned again without re-calculating it
$this->assertEquals(0, $bankAccount->getBalance());
}
}