Unable to run PHP unit - php

I followed the below tutorial step by step to run PHPUnit. But I am only getting options and configuration part.
https://www.youtube.com/watch?v=cRVkchE5kuE&t=393s
Followed the below steps to setup PHPUnit:-
Installed the composer.
Installed the PHPUnit via command line
PHP composer.phar require --dev phpunit/phpunit ^8
After these steps, I am able to get the version, options, configuration, and other information by using the command
PHPUnit (on path cd /vendor/bin/)
In the tutorial below steps used to run the PHPUnit:-
create phpunit.xml file on root and defined the below code.
<?xml version = "1.0" encoding = "UTF-8" ?>
<phpunit bootstrap = "vendor/autoload.php">
<testsuits>
<testsuit name = "unit">
<directory>tests</directory>
</testsuit>
</testsuits>
</phpunit>
Created the tests directory on the root and also create unit directory in the tests directory and again run the below command.
phpunit on path cd /vendor/bin/
But still, I am getting information part. but in the video, he is getting tested ok line after the command.
Please suggest me, what am I doing wrong?
This is the file structure
R:
vendor
bin
composer
doctrine
.
.
.
.
.
.
autoload.php
tests
unit
composer.json
composer.lock
composer.phar
phpunit.xml

Don't run phpunit from the /vendor/bin/ directory, run it from your project's root directory (the same place as the phpunit.xml file):
/project/dir # vendor/bin/phpunit

Related

Access phpunit inside my project folder. phpunit: command not found

I have installed phpunit by opening gitbash of my windows system and running the following command.
composer require --dev phpunit/phpunit ^9
It installed successfully and when I type following command
./vendor/bin/phpunit --version
it returns PHPUnit 9.5.21 . I think phpunit is installed in this location
C:\Users\ERIKSON\vendor\phpunit
Now I have a project in my xampp folder
C:\xampp\htdocs\erikson-project\wp-content\plugins\youtube-plugin
when I open GitBash in this project location and run the command PHPUnit it says
bash: phpunit: command not found
. How can I solve this ? Please help.
I have composer.json file inside youtube-plugin folder and the code is
C:\xampp\htdocs\erikson-project\wp-content\plugins\youtube-plugin\composer.json
{
"require-dev": {
"phpunit/phpunit": "9.5"
}
}
Do I need to add anything in my windows system environment variable?

symfony bridge isntalling phpunit to bin directory

I have a fresh symfony project and I need to install phpunit, so I run composer require --dev symfony/phpunit-bridge to install it. It creating symlink to phpunit executable in bin/ folder. But when I'm running tests using bin/phpuinit tests/ command I'm getting message "No composer.json found in the current directory, showing available packages from packagist.org
" and it starting phpunit installation into bin directory and at the end I have bin/.phpunit folder and all the phpunit related files there. Why it installing php unit there and not into vendor, why it's not see composer??? what I'm doing wrong ? Thanks in advance!
What you are using is the symfony/phpunit-bridge
It basically is way more flexible than just phpunit in the vendor folder, allowing to adapt to multiple versions of PHPUnit based on your environment.
Please read the documentation linked above for more details. You're not doing anything wrong!
Another way would be a plain composer require phpunit/phpunit, which would work the "basic"/"standard" way.

Can't test the PHP package using PHPUnit, once I publish it

I've created a simple PHP library using PHP 5.6.
It includes PHPUnit test cases and I succeeded in creating the package.
Then I found that I can install these packages directly from GitHub, using composer, in other projects also. I can run tests independently at the development stage. But once I publish the package I can't run the tests, as it is not properly finding the autoloader files.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
I've added the xml files in root directory. The path of the autoload file is correct when it is independent. But the directory structure will be a different one once it is installed.
The problem still persist if we use require_once the autoloader file, as the directory structure change after installation.
Questions:
Can we test our package after installing it into any frameworks (laravel/Symfony) or in any other projects?
What is the best practice? Is the test needed for the developers who is using the package?
If yes, then any solution to solve this? Is there any other method to autoload problem in both environments?
Repo in Github
I would say you're best to look at the other repositories and see if it works or not.
For me
cd vendor/phpunit/phpunit && composer install && phpunit => worked
cd doctrine/collections/ && composer install && phpunit => worked
So it seems that it should be working for you.
If you look at the phpunit.xml.dist for the other vendors they use:
bootstrap="./tests/Doctrine/Tests/TestInit.php" (doctrine)
bootstrap="tests/bootstrap.php" (phpunit)
It looks like your file isn't so different. Are you sure you ran composer install from inside your packages directory inside the vendor folder?
Update:
I added your repository as a dependency to a default Symfony installation.
"repositories": [
{
"url": "https://github.com/jerintk/Validator.git",
"type": "git"
}
],
And in the require block:
"Jthedev/Validators": "dev-master"
I then ran
composer update
It ran fine. You need to run composer update and check in the new composer.lock because it's out of date.
From there I cd'ed into the directory for your repo.
cd vendor/Jthedev/Validators
I then ran
composer install
and
vendor/phpunit/phpunit/phpunit
and got
OK (2 tests, 2 assertions)
Update Two
(since this was too long for a comment)
#JTheDev composer update adds your dependencies for the laravel project, but it doesn't create the vendor folder inside your vendor/your-project directory. If composer installed all dependencies separately for each project, like:
vendor/
my-project/
vendor/
dependency-A
dependency-C
another-library/
vendor/
dependency-A
dependency-B
it would waste a lot of space and bandwidth. Instead composer gets all the dependencies and installs them in separate folders and they work with each other because composer loads them all using vendor/autoload.php for that project. What you're talking about is creating the vendor folder inside vendor/your-project. This is not normal - usually you only have to run tests when developing yourself, but anyway your question is about how to run your tests for your project when it's added as a dependency for another project.
To do this you need to run composer install inside the vendor/your-project directory, that means:
cd vendor/Jthedev/Validators && composer install && vendor/phpunit/phpunit/phpunit
Final update (hopefully)
From chat:
The autoloader is only generated when you run composer install inside your project directory. You are correct, the vendor folder should not be there usually but you need it if you want to do what you are trying to do. It is not conventional. Normally developers run their tests inside their project root folder, not on the dependencies, but your questions was "how can I run my tests when it is a dependency". The answer is you need to create the vendor and autoload files inside your project folder
The tests only run if you run composer install inside the project folder. But it's not a problem if the tests don't run without doing that.

Execute PHPUnit when is installed as part of a project

I have added composer to a standalone project as:
{
"require-dev": {
"phpunit/phpunit": "5.4.*"
}
}
After I run composer require "phpunit/phpunit=5.4.*" --dev the libraries gets installed under vendor/. I have write a small test case and I put it under tests/CollectionTest.php and I want to run it but ....
# phpunit
bash: phpunit: command not found
# phpunit --bootstrap vendor/autoload.php tests
bash: phpunit: command not found
I have also added this:
~/.composer/vendor/bin/
to ~/.bash_rc file. Did I miss something? How do I execute the test case?
PHPUnit is installed in vendor folder in your project directory rather than in global directory. Try to cd into your projects directory and run the PHPUnit by:
$ cd /{project-directory}
$ ./vendor/bin/phpunit
Detailed test setup can be configured with the phpunit.xml file.
You should find an alias in the bin directory to the ./vendor/phpunit/phpunit/phpunit path. So try:
>bin/phpunit
or
>vendor/phpunit/phpunit/phpunit
Hope this help

PHPUnit "Could not read phpunit.xml" on Travis CI

I am encountering a strange issue while trying to run PHP unit tests on Travis CI.
.travis.yml
sudo: false
language: php
php:
- 5.4
env:
- VUFIND_HOME=$PWD VUFIND_LOCAL_DIR=$PWD/local
before_script:
- pear install pear/PHP_CodeSniffer
- pear channel-discover pear.phing.info
- pear install phing/phing
- composer global require fabpot/php-cs-fixer
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- phpenv rehash
script:
- phpunit --stderr --configuration module/VuFind/tests/phpunit.xml
- phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
- phpcs --standard=PEAR --ignore=*/config/*,*/tests/* --extensions=php $PWD/module
- phing php-cs-fixer-dryrun
module/VuFind/tests/phpunit.xml is a third party framework
module/Swissbib/tests/phpunit.xml is our own code
module/Swissbib/tests/phpunit.xml
<phpunit bootstrap="Bootstrap.php">
<testsuites>
<testsuite name="sbvfrd">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>
The tests from the third party framework run without errors. Our own tests do not work and we get the error message:
$ phpunit --stderr --configuration module/Swissbib/tests/phpunit.xml
Could not read "module/Swissbib/tests/phpunit.xml".
Locally (Mac OS X) all the tests run through. Strangely enough the Bootstrap.php defined in module/Swissbib/tests/phpunit.xml runs completely through on Travis CI, I verified this using echo statements. Nevertheless phpunit tells us that it could not read phpunit.xml.
Travis: https://travis-ci.org/swissbib/vufind
Repo: https://github.com/swissbib/vufind (development branch)
Any ideas what could be going wrong?
I found the solution by downloading the phpunit source and debugging with it.
We were changing the directory within the Bootstrap.php file to a different location then the phpunit command was run from. We run the phpunit command from our project root folder and then changed the working directory to the tests folder, because we were using relative paths. I changed everything to absolute paths (using __DIR__) so we do not have to change the working directory anymore.
Bottom line: Do not change the directory in the bootstrap file as it causes phpunit to fail with this error message: Could not read phpunit.xml.

Categories