BC Detection And Locking With Composer!

Background: It is often the case that our project has dependencies under development. If a new version of a dependency is released on packagist and introduces a BC break that you are not aware of then things can happen. You don’t want to become aware of this problems after you deploy!

Breaks of your application due to these dependency BC breaks can be detected writing good integration tests. In addition, you want to find out when these BC breaks were introduced and lock your good versions right before.

Here we show you an example on how to tackle this problem. We will use a silex app called Fazer with with a package dependency found in packagist, namely TodoList (cordoval/todolist).

First instantiate the project:

composer create-project cordoval/fazer /path/to/project/fazer
cd /path/to/project/fazer

Second copy bisect.sh script to a location outside of the project. The reason why copying the latest version is that you may have versioned this script and earlier versions could be different. To measure whether a version is good or not we need a fix standard rather than a moving target.

~/path/to/project/fazer$ cp bisect.sh ../bisect.sh

Next, start the BC detection:

~/path/to/project/fazer$ git bisect start

Next mark the current commit as bad (project shows already broken == tests fail):

~/path/to/project/fazer$ git bisect bad

Next mark the last known good commit (using good criteria and memory):

~/path/to/project/fazer$ git bisect good ee55ec2

Next run the script to bisect and find the last good commit right before the BC dependency break:

~/path/to/project/fazer$ git bisect run ../bisect.sh

The script bisect.sh will install cleanly the dependency versions at the time the commit was current in order to find when was the last time that everything worked ok. It will use composer for this and will run after every install the tests provided as shown here:

rm -rf vendor
rm composer.lock
composer install
phpunit -c phpunit.xml.dist

The process will start:

running ../bisect.sh
Installing dependencies
  - Installing pimple/pimple (dev-master)
    Cloning 86cae5c9e85cd02f1703c774ddf12f2448a5938c
PHPUnit 3.6.4 by Sebastian Bergmann.
Configuration read from /home/cordoval/sites-2/fazer/phpunit.xml.dist
Time: 0 seconds, Memory: 5.25Mb
OK (1 test, 2 assertions)
Bisecting: 0 revisions left to test after this (roughly 0 steps)

And will finish finding the last good commit:

running ../bisect.sh
Installing dependencies
  - Installing pimple/pimple (dev-master)
    Cloning 86cae5c9e85cd02f1703c774ddf12f2448a5938c
PHPUnit 3.6.4 by Sebastian Bergmann.
Configuration read from /home/cordoval/sites-2/fazer/phpunit.xml.dist
Time: 0 seconds, Memory: 4.75Mb
There was 1 error:
1) Fazer\Tests\FazerTest::testBisect
Missing argument 2 for Fazer\TodoList::__construct(), called in /home/cordoval/sites-2/fazer/src/controllers.php on line 36 and defined
Tests: 1, Assertions: 0, Errors: 1.
9235d39bdeb0317d4f098bb4f8300f0a7f4e2920 is the first bad commit
commit 9235d39bdeb0317d4f098bb4f8300f0a7f4e2920
Author: Luis Cordova <cordoval@gmail.com>
Date:   Sat Jun 30 16:52:21 2012 -0500
    update vendors
:100644 100644 42534fd95ab08a782f1df71af131a00f515f2427 c8aed6e4d61aae84b8e8693f348fbb6519f85941 M	composer.json
bisect run success

Finally issue a `bisect reset` to go back to your former branch and continue work to do the corresponding fixes.

If you have reached this far, congratulations! I hope it was useful.
I thank the Lord Jesus for His undeserved grace given to me so far for enjoying leaning these things. Thanks Raul for hanging out.
Again if you would like to support me to attend symfonyLive event on San Francisco and this blog post has helped you please do consider donating towards the fund raising of $1800 for the air ticket.
I would really appreciate your help!

More blog posts coming! Please tweet to your friends and spread the word!

Kiss Your git clone’s Good-bye!: Part I

You heard me right, get ready to kiss your git clone’s good-bye with composer. If before you were accustomed to:

git clone git://github.com/username/repoName.git .

Then you would usually run more commands to get actually setup. And guess what, we don’t have time and we don’t have enough info for that. As PHP devs we are to get things done fast and in the best elegant way.
No more. Composer is here!

Sometimes we ignore that composer is all we need to start in the world of PHP. Today we are going to talk about the create-project command found in composer. I will assume you have a composer dev version installed on another folder and symlinked to /usr/bin/composer. The ability to bootstrap with dependencies ready a complete skeleton application lies into one command now thanks to this composer command. As long as there is a package registered in packagist.org with the ability to setup a project with dependencies of any kind you can just tell composer to ready it for you. The example could be a silex application:

composer create-project fabpot/silex-skeleton

Thankfully @fabpot took the job to create a sample project skeleton for silex. Let’s remember that one thing is the framework or microframework and another different is the folders, organization, bootstrap files, that carry along with a proper application using those frameworks. For the case of symfony, don’t worry guys, we have know it all along just we did not look hard enough:

composer create-project symfony/framework-standard-edition

You really name it, as long as there was someone careful enough to prepare a bootstrapped skeleton for that particular lib or framework you love, there will be an easy 1 punch setup.
Furthermore, if you were looking for a particular version you can do:

composer create-project symfony/framework-standard-edition sf21beta1Dir v2.1.0-BETA1

If you want to go straight to the source you could do right on the same directory bootstrap in a php-flow subdirectory the project Bergie’s nice project PHPFlow like this:

composer create-project PhpFlo/PhpFlo php-flow --dev --prefer-source

Note: If create-project uses a source install, you generally want to delete the .git folder and init a clean git repo for your project. (thanks @stof)

Finally, it is in the responsibility of the package maintainer to work on composer post install hooks. This is the information that will make the development faster and easier. Using those post install scripts is not exactly easy but it can be done. This will benefit anyone coming and just doing work on a completely setup environment!

If you enjoy this blog post please consider donating. I am fund raising 1,800$ for my ticket to SymfonyLive 2012 in San Francisco.
If you want to help me go there, or would like to meet me, I would love to meet you there and hangout with the community. Please help me get there! Thanks!

I thank the Lord Jesus for His grace so far on learning symfony2 and for the opportunity that is opening to attend my first SymfonyLive event. I am also thankful to KnpLabs, happy developers! for helping me with the conference ticket!
You guys rock!

Interesting saying

“uma coisa é sempre perceptível nos eventos de TI. aqueles q reclamam q o mercado paga mal, q n ganham o q merecem, nunca estão lá.” Jean Pimentel

“one thing is always felt on IT events. those who claim the job market pays are low, that they don’t make what they deserve, never show up.” Jean Pimentel

“una cosa que siempre se siente en los eventos de IT es que aquellos que dicen que la paga en el mercado laboral es baja que ellos no ganan lo que merecen nunca atienden.” Jean Pimentel

SFCON’2012 Conference Report

The conference was a well attended ~60+ conference on symfony2. More people tried to register however there was a limitation already fixed due to lack of bigger facilities. The first conference in Latin America on Symfony2 was held this past weekend on Bello Horizonte, Brasil. Bello Horizonte had the best weather I have ever seen with green surroundings, my idea of it is a spread out city with one main center and 12-14 districts. Downtown is busy, people all over like to party and drink beer and coffee and go to the market to eat and eat out at restaurants too. I rather like the more simple view of the districts which resemble the life in province back in Peru. People is simple and kind, they have problems with education and opportunities for the poorest. Food is good, I liked beans, rice, and mixtures of vegetables, not so much pork, or the grease in the meat, but they have an excellent meat and sort of juices. I thank God for such a great weather I enjoyed for few days, it felt like Napa, California but with the district’s people touch from province. Could go back at anytime, the only impediment is the price of the tickets.

The conference started early on Saturday:

Desenvolvimento de aplicações PHP utilizando o Symfony 2
This one was given by Hugo Magalhães. It was an introduction to symfony2 which tried to be a warming up for those who were not using symfony2 already.
There was a mention of the cmf but not much detail. The good thing mentioned was a slight argument symfony2 vs drupal from a developer pov.

Doctrine 2 – Camada de persistência para PHP
This one was given by Fabio Silva. This was my first live introduction to doctrine. I learned some specifics about configuring cache mechanism for the ORM layer. Also was explained annotations and there were more things to review. There was not much details on more annotation arguments beyond the basic one but the explanations were good and simple. I did not know doctrine was actually based on Active Record. Also I answered a question on the origins of doctrine’s specification: JPA and won a doctrine t-shirt. One interesting thing was the concept of concrete table inheritance, single table inheritance, and class table inheritance. Also there was a quick mention on doctrine’s DQL and performance. The more one hears details on Doctrine the more one learns what one is already using unknowingly, it is so neat.

Silex – Talk is cheap, show me the code!
Jean was good and clear explaining the reason for silex. He showed code on most of his slides which made it very insightful and clear. I am looking forward to put here a link to the silex code on github or to his slides. The talk introduced details on silex I did not know like the after and before methods and also some mention on Session Provider. He was the first one to show something of composer as silex works well with it. Also there was methods to validate and a word on the importance of priorities for the method calls.

Conceitos de engenharia reversa aplicados na migração de sistemas legados Symfony 1.x para Symfony 2.x.
After lunch there came this talk. Guilherme Veras knows a lot more than what he presented but he was limited to talk more details on symfony2 by time and scope. I become sleepy after lunch and when the talk is about symfony1.4, however it was good to unwillingly learn some details about it because that gives me more of a proper view of what has been changed and improved. The reversing engineering did not mention the bundle to transfer 1.4 to 2 apps, however it was insightful in that there is a market for migrating old app from clients to latest. I think that recipes are good however they probably represent the 3% of the project advance. Sorry I was not very much brought in because lack of strength.

Inversão de Controle e Injeção de Dependência no Symfony 2 – E faça-se a luz!
I liked this talk for the reason that I am interested on this topic. Welington is a good friend and he went ahead and showered all DI information I can hardly find on the documentation. The example of the fish in a bowl and chest made it all very easy to follow. He had a fast accent but thanks to his “Belleza!” at the end I was able to catch up on the explanations. Welington seems very rounded and even though I cannot find anything on his github account I can at least see he has its eyes in good stuff. About the DI the explanation even touched on advance things such as scopes. Even though he went that far to explain DI that far, I felt like the Extensions concept and operation on containers was left out. The talk was on DI standlone so it was very good. Maybe there is a place to do a talk on DI + Symfony2. The thing that was very well laid out was the purpose of DI and when it should be used. I hope we get in the following days links to the code on the talk and his slides which well serve as a documentation grade intro to DI. Belleza!

TDD com Symfony 2
Augusto is a nice gentleman, very generous and real. He is one of the top devs in Brasil and very keen to explain newbies about his findings and experiences (at least the ones he can tell). Btw he is looking for a job and I just hope he finds a very core dev job with the best pay, he is a total asset for the real companies out there on PHP. I would hire him if I were you :D. About the lecture it was exactly what I expected besides one thing. In the talk he conveyed a full coded explanation on how to do TDD the right way, the basic concepts and why one does what. The code was a class Task which had injected PDO-like layer with some API methods. The meat was seeing the thought process live and the phpunit dotted screen everytime it failed or passed. The catch phrase was TDD is a process in which we better get accustomed to. Another take on his talk is the way about mocking one service/class one by one on the chain until we truly finish. This is probably the hardest step for when writing tests. Remember: “Tests ain’t gonna write themselves!”. He also confirmed this to me in that even when we end up testing a simple if statement after mocking the last service, it is all worth it and we should do it. Another idea that was bounced between us it was that in order to mock well one has to know the boundaries of surrounding services/classes well enough or the nature of the messages being passed at least.
The critique comes after I got off the plane and looked at the title again. I thought it was symfony2 TDD :D. Well, Augusto is very knowledgeable and have used symfony2 and knows it ok even to use his components on Respect project, the problem is that he did not feel perhaps very confident on doing TDD with symfony2. It is like someone that knows a lot and he only talks about something when he is totally twice the expert on. Anyway, it is cool, I am sure he will rock it in the next lecture with Symfony2 and become the jakuza for Latin America. I made a great friend in Augusto!

Aplicações Comportadas com Behat
Alexandre gave a great disclosure on his experience with BDD. It was a great joint between Augusto and Alexandre’s talk that make it even better to follow up. I have never seen the evolution of BDD’eing things so clearly explained, even though the talk was in Portuguese Alexandre was very direct and clear and showed his mastery on the topics and his experience. Alexandre and Augusto work together on Respect and have a great baggage to share. He made a like facebook type of example that carried the message to the four winds throughout the presentation. If these two things are not the most important tips, then I don’t know what else to tell you. First 1) show simple code on your presentation but show the evolution of that simple one code, 2) work things around that code and stick to that code to explain things. These guys know a lot from their experience with yahoo and personal experiences, I wish there could be more times in the future to learn from them and also ended up very interested in respect and working with them on bringing a great community movement to the LA region.

Three questions I would have wanted to be answered are:

1. The way one BDD with behat off-site authentication and if this can be done with the behat tool and how (this is for knpbundles.com).
2. DI how to generate from reading the Configurator.php class a yaml dump on the default configuration for a bundle repo clone (this is also for knpbundles.com).
3. Show us more experience and strategy on symfony2 TDD

Congratulations to organizers and community. I cannot emphasize enough the hospitality of the people of Bello Horizonte. Belleza!
Personal acknowledgements go to Moises, Renato, Augusto, Daniel, Diego, Alexandre, and many more I am forgetting their names sorry. My mind was all inmersed in portuguese and I now need to go back to English mindset. You guys keep the good work!

Special thanks to those who supported the @php_peru participation in the event:

– I thank God to give me the undeserved opportunity to travel to Brasil and accomplish much learning!
– ServerGrove: best hosting for php symfony2 projects!
– Diego Oliveira: thanks man, you rock!
– Daniel Oliveira: best friend in Brasil!
– Raul Rodriguez: He printed my eticket so that i can travel without problems!
– @php_peru: thanks my friends!
– and more! you know who you are!

PHP Storm Tricks: Add your USE Statements Automagically!

Recently there are some tricks that we were experimenting with. Like the Alt+Enter+Enter to display the light bulb to add getters/setters for a particular property.

There is a new trick to autoagenerate the use statements. It is basically the same you position your cursor on the Class name that you want to be recognized by PHP Storm, then hit Alt+Enter+Enter and the option will appear to add the use statement, after you select the FQCN and then it will get add to the top of your headers of your php file.