I recently got a request on how you might Unit Test an individual class that doesn’t belong to a framework. So, here’s an example class we will be unit testing using FireTest.
Let’s start out by creating a new project from scratch:
mkdir fire-test-example
cd fire-test-example
composer init
...
composer require ua1-labs/firetest
Now, update composer.json
to include the test runner file script.
"scripts": {
"test": "php vendor/ua1-labs/firetest/scripts/runner.php --dir=/. --ext=.TestCase.php"
}
Now let’s create our example class at src/FormPost.php
<?php
use \Exception;
class FormPost
{
public $writeToDb;
public function __construct()
{
$this->writeToDb = false;
}
public function handleFormPost()
{
$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : '';
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : '';
if ($this->validateFormData($firstName, $lastName)) {
$this->writeToDatabase($firstName, $lastName);
} else {
throw new Exception('Please fill in all form data.');
}
}
private function validateFormData($firstName, $lastName)
{
// code to verify form data
if (!$firstName || !$lastName) {
return false;
}
return true;
}
private function writeToDatabase()
{
// code to write to database
$this->writeToDb = true;
}
}
Now add it to Composer’s Autoload schema by adding the following lines to the composer.json
file.
"autoload": {
"files": ["src/FormPost.php"]
}
Now, let’s create our src/FormPost.TestCase.php
file. I’ve already created the unit test scenarios we are going to test for.
<?php
use \UA1Labs\Fire\Test\TestCase;
class FormPostTestCase extends TestCase
{
private $formPost;
public function beforeEach()
{
$_POST = [];
$this->formPost = new FormPost();
}
public function testHandleFormPost()
{
$this->should('Validate a form and write the values to a database.');
$_POST['firstName'] = 'UA1';
$_POST['lastName'] = 'LABS';
$this->formPost->handleFormPost();
$this->assert($this->formPost->writeToDb === true);
}
public function testHandleFormPostBadRequest()
{
$this->should('Throw an Exception because form data is missing.');
try{
$this->formPost->handleFormPost();
$this->assert(false);
} catch (Exception $e) {
$this->assert(true);
}
}
}
Now that you have your Class and your TestCase file, you are ready to run the unit tests. Remember before we added a run script to the composer.json
file? We are now going to run that.
composer run test
Or just run the runner script using the PHP command:
php vendor/ua1-labs/firetest/scripts/runner.php --dir=/. --ext=.TestCase.php
You will get the following results:
FireTest: *************************************************************
FireTest: ███████╗██╗██████╗ ███████╗████████╗███████╗███████╗████████╗
FireTest: ██╔════╝██║██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
FireTest: █████╗ ██║██████╔╝█████╗ ██║ █████╗ ███████╗ ██║
FireTest: ██╔══╝ ██║██╔══██╗██╔══╝ ██║ ██╔══╝ ╚════██║ ██║
FireTest: ██║ ██║██║ ██║███████╗ ██║ ███████╗███████║ ██║
FireTest: ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝
FireTest: *************************************************************
FireTest: [STARTING] Test suite is located at "/home/tutorials/fire-test-example/src"
FireTest: [STARTING] Finding all files with the extension ".TestCase.php"
FireTest: [LOADING] Test file "/home/tutorials/fire-test-example/src/FormPost.TestCase.php"
FireTest: [LOADING] Test class "FormPostTestCase"
FireTest: [RUNNING] FormPostTestCase::testHandleFormPost()
FireTest: [PASSED] Validate a form and write the values to a database.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] FormPostTestCase::testHandleFormPostBadRequest()
FireTest: [PASSED] Throw an Exception because form data is missing.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: ***********************************************************
FireTest: ███████╗██╗ ██╗ ██████╗ ██████╗███████╗███████╗███████╗
FireTest: ██╔════╝██║ ██║██╔════╝██╔════╝██╔════╝██╔════╝██╔════╝
FireTest: ███████╗██║ ██║██║ ██║ █████╗ ███████╗███████╗
FireTest: ╚════██║██║ ██║██║ ██║ ██╔══╝ ╚════██║╚════██║
FireTest: ███████║╚██████╔╝╚██████╗╚██████╗███████╗███████║███████║
FireTest: ╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝╚══════╝╚══════╝╚══════╝
FireTest: ***********************************************************
FireTest: [FINAL] (Passed: 2, Failed: 0)
Happy coding!
The post FireTest – Example Unit Testing Individual Classes Without A Framework appeared first on UA1 Labs.
Top comments (0)