public function testPersistInsertsNewAccountsIntoTheDatabaseMockedVersion() { $stmt = $this->mock('stdClass')->execute([500], $this->once())->new(); $pdo = $this->mock('PDO')->prepare(['insert into account (balance) values (?)'], $stmt, $this->once())->lastInsertId(1, $this->once())->new('sqlite::memory:'); $service = new BankAccountService($pdo); $account = new BankAccount(); $account->deposit(500); $newAccountId = $service->persist($account); $this->assertEquals(1, $newAccountId); }
public function testPersistInsertsNewAccountsIntoTheDatabaseMockedVersion() { $stmt = $this->getMockBuilder('stdClass')->setMethods(['execute'])->getMock(); $stmt->expects($this->once())->method('execute')->with([500]); $pdo = $this->getMockBuilder('PDO')->setConstructorArgs(['sqlite::memory:'])->getMock(); $pdo->expects($this->once())->method('prepare')->with('insert into account (balance) values (?)')->will($this->returnValue($stmt)); $pdo->expects($this->once())->method('lastInsertId')->will($this->returnValue(1)); $service = new BankAccountService($pdo); $account = new BankAccount(); $account->deposit(500); $newAccountId = $service->persist($account); $this->assertEquals(1, $newAccountId); }
/** * Persists the provided account. * * @param BankAccount $account * @return int The account ID */ public function persist(BankAccount $account) { if ($account->getId()) { $stmt = $this->database->prepare('update account set balance = ? where id = ?'); $stmt->execute([$account->getBalance(), $account->getId()]); return $account->getId(); } else { $stmt = $this->database->prepare('insert into account (balance) values (?)'); $stmt->execute([$account->getBalance()]); return $this->database->lastInsertId(); } }
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage Withdrawal amount cannot be negative! */ public function testExceptionIsThrownWhenWithdrawingNegativeMoney() { $account = new BankAccount(); $account->withdraw(-100); }