How to install PHPUnit in a PHP project?

To install PHPUnit in a PHP project, you can follow these steps:

  1. Ensure that you have Composer installed globally on your system. Composer is a dependency manager for PHP that PHPUnit uses to install and manage its dependencies.

  2. Navigate to your project's root directory using the command line.

  3. Run the following command to initialize a new Composer configuration file (composer.json) in your project:

    composer init

    This command will prompt you to provide information about your project, such as its name, description, version, etc. You can press Enter to accept the default values or provide your own.

  4. Once the composer.json file is created, open it and add the following line in the "require-dev" section:

    "phpunit/phpunit": "^9.0"

    This tells Composer to install PHPUnit as a development dependency for your project.

  5. Save the composer.json file and run the following command to install PHPUnit and its dependencies:

    composer install

    Composer will download and install the specified version of PHPUnit and its required dependencies.

  6. After the installation is complete, you should see a new directory named "vendor" in your project's root directory. This directory contains all the dependencies managed by Composer, including PHPUnit.

  7. PHPUnit should now be available for you to use in your PHP project. You can verify its installation by running the following command in the command line:

    vendor/bin/phpunit --version

    You should see the PHPUnit version displayed if it is installed successfully.

You can now start writing and running PHPUnit tests in your PHP project.