QA	  for	  PHP	  projectsusing	  PHPStormJetBrains	  Webinar,	  April	  25	  2013
2Michelangelo	  van	  Dam
Goals3• improve	  QA	  in	  your	  PHP	  projects• deliver	  higher	  quality	  of	  applicaIons• become	  familiarized	  ...
Our	  host	  for	  this	  webinar4
Why	  Quality	  Assurance5
6Safeguarding	  code
7Detect	  bugs	  early
8Observe	  behaviour
9Prevent	  accidents
10Tracking	  progress
Let’s	  get	  our	  hands	  dirty11
Revision	  Control12
13FTP
Advantages of SCM• team development possible• tracking multi-versions of source code• moving back and forth in history• ta...
PHP	  Lint15
PHP	  Lint16• checks the syntax of code• build in PHP core• is used per file- pre-commit hook for version control system- b...
Running	  on	  command	  line17
SCM	  commit	  hook18#!/bin/sh## Pre-commit hook to validate syntax of incoming PHP files, if no failures it# accepts the ...
No	  syntax	  failures19
PHPStorm	  syntax	  checking20
DocumentaIon21
Why	  documenIng?22• new	  members	  in	  the	  team• working	  with	  remote	  workers• analyzing	  improvements• think	 ...
PHPDocumentor223
Class	  details24
Uses	  docblock	  in	  code25
Class	  graphs26
Debugging27
The	  art	  of	  finding	  a	  bug28• Debugging	  allows	  you	  to	  walk	  step-­‐by-­‐step	  through	  your	  code	  bas...
XDebug	  php.ini	  se`ngs[xdebug]zend_extension=/usr/lib/php/extensions/xdebug.soxdebug.default_enable=1xdebug.cli_color=1...
Debugging	  in	  PHPStorm30
TesIng31
Most	  common	  excuses32• no	  Ime• not	  within	  budget• development	  team	  does	  not	  know	  how• tests	  are	  pr...
NO	  EXCUSE!33
Benefits	  of	  tesIng34• beder	  code	  with	  smaller	  footprint• allows	  refactoring• detects	  bugs	  in	  an	  early...
Se`ng	  things	  up35
ConfiguraIon:	  phpunit.xml36<phpunit bootstrap="./Bootstrap.php"><testsuite name="Unit test suite"><directory>./</director...
Bootstrapping	  app<?php// set our app paths and environmentsdefine(BASE_PATH, realpath(dirname(__FILE__) . /../));define(...
WriIng	  tests38
TesIng	  a	  class39
WriIng	  a	  test40<?phpclass Application_Model_CommentTest extends PHPUnit_Framework_TestCase{protected $_comment;protect...
WriIng	  the	  class<?phpclass Application_Model_Comment{protected $_id = 0; protected $_fullName; protected $_emailAddres...
Adding	  validaIonprotected $_filters;protected $_validators;public function __construct($params = null){$this->_filters =...
Modify	  seders	  &	  gederspublic function setId($id){$input = new Zend_Filter_Input($this->_filters, $this->_validators)...
Modify	  seders	  &	  geders	  2public function setEmailAddress($emailAddress){$input = new Zend_Filter_Input($this->_filt...
Modify	  geders	  &	  seders	  3public function setComment($comment){$input = new Zend_Filter_Input($this->_filters, $this...
Running	  tests46
Or	  in	  PHPStorm47
Mess	  DetecIon48
Mess	  DetecIon49• code	  smells• possible	  bugs• sub-­‐opImal	  code• over	  complicated	  expressions• unused	  paramet...
Running	  on	  command	  line50
Running	  in	  PHPStorm51
AutomaIon52
Key	  reason53“computers are great at doing repetitive tasks very well”
AutomaIon54• Limit	  risk	  of	  human	  error• Will	  always	  be	  executed	  in	  same	  order• Can	  be	  shared	  amo...
55AutomaIon	  with	  Phing
Running	  Phing	  on	  CLI56
Running	  in	  PHPStorm57
Summary58
Overview	  of	  tools59• SCM	  (SVN,	  GIT,	  …)• PHP	  Lint• PHP	  Document	  Generator• Debugging• PHPUnit	  TesIng• PHP...
Quality	  Assurance• is	  part	  of	  development	  process• will	  only	  work	  if	  the	  tools	  are	  available• and	...
PHPStorm	  IDE61
Contact62Michelangelo van DamZend Certified Engineeremail: michelangelo@in2it.beskype: michelangelovandamtwitter: @DragonBe...
CreditsI’d like to thank the following people for sharing their creative commons picturesmichelangelo: http://www.flickr.co...
Fork	  this	  codehttp://github.com/DragonBe/zftest64
Thank	  you65
Upcoming SlideShare
Loading in …5
×

Quality assurance for php projects with PHPStorm

15,185 views

Published on

Everyone talks about raising the bar on the quality of code, but it’s hard to implement when you have no clue where to start. This talk is geared toward all levels of developers, and will teach you how to improve by using the right tools effectively – a must-attend for any PHP developer who wants to scale up their quality.


Michelangelo will tell us about Quality Assurance for PHP in general and show how different QA-related actions can be performed using PhpStorm IDE. The webinar will cover topics including:

Revision control
Syntax checking
Code documentation
Unit Testing with PHPUnit
Measuring code health with a variety of tools
Profiling and debugging with Xdebug
Automation with Phing
Team work and more.

Published in: Technology
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total views
15,185
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
33
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide

Quality assurance for php projects with PHPStorm

  1. 1. QA  for  PHP  projectsusing  PHPStormJetBrains  Webinar,  April  25  2013
  2. 2. 2Michelangelo  van  Dam
  3. 3. Goals3• improve  QA  in  your  PHP  projects• deliver  higher  quality  of  applicaIons• become  familiarized  with  available  tools
  4. 4. Our  host  for  this  webinar4
  5. 5. Why  Quality  Assurance5
  6. 6. 6Safeguarding  code
  7. 7. 7Detect  bugs  early
  8. 8. 8Observe  behaviour
  9. 9. 9Prevent  accidents
  10. 10. 10Tracking  progress
  11. 11. Let’s  get  our  hands  dirty11
  12. 12. Revision  Control12
  13. 13. 13FTP
  14. 14. Advantages of SCM• team development possible• tracking multi-versions of source code• moving back and forth in history• tagging of milestones• backup of source code• accessible from- command line- native apps- IDE’s like JetBrain’s IDE- analytical toolsTIP:  hooks  for  tools
  15. 15. PHP  Lint15
  16. 16. PHP  Lint16• checks the syntax of code• build in PHP core• is used per file- pre-commit hook for version control system- batch processing of files• can provide reports- but if something fails -> the build failsTIP:  pre-­‐commit  hook
  17. 17. Running  on  command  line17
  18. 18. SCM  commit  hook18#!/bin/sh## Pre-commit hook to validate syntax of incoming PHP files, if no failures it# accepts the commit, otherwise it fails and blocks the commitREPOS="$1"TXN="$2"# modify these system executables to match your systemPHP=/usr/bin/phpAWK=/usr/bin/awkGREP=/bin/grepSVNLOOK=/usr/bin/svnlook# PHP Syntax checking with PHP Lint# originally from Joe Stump at Digg# https://gist.github.com/53225#for i in `$SVNLOOK changed -t "$TXN" "$REPOS" | $AWK {print $2}`doif [ ${i##*.} == php ]; thenCHECK=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHP -d html_errors=off -l || echo $i`RETURN=`echo $CHECK | $GREP "^No syntax" > /dev/null && echo TRUE || echo FALSE`if [ $RETURN = FALSE ]; thenecho $CHECK 1>&2;exit 1fifidone
  19. 19. No  syntax  failures19
  20. 20. PHPStorm  syntax  checking20
  21. 21. DocumentaIon21
  22. 22. Why  documenIng?22• new  members  in  the  team• working  with  remote  workers• analyzing  improvements• think  before  doing• used  by  IDE’s  and  editors  for  code  hinIng  ;-­‐)
  23. 23. PHPDocumentor223
  24. 24. Class  details24
  25. 25. Uses  docblock  in  code25
  26. 26. Class  graphs26
  27. 27. Debugging27
  28. 28. The  art  of  finding  a  bug28• Debugging  allows  you  to  walk  step-­‐by-­‐step  through  your  code  base  unIl  you  reach  the  point  of  failure.
  29. 29. XDebug  php.ini  se`ngs[xdebug]zend_extension=/usr/lib/php/extensions/xdebug.soxdebug.default_enable=1xdebug.cli_color=1xdebug.remote_enable=onxdebug.remote_connect_back=1xdebug.remote_handler=dbgpxdebug.remote_host=127.0.0.1xdebug.remote_port=9000xdebug.scream=129
  30. 30. Debugging  in  PHPStorm30
  31. 31. TesIng31
  32. 32. Most  common  excuses32• no  Ime• not  within  budget• development  team  does  not  know  how• tests  are  provided  aaer  delivery• …
  33. 33. NO  EXCUSE!33
  34. 34. Benefits  of  tesIng34• beder  code  with  smaller  footprint• allows  refactoring• detects  bugs  in  an  early  stage• saves  Ime  in  maintenance  stage
  35. 35. Se`ng  things  up35
  36. 36. ConfiguraIon:  phpunit.xml36<phpunit bootstrap="./Bootstrap.php"><testsuite name="Unit test suite"><directory>./</directory></testsuite><filter><whitelist><directory suffix=".php">../application/</directory><directory suffix=".php">../library/</directory></whitelist></filter></phpunit>
  37. 37. Bootstrapping  app<?php// set our app paths and environmentsdefine(BASE_PATH, realpath(dirname(__FILE__) . /../));define(APPLICATION_PATH, BASE_PATH . /application);define(TEST_PATH, BASE_PATH . /tests);define(APPLICATION_ENV, testing);// Include pathset_include_path(. PATH_SEPARATOR . BASE_PATH . /library. PATH_SEPARATOR . get_include_path());// Set the default timezone !!!date_default_timezone_set(Europe/Brussels);// We wanna catch all errors en strict warningserror_reporting(E_ALL|E_STRICT);require_once Zend/Application.php;$application = new Zend_Application(APPLICATION_ENV,APPLICATION_PATH . /configs/application.ini);$application->bootstrap();37
  38. 38. WriIng  tests38
  39. 39. TesIng  a  class39
  40. 40. WriIng  a  test40<?phpclass Application_Model_CommentTest extends PHPUnit_Framework_TestCase{protected $_comment;protected function setUp(){$this->_comment = new Application_Model_Comment();parent::setUp();}protected function tearDown(){parent::tearDown();$this->_comment = null;}public function testModelIsEmptyAtConstruct(){$this->assertSame(0, $this->_comment->getId());$this->assertNull($this->_comment->getFullName());$this->assertNull($this->_comment->getEmailAddress());$this->assertNull($this->_comment->getWebsite());$this->assertNull($this->_comment->getComment());}}
  41. 41. WriIng  the  class<?phpclass Application_Model_Comment{protected $_id = 0; protected $_fullName; protected $_emailAddress;protected $_website; protected $_comment;public function setId($id) { $this->_id = (int) $id; return $this; }public function getId() { return $this->_id; }public function setFullName($fullName) { $this->_fullName = (string) $fullName; return $this; }public function getFullName() { return $this->_fullName; }public function setEmailAddress($emailAddress) { $this->_emailAddress = (string) $emailAddress; return $this; }public function getEmailAddress() { return $this->_emailAddress; }public function setWebsite($website) { $this->_website = (string) $website; return $this; }public function getWebsite() { return $this->_website; }public function setComment($comment) { $this->_comment = (string) $comment; return $this; }public function getComment() { return $this->_comment; }public function populate($row) {if (is_array($row)) {$row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);}if (isset ($row->id)) $this->setId($row->id);if (isset ($row->fullName)) $this->setFullName($row->fullName);if (isset ($row->emailAddress)) $this->setEmailAddress($row->emailAddress);if (isset ($row->website)) $this->setWebsite($row->website);if (isset ($row->comment)) $this->setComment($row->comment);}public function toArray() {return array (id => $this->getId(),fullName => $this->getFullName(),emailAddress => $this->getEmailAddress(),website => $this->getWebsite(),comment => $this->getComment(),);}}41
  42. 42. Adding  validaIonprotected $_filters;protected $_validators;public function __construct($params = null){$this->_filters = array (id => array (Int),fullName => array (StringTrim, StripTags, new Zend_Filter_Alnum(true)),emailAddress => array (StringTrim, StripTags, StringToLower),website => array (StringTrim, StripTags, StringToLower),comment => array (StringTrim, StripTags),);$this->_validators = array (id => array (Int),fullName => array (new Zftest_Validate_Mwop(),new Zend_Validate_StringLength(array (min => 4, max => 50)),),emailAddress => array (EmailAddress,new Zend_Validate_StringLength(array (min => 4, max => 50)),),website => array (new Zend_Validate_Callback(array(Zend_Uri, check)),new Zend_Validate_StringLength(array (min => 4, max => 50)),),comment => array (new Zftest_Validate_TextBox(),new Zend_Validate_StringLength(array (max => 5000)),),);if (null !== $params) { $this->populate($params); }}42
  43. 43. Modify  seders  &  gederspublic function setId($id){$input = new Zend_Filter_Input($this->_filters, $this->_validators);$input->setData(array (id => $id));if (!$input->isValid(id)) {throw new Zend_Exception(Invalid ID provided);}$this->_id = (int) $input->id;return $this;}public function setFullName($fullName){$input = new Zend_Filter_Input($this->_filters, $this->_validators);$input->setData(array (fullName => $fullName));if (!$input->isValid(fullName)) {throw new Zend_Exception(Invalid fullName provided);}$this->_fullName = (string) $input->fullName;return $this;}43
  44. 44. Modify  seders  &  geders  2public function setEmailAddress($emailAddress){$input = new Zend_Filter_Input($this->_filters, $this->_validators);$input->setData(array (emailAddress => $emailAddress));if (!$input->isValid(emailAddress)) {throw new Zend_Exception(Invalid emailAddress provided);}$this->_emailAddress = (string) $input->emailAddress;return $this;}public function setWebsite($website){$input = new Zend_Filter_Input($this->_filters, $this->_validators);$input->setData(array (website => $website));if (!$input->isValid(website)) {throw new Zend_Exception(Invalid website provided);}$this->_website = (string) $input->website;return $this;}44
  45. 45. Modify  geders  &  seders  3public function setComment($comment){$input = new Zend_Filter_Input($this->_filters, $this->_validators);$input->setData(array (comment => $comment));if (!$input->isValid(comment)) {throw new Zend_Exception(Invalid comment provided);}$this->_comment = (string) $input->comment;return $this;}45
  46. 46. Running  tests46
  47. 47. Or  in  PHPStorm47
  48. 48. Mess  DetecIon48
  49. 49. Mess  DetecIon49• code  smells• possible  bugs• sub-­‐opImal  code• over  complicated  expressions• unused  parameters,  methods  and  properIes• wrongly  named  parameters,  methods  or  properIes
  50. 50. Running  on  command  line50
  51. 51. Running  in  PHPStorm51
  52. 52. AutomaIon52
  53. 53. Key  reason53“computers are great at doing repetitive tasks very well”
  54. 54. AutomaIon54• Limit  risk  of  human  error• Will  always  be  executed  in  same  order• Can  be  shared  amongst  team  members• Allows  to  fine-­‐tune  and  improve  features
  55. 55. 55AutomaIon  with  Phing
  56. 56. Running  Phing  on  CLI56
  57. 57. Running  in  PHPStorm57
  58. 58. Summary58
  59. 59. Overview  of  tools59• SCM  (SVN,  GIT,  …)• PHP  Lint• PHP  Document  Generator• Debugging• PHPUnit  TesIng• PHP  Mess  detecIon• Phing  automaIon}
  60. 60. Quality  Assurance• is  part  of  development  process• will  only  work  if  the  tools  are  available• and  developers  see  the  benefits  of  QA60
  61. 61. PHPStorm  IDE61
  62. 62. Contact62Michelangelo van DamZend Certified Engineeremail: michelangelo@in2it.beskype: michelangelovandamtwitter: @DragonBetel EU: +32 15 34 52 90tel US: 202 559-7401www.in2it.befacebook.com/in2itvof | @in2itvofContact us forConsultancy - Training - QA - Webdesign
  63. 63. CreditsI’d like to thank the following people for sharing their creative commons picturesmichelangelo: http://www.flickr.com/photos/dasprid/5148937451birds: http://www.flickr.com/photos/andyofne/4633356197safeguarding: http://www.flickr.com/photos/infidelic/4306205887/bugs: http://www.flickr.com/photos/goingslo/4523034319behaviour: http://www.flickr.com/photos/yuan2003/1812881370prevention: http://www.flickr.com/photos/robertelyov/5159801170progress: http://www.flickr.com/photos/dingatx/4115844000file cabinet: http://www.flickr.com/photos/manc/1427691715documentation: http://www.flickr.com/photos/dennis_matheson/3269442687exam: http://www.flickr.com/photos/albertogp123/5843577306dead roach: http://www.flickr.com/photos/stevensnodgrass/7504408776garbage: http://www.flickr.com/photos/amstersam/4608512202gears: http://www.flickr.com/photos/freefoto/5982549938elephpant: http://www.flickr.com/photos/drewm/3191872515
  64. 64. Fork  this  codehttp://github.com/DragonBe/zftest64
  65. 65. Thank  you65

×