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

PHP Testing

PHP Testing

PHP is the most commonly used server-side scripting language to create dynamic websites. Danish programmer Rasmus Lerdorf developed PHP in 1993. Originally, PHP stood for Personal Home Page, but now it’s known as Hypertext Preprocessor. The latest stable version currently available on the market is PHP 7.4. Many content management sites, like WordPress, Drupal, etc., use PHP. As per W3Tech reports, as of October 2022, PHP has been used by 80% of websites across the globe. PHP scripts are executed on the server side, then the server builds the output, and next, the HTML result is sent to the browser for rendering. Developers can directly embed PHP code into the HTML pages.

PHP has been one of the most favorable languages for developing dynamic websites for over a decade. But currently, the websites are getting very complex, and developers have also started using other components such as CMS. So for developing a new web application, creating the codebase from scratch requires a lot of effort and time; it's monotonous and repetitive. This is where PHP frameworks come into the picture. These frameworks provide built-in libraries and components to improve the workflow and streamline the process. The main advantages of using the frameworks are faster development, better performance, less code, common libraries, better maintainability, etc. Let's take a look at a list of the most widely used PHP frameworks:
  • Laravel
  • Symfony
  • CodeIgniter
  • Zend Framework / Laminas Project
  • Phalcon
  • CakePHP
  • Yii (Framework)
  • Slim
  • FuelPHP
  • Fat-Free Framework
So when we consider developing a web application, testing plays a crucial role in it. We can test a PHP website using different methods or tools. According to test automation pyramid, a robust testing strategy should include at least 3 layers:
  • Unit testing
  • Integration testing
  • System or end-to-end testing

Unit and Integration Testing

As you know, unit testing is responsible for testing individual chunks of code, vs. integration testing aims to test integration between modules. Any solid testing strategy ought to include both of these testing types.

When it comes to frameworks, below are the most commonly used ones:
  • PHPUnit
  • Codeception
  • Atoum
  • Guzzle

PHPUnit

PHPUnit is an open-source testing framework widely used for unit testing of PHP applications. Based on xUnit architecture for the unit testing framework, it's a straightforward tool generally executed from the command line. Testing is primarily performed based on the assertions we add to the code. PHPUnit provides a robust framework for unit testing, where developers can test various controllers. However, it's not an ideal choice when it comes to API unit testing.

Below is a test code example:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class EmailTest extends TestCase
{
  public function testCanBeCreatedFromValidEmailAddress(): void
  {
    $this->assertInstanceOf(
      Email::class,
      Email::fromString('[email protected]')
    );
  }

  public function testCannotBeCreatedFromInvalidEmailAddress(): void
  {
    $this->expectException(InvalidArgumentException::class);

    Email::fromString('invalid');
  }

  public function testCanBeUsedAsString(): void
  {
    $this->assertEquals(
      '[email protected]',
      Email::fromString('[email protected]')
    );
  }
}

Codeception

Codeception is a solid wrapper on top of PHPUnit. Codeception is a testing framework mainly used for testing integration scenarios (although you can also use it for unit testing). We need to have a composer for installing Codeception. It's based on the BDD framework, which makes the code easy to read and debug. It provides various integration modules for different types of testing like database testing or API testing. It has inbuilt plugins to integrate with Jenkins. Since it supports different integrations and testing types, it's also called a full-stack testing framework.

Here's how your unit test might look like:
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']));
  }
}

Atoum

Atoum is another unit testing framework that provides a lot of inbuilt execution engines, such as the inline engine, isolation engine, and concurrent engines. Atoum delivers a high level of security for test execution as it isolates test methods from its process. We can execute test cases either in parallel or sequential. It also helps in mocking native PHP functions. Developers can easily integrate Atoum into the development project without any hassle.

Guzzle

Guzzle is an HTTP client framework for API testing between two PHP modules and can be used together with PHPUnit. Guzzle has a simple interface for building query strings, POST requests, streaming large uploads, etc. Guzzle uses a handler and middleware system to send HTTP requests. Users can set a proxy in Guzzle to hide the IP, modify request-response on the proxy side, and gather statistics.

End-to-End Testing

End-to-End testing or system testing mainly focuses on testing the entire user flow. These tests typically take a longer time to run; however, they are most accurate when built correctly. E2E tests cover the whole flow, not just its individual components. This type of testing can be performed either manually or through an automated tool.

Manual Testing

Manual testing is the easiest and fastest way to catch many ad-hoc issues. Testers can execute scenarios based on a test case or perform exploratory testing to capture a lot of edge cases. Manual execution of test cases is time-consuming, however, and if the development model is Agile, the QA team gets less time for testing the new features and the regression test cases. Another limitation is the visual regression testing which is impossible to perform manually. It is common nowadays to depend on automated testing to overcome these obstacles and ensure a more robust process.

Automated Testing

Automated testing helps to execute a vast volume of test cases within a short timeframe. With the support of automation, the development and QA teams will be able to meet the deadlines for project releases. There are a lot of tools and frameworks in the market for PHP websites. Let's look into the main options when it comes to end-to-end and system testing.

Here are the most popular and widely used tools:
  • Storyplayer
  • Kahlan
  • Selenium
  • testRigor

Storyplayer

Storyplayer is an open-source testing framework for end-to-end testing. Storyplayer, we can test both web and API scenarios or create scenarios involving both web and API. In Storyplayer, all tests are written in PHP. There is no domain-specific language used. A composer is required to install Storyplayer. For developing scenarios, a good understanding of PHP is required.

Kahlan

Kahlan is a framework for end-to-end testing based on BDD (Behavior Driven Development). It uses Mocha capabilities, i.e., test cases are written in "describe-it" format. Kahlan supports code coverage testing and also has its own built-in reports and exporters. Users can develop scripts using Python and Ruby languages.

Selenium WebDriver

Selenium is one of the legacy automation frameworks currently available in the market. Selenium is mainly intended for web browser automation, and many third-party plugins are available in the market for generating reports and reading test data.

Here's an example of a test created with Selenium for a login function:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LoginAutomation {
  @Test
  public void login() {
    System.setProperty("webdriver.chrome.driver", "path of driver");
    WebDriver driver=new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://mywebsite.com/users/log_in");
    WebElement username=driver.findElement(By.id("user_email_Login"));
    WebElement password=driver.findElement(By.id("user_password"));
    WebElement login=driver.findElement(By.name("commit"));
    username.sendKeys("[email protected]");
    password.sendKeys("your_password");
    login.click();
    String actualUrl="https://mywebsite.com/dashboard";
    String expectedUrl= driver.getCurrentUrl();
    Assert.assertEquals(expectedUrl,actualUrl);
  }
}

testRigor

testRigor is an AI-integrated no-code automation tool that excels specifically with end-to-end testing. testRigor helps the QA team write test scripts in plain English; even manual QA can write robust test scripts with nearly zero test maintenance.

Let's mention a few important advantages of testRigor over other end-to-end testing tools discussed here:
  • Tests are extremely easy and fast to write, allowing companies to scale test coverage quickly.
  • There is no dependency on the HTML page structure. Typically, if any changes are made to the DOM, the test will fail, leading to false negative scenarios. testRigor uses integrated AI to capture all element locators and survives subsequent modifications.
  • The test maintenance issue is solved for good. Updating tests is as easy as using find and replace functionality.
Here's how an example for a login aboe looks like in testRigor:
click "Sign in"
enter "[email protected]" into "Email"
enter "your_password" into "Password"
click “Login”

You can read more about the features of testRigor here.

How to do End-to-end Testing with testRigor

Let us take the example of an e-commerce website that sells plants and other gardening needs. We will create end-to-end test cases in testRigor using plain English test steps.

Step 1: Log in to your testRigor app with your credentials.

Step 2: Set up the test suite for the website testing by providing the information below:

  • Test Suite Name: Provide a relevant and self-explanatory name.
  • Type of testing: Select from the following options: Desktop Web Testing, Mobile Web Testing, Native and Hybrid Mobile, based on your test requirements.
  • URL to run test on: Provide the application URL that you want to test.
  • Testing credentials for your web/mobile app to test functionality which requires user to login: You can provide the app’s user login credentials here and need not write them separately in the test steps then. The login functionality will be taken care of automatically using the keyword login.
  • OS and Browser: Choose the OS Browser combination on which you want to run the test cases.
  • Number of test cases to generate using AI: If you wish, you can choose to generate test cases based on the App Description text, which works on generative AI.

Step 3: Click Create Test Suite.

On the next screen, you can let AI generate the test case based on the App Description you provided during the Test Suite creation. However, for now, select do not generate any test, since we will write the test steps ourselves.

Step 4: To create a new custom test case yourself, click Add Custom Test Case.

Step 5: Provide the test case Description and start adding the test steps.

For the application under test, i.e., e-commerce website, we will perform below test steps:

  • Search for a product
  • Add it to the cart
  • Verify that the product is present in the cart

Test Case: Search and Add to Cart

Step 1: We will add test steps on the test case editor screen one by one.

testRigor automatically navigates to the website URL you provided during the Test Suite creation. There is no need to use any separate function for it. Here is the website homepage, which we intend to test.

First, we want to search for a product in the search box. Unlike traditional testing tools, you can identify the UI element using the text you see on the screen. You need not use any CSS/XPath identifiers.

For this search box, we see the text “What are you looking for?” So, to activate the search box, we will use the exact text in the first test step using plain English:
click "What are you looking for?"

Step 2: Once the cursor is in the search box, we will type the product name (lily), and press enter to start the search.

type "lily"
enter enter

Search lists all products with the “lily” keyword on the webpage.

Step 3: The lily plant we are searching for needs the screen to be scrolled; for that testRigor provides a command. Scroll down until the product is present on the screen:

scroll down until page contains "Zephyranthes Lily, Rain Lily (Red)"

When the product is found on the screen, testRigor stops scrolling.

Step 4: Click on the product name to view the details:

click "Zephyranthes Lily, Rain Lily (Red)"

After the click, the product details are displayed on the screen as below, with the default Quantity as 1.

Step 5: Lets say, we want to change the Quantity to 3, so here we use the testRigor command to select from a list.

select "3" from "Quantity"
After choosing the correct Quantity, add the product to the cart.
click "Add to cart"

The product is successfully added to the cart, and the “Added to your cart:” message is displayed on webpage.

Step 6: To assert that the message is successfully displayed, use a simple assertion command as below:

check that page contains "Added to your cart:"

Step 7: After this check, we will view the contents of the cart by clicking View cart as below:

click "View cart"

Step 8: Now we will again check that the product is present in the cart, under heading “Your cart” using the below assertion. With testRigor, it is really easy to specify the location of an element on the screen.

check that page contains "Zephyranthes Lily, Rain Lily (Red)" under "Your cart"

Complete Test Case

Here is how the complete test case will look in the testRigor app. The test steps are simple in plain English, enabling everyone in your team to write and execute them.

Click Add and Run.

Execution Results

Once the test is executed, you can view the execution details, such as execution status, time spent in execution, screenshots, error messages, logs, video recordings of the test execution, etc. In case of any failure, there are logs and error text that are available easily in a few clicks.

You can also download the complete execution with steps and screenshots in PDF or Word format through the View Execution option.

testRigor’s Capabilities

Apart from the simplistic test case design and execution, there are some advanced features that help you test your application using simple English commands.

  • Reusable Rules (Subroutines): You can easily create functions for the test steps that you use repeatedly. You can use the Reusable Rules to create such functions and call them in test cases by simply writing their names. See the example of Reusable Rules.
  • Global Variables and Data Sets: You can import data from external files or create your own global variables and data sets in testRigor to use them in data-driven testing.
  • 2FA, QR Code, and Captcha Resolution: testRigor easily manages the 2FA, QR Code, and Captcha resolution through its simple English commands.
  • Email, Phone Call, and SMS Testing: Use simple English commands to test the email, phone calls, and SMS. These commands are useful for validating 2FA scenarios, with OTPs and authentication codes being sent to email, phone calls, or via phone text.
  • File Upload/ Download Testing: Execute the test steps involving file download or file upload without the requirement of any third-party software. You can also validate the contents of the files using testRigor’s simple English commands.
  • Database Testing: Execute database queries and validate the results fetched.

testRigor enables you to test web, mobile (hybrid, native), API, and desktop apps with minimum effort and maintenance.

Additional Resources

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