Skip to content

Instantly share code, notes, and snippets.

View webmozart's full-sized avatar

Bernhard Schussek webmozart

View GitHub Profile

Symfony 2 Testing Conventions

Symfony 2 is programmatically tested using unit tests. You can read more about unit testing on Wikipedia.

Test Organization

<?php
// Entity.val.php
namespace Symfony\Components\Validator\Constraints;
$metadata->setGroupSequence(array('\Normal', '\Strict'));
$metadata->addPropertyConstraint('longitude', new NotNull(array('groups' => '\Strict')));
$metadata->addPropertyConstraint('latitude', new Size(array('min' => 10)));
$metadata->addPropertyConstraint('latitude', new Size(array('max' => 1, 'groups' => '\Normal')));
<?php
use Symfony\Components\Form\Form;
use Symfony\Components\Form\FieldGroup;
use Symfony\Components\Form\ChoiceField;
use Symfony\Components\Form\TextField;
use Symfony\Components\Form\CheckboxField;
use Symfony\Components\Form\NumberField;
use Symfony\Components\Form\PasswordField;
use Symfony\Components\Form\DoubleTextField;
Solution 1:
Constraints can be defined on the top level.
Advantages:
Little code
Disadvantage:
Min does not have a context. If multiple constraints of the same type exist,
they need to be wrapped in another annotation.
<?php
use Symfony\Foundation\UniversalClassLoader;
require_once __DIR__.'/src/Symfony/Foundation/UniversalClassLoader.php';
$loader = new UniversalClassLoader();
$loader->registerNamespace('Symfony', __DIR__.'/src');
$loader->register();
@webmozart
webmozart / configurable_vs_optionsupport.php
Created October 25, 2010 15:16
Benchmark to compare runtime Configurable with annotation-based OptionSupport behaviour
<?php
abstract class Configurable
{
private $options = array();
private $knownOptions = array();
private $requiredOptions = array();
@webmozart
webmozart / gist:730993
Created December 6, 2010 21:33
ProjectConfiguration adaptation to prevent sessions starting for bots
// avoid session starting for bots
$agents = array(
'googlebot',
'yahoo! slurp',
'baiduspider',
'sosospider',
'bingbot',
'nagios-plugins',
);
<?php
class OptionSupport
{
protected static $definitions = array();
public static function getDefinition($class)
{
if (!isset(self::$definitions[$class])) {
self::$definitions[$class] = new OptionDefinition($class);
<?php
class AuthorForm extends Form
{
protected function configure()
{
$this->setDataClass('Application\Entities\Author');
// 1. problem