Changeset 11922
- Timestamp:
- 10/03/08 09:13:12 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
doc/branches/1.1/book/15-Unit-and-Functional-Testing.txt
r11259 r11922 314 314 $autoload->addDirectory($sf_symfony_lib_dir.'/util'); 315 315 $autoload->register(); 316 317 >**TIP**318 >The generated Propel objects rely on a long cascade of classes, so as soon as you want to test a Propel object, autoloading is necessary. Note that for Propel to work, you also need to include the files under the `plugins/sfPropelPlugin/lib/vendor/propel/` directory and to add the Propel core to the include path (by calling `set_include_path($configuration->getSymfonyLibDir().'/plugins/sfPropelPlugin/lib/vendor'.PATH_SEPARATOR.$configuration->getRootDir().PATH_SEPARATOR.get_include_path()`.319 316 320 317 Another good workaround for the autoloading issues is the use of stubs. A stub is an alternative implementation of a class where the real methods are replaced with simple canned data. It mimics the behavior of the real class, but without its cost. A good example of stubs is a database connection or a web service interface. In Listing 15-7, the unit tests for a mapping API rely on a `WebService` class. Instead of calling the real `fetch()` method of the actual web service class, the test uses a stub that returns test data. … … 393 390 } 394 391 392 ### Unit testing Propel classes 393 394 Testing Propel classes is a bit more involving as the generated Propel objects rely on a long cascade of classes. Moreover, you need to provide a valid database connection to Propel and you also need to feed the database with some test data. 395 396 Thankfully, it is quite easy as symfony already provides everything you need: 397 398 * To get autoloading, you need to initialize a configuration object 399 * To get a database connection, you need to initialize the `sfDatabaseManager` class 400 * To load some test data, you can use the `sfPropelData` class 401 402 A typical Propel test file is shown in Listing 15-9. 403 404 Listing 15-9 - Testing Propel classes 405 406 [php] 407 <?php 408 409 include(dirname(__FILE__).'/../bootstrap/unit.php'); 410 411 new sfDatabaseManager(ProjectConfiguration::getApplicationConfiguration('frontend', 'test', true)); 412 $loader = new sfPropelData(); 413 $loader->loadData(sfConfig::get('sf_data_dir').'/fixtures'); 414 415 $t = new lime_test(1, new lime_output_color()); 416 417 // begin testing your model class 418 $t->diag('->retrieveByUsername()'); 419 $user = UserPeer::retrieveByUsername('fabien'); 420 $t->is($user->getLastName(), 'Potencier', '->retrieveByUsername() returns the User for the given username'); 421 395 422 Functional Tests 396 423 ---------------- doc/branches/1.2/book/15-Unit-and-Functional-Testing.txt
r11259 r11922 314 314 $autoload->addDirectory($sf_symfony_lib_dir.'/util'); 315 315 $autoload->register(); 316 317 >**TIP**318 >The generated Propel objects rely on a long cascade of classes, so as soon as you want to test a Propel object, autoloading is necessary. Note that for Propel to work, you also need to include the files under the `plugins/sfPropelPlugin/lib/vendor/propel/` directory and to add the Propel core to the include path (by calling `set_include_path($configuration->getSymfonyLibDir().'/plugins/sfPropelPlugin/lib/vendor'.PATH_SEPARATOR.$configuration->getRootDir().PATH_SEPARATOR.get_include_path()`.319 316 320 317 Another good workaround for the autoloading issues is the use of stubs. A stub is an alternative implementation of a class where the real methods are replaced with simple canned data. It mimics the behavior of the real class, but without its cost. A good example of stubs is a database connection or a web service interface. In Listing 15-7, the unit tests for a mapping API rely on a `WebService` class. Instead of calling the real `fetch()` method of the actual web service class, the test uses a stub that returns test data. … … 393 390 } 394 391 392 ### Unit testing Propel classes 393 394 Testing Propel classes is a bit more involving as the generated Propel objects rely on a long cascade of classes. Moreover, you need to provide a valid database connection to Propel and you also need to feed the database with some test data. 395 396 Thankfully, it is quite easy as symfony already provides everything you need: 397 398 * To get autoloading, you need to initialize a configuration object 399 * To get a database connection, you need to initialize the `sfDatabaseManager` class 400 * To load some test data, you can use the `sfPropelData` class 401 402 A typical Propel test file is shown in Listing 15-9. 403 404 Listing 15-9 - Testing Propel classes 405 406 [php] 407 <?php 408 409 include(dirname(__FILE__).'/../bootstrap/unit.php'); 410 411 new sfDatabaseManager(ProjectConfiguration::getApplicationConfiguration('frontend', 'test', true)); 412 $loader = new sfPropelData(); 413 $loader->loadData(sfConfig::get('sf_data_dir').'/fixtures'); 414 415 $t = new lime_test(1, new lime_output_color()); 416 417 // begin testing your model class 418 $t->diag('->retrieveByUsername()'); 419 $user = UserPeer::retrieveByUsername('fabien'); 420 $t->is($user->getLastName(), 'Potencier', '->retrieveByUsername() returns the User for the given username'); 421 395 422 Functional Tests 396 423 ----------------