Turn your manual testers into automation experts Request a DemoStart testRigor Free

Phalcon Framework Testing

Phalcon Framework Testing

Phalcon is an open-source, full-stack PHP framework for web application development, optimized for low server consumption and high performance. It was designed to increase performance and reduce overhead compared to traditional PHP frameworks. Phalcon is implemented as a web server extension written in C, which makes it faster and more efficient than other PHP frameworks that are written in PHP and executed as scripts.

Phalcon is loosely coupled, which allows developers to use only the objects that they need as components based on the needs of their applications. Some key features of Phalcon include MVC (Model-View-Controller) architecture, caching, security, ORM (Object-Relational Mapping), and routing. These features allow developers to build fast, scalable, and secure web applications in a streamlined and efficient manner.

You can test Phalcon framework-based applications in the following ways:

It's important to note that testing is an integral part of the development process and should be a part of your workflow from the start. This can help catch bugs early on and ensure that your application works as desired.

Unit testing

Unit testing is the testing of individual components of the application in isolation. As a rule of thumb, make sure to write small unit tests. This means that your methods focus on a single functionality so that its corresponding unit test is straightforward. For unit testing, if your method depends on other methods, data sources, or APIs, then you will have to mock the response values in your unit test cases.

Phalcon supports PHPUnit for testing PHP code. All tests need to be stored under the 'tests' directory. To help build unit tests, Phalcon offers a few abstract classes that can be used to bootstrap the unit tests. These files exist in the Phalcon Incubator. You can use the Incubator library by adding it as a dependency or by adding it to composer.json.

Below is an example of a unit test. First, create a base unit test called UnitTestCase.php in your tests directory.

<?php

use Phalcon\Di;
use Phalcon\Test\UnitTestCase as PhalconTestCase;

abstract class UnitTestCase extends PhalconTestCase
{
  /**
   * @var bool
   */
  private $_loaded = false;

  public function setUp()
  {
    parent::setUp();

    // Load any additional services that might be required during testing
    $di = Di::getDefault();

    // Get any DI components here. If you have a config, be sure to pass it to the parent

    $this->setDi($di);

    $this->_loaded = true;
  }

  /**
   * Check if the test case is setup properly
   *
   * @throws \PHPUnit_Framework_IncompleteTestError;
   */
  public function __destruct()
  {
    if (!$this->_loaded) {
      throw new \PHPUnit_Framework_IncompleteTestError(
        "Please run parent::setUp()."

It is a good idea to separate your unit tests into namespaces. Hence for this test, we will use the namespace 'Test'. The file will look like this: tests\Test\UnitTest.php.

<?php

  namespace Test;
  
  /**
   * Class UnitTest
   */
  class UnitTest extends \UnitTestCase
  {
    public function testTestCase()
    {
      $this->assertEquals(
        "works",
        "works",
        "This is OK"
      );
  
      $this->assertEquals(
        "works",
        "works1",
        "This will fail"

Besides PHPUnit, Phalcon also supports Codeception for writing tests (we will cover this tool in the next section).

Integration testing

Integration tests aim to verify if two or more components work properly together. The test cases often target scenarios involving integrations with databases, file systems, network infrastructure, or other components. Unlike unit tests, where you fake or mock input parameters, integration tests utilize real data.

Some scenarios where integration tests are used:
  1. Verifying crucial read-write operations in databases.
  2. Services can be tested by mocking HTTP calls.
  3. Checking GET and POST routes, controllers, and pipelines.

Here is an example of an HTTP test:

<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;

class ExampleTest extends TestCase
{
  /**
  * A basic test example.
  *
  * @return void
  */
  public function test_a_basic_request()
  {
    $response = $this->get('/');

    $response->assertStatus(200);
  }
}

Codeception for integration testing

Codeception is a testing framework for PHP that allows you to write and run tests for web applications. It provides an easy-to-use interface for writing unit and integration tests. To use Codeception for integration testing of Phalcon applications, you first need to install it along with a Phalcon module.

You can install Codeception using Composer:
composer require codeception/codeception

Next, you need to install the Phalcon module for Codeception:
composer require codeception/module-phalcon

Next, you can create a new test by running the following command:
php vendor/bin/codecept generate:cest Integration LogIn

The above command will create a new LogIn test. To write your integration tests, you will need to define a test scenario using the Codeception API. Here's an example:

<?php

use Codeception\Util\Fixtures;
use Codeception\Module\Phalcon;

class LogInCest
{
    /**
     * @var Phalcon
     */
    protected $phalcon;

    public function _before(Phalcon $phalcon)
    {
        $this->phalcon = $phalcon;
    }

    public function tryToTest(Phalcon $phalcon)
    {
        $phalcon->amOnPage('/');
        $phalcon->see('Welcome to the store');
        $phalcon->fillField('username', 'my-username');
        $phalcon->fillField('password', 'my-password');
        $phalcon->click('Sign In');
        $phalcon->see('You are logged in');
    }
}

Finally, to run the tests, you can simply run the following command:
./vendor/bin/codecept run

This command will execute all the tests defined in the test file and output the results to the console.

End-to-End testing

End-to-end (E2E) testing focuses on verifying the behavior of an entire system, from start to finish, simulating a real user's interaction. The aim of E2E testing is to catch bugs and issues that might not be detected by unit or integration tests.

  • Selenium-based tools
  • Nightwatch.js
  • testRigor

Selenium-based tools for end-to-end testing

Selenium is a dated but still popular open-source tool for automating browser testing. With Selenium, you can write E2E tests that simulate a user's interaction with a web application, including clicking buttons, filling out forms, and navigating through the application.

Nightwatch.js for end-to-end testing

Nightwatch.js is an automated testing framework for Node.js applications that provides a simple API for writing E2E tests. Nightwatch.js uses the WebDriver API to interact with a web browser, and can be used to test Phalcon applications by simulating a user's interaction with the application through a web browser.

testRigor for end-to-end testing

With the testRigor cloud-hosted AI tool, you can achieve the closest representation of the actual user behavior. Tests are created based on the visual layer and don’t rely on implementation details - making them as close to the real human view as technically possible. As an added benefit, it gives extra stability - tests will survive scenarios where an element locator is accidentally changed, although the UI stays the same.

testRigor is no-code, meaning even manual QA testers are able to automate and maintain any tests, add assertions, and modify as desired. Tests in testRigor look more like manual test steps than typical automated scenarios. Thus, the speed of test creation is up to 15X more, and the test maintenance issue is solved entirely. Tests are extremely stable, and you can receive results in as soon as 30 minutes.

You can easily test web, API calls, databases, emails, SMS alerts, and much more. Below are a couple of examples.

enter "My_input!" into table "Actions" at row "251" and column "Additional Data"
click "Open channel" within the context of second table at row "184" and column "Action"
send email to "user@customer.com" with subject "Test message" and  body "Hi, new content is available."
check that email from "user@customer.com" is delivered

Besides these capabilities, testRigor has integrations with issue management, test management, CI/CD, and monitoring tools. Feel free to explore more here.

Conclusion

You can ensure the quality of your PHP code by performing unit tests, integration tests, and end-to-end tests. You can deliver a superior product by leveraging Phalcon's advanced development capabilities and utilizing the right testing tools.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.