A typical Developer Blog
by Gordon Franke
Icon

How can i override user attribute in my functional test?

Some times your code depend on a user attribute, so you want set the user attribute in your functional test.

The Problem is that the doCall method in the sfBrowser class rebuild the context and in the end calls theshutdown method from user and storage object to write the session data.

So you must create your own Browser class and override the doCall function and simple move the shutdown calls to the beginning.

Browser Class:

class myBrowser extends sfBrowser
{
  /**
   * Calls a request to a uri.
   */
  protected function doCall()
  {
    // manually shutdown user to save current session data
    if (isset($this->context) AND $this->context->getUser())
    {
      $this->context->getUser()->shutdown();
      $this->context->getStorage()->shutdown();
    }
 
    // recycle our context object
    $this->context = $this->getContext(true);
 
    sfConfig::set('sf_test', true);
 
    // we register a fake rendering filter
    sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
 
    $this->resetCurrentException();
 
    // dispatch our request
    ob_start();
    $this->context->getController()->dispatch();
    $retval = ob_get_clean();
 
    // append retval to the response content
    $this->context->getResponse()->setContent($retval);
  }
}

Test code:

include dirname(__FILE__).'/../../bootstrap/functional.php';
 
$browser = new sfTestFunctional(new myBrowser());
 
$browser->
  get('/site-one');
 
// override the session data
$browser->getUser()->setAttribute('key', $value);
 
$browser->
  get('/site-one');

Simple script to find missing translation strings

How can i use field credentials for symfony admin generator forms?

When you would restrict the display of a field or fieldset group on user credential. you must do the following

1. open the action class and add

public function preExecute()
{
  parent::preExecute();
 
  $this->configuration->setUser($this->getUser());
}

2. open the *GeneratorConfiguration class and add

protected $user;
 
public function setUser($user)
{
  $this->user = $user;
}
 
public function getUser()
{
  return $this->user;
}
 
public function getEditDisplay()
{
  $fieldsets = parent::getEditDisplay();
  if (!$this->getUser()->hasCredential('admin'))
  {
    unset(
      $fieldsets['Auth'][2], // field
      $fieldsets['Rights']   // field group
    );
  }
 
  return $fieldsets;
}
 
public function getNewDisplay()
{
  ... // see getEditDisplay
}
 
public function getFormOptions()
{
  return array('user' => $this->getUser());
}

3. last remove the fields from the form, open the *Form class and add

public function configure()
{
  ...
  if (!$this->getOption('user')->hasCredential('admin'))
  {
    unset($this['is_super_admin'], $this['groups_list'], $this['permissions_list']);
  }
  ...
}

How can i automatic add ids to my trans-unit nodes in my XML Xliff Tanslation files?

When you validate you XML Xliff file you have often the problem that a trans-unit node has no or a already existing id. So i have wrote a small script for that problem.

fix_id.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
$doc = new DOMDocument();
$doc->load($argv[1]);
 
$xpath = new DOMXPath($doc);
 
$query = '//trans-unit';
 
$entries = $xpath->query($query);
foreach ($entries as $i => $entry) {
    $entry->setAttribute('id', $i+1);
}
 
$doc->save($argv[1]);

Execute the following command in your symfony1 root directory ;)

for FILE in `find apps/*/i18n -name *\.xml`; do php fix_id.php $FILE; done;

How can i simple mark all required form fields in symfony 1.3?

Create a function in your BaseForm class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static function listenToPostConfigure($event)
{
  $form            = $event->getSubject();
  $widgetSchema    = $form->getWidgetSchema();
  $validatorSchema = $form->getValidatorSchema();
 
  $fields = $form->getFormFieldSchema()->getWidget()->getFields();
  foreach ($fields as $key => $object)
  {
    $label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
    if (isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true)
    {
      $title  = $key . '_field_is_required';
      $label .= '<sup>getFormFormatter()->translate($title) . '">*</sup>';
    }
    $widgetSchema->setLabel($key, $label);
  }
}

Add a listener to the form.post_configure event with your function

1
2
$dispatcher = $this->getEventDispatcher();
$dispatcher->connect('form.post_configure', array('BaseForm', 'listenToPostConfigure'));

How can i change the netbeans color schema to look like symfony documentations?

Simple extract this file into your netbeans-6.8/nb6.8 folder:
SymfonyColorScheme.zip

Thanks Ze Technology for the color schema

http://www.ze-technology.com/2009/12/11/netbeans-aux-couleurs-de-symfony/

Change from Doctrine to Doctrine_Core with one command

To change all your code from Doctrine to Doctrine_Core you can use the following command

1
for fl in `find apps/ config/ lib/ test/ -name *.php`; do mv $fl $fl.old; sed 's/Doctrine::/Doctrine_Core::/g' $fl.old > $fl; rm -f $fl.old; done

When you have a plugin you can use this command

1
for fl in `find . -name *.php`; do mv $fl $fl.old; sed 's/Doctrine::/Doctrine_Core::/g' $fl.old > $fl; rm -f $fl.old; done

How can i remove all generated symfony base files automatically from svn?

call this from your project root

for DIR in `find lib -name base -type d`; do svn propset svn:ignore base $DIR/..;svn rm $DIR; done;

How can i use embed forms or simgle form fields depend on a form field?

You have a register form with a field account_type and two embed forms Company and Address.

You want remove the embed Company form, when the field account_type is sfGuardUserProfile::TYPE_PRIVATE otherwise you want remove the embed Address form.

The solution is to override the bind method in your form.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    // unset request param and validator
    if($taintedValues['account_type'] == sfGuardUserProfile::TYPE_PRIVATE)
    {
      unset(
        $taintedValues['Company'],
        $this->validatorSchema['Company']
      );
    }
    else
    {
      unset(
        $taintedValues['Address'],
        $this->validatorSchema['Address']
      );
    }
 
    parent::bind($taintedValues, $taintedFiles);
  }

How can i check the count of database queries in my unit test?

1
2
3
$manager    = new sfDatabaseManager($configuration);
$connection = $manager->getDatabase('doctrine')->getDoctrineConnection();
$connection->count();