Note also that if one of the functions in the class is named the same as a field in the
database, then the function will be called rather that the value of the field returned. This is handy if you want to
do some processing on a given field before passing it through to the rest of the application, though such a usecase
is rare.
5.3.1 Testing the Model
As we have mentioned before, it is good practice to test your code and it is even better practice to test it more
than once! To test the users model, I have used PHPUnit and written some tests in the tests/models directory of
the places application.
Setting up and tearing down
One key thing about running tests automatically is that each test must not interfere with another test. Also, a
given test must not depend upon a previous test having run. To achieve this separation, PHPUnit provides the
functions setUp() and tearDown() which are run just before (and after in the case of tearDown(), just after)
each test.
In order to separate each database test, we use the setUp() function to recreate the database tables and
populate them to a known state. This obviously means that we do not use the master database for testing! I
have chosen to use a different database which is configured in the main config.
Pages:
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168