summaryrefslogtreecommitdiff
path: root/demos/time-tracker/protected/pages/Docs/CreateBusinessCode.page
blob: e5afa572854c7b8c91588687bcb12e52a18f6de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<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">
&lt;?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 = '';
}
?&gt;
</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">
&lt;?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);
	}
}
?&gt;
</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">
&lt;?php
class BaseDao
{
	private $_connection;
	
	public function setConnection($connection)
	{
		$this->_connection = $connection;
	}
	
	protected function getConnection()
	{
		return $this->_connection;
	}
}
?&gt;
</com:TTextHighlighter>
<p>And finally our <tt>ProjectDao</tt> class.</p>
<com:TTextHighlighter Language="php" CssClass="source">
&lt;?php
Prado::using('Application.APP_CODE.BaseDao');
class ProjectDao extends BaseDao
{
}
?&gt;
</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>