In this tutorial, I will let you know how to install PHPUnit pears in the system. I also create a class and define method into them. We’ll create a test case and run test cases using the command.
PEAR is a framework and distribution system for reusable PHP components. You can find PHP pears Here. PHP provides many pears like PHPUnit, GDlib, etc, here you will learn how to install and upgrade your PHP pears in Windows.
Below are sample steps to needed install the PHP unit in your system, I am assuming you have installed PHP in your system.
PHPUnit is supposed to be a dev-dependency
because testing as a whole should only happen during development.
First off I’m assuming xampp is installed to C:\xampp
1. Open a command prompt and go to C:\xampp\php
2. Type php -v
, if you get php version that means PHP is installed otherwise first install PHP.
3. Type pear update-channels
(This command will updates all channel definitions)
4. Type pear upgrade
(This command will upgrades all existing packages and pear)
5. Type pear channel-discover pear.phpunit.de
(This IS phpunit pear)
6. Type pear install --alldeps phpunit/PHPUnit
(installs PHPUnit and all dependencies)
The PHPUnit development team announces the immediate availability of PHPUnit 9. This release adds new features, changes and removes existing features, and fixes bugs.
A detailed list of changes is available here.
Here are a few conventions to get you started:
Employee
class, the test class would be named EmployeeTest
.PHPUnit\Framework\TestCase
class.test
as a prefix. For example, to test a getEmp
Name method on the Employee
class, the method will be named test
GetEmpName.assertSame
to see that some method returns some expected value in the test
GetEmpName() method.Let’s create a project and cd into this directory:
$ cd test-project $ composer init
Define PHPUnit as dev dependency in the package.json
file:
"require-dev": { "phpunit/phpunit": "^9.5" },
The method into Employee.php
Class:
public function getEmpName(): string { return "Emp name is " . $this->name . "."; }
Let’s define the test case in EmployeeTest.php
class:
public function testGetEmpName() { $emp = new Employee(18, 'parvez'); $this->assertIsString($emp->getEmpName()); $this->assertStringContainsStringIgnoringCase('parvez', $emp->getEmpName()); }
You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder.
$ ./vendor/bin/phpunit --verbose tests
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
Thanks for sharing