sfPhpunitPlugin - 1.0.4

symfony Phpunit plugin

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

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/Doctrine extended classes.

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
    
  • Enable Plugin (Only for Symfony 1.2 and above)

    Modify config/ProjectConfiguration.class.php

    public function setup()
      {
        // for compatibility / remove and enable only the plugins you want
        $this->enableAllPluginsExcept('sfDoctrinePlugin');
     
        // or
     
        $this->enablePlugins(array('sfPropelPlugin', 'sfPhpunitPlugin'));
        $this->disablePlugins(array('sfDoctrinePlugin'));
     
      }        
  • Create all tests (Propel)

    $ symfony phpunit:create application
    

    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

    The default database connection will be propel. You can change it by adding -c myConn

  • Create all tests (Doctrine)

    $ symfony phpunit:create application --type=doctrine
    

    This command will go over the lib/model/doctrine 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

    The default database connection will be doctrine. You can change it by adding -c myConn

  • Create tests for additional/other model directories

    $ symfony phpunit:create application --model_path=apps/admin/lib
    
  • Create tests for a single model Class

    $ symfony phpunit:create application --model=ClassName
    
  • 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

<?php
require_once 'PHPUnit/Framework.php';
 
define( 'SF_APP_NAME', 'admin' );
define( 'SF_ENV', 'test' );
define( 'SF_CONN', 'propel' );
 
if ( SF_APP_NAME != '' )
{
    require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
    AllTests::$configuration = ProjectConfiguration::getApplicationConfiguration( SF_APP_NAME , SF_ENV, true);
    sfContext::createInstance(AllTests::$configuration);
}
 
class AllTests
{
    public static $configuration = null;
    public static $databaseManager = null;
    public static $connection = null;
 
    protected function setUp()
    {
 
        if ( self::$configuration )
        {
            // initialize database manager
            self::$databaseManager = new sfDatabaseManager(self::$configuration);
            self::$databaseManager->loadConfiguration();
 
            if ( SF_CONN ) self::$connection = self::$databaseManager->getDatabase( SF_CONN );
        }
 
    }
 
    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 );
 
            }
            $dir->next();
        }        
 
        return $suite;
    }
}
?>

Example of Test Class File

<?php
 
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->markTestIncomplete(
          'This test has not been implemented yet.'
        );
    }    
 
    public function testUpdateUse()
    {
       $this->markTestIncomplete(
          '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.
  • file_table_template.tpl - Template for generating the individual test class files for Doctrine table classes.
  • method_template.tpl - Template for each test method inside the test class file.

TODO

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