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

Smart Framework PHP Testing

Smart Framework PHP Testing

Having a robust web development framework is pivotal for crafting high-quality websites. Smart Framework PHP stands out as a web development platform allowing developers to utilize both PHP and JavaScript. As an open-source framework, it enables the development of web-based applications tailored for web clients, mobile devices, and desktops. Notably, Smart Framework employs the MVC (model-view-controller) pattern, which comes with built-in dependency injection. In this pattern, the model embodies the data and business logic of the application, the view manages the presentation layer, and the controller serves as a liaison between the model and the view. Compared to other PHP frameworks such as Zend, Laravel, or Symphony, Smart Framework is lightweight, packed with features, and boasts superior performance.

In this article, we'll look into methods for testing the PHP code you craft using this framework. Many of these testing strategies remain applicable for frontend code, though the specific frameworks for unit and integration testing may differ. By adhering to the testing pyramid, you can evaluate your application developed with Smart.Framework through:

Unit Testing

Unit tests are the base of the testing pyramid and comprise the biggest chunk of test cases. The intention of these tests is to validate every single unit of code like functions, classes, or APIs. Just like the areas these tests focus on, unit tests are expected to be small and concise, focusing on just a single outcome per test. There are many test frameworks available for performing unit and integration testing on PHP code. Let’s look at some of the common ones that are used with this framework. PHPUnit
PHPUnit is widely used to write unit tests for PHP code. It provides a comprehensive set of features and tools to write and execute unit tests. It offers capabilities like writing assertions in tests, performing mocking, using fixtures and stubs, data providers for parameterized tests, setup and teardown methods, test suites for grouping tests, and test coverage reporting. By default, PHPUnit looks for files with "Test" in their names or located in directories named "tests". You can also specify the path to a specific test file or directory to run only those tests. Here is a simple example of a test using PHPUnit.
use PHPUnit\Framework\TestCase;

final class GreeterTest extends TestCase
{
  public function testGreetsWithName(): void
  {
    $greeter = new Greeter;
    $greeting = $greeter->greet('Alice');
    $this->assertSame('Hello, Alice!', $greeting);
  }
}

Codeception

Codeception is a versatile testing framework for PHP that supports various testing types, including unit, integration, and acceptance testing. It provides an expressive syntax, built-in support for popular PHP frameworks, and a range of powerful testing features. Codeception can also execute any tests written in PHPUnit. Here is an example of a test case using Codeception.
namespace Tests\Unit;

use \Tests\Support\UnitTester;

class UserTest extends \Codeception\Test\Unit
{
  public function testValidation()
  {
    $user = new \App\User();
    $user->setName(null);
    $this->assertFalse($user->validate(['username']));
    $user->setName('toolooooongnaaaaaaameeee');
    $this->assertFalse($user->validate(['username']));
    $user->setName('davert');
    $this->assertTrue($user->validate(['username']));
  }
}

PHPSpec

PHPSpec is a testing framework that follows a behavior-driven development (BDD) approach, focusing on specifying the behavior and design of classes and objects. While PHPUnit is a widely used unit testing framework in PHP, PHPSpec takes a different approach by encouraging developers to describe the expected behavior of code through specifications. PHPSpec aims to create more expressive and readable tests that describe how objects should behave.

Integration Testing

Just testing the different units of an application in isolation is not enough. It is important to see if all these units are working together in sync. Integration testing helps in achieving this. By targeting integrations between various components of the application like an API and a database. Here are some scenarios where integration testing is likely to be used.

  • Testing if the application interacts smoothly with the database during various read and write operations.
  • Testing the integration with external services, such as payment gateways, email providers, or APIs. This involves handling requests and responses, along with error and exception handling.
  • Testing the integration of file uploads and downloads.
  • Verifying the integration of authentication and authorization mechanisms, such as login, registration, and user access control. This can also include the integration of identity providers, such as OAuth or LDAP.
  • Testing the integration of internal APIs or web services with other components of the application.

Most of the unit testing tools can be used to write integration tests. Many of these test frameworks offer features such as environment configurations and capabilities to interact with the modules without fully booting the system to make the process faster.

End-to-end Testing

The top tier of the testing pyramid is reserved for end-to-end tests. These tests should be framed from an end user’s perspective, meaning that they will comprise user journeys, and can often be lengthy and complex. Members from other teams like business analysts, manual testers, and product owners sometimes all collaborate to create these tests. End-to-end tests tend to exercise all layers of the application and quite often start from the UI, since that is where the user would interact with the application. Let’s take a look at some tools to do end-to-end testing of applications built using Smart Framework.

Codeception

Along with unit and integration testing, Codeception supports writing functional and user acceptance testing as well. Codeception allows this through PhpBrowser or WebDriver. BDD (behavior-driven development) is another important form of test writing that is supported here. Codeception provides support for Gherkin language, that is, the given-when-then format of writing tests. Here is an example of an e-commerce application that is testing the checkout process.
use Codeception\Attribute\Given;
use Codeception\Attribute\When;
use Codeception\Attribute\Then;

class AcceptanceTester extends \Codeception\Actor
{
  use _generated\AcceptanceTesterActions;

  #[Given('I have product with :num1 price in my cart')]
  
	public function iHaveProductWithPriceInMyCart($num1)
  {
    $productId = $this->haveRecord('Product', ['name' => 'randomProduct'.uniqid(), 'price' => $num1]);
    $this->amOnPage("/item/$productId");
    $this->click('Order');
  }

  #[When('I go to checkout process')]
  
	public function iGoToCheckoutProcess()
  {
    $this->amOnPage('/checkout');
  }
  
	#[Then('I should see that total number of products is :num1')]
  
	public function iShouldSeeThatTotalNumberOfProductsIs($num1)
  {
    $this->see($num1, '.products-count');
  }
  
	#[Then('my order amount is :num1')]
  
	public function myOrderAmountIs($num1)
  {
    $this->see($num1, '.total');
  }
}

Selenium

Selenium is a popular open-source framework for automating web-based test cases. It enables testers to automate the testing of web applications across different browsers and platforms. Selenium provides a suite of tools that allows users to interact with web browsers programmatically, mimicking user actions and verifying the behavior of web applications. It supports various programming languages like Java, Python, C#, Ruby, and more.

WebDriver is a key part of Selenium and allows users to automate browser actions, such as opening web pages, clicking links, filling out forms, and extracting data from web pages. Selenium also provides methods to assert and verify various elements and attributes on web pages, allowing users to validate expected behaviors and content. However, Selenium has certain limitations like requiring intensive test maintenance efforts, difficulty in cross-platform testing, and instability due to element-based locators like CSS and XPaths.

testRigor

testRigor is an AI-based end-to-end testing tool that ensures that you can automate your test cases without worrying about test maintenance. It uses AI to reduce test maintenance time by 99.5%, thus doing the heavy lifting for you. This is not the only place it uses AI though. It makes use of generative AI to help you create test cases for your application as well. Moreover, thanks to its smart AI-based engine, you get the liberty of writing test cases in plain English language, without having to code it. As we discussed earlier, end-to-end testing requires writing tests from an end user’s perspective which may not be possible if different team members are unable to collaborate. If you want a testing tool that is truly codeless and is effective in terms of ease of use, scalability, test creation, execution, and maintenance, then opt for testRigor.

Let's talk about test creation. Unlike these above-mentioned automation tools, you need not spend on a separate team of test automation engineers because testRigor lets you write tests in plain English language. Here’s an example of the same Codeception test, but this time in testRigor syntax.
login //pre-defined rule
navigate to catalog //pre-defined rule
check that page contains "Bluetooth earphones (Black)" to the left of "$600"
click on "Order" below "Bluetooth earphones (Black)"
check that page contains "Bluetooth earphones (Red)" to the left of "$600"
click "Order" below "Bluetooth earphones (Red)"
click "cart" 
click "checkout"
check that page contains "2 items"
check that page contains "Order total : $1200"

You might have noticed that testRigor makes referencing UI elements very easy, by just mentioning where you see the element on the screen. With testRigor, playing around with table data, diving into email and SMS content, or using test data is super easy. Plus, you can chat with APIs to see how they're doing and even peek into databases. Oh, and bonus: testRigor plays nice with all sorts of test management tools, databases, and those fancy infrastructure frameworks.

Conclusion

The task of building a web application can be simplified if one chooses the right framework not just for development, but also for testing. Using modern frameworks like testRigor that are truly capable of handling the load of end-to-end testing of a matured web application can help cut down costs so that you and your team can focus on the activities that really matter rather than on repetitive and cumbersome tasks like test maintenance.

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