Setting PHPUnit for symfony projects on Ubuntu

Just a copy& paste guide:

 
#install pear, phpunit
sudo apt-get install php-pear phpunit
 
#upgrade pear - this is necessary
sudo pear upgrade PEAR
 
#install phpunit
sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/PHPUnit
 

All done! Now you can run symfony tests:

 
cd vendor/symfony
php vendors.php
phpunit -c phpunit.xml.dist tests/
 

PHP wildcard matching function

Just published my PHP wildcard matching function on github - https://github.com/andrewtch/phpwildcard.

See doc there and use it like this:

 
wildcard_match('foo.*', 'foo.xy'); //true
wildcard_match('foo.?', 'foo'); //false
 

array wildcard matching is supported too:

 
wildcard_match('foo.*', array('boo.bar', 'foo.buz', 'buz.bar') // array('foo.buz')
 

AppKernel and DependencyInjection container in Symfony 2 tests

It's fairly simple to use app kernel and DI container in symfony tests, just require AppKernel.php:

 
require_once __DIR__.'/../../../../../../app/AppKernel.php';
 

and then boot your app in setUp method:

 
 
    private $container;
 
    public function setUp()
    {
        $this->app = new \AppKernel('test', true);
        $this->app->boot();
        $this->container = $this->app->getContainer();
    }
 

then, use a container where needed:

 
    public function testFoo()
    {
        $this->container->get('logger')->debug('Foo');
    }
 

Bash: how to copy permissions recursively

Imagine you were copying an, eg, virtual server from /server to /newserver, and messed permissions in /etc; thre's a way to copy permissions with this simple script:

 
#!/bin/bash
 
source=$1
target=$2
 
echo "Copy files from $source to $target"
 
cd $source
files=`find $source`
 
for file in $files
do
  #this is some bash voodoo - string part replacement
  targetFile=$target${file#$source}
  #stat command can accept -c parameter and return data in needed format
  #where %u and %g are numeric user/group ids, and %a - octal permissions
  permissions=`stat -c%a $file`
  ownership=`stat -c%u:%g $file`
  echo $targetFile to $ownership $permissions
  chmod $permissions $targetFile
  chown $ownership $targetFile
done
 

invoke this like

 
./permissions.sh /server/etc /newserver/etc
 

change %u:%g to %U:%G if you prefer string id's (www-data:www-data) more than numeric (0:0)