14.10. Tests

A test class includes methods that target logic provided by controllers, models, etc., and applies assertions to verify that the logic works as intended. Perforce Chronicle was built with an extensive test suite that uses PHPUnit.

To add PHPUnit tests for your own module, create a tests folder under your module's folder and include the following files:

  1. A phpunit.xml file located in the tests folder. Here is an example for the Foo module:
    <phpunit bootstrap="../../../../../tests/phpunit/TestBootstrap.php">
     <testsuites>
      <testsuite name="Foo Module">
       <directory>.</directory>
      </testsuite>
     </testsuites>
    </phpunit>
    
  2. A test file for the functionality that you want to test. Here is an example test file that exercises the Foo module's index controller:
    <?php
    /**
     * Description of tests for Foo_IndexController
     *
     * @copyright   copyright info
     * @license     license info
     * @version     version info
     */
    class Foo_Test_IndexControllerTest extends ModuleControllerTest
    {
        /**
         * Runs before each test method
         */
        public function setUp()
        {
        }
    
        /**
         * Runs after each test method
         */
        public function tearDown()
        {
        }
    
        /**
         * Test indexAction
         */
        public function testIndex()
        {
            $this->utility->impersonate('administrator');
    
            $this->dispatch('/foo');
            $this->assertModule('foo');
            $this->assertController('index');
            $this->assertAction('index');
        }
    }
    

To run all tests, open a terminal, change directories to the CMSDIR/tests/phpunit directory and issue the phpunit command.

Perforce Chronicle - Release: 2012.2/486814