Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Coding Standards and Unit Testing

Avatar for Brian Brian
August 17, 2017

Coding Standards and Unit Testing

What, Why, When, Where. And A Little Bit of How

Avatar for Brian

Brian

August 17, 2017

More Decks by Brian

Other Decks in Technology

Transcript

  1. About Me • Writing PHP for over 5 years •

    Using Linux (Fedora, among others) for over 10 years • Advocating use of PSR and Unit Testing because of past experiences
  2. PSR (What) • PHP Standards **Recommendations** • “Levels” refer to

    numerical IDs that are associated with a type of standard. • 9 Accepted levels
  3. PSR Levels Level 1 Basic Coding Level 2 Coding Style

    Guide Level 3 Logger Interface Level 4 Auto-loading Level 6 Caching Interface Level 7 HTTP Messaging Interface Level 11 Container Interface Level 16 Hypermedia Links (URL is on next slide) Level 17 Simple Cache
  4. PSR (Where) • PHPCS/PHPCBF source code: https://github.com/squizlabs/PHP_CodeSniffer • PSR website:

    http://www.php-fig.org/psr/ • More about rule-sets: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml • Rule-set generator (I did not make this): http://edorian.github.io/php-coding-standard-generator/#phpcs
  5. PSR (Why) • Provide consistency in your codebase. The less

    mental effort it takes you to read code you didn’t write, the better.* • Potential to reduce conflicts during code merges. • Potential to reduce size of code merges. • Expectations are set as a team • Expectations are enforced by a computer (automated build systems) * This is something you have to determine. There is no magic solution/silver bullet to find what is or isn't too much mental effort.
  6. PSR (When) Parental Discretion Is Advised (You know what you’re

    doing, so you should know when to use it.)
  7. PSR (How) composer require "squizlabs/php_codesniffer" (optionally add ‘global’) curl -OL

    https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar pear install PHP_CodeSniffer phpcs -sp --colors --report=full --standard=PSR2
  8. Unit Testing (What) • Frameworks (PHPUnit, CodeCeption) • Automated, sane

    way to know if your changes broke other parts of your application.
  9. Unit Testing (Why) • You can know with much more

    certainty that everything* is working. • One piece to continuous integration. • Reports such as code coverage. *Everything that you write a test for.
  10. Unit Testing (How) composer require --dev "phpunit/phpunit" phpunit --generate-configuration (first

    run only) phpunit composer require --dev "codeception/codeception" codecept bootstrap (first run only) codecept run