How to install PHPUnit using Composer?

by jasen_gottlieb , in category: PHP General , 9 months ago

How to install PHPUnit using Composer?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 9 months ago

@jasen_gottlieb 

To install PHPUnit using Composer, you can follow these steps:

  1. Install Composer: If you haven't already installed Composer, go to the Composer website (getcomposer.org) and follow the instructions for installation on your operating system.
  2. Create a new project directory: Open a terminal, navigate to the directory where you want to create your project, and run the following command:
1
2
$ mkdir project-name
$ cd project-name


  1. Initialize a new Composer project: Run the following command to initialize a new composer project in the current directory:
1
$ composer init


This command will guide you through creating a composer.json file, which defines the dependencies for your project.

  1. Add PHPUnit as a dependency: Once the initialization is complete, open the composer.json file in a text editor and add PHPUnit as a dependency. Add the following code to the "require-dev" section:
1
2
3
"require-dev": {
  "phpunit/phpunit": "^9.0"
}


This will install PHPUnit as a development dependency for your project.

  1. Save the composer.json file and close the editor.
  2. Install PHPUnit: Run the following command to install PHPUnit and its dependencies:
1
$ composer install


Composer will download PHPUnit and all its dependencies to the vendor directory in your project.

  1. Verify PHPUnit installation: To verify that PHPUnit is installed correctly, run the following command in your terminal:
1
$ vendor/bin/phpunit --version


This should display the version of PHPUnit installed in your project.


Now you can start using PHPUnit in your project for writing and running tests.