Automated TestingUsing PHPUnit for Testing and Deployment |
Summary
Adding PHPUnitA PHP Library for Code Test Automation to the project to automate unit and integration testing which will become a part of the DevOps process.
Overview
Purpose | To automate code testing as part of a devops deployment strategy. |
Goals |
|
Technology | PHPUnitA PHP Library for Code Test Automation, HomebrewApplication used to install XDebug and NodeJS on Macbook., Auryn InjectorA PHP Dependency Injector library to support Inversion of Control design pattern., XDebugA PHP extension which provides debugging and profiling capabilities. |
Resources | Udemy class "Introduction to Testing with PHPUnitA PHP Library for Code Test Automation" taught by Trevor Sawler, Ph.D. (This class was a great introduction to automated testing principles using PHPUnitA PHP Library for Code Test Automation but not necessarily novice friendly.) |
Implementation
-
Install PHPUnitA PHP Library for Code Test Automation
Use ComposerUsed to manage the installation of third-party PHP libraries. to install PHPUnitA PHP Library for Code Test Automation:
composer require phpunit/phpunit --dev
-
Create Directories for Test Classes
In the Document Root create directory Test. Inside this directory other directories should be created to mirror the locations of the classes to be tested. (For examples tests for App/Controllers/User.php should be in Test/App/Controllers.
-
Create Test Database
This was done by exporting the orginal database into a copy called tbd_test.
-
Create PHPUnitA PHP Library for Code Test Automation Configuration File
Create phpunit.xml in the Document Root which specifies the location of the test classes, directories of the classes to be tested and environmental variables to be used during testing. Note: parameters to log into the database should point to the new tbd_test database.
-
Install HomebrewApplication used to install XDebug and NodeJS on Macbook. and XDebugA PHP extension which provides debugging and profiling capabilities.
XDebugA PHP extension which provides debugging and profiling capabilities. is an application that works with PHPUnitA PHP Library for Code Test Automation to analyze test coverage. It calculates the percentage of the code that is tested and shows the lines that are untested. In order to install XDebugA PHP extension which provides debugging and profiling capabilities., HomebrewApplication used to install XDebug and NodeJS on Macbook. a package management application was first installed.
-
Install HomebrewApplication used to install XDebug and NodeJS on Macbook.
install homebrew - /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/HomebrewApplication used to install XDebug and NodeJS on Macbook./install/master/install)"
-
Get the XDebugA PHP extension which provides debugging and profiling capabilities. version and install:
get xdebug version - brew search xdebug
install xdebug - brew install homebrew/php/phpenter-version-above-xdebug -
Edit PHPServer-side scripting language for web development. Initialization File to include the below line (default location in MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. /Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./bin/php/php7.1.0/conf/php.ini
zend_extension="/Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./bin/php/php7.1.0/lib/php/ extensions/no-debug-non-zts-20160303/xdebug.so"
-
Stop and start MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. for the change to take effect.
-
-
Install PHPServer-side scripting language for web development. Dependency Injector
In order to make the code easier to test code was refactored to use a dependency injection design pattern. To facilitate this effort the PHPServer-side scripting language for web development. package rdlowrey/auryn was installed via ComposerUsed to manage the installation of third-party PHP libraries.:
composer require rdlowrey/auryn
-
Modify Front Controller to Use Dependency Injector
The public/index.php was modified to establish the shared objects and initialize the Router by injecting these dependencies.
-
Modify PHPServer-side scripting language for web development. Class Constructors
Almost all of the the PHPServer-side scripting language for web development. classes had to modified to accept the dependent objects passed into the constructor instead of creating instances of the dependent objects inside the class.
-
Create Unit and Integration Test Classes
For each classes in the App and Core directories two PHPServer-side scripting language for web development. class files were created, one for unit tests where all dependencies on other classes were mocked and one for integration test where the classes were allowed to interact. For example App/Home.php would have /Tests/App/Home_UnitTest.php and /Tests/App/Home_IntegrationTest.php
-
Execute PHPUnitA PHP Library for Code Test Automation Test
-
From the Terminal window in the Document Root execute the PHPUnitA PHP Library for Code Test Automation test suite:
vendor/bin/phpunit
-
To determine the code coverage using XDebugA PHP extension which provides debugging and profiling capabilities. use the below command:
vendor/bin/phpunit --coverage-html Tests/coverage
To see the results open Tests/coverage/index.html in a browser.
-