]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Samples/BankAccountDB/BankAccount.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Samples / BankAccountDB / BankAccount.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Mike Lively <m@digitalsandwich.com>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.2.0
45  */
46
47 class BankAccountException extends RuntimeException {}
48
49 /**
50  * A bank account.
51  *
52  * @category   Testing
53  * @package    PHPUnit
54  * @author     Mike Lively <m@digitalsandwich.com>
55  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
56  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
57  * @version    Release: 3.3.17
58  * @link       http://www.phpunit.de/
59  * @since      Class available since Release 3.2.0
60  */
61 class BankAccount
62 {
63     /**
64      * The bank account's balance.
65      *
66      * @var    float
67      */
68     protected $balance = 0;
69
70     /**
71      * The bank account's number.
72      *
73      * @var    float
74      */
75     protected $accountNumber = 0;
76
77     /**
78      * The PDO connection used to store and retrieve bank account information.
79      *
80      * @var PDO
81      */
82     protected $pdo;
83
84     public function __construct($accountNumber, PDO $pdo)
85     {
86         $this->accountNumber = $accountNumber;
87         $this->pdo = $pdo;
88
89         $this->loadAccount();
90     }
91
92     /**
93      * Returns the bank account's balance.
94      *
95      * @return float
96      */
97     public function getBalance()
98     {
99         return $this->balance;
100     }
101
102     /**
103      * Sets the bank account's balance.
104      *
105      * @param  float $balance
106      * @throws BankAccountException
107      */
108     protected function setBalance($balance)
109     {
110         if ($balance >= 0) {
111             $this->balance = $balance;
112             $this->updateAccount();
113         } else {
114             throw new BankAccountException;
115         }
116     }
117
118     /**
119      * Returns the bank account's number.
120      *
121      * @return float
122      */
123     public function getAccountNumber()
124     {
125         return $this->accountNumber;
126     }
127
128     /**
129      * Deposits an amount of money to the bank account.
130      *
131      * @param  float $balance
132      * @throws BankAccountException
133      */
134     public function depositMoney($balance)
135     {
136         $this->setBalance($this->getBalance() + $balance);
137
138         return $this->getBalance();
139     }
140
141     /**
142      * Withdraws an amount of money from the bank account.
143      *
144      * @param  float $balance
145      * @throws BankAccountException
146      */
147     public function withdrawMoney($balance)
148     {
149         $this->setBalance($this->getBalance() - $balance);
150
151         return $this->getBalance();
152     }
153
154     /**
155      * Loads account information from the database.
156      */
157     protected function loadAccount()
158     {
159         $query = "SELECT * FROM bank_account WHERE account_number = ?";
160
161         $statement = $this->pdo->prepare($query);
162
163         $statement->execute(array($this->accountNumber));
164
165         if ($bankAccountInfo = $statement->fetch(PDO::FETCH_ASSOC))
166         {
167             $this->balance = $bankAccountInfo['balance'];
168         }
169         else
170         {
171             $this->balance = 0;
172             $this->addAccount();
173         }
174     }
175
176     /**
177      * Saves account information to the database.
178      */
179     protected function updateAccount()
180     {
181         $query = "UPDATE bank_account SET balance = ? WHERE account_number = ?";
182
183         $statement = $this->pdo->prepare($query);
184         $statement->execute(array($this->balance, $this->accountNumber));
185     }
186
187     /**
188      * Adds account information to the database.
189      */
190     protected function addAccount()
191     {
192         $query = "INSERT INTO bank_account (balance, account_number) VALUES(?, ?)";
193
194         $statement = $this->pdo->prepare($query);
195         $statement->execute(array($this->balance, $this->accountNumber));
196     }
197
198     static public function createTable(PDO $pdo)
199     {
200         $query = "
201             CREATE TABLE bank_account (
202                 account_number VARCHAR(17) PRIMARY KEY,
203                 balance DECIMAL(9,2) NOT NULL DEFAULT 0
204             );
205         ";
206
207         $pdo->query($query);
208     }
209 }
210 ?>