12

I have added the PHPUnit dependency in the composer.json:

"require": {
  "php-ds/php-ds": "v1.2.0",
  "phpunit/phpunit": "v7.5.16"
}

and ran composer update as I have php-ds installed already.

This installed the PHPUnit in the vendor directory, but when I check phpunit in command line it says:

phpunit command not found

3
  • 4
    how did you type the command? you are installing phpunit using composer, so you need to type the absolute path of phpunit executable file to the command line, i.e: [yourprojectdir]/vendor/bin/phpunit testfile.php Commented Sep 29, 2019 at 23:24
  • 2
    also you are placeing phpunit to "require" section which will install phpunit in production mode, it is recommend to put dependencies which you only need during development mode inside "required-dev" section, make your project cleaner and lighter. Commented Sep 29, 2019 at 23:29
  • Thanks, I accessed it as /vendor/bin/phpunit and it worked. Also, as advised i have changed to use in require-dev. Commented Sep 29, 2019 at 23:40

1 Answer 1

15

When you run $ phpunit on the command-line (e.g. bash), the system will look for phpunit using the PATH variable, from the bash docs:

PATH   The search path for commands.  It is a colon-separated list of directories in which the shell
      looks for commands (see COMMAND EXECUTION below).  A zero-length (null) directory name in the
      value of PATH indicates the current directory.  A null directory name may appear as two adja‐
      cent  colons,  or as an initial or trailing colon.  The default path is system-dependent, and
      is set by the administrator who installs bash.  A common value is ``/usr/local/bin:
      /usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin''.

You can bypass search using the PATH variable by using an absolute path:

$ /absolute/path/to/vendor/bin/phpunit

Or a relative path (the stop character (.) means the current directory):

$ ./vendor/bin/phpunit

You actually omit the stop slash part: $ vendor/bin/phpunit.

To avoid having to type the path you can use a bash alias (if you're using bash):

$ alias phpunit='./vendor/bin/phpunit'

Or to save typing:

$ alias p='./vendor/bin/phpunit'

See How do I create a permanent bash alias for more information on aliases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.