DEV Community

Joshua Johnson for UA1 Labs

Posted on • Originally published at ua1.us on

FireTest – Getting Started

Getting started with FireTest is easy. Our goal with FireTest was to make it as simple as possible to integrate it into your project and start writing tests for the code you’ve written.

Install FireTest

composer require ua1-labs/firetest

Create Your First Test

Creating test cases in FireTest is as simple as adding a TestCase class and running the FireTest runner.php script. Your first step into creating FireTest Test Cases are to find the class you’d like to test. Create a file in the same directory and call it {classname}.TestCase.php. Copy and paste the boiler plate code into the newly created Test Case file.

<?php

use UA1Labs\Fire\Test\TestCase;

class MyTestCase extends TestCase
{
    //my test suite logic
}

Now you may use event hook methods to bootstrap your tests.

Event Hook Methods In Test Cases

With every test that you write, you will need to setup the environment to setup your test environment to run your code in. For example, almost for every test you write, you will first need to obtain a new instance of the object you are testing. Event Hook Methods are going to give you the ability to do this.

  • setUp() A method that is invoked when the when the testcase is first intialized.
  • beforeEach() A method that is invoked before each test method is invoked.
  • afterEach() A method that is invoked after each test method is invoked.
  • tearDown() A method that is invoked when the test case is finish running all test methods.

Test Methods

Test methods are another kind of event hook. The test method is where you will actually test your code. FireTest will loop through each of these test methods and execute the code. These methods are ran between the beforeEach() and afterEach() method.

Test methods begin with the work “test.” This is how FireTest dictates if it should be running the method during the testing phase of the life cycle. You can name the method whatever you want, however, it will need to begin with the work test (all lowercase).

public function testMyCode1()
{
    // my test code here
}

public function testMyCode2()
{
    // my test code here
}

Testing Your Code

Once you have mastered the event hooks and have setup your testing environment, you will then need to be able to tell what test are you running and how you will prove that it was successful. that is where the should() and assert() methods come in. To write tests start out by telling what this test is going to prove using a should statement. Think of testing like saying “this test should…” and fill in the blank.

$this->should('Return a number when I pass in a number.');
$result = my_number_converter(4);
$this->assert(is_number($result));

Now Run FireTest Runner.php

FireTest has a built in test runner so that you don’t have to bootstrap your own test suites together. Simply run the following command from the root directory of your project and watch FireTest do its thing.

php vendor/ua1-labs/firetest/scripts/runner.php

Test Results

Test results will then provide you with feedback whether your tests have passed or not.

ua1-labs:firedi josjohns$ php vendor/ua1-labs/firetest/scripts/runner.php
FireTest: *************************************************************
FireTest: ███████╗██╗██████╗ ███████╗████████╗███████╗███████╗████████╗
FireTest: ██╔════╝██║██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
FireTest: █████╗ ██║██████╔╝█████╗ ██║ █████╗ ███████╗ ██║   
FireTest: ██╔══╝ ██║██╔══██╗██╔══╝ ██║ ██╔══╝ ╚════██║ ██║   
FireTest: ██║ ██║██║ ██║███████╗ ██║ ███████╗███████║ ██║   
FireTest: ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚══════╝ ╚═╝   
FireTest: *************************************************************
FireTest: [STARTING] Test suite is located at "/home/projects/firedi"
FireTest: [STARTING] Finding all files with the extension ".TestCase.php"
FireTest: [LOADING] Test file "/home/projects/firedi/UA1Labs/Fire/Di/Graph.TestCase.php"
FireTest: [LOADING] Test class "Test\UA1Labs\Fire\Di\GraphTestCase"
FireTest: [LOADING] Test file "/home/projects/firedi/UA1Labs/Fire/Di/ClassDefinition.TestCase.php"
FireTest: [LOADING] Test class "Test\UA1Labs\Fire\Di\ClassDefinitionTestCase"
FireTest: [LOADING] Test file "/home/projects/firedi/UA1Labs/Fire/Di.TestCase.php"
FireTest: [LOADING] Test class "Test\UA1Labs\Fire\DiTestCase"
FireTest: [LOADING] Test file "/home/projects/firedi/vendor/ua1-labs/firetest/UA1Labs/Fire/Test/TestCase.TestCase.php"
FireTest: [LOADING] Test class "Test\UA1Labs\Fire\Test\TestCaseTestCase"
FireTest: [LOADING] Test class "Test\UA1Labs\Fire\Test\TestCaseMock"
FireTest: [LOADING] Test file "/home/projects/firedi/vendor/ua1-labs/firetest/UA1Labs/Fire/Test/Suite.TestCase.php"
FireTest: [LOADING] Test class "Test\UA1Labs\Fire\Test\SuiteTestCase"
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testConstructor()
FireTest: [PASSED] Not throw an exception when the class is constructed
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testAddResource()
FireTest: [PASSED] Add a resource to the resource graph.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testAddDependencies()
FireTest: [PASSED] Add dependencies to a resource.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testAddDependency()
FireTest: [PASSED] Add a dependency to a resource.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testRunDependencyCheck()
FireTest: [PASSED] Return an error code of "1" (Resourece Not Found).
FireTest: [PASSED] Contain which resource was missing.
FireTest: [PASSED] Return and error code "2" (Circular Dependnecy).
FireTest: [PASSED] Contain the resource of which caused the circular dependency.
FireTest: [RESULT] (Passed: 4, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testGetDependencyResolveOrder()
FireTest: [PASSED] Resolve dependencies in the order they need to be resolved.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\GraphTestCase::testResetDependencyCheck()
FireTest: [PASSED] Reset a dependency check.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Di\ClassDefinitionTestCase::testConstructor()
FireTest: [PASSED] Not throw an exception when the class is constructed
FireTest: [PASSED] Have set a serviceId of "Test\UA1Labs\Fire\Di\MyTestClass".
FireTest: [PASSED] Have set a classDef as a ReflectionClass object.
FireTest: [PASSED] Have set a dependency of "Test\UA1Labs\Fire\Di\MyDependentClass"
FireTest: [RESULT] (Passed: 4, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\DiTestCase::testConstructor()
FireTest: [PASSED] The constructor should not throw an execption and be an instance of Di.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\DiTestCase::testPutObject()
FireTest: [PASSED] Put an object in the cache without an exception.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\DiTestCase::testGetObject()
FireTest: [PASSED] Resolve all dependencies for TestClassA and return the TestClassA object.
FireTest: [PASSED] Have placed TestClassA, TestClassB, and TestClassC within the object cache.
FireTest: [PASSED] Resolve all dependencies for TestClassD and return it.
FireTest: [PASSED] Have set ::A as TestClassA on TestClassD object.
FireTest: [PASSED] Have set ::B as TestClassB on TestClassD object.
FireTest: [PASSED] Have set ::C as TestClassC on TestClassB.
FireTest: [PASSED] Throw an exception if a the class you are trying to get does not exists.
FireTest: [PASSED] Throw an exception if a circular dependency is detected.
FireTest: [RESULT] (Passed: 8, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\DiTestCase::testGetWithObject()
FireTest: [PASSED] Return a TestClassB object.
FireTest: [PASSED] Prove that TestClassB has a $C variable that is TestClassC.
FireTest: [PASSED] Prove that the object cache does not contain a TestClassB
FireTest: [PASSED] Throw and exception if a the class you are trying to get does not exists.
FireTest: [RESULT] (Passed: 4, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\DiTestCase::testGetObjectCache()
FireTest: [PASSED] Return an object cache array with a key "TestObject".
FireTest: [PASSED] Return an object cache with the object we put into it.
FireTest: [RESULT] (Passed: 2, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\DiTestCase::testClearObjectCache()
FireTest: [PASSED] Remove all objects from the object cache
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Test\TestCaseTestCase::testConstructor()
FireTest: [PASSED] Return an instance object of the TestCase object without throwing an exception.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: [RUNNING] Test\UA1Labs\Fire\Test\SuiteTestCase::testConstructor()
FireTest: [PASSED] Return an instance object of the Suite object without throwing an exception.
FireTest: [RESULT] (Passed: 1, Failed: 0)
FireTest: ***********************************************************
FireTest: ███████╗██╗ ██╗ ██████╗ ██████╗███████╗███████╗███████╗
FireTest: ██╔════╝██║ ██║██╔════╝██╔════╝██╔════╝██╔════╝██╔════╝
FireTest: ███████╗██║ ██║██║ ██║ █████╗ ███████╗███████╗
FireTest: ╚════██║██║ ██║██║ ██║ ██╔══╝ ╚════██║╚════██║
FireTest: ███████║╚██████╔╝╚██████╗╚██████╗███████╗███████║███████║
FireTest: ╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝╚══════╝╚══════╝╚══════╝
FireTest: ***********************************************************
FireTest: [FINAL] (Passed: 33, Failed: 0)

Add The Test Command To Your Composer.json

To make things a lot simpler for you to run all of your test cases, simply add the test runner to the script section of your project’s composer.json.

"scripts": {
    "test": "php vendor/ua1-labs/firetest/scripts/runner.php"
}

The post FireTest – Getting Started appeared first on UA1 Labs.

Top comments (0)