installing PHPUnit

installing PHPUnit

Created:18 Feb 2017 15:10:51 , in  Host development,  Web development

If you are any serious about writing code in PHP computer language you have tried some unit testing frameworks by now. Some of them are better than others, one that really stands out, both in terms of code quality and documentation is Sebastian's Bergmann PHPUnit. It's been around for more than a decade and has proven an invaluable tool for many PHP developers in this time.

PHPUnit can be downloaded from Sebastian's website. It comes in the form of a PHAR archive. PHP PHAR archives are whole applications reduced to a single file. What's cool about them, they can be installed easily and executed both via a web server or on the command line. PHPUnit is a command line application.

In this article I present a BASH script which makes it a breeze to install PHPUnit.

PHPUnit installing and executing script

The script below, called install_phpunit.sh, can be run with no arguments, in such circumstances it installs current stable release of PHPUnit. It checks for root privileges before it begins installation process. In the case install_phpunit.sh is invoked with an argument, which is PHPunit version, it makes a check for existence of PHPUnit in that version on Sebastian's Bergman website before it embarks on downloading and installing the software. The version bit is important here because not every PHPUnit version is compatible with every PHP version. For example, PHPUnit version 6.0 is not compatible with PHP in a version below 7.0. So make sure you know your PHP version before you begin. For PHP 5.6 you would want PHPUnit in version 5.7

.

#!/usr/bin/env bash

# This script installs PHPunit PHP testing framework by Sebastian Bergmann
# author : Sylwester Wojnowski
# www : wojnowski.net.pl

# Usage: install_phpunit.sh [phpunit_version]
# When invoked with no parameter, installs the latest version (latest stable release) of PHPUnit
# If passed a version of PHPunit will try to install it. 
# Versions of PHPUnit can be found at https://phpunit.de/

check_privileges(){
  [[ $(id -u) -ne 0 ]] && {
    echo "This script requires root privileges! Exiting ..."
    exit 1
  } 
}  

begin(){
  echo "installing phpunit ..."
}

download_and_install(){
  local puv=$1
  puv=${puv:+"-${1}"}
  
  # download from https://phpunit.de/
  wget -qO /dev/null -o /dev/null "https://phar.phpunit.de/phpunit${puv}.phar" 
  
  [[ $? -ne 0 ]] && {
    echo "Error!" 
    echo "Incorrect version of PHPunit given. Check https://phpunit.de/ for PHPunit versions."
    echo "Exiting ..."
    exit 1
  }
  
  wget "https://phar.phpunit.de/phpunit${puv}.phar"
  chmod +x "phpunit${puv}.phar"
  mv "phpunit${puv}.phar" /usr/local/bin/phpunit
  return
}

end(){
  [[ $? -eq 0 ]] && {
    echo "PHPunit installation process complete!"
  } || {
    echo "PHPunit installation process failed!"
  }
}

check_privileges
begin
download_and_install $1
end

Now, what you need to do to make install_phpunit.sh work for you are the usual steps you carry out before executing a script on the command line. That is, you save the code above in a file called install_phpunit.sh. Then you make install_phpunit.sh executable by changing its mode with chmod command. Finally, you execute the script as a privilaged user. It will install PHPUnit in /usr/local/bin with executable being PHPUnit.

 
chmod +x ./install_phpunit.sh
./install_phpunit.sh 

The code above installs the latest stable version of PHPUnit. If, for example you need version 5.7, to supports PHP in version 5.6 or 7.x, then you would execute code below:


chmod +x ./install_phpunit.sh
./install_phpunit.sh 5.7

To confirm that the installation process worked, execute:


phpunit --version

Conclusion

PHPUnit installation process is rather simple, still it can fail if you are not careful with it. My script makes the possibility of failure a bit less likely. The script has been designed to be a simple part of a broader suite of programs but it is also perfectly usable on its own. One thing to be aware of about it is, it contains no code, which verifies PHPUnit PHAR release using PGP signatures and SHA1 hashes. If you need that, Sebastian Bergman describes the process clearly in PHPunit documentation. He even provides a sample BASH script for the purpose.

This post was updated on 27 Apr 2017 16:13:16

Tags:  BASH ,  php ,  PHPUnit 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, is sWWW admin and owner.He enjoys doing Maths and studying algorithms, writing code in scripting and command languages, Thrash Metal music and playing electric guitar.

Copyrights

©Copyright, 2019 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "installing PHPUnit." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/installing-phpunit

Post navigation

Previous:
  Arrays in BASH

Next:
  Skeleton for unit testing with PHPUnit