Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^114) CHAPTER 8 ■ TESTING, DEPLOYMENT, AND CONTINUOUS INTEGRATION
The setUp() and tearDown() methods are called with each test and on a clean instance of
the test class, so your setup code will not persist between tests and can be expected not to leak
state between each test.
You can also create a test suite that runs multiple test cases, as shown in Listing 8-4.
Listing 8-4. A Suite of Tests
<?php
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'DemoTest.php';
class AllTests {
public static function main() {
PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite() {
$suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend');
$suite->addTestSuite('DemoTest');
return $suite;
}
}
To execute the suite, just use the AllTests command:



phpunit AllTests
PHPUnit 3.1.9 by Sebastian Bergmann.
..
Time: 0 seconds
OK (2 tests)
Now that you have a couple of unit tests and a utility class, create an index.php document
to use your Demo class, as shown in Listing 8-5.
Listing 8-5. Using the Demo Class (code/index.php)
<?php
require_once('Demo.php');
$demo = new Demo();
echo "Two plus Two = ". $demo->sum(2,2);
Next, run svn status, and then add any files or folders that are denoted with a ?. Finally,
run svn commit. You now have a complete web site under unit testing and revision control. The
next step is to automate the deployment of these files.
McArthur_819-9C08.fm Page 114 Friday, February 22, 2008 9:06 AM


Free download pdf