Skip to content

Instantly share code, notes, and snippets.

@spp
Created January 29, 2010 13:47
Show Gist options
  • Save spp/289730 to your computer and use it in GitHub Desktop.
Save spp/289730 to your computer and use it in GitHub Desktop.
How to test your code snippets in Symfony
<?php
// These following lines load the project context
require_once(dirname(__FILE__).'/config/ProjectConfiguration.class.php');
// I use 'prod' all the time, and 'dev' only when something goes wrong
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
// Generally keep 'dev', I use 'prod' all the time, 'dev' only when something goes wrong
// If you want to test just the models and business logic:
// sfContext::createInstance($configuration);
// If you want to test URL helpers and other helpers that require controller context
sfContext::createInstance($configuration)->dispatch();
// Decide whether you want to test UI / controller or just Models. If you use dispatch, it prints out a lot more
// stuff than you expect. I personally do not use dispatch unless i *have* to
// Write your test code here. This file loads the whole environment. It gives you full access to all code
// written Model / Business Logic
// As a bounty: This is the idiomatic way of writing queries in doctrine and eager-loading associations.
$users = Doctrine::getTable('User')->createQuery('u')
->where('u.first_name like ?', '%jack%')
->leftJoin('u.gists g')
->andWhere('g.public = ?', 1)
->execute()
;
foreach($users as $user){
echo $user->getFullname() . "\n";
echo $user->getAllProjects()->count() . "\n";
// Eager loaded. This will not fire another query
echo $user->gists->count();
}
$first_user = $users[0];
sfProjectConfiguration::getActive()->loadHelpers('Url', 'Custom');
echo "\n\n" . url_for('user/show?permalink=' . $first_user->permalink) . "\n\n";
$param1 = 'foo';
$param2 = 'bar';
// Call a custom function written in CustomHelper
echo myCustomFunction($param1, $param2) ;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment