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.
- Laravel
- Symfony
- CodeIgniter
- Zend Framework / Laminas Project
- Phalcon
- CakePHP
- Yii (Framework)
- Slim
- FuelPHP
- Fat-Free Framework
- 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.
- 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.
<?php declare(strict_types=1); use PHPUnit\Framework\TestCase; final class EmailTest extends TestCase { public function testCanBeCreatedFromValidEmailAddress(): void { $this->assertInstanceOf( Email::class, Email::fromString('user@example.com') ); } public function testCannotBeCreatedFromInvalidEmailAddress(): void { $this->expectException(InvalidArgumentException::class); Email::fromString('invalid'); } public function testCanBeUsedAsString(): void { $this->assertEquals( 'user@example.com', Email::fromString('user@example.com') ); } }
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.
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.
- 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.
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("abc@gmail.com"); 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.
- 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.
click "Sign in" enter "abc@gmail.com" into "Email" enter "your_password" into "Password" click “Login”
You can read more about the features of testRigor here.