README 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. = Running The Propel Unit Tests =
  2. == Background ==
  3. Propel uses [http://www.phpunit.de PHPUnit 3.9] to test the build and runtime frameworks.
  4. You can find the unit test classes and support files in the [browser:branches/1.4/test/testsuite] directory.
  5. == Install PHPUnit ==
  6. In order to run the tests, you must install PHPUnit, PEAR:Log, and Phing:
  7. {{{
  8. > pear channel-discover pear.phpunit.de
  9. > pear install phpunit/PHPUnit-3.3.9
  10. }}}
  11. {{{
  12. > pear channel-discover pear.phing.info
  13. > pear install phing/phing-2.3.3
  14. }}}
  15. {{{
  16. > pear install log
  17. }}}
  18. Tip: The latest release of PHPUnit (3.4) is not totally BC with the 3.3, and doesn't have a Phing adapter yet. That's why the Propel unit tests still use PHPUnit version 3.3.
  19. == Configure the Database to be Used in the Tests ==
  20. You must configure both the generator and the runtime connection settings.
  21. {{{
  22. // in test/fixtures/bookstore/build.properties
  23. propel.database = mysql
  24. propel.database.url = mysql:dbname=test
  25. propel.mysqlTableType = InnoDB
  26. propel.disableIdentifierQuoting=true
  27. # For MySQL or Oracle, you also need to specify username & password
  28. propel.database.user = myusername
  29. propel.database.password = p@ssw0rd
  30. }}}
  31. {{{
  32. // in test/fixtures/bookstore/runtime-conf.xml
  33. <datasource id="bookstore">
  34. <!-- the Propel adapter to use for this connection -->
  35. <adapter>mysql</adapter>
  36. <!-- Connection parameters. See PDO documentation for DSN format and available option constants. -->
  37. <connection>
  38. <classname>DebugPDO</classname>
  39. <dsn>mysql:dbname=test</dsn>
  40. <user>myusername</user>
  41. <password>p@ssw0rd</password>
  42. <options>
  43. <option id="ATTR_PERSISTENT">false</option>
  44. </options>
  45. <attributes>
  46. <!-- For MySQL, you should also turn on prepared statement emulation,
  47. as prepared statements support is buggy in mysql driver -->
  48. <option id="ATTR_EMULATE_PREPARES">true</option>
  49. </attributes>
  50. <settings>
  51. <!-- Set the character set for client connection -->
  52. <setting id="charset">utf8</setting>
  53. </settings>
  54. </connection>
  55. </datasource>
  56. }}}
  57. == Build the Propel Model and Initialize the Database ==
  58. {{{
  59. > cd /path/to/propel/test
  60. > ../generator/bin/propel-gen fixtures/bookstore main
  61. > mysqladmin create test
  62. > ../generator/bin/propel-gen fixtures/bookstore insert-sql
  63. }}}
  64. **Tip**: To run the unit tests for the namespace support in PHP 5.3, you must also build the `fixtures/namespaced` project.
  65. == Run the Unit Tests ==
  66. Run all the unit tests at once using Phing:
  67. {{{
  68. > cd /path/to/propel/test
  69. > phing -f test.xml -verbose
  70. }}}
  71. '''Tip''': The `-verbose` option will force the display of PHP notices, which are hidden by default.
  72. To run a single test, specify the classname (minus 'Test' ending) on the commandline, using the `test` property. For example to run only GeneratedObjectTest:
  73. {{{
  74. > phing -f test.xml -verbose -Dtest=GeneratedObject
  75. }}}
  76. Tip: If you want to set up custom Phing properties for your unit tests, create a `test.properties` file inside the main `test/` directory. Phing will automatically try to load it if it exists.
  77. == How the Tests Work ==
  78. Every method in the test classes that begins with 'test' is run as a test case by PHPUnit. All tests are run in isolation; the `setUp()` method is called at the beginning of ''each'' test and the `tearDown()` method is called at the end.
  79. The [browser:branches/1.4/test/tools/helpers/bookstore/BookstoreTestBase.php BookstoreTestBase] class specifies `setUp()` and `tearDown()` methods which populate and depopulate, respectively, the database. This means that every unit test is run with a cleanly populated database. To see the sample data that is populated, take a look at the [browser:branches/1.4/test/tools/helpers/bookstore/BookstoreDataPopulator.php BookstoreDataPopulator] class. You can also add data to this class, if needed by your tests; however, proceed cautiously when changing existing data in there as there may be unit tests that depend on it. More typically, you can simply create the data you need from within your test method. It will be deleted by the `tearDown()` method, so no need to clean up after yourself.
  80. == Writing Tests ==
  81. If you've made a change to a template or to Propel behavior, the right thing to do is write a unit test that ensures that it works properly -- and continues to work in the future.
  82. Writing a unit test often means adding a method to one of the existing test classes. For example, let's test a feature in the Propel templates that supports saving of objects when only default values have been specified. Just add a `testSaveWithDefaultValues()` method to the [browser:branches/1.4/test/testsuite/generator/engine/builder/om/php5/GeneratedObjectTest.php GeneratedObjectTest] class, as follows:
  83. {{{
  84. #!php
  85. <?php
  86. /**
  87. * Test saving object when only default values are set.
  88. */
  89. public function testSaveWithDefaultValues() {
  90. // Relies on a default value of 'Penguin' specified in schema
  91. // for publisher.name col.
  92. $pub = new Publisher();
  93. $pub->setName('Penguin');
  94. // in the past this wouldn't have marked object as modified
  95. // since 'Penguin' is the value that's already set for that attrib
  96. $pub->save();
  97. // if getId() returns the new ID, then we know save() worked.
  98. $this->assertTrue($pub->getId() !== null, "Expect Publisher->save() to work with only default values.");
  99. }
  100. ?>
  101. }}}
  102. Run the test again using the command line to check that it passes:
  103. {{{
  104. > phing -f test.xml -Dtest=GeneratedObject
  105. }}}
  106. You can also write additional unit test classes to any of the directories in `test/testsuite/` (or add new directories if needed). The Phing task will find these files automatically and run them.