<com:TContent ID="body"> <h1>Create Business Code</h1> <p>We start the design with the database, the entity relationships are shown in the diagram below.</p> <img src=<%~ db.png %> class="figure" /> <p>Now we can begin to create and test some business code. Let us begin with the <tt>Project</tt> defintions. First, we add some properties or fields.</p> <com:TTextHighlighter Language="php" CssClass="source"> <?php class Project { public $ActualDuration = 0; public $CreatorUserName = ''; public $CompletionDate = 0; public $DateCreated = 0; public $Description = ''; public $EstimateDuration = 0; public $ID = 0; public $ManagerUserName = ''; public $Name = ''; } ?> </com:TTextHighlighter> <p>All the fields should be self explainatory. The <tt>ManagerUserName</tt> is the user name of the project manager. Notice that the fields are public, later on, we can change some or all of them to be private and provide some accessor and mutators (i.e. getters and setters). If we want we can let the <tt>Project</tt> class inherit <tt>TComponent</tt> such that the getters and setters can be used like properties, such as those found in Prado.</p> <h2>Business Services, test case first</h2> <p>Next we want to add users to the project. For this, we start with some unit tests. We are going to design the business logic around the concept of <a href="java">Data Access Objects</a> (DAO).</p> <com:TTextHighlighter Language="php" CssClass="source"> <?php Prado::using('Application.APP_CODE.*'); class ProjectDaoTestCase extends UnitTestCase { function testProjectDaoCanCreateNewProject() { $project = new Project(); $project->Name = "Project 1"; $dao = new ProjectDao(); $this->assertTrue($dao->createNewProject($project)); $this->assertEqual($dao->getProjectByID(1), $project); } } ?> </com:TTextHighlighter> <p>So what are we doing here? First we create a new <tt>Project</tt> named "Project 1". Then we create a new <tt>ProjectDao</tt> so we can insert new projects and retrieve it. We assert that a project will be create sucessfully using <tt>assertTrue($do->createNewProject(...))</tt>. We also assert that <tt>getProjectByID(1)</tt> will return an instance of <tt>Project</tt> class with same data (the reference may be different).</p> <p>If we run the above unit test case, nothing is going to pass since we have not even defined the <tt>ProjectDao</tt> class. So lets do that first and import the class in the tests as well.</p> <p>We will create a base Dao class as follows, and we save as <tt>BaseDao.php</tt> in our <tt>APP_CODE</tt> directory.</p> <com:TTextHighlighter Language="php" CssClass="source"> <?php class BaseDao { private $_connection; public function setConnection($connection) { $this->_connection = $connection; } protected function getConnection() { return $this->_connection; } } ?> </com:TTextHighlighter> <p>And finally our <tt>ProjectDao</tt> class.</p> <com:TTextHighlighter Language="php" CssClass="source"> <?php Prado::using('Application.APP_CODE.BaseDao'); class ProjectDao extends BaseDao { } ?> </com:TTextHighlighter> <p>If we run the unit test again, we get an error message something like "<i>Fatal error: Call to undefined method ProjectDao::createNewProject() in ...</i>". So let us, fix this. </p> <com:TTextHighlighter Language="php" CssClass="source"> class ProjectDao extends BaseDao { public function createNewProject($project) { } public function getProjectByID($projectID) { } } </com:TTextHighlighter> <p>Run the unit test again, we see a different type of error now. This time the unit test runner complians that the test fails.</p> <h2>Write Test, Run Test, Repeat</h2> <div class="info"> <b>At this point, you may notice a repetition</b> <ol> <li>write some test code,</li> <li>run the test, test gives an error,</li> <li>write some actual code to only remove the previous error,</li> <li>goto step 1 to repeat.</li> </ol> </div> <p>We shall see how we can make this test pass in the next section.</p> </com:TContent>