sfPhpunitPlugin - 0.0.1

symfony Phpunit plugin

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
Show source

sfPhpunit plugin

The sfPhpunitPlugin is a symfony plugin that creates stub classes for PHPUnit of classes in lib/model (and other directories).

Symfony provides a testing framework, but many times it is necessary to use an existing PHPUnit installation. This plugin helps creating the files necessary for PHPUnit to work with Symfony classes like Propel extended classes.

By default, sfPhpunitPlugin does not create test methods for methods on the base classes of the generated model. It only creates test methods for the custom methods added by the developer in the classes in lib/model.

sfPhpunitPlugin also creates an AllTests.php class file that can be used by PHPUnit to run all tests. This created file will include the creation of the configuration object with the provided application name and create a connection to the database so propel based objects can be tested.

Installation & Usage

  • Install the plugin

    $ symfony plugin:install sfPhpunitPlugin
  • Create all tests

    $ symfony phpunit:create admin

    This command will go over the lib/model directory and for each .php file it will create a fileTest.php in test/model/.

    It will also create a test/AllTests.php that can be used to run all tests with symfony phpunit:testall or phpunit test/AllTests.php

  • Create tests for additional/other model directories

    $ symfony phpunit:create admin --model_path=apps/admin/lib
  • Run all tests

    $ symfony phpunit:testall

    This is similar to running phpunit test/AllTests.php

  • Help of all arguments and options

    $ symfony help phpunit:create

Example of AllTests.php


require_once 'PHPUnit/Framework.php';

define( 'SF_APP_NAME', 'admin' );
define( 'SF_ENV', 'test' );

if ( SF_APP_NAME != '' )
    AllTests::$configuration = ProjectConfiguration::getApplicationConfiguration( SF_APP_NAME , SF_ENV, true);

class AllTests
    public static $configuration = null;
    public static $databaseManager = null;

    protected function setUp()

        if ( self::$configuration )
            // initialize database manager
            self::$databaseManager = new sfDatabaseManager(self::$configuration);


    public static function suite()
        $suite = new PHPUnit_Framework_TestSuite('PHPUnit');

        $dir = new DirectoryIterator( dirname(__FILE__). '/model' );

        while($dir->valid()) {
            if( strpos( $dir, 'Test.php' ) !== false ) {
                $suite->addTestFile(  dirname(__FILE__). '/model/'. $dir );


        return $suite;


Example of Test Class File


require_once 'PHPUnit/Framework.php';

class PinTest extends PHPUnit_Framework_TestCase
     * Pin
     * @var Pin
    protected $o;

    public function setup()
        $this->o = new Pin();

    public function testIsValid()
          'This test has not been implemented yet.'

    public function testUpdateUse()
          'This test has not been implemented yet.'



Customize sfPhpunitPlugin generated class files

By default, sfPhpunitPlugin comes with a set of templates to generate the stub class files, these are located inside the data plugin directory:

  • alltests_template.tpl - Template for generating AllTests.php file.
  • file_template.tpl - Template for generating the individual test class files.
  • method_template.tpl - Template for each test method inside the test class file.


  • add support to run individual tests
  • Contact me for any other requirements/ideas