Trick or Tip - Symfony Edition

15
Trick -or- Tip Symfony edition Dionysis Tsoumas Backend developer at Zoottle

Transcript of Trick or Tip - Symfony Edition

Trick -or- TipSymfony edition

Dionysis Tsoumas Backend developer at Zoottle

Today’s Agenda

• Better commands • Better autoloading • Better logs • Better security

and my favourite

Better Testing :)

Better Console Commands

Typical:

php app/console doctrine:schema:update —force —env=dev —no-debug

Better:

php app/console d:s:u —f

or even better..

dev d:s:u —f

where alias dev='php app/console --env=dev --no-debug'

Better autoloading

//composer.json "autoload": { "psr-0": { "": "src/" } },

Instead:

"autoload": { "psr-0": { "": "src/" } }, “autoload-dev": { "psr-0": { "": “src/ZoottleBundle/Tests/“ } },

Of course don’t forget :

composer dump-autoload —optimize

Better logs

Hide event logs//app/config/dev.yml

monolog: handlers: main: type: stream path: “%kernel.logs_dir%/%kernel.emvironment%.log” level: debug channels: “!event”

Secure Ajax Requests :)Step 1: Create a simple Twig extension

And an even simpler service

Step 2: Use it on your ajax calls $.ajax({ async: true, method: "POST", url: "{{ path('geoip') }}", data: "token={{ csrf_token() }}", …

Step 3: Validate within your controller public function customerHasCreditCardAction(Request $request) { if ($request->isXmlHttpRequest() && $this->get("ens_peewee.csrf_validator")->isValid()) { $braintree = $this->get(“ens_peewee.braintree");

… do awesome stuff …

is valid?is ajax request?

• When smoke testing

Quick functional tests

• Or when your application is decoupled enough that performance really matters

“Smoke testing is a precondition to unit and other forms of testing: if the smoke test fails there's no point in even starting up a unit test”

- some cool developer said

$client = static::createClient(array(), array('HTTP_HOST' => "panel.zoottle.dev")); $this->assertEquals(200, $client->getResponse()->getStatusCode());

This is slow!

Instead:

protected function setUp(){ $this->app = new \AppKernel(‘test’, false); $this->app->boot();}

public function testUrl($url){ $request = new Request::create($url, ‘GET’); $response = $this->app->handle($request); $this->assertEquals(200, $client->getResponse()->getStatusCode()); }

http://gnugat.github.io/2014/11/15/sf2-quick-functional-tests.html

But I need speed!https://github.com/liuggio/fastestLike Parallel — but it supports functional tests

#composer.json "require-dev": { "liuggio/fastest": "dev-master" }

However it does not work with code coverage :’(

find src/ “*Test.php” | vendor/bin/fastest “vendor/bin/phpunit -c app/ {};”

Voila! Multiple threads for your tests

Tip: use system variables to deal with multiple databases ex: getenv('ENV_TEST_CHANNEL_READABLE'); //eg. test_2

Questions?How about helping Flash on his next

presentation? Find a topic that you like and propose it for our next meetup, in

early summer!