How do I install PHPUnit?

PHPUnit is an essential tool for every PHP developers. It is one of those tools that every PHP developer should have installed in their development environment. The problems most first time PHPUnit developers run into are where to install it and how to install it. This quick guide will walk you through the process and answer both questions.

How do I install PHPUnit

The Easy Way

In your project’s root directory use this command.

composer require --dev phpunit/phpunit ^6.0

This command adds PHPUnit to your project as a development dependency. This is the absolute best way to install PHPUnit. It is the best way because this way the version of PHPUnit does not change unless you change it. We specified ^6.0 as the version which means we’ll get all the updates to the 6.0 branch but not 6.1. While BC breaks don’t happen often in PHPUnit, they have happened. If you have a globally installed version of PHPUnit and you upgrade it to a version that breaks BC, you have to go update all of your tests immediately. This is a lot of hassle if you have a lot of projects with a lot of tests. Keeping a copy of PHPUnit installed as a dev requirement in each project means that each project has its own copy that can be upgraded as necessary.

The Hard Way

In a command prompt regardless of where you are in your file system, use this command.

composer global require phpunit/phpunit ^6.0

On MacOS and Linux machines, this will install PHPUnit in ~/.composer/vendor/bin. If you add this directory to your path, then from any project, you can execute PHPUnit. However, as noted above, if you ever upgrade your globally installed packages then you will have problems.

composer global update

Run that when there is a new version of PHPUnit, it will be installed, regardless of whether this will break your existing unit tests on one or more of your projects. Windows users will need to locate the .composer/vendor/bin directory in your user’s home directory.

The “ZOMG why would you do it this way” Way

Here is the old-school use wget and move it into the correct position manually. You can do it this way, but you will have to take care of all upgrades manually as well. If you only have a single project on the computer and you never ever plan on changing the version of PHPUnit….nope, still better to use composer require --dev.

wget https://phar.phpunit.de/phpunit-6.0.phar
chmod +x phpunit-6.0.phar
sudo mv phpunit-6.0.phar /usr/local/bin/phpunit
phpunit --version

These instructions are of course for MacOS or Linux. Windows user won’t need to do chmod or sudo but will need a BAT file.

That’s it. One of those commands should get you a working copy of PHPUnit on your computer.

Resources:

How do I install composer?

Composer is a must-have tool for every PHP developer these days. This page is a simple breakdown of quick-install instructions.

How do I install composer?

  1. Use PHP to download the composer installer, place it in the current directory, and name it composer-setup.php
  2. Use PHP to check the hash of the file you downloaded and compare it to the known value of the hash. You can always find the current value of the hash for the installer on the Composer Public Keys / Signatures page.
  3. Run the setup program to install composer. This does more than just download the latest copy of composer, it also sets up your local ~/.composer directory. This will install composer into the current directory. You can add the --install-dir=DIR to specify where you want composer installed. You can also specify --filename=composer to chang ethe installed filename. You can use anything you like that doesn’t already exist in your specified directory, you don’t have to use the name composer. This is a great way to get rid of the .phar at the end of the name if you don’t like it.
  4. Use PHP to remove the installer from the current directory.

To install composer for PHP you use PHP to download the installer, set a few options, and then actually perform the install.

  1. php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  2. php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  3. php composer-setup.php
  4. php -r "unlink('composer-setup.php');"

Below is a version you can copy ‘n paste.

Breakdown:

Recommended Setup for Linux and macOS:

1
2
3
4
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"

WARNING: Line two IS WHERE IT compares the hasH of the installer you just downloaded to a hard-coded value. The value specified in the script is the value for the current version of the installer as of this writing. If it fails, check the Composer Public Keys / Signatures page and get the latest version. Plug it into the script below and try again.

Windows user, change the --installdir= to a directory in your path.

Recommended Setup for Windows

If you are running Microsoft Windows, the instructions above will work for you as long as you use the proper install paths. You can however download ComposerSetup.exe from the Composer Introduction page. Execute this and it will install composer and set your path so that you can run composer from anywhere. You will have to close your terminal window and open a new one after the install for the path to be updated.

That’s it, you should now have Composer installed.

Composer installation Resources

MySQL: Analysis, Understanding, and
Optimization of Queries

register now

May 2017

Presented by Michael Moussa (@michaelmoussa)

Date: May 18, 2017
Time: 20:00 CDT

18:00 PDT, 3:00 CEST (May 19). 2:00 BST (May 19)
Not sure of the time in your area? Check it on timeanddate.com

Your new database query ran quickly when you tested it, but seconds after deploying it, alarms are blaring and you’re scrambling to rollback before the site goes down. What happened?!?

I can “EXPLAIN”.

Queries that perform well under development load with limited datasets can easily bring a database to its knees under production load. In this talk, you will learn to decipher MySQL query execution plans, recognize portions that can be improved, and take the necessary steps to optimize your queries – all without starting any fires!

Michael Moussa

Michael Moussa

Michael has over 17 years experience building PHP applications from the ground up. He is a Solutions Architect on Rackspace’s Fanatical Support for AWS team helping customers solve their technical challenges and launch their products in the cloud. In his spare time, he’s a regular open-source contributor, Zend Expressive maintainer, and competitive homebrewer.

 

Code Coverage for Total Security
in Application Migrations!

register now

May 2017

Presented by Dana Luther (@danaluther)

Date: May 18, 2017
Time: 20:00 CEST

19:00 BST, 13:00 CDT, 11:00 PDT
Not sure of the time in your area? Check it on timeanddate.com

So the time has come to take the leap and upgrade your application to a new major version of the underlying framework, or, perhaps, to an entirely different framework… how do you ensure that none of your functionality or usability is impacted by a potentially drastic rewrite of the underlying systems? How can you move forward with 100% confidence in your migrated codebase? Testing, testing and more testing. Using a combination of unit, functional and acceptance tests can give you the certainty you need. In this talk, we will go over key strategies for ensuring that you begin with full code coverage and move forward with confidence.

Testing Framework: Codeception 2.2.x

Dana Luther

Dana Luther

Dana is a Senior Web Application Architect at Envisage International. She has been developing in PHP for nearing 20 years, and building almost exclusively in the Yii Framework for the last 6 years. She is a graduate of the University of Florida, and works with a small agile group of Yii developers to meet the demands of a diverse international company that serves the needs of international students around the world with over 650,000 monthly visitors and generating over 1,200,000 page views. Dana is one of the co-organizers of the Yii-Jax Meetup group, a regular contributor to the Envisage International Blog and maintains her own blog on the Yii Framework.

 

Transparent Session Management with Sessionz

Eric Mann

Speaker: Eric Mann @ericmann

Sessionz is a new PHP library that adds a custom session manager to your application. On top of this custom manager, you can mix any number of specific handlers into a stack to deal with your data:

  • Write sessions out to the default, filesystem-based session store.
  • Cache session data in-memory for faster reads (and faster performance).
  • Encrypt stored data so it’s protected at rest.
  • Synchronize data to an external storage system so multiple application servers can reference it.