For testing our models, we want to ensure that the database is in a known state for every test. I have
written a separate function, setupDatabase(), that does this for us. It is not part of this class as it will be used
for testing more than just the Places model. Our initial test, testFetchAll(), does a simple sanity check to ensure
that everything is working. We collect all the places in our test database and count them. In this case we are
expecting three places and that??™s what we get as shown in Figure 3.7.
Figure 3.7: PHPUnit is run from the command line. This run is successful as all tests passed.
In order for our test case to work, and to support multiple test case classes, we will organize our tests into
two suites, one for models and one for controllers. We will group these suites into an encompassing suite, so
that we can run all the tests with the simple command phpunit AllTests.php. A certain amount of
initialization is required, and so we will put this into a separate file called TestConfiguration.php as shown in
Figure 3.8.
Figure 3.8: The unit tests are divided into controllers and models. The AllTests.php scripts allow each to be run independently if
required.
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.
Pages:
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