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

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.

Related

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.

How to include Composer dependencies in a Git repo

When creating sites using a framework like Silverstripe I often want to use helper modules like gridfieldextensions and lumberjack.
I therefore use composer require to add the dependencies.
However when I follow my regular development work flow and use git add -A to add the module to the repo rather than the code being added to the repo I get a reference to it.
This causes problems when I then try to clone the site elsewhere (using Jenkins or another developer). The git clone or git pull leaves an empty directory.
I solve this by deleting the .git dir of the module and adding all the files.
Is there a better way to do this? Is using git submodule an option?
Somewhere i found a good .gitignore file that ignores everything and i have to tell it to include the custom modules for my project. It's like:
# ignore everything...
/*
# ...but
!/.htaccess
!/.gitignore
!/composer.json
!/composer.lock
!/Capfile
!/Gemfile
!/favicon.ico
!/touch-icon-*
!/mysite
!/some-module
#...other modules
# theme stuff
!/themes/
**/.sass-cache
**/node_modules
!**/node_modules/_manifest_exclude
#no assets in general, but /assets/.htaccess
!/assets
/assets/*
!assets/.htaccess
As FinBoWa already said you need the composer.json and composer.lock file in your project and running
composer install
on another machine it'll install the packages in the versions saved in the composer.lock file on that machine
composer install --no-dev
will only install the "normal" requirements, no dev-requirements like phpunit or other stuff you only need for developing or testing but not live
composer install --no-dev -o
will also optimize (-o) the auto loader, so it'll be a bit faster.
composer update
will update your packages, which might have funny side effects and break your site. So use it carefully and test afterwards.
composer update silverstripe/framework
will just update that package and finally
composer update silverstripe/*
will update all packages by the vendor silverstripe (e.g. framework and cms package)
See also:
gitignore documentation
composer documentation

Can't run phpunit tests from command line

I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command:
./vendor/bin/phpunit /tests/ExampleTest.php
And then I get the following message:
You need to set up the project dependencies using the following commands:
wget http://getcomposer.org/composer,phar
php composer.phar install
I already have composer installed in may system and I install Laravel 5 using composer. Isn't phpunit installed when I install a new laravel 5 application? If not, how can I install it in a existent laravel 5 application?
I known that I can also install phpunit globaly and resolve the problem. But maybe it will be a duplication since I have all the phpunit code already in may laravel application.
I had the same problem and it had a specific solution that may apply to other people. I store my files in Dropbox, and the vendor/bin/phpunit file should be an symlink like this
$ ls -lo vendor/bin/phpunit
lrwxr-xr-x vendor/bin/phpunit -> ../phpunit/phpunit/phpunit
However, Dropbox will occasionally replace symlinks with the original file, and this will cause the error above. The behaviour is inconsistent, and with relative symlinks seems to break when 2 machines are accessing Dropbox at the same time. Fixing the symlink worked for me or you could make a new symlink directly to the vendor/phpunit/phpunit/phpunit outside of Dropbox and run that.
Edit: Nowadays I exclude Vendor and node_modules from Dropbox - and simply run composer install when necessary. This works really well, and also deals with the hassle of syncing so many files on Dropbox. What you can do is go into the folder and delete all the files. Wait for Dropbox to sync. Then mark the folder as excluded. Finally, run composer install and you get the contents as you need. (Delete + composer install often solves other issues too).
Running composer install did nothing in my case. However, removing vendor folder and then calling composer install fixed it.
You need to have Composer installed and run composer install or composer update in your application to install the different packages listed in your composer.json.
When you install your Laravel application it doesn't install the packages right away.
You can verify the packages are installed by looking in the vendor directory of your application and check that phpunit is in there.
did you install phpunit globally? I recommend you do it.
just type in your laravel's root directory (e.g. /var/www)
cd /var/www
phpunit
if you what to test just one file, you can do something like this:
phpunit tests/ExampleTest.php
Unit Test:
D:\xampp\htdocs\Samplemed\vendor\bin>
phpunit ../../tests/Unit/Company/CompanyUnitTest
Feature Test:
D:\xampp\htdocs\Samplemed\vendor\bin>phpunit
../../tests/Feature/Company/CompanyFeatureTest
Please try this. its working fine.

Laravel - Export project in other folder for deployment

Is there a way to clean unused dependencies and composer dev requires to reduce a Laravel project, because it's so heavy (43,3 Mb) and it's a small project. Btw, I'm using some dev helpers like Debugbar and IDEHelpers which are not used for deployment...
Is there a way to make a deployment version of my project in other folder
The recommended way to deploy your app is without the vendor directory. I'm going to assume that you're using git for your project. First, put the following in your .gitignore.
/vendor/
Now remove the vendor directory from your repository
git rm -r --cached vendor
git commit -m 'Removed vendor directory'
Now you have a two step deployment:
Update the app using git pull or however you usually deploy.
Run composer install --no-dev --optimize-autoloader. This will generate your vendor directory omitting any development only dependencies.
In order to take advantage of the --no-dev flag, you need to put your development dependencies in the require-dev section in your composer.json. For example:
"require-dev": {
"phpunit/phpunit": "~4.3"
}
Now PHPUnit will be required for development, but not when the --no-dev flag is specified.
Maybe I'm misunderstanding your question but when you deploy a project, you shouldn't be deploying the laravel app with it (/vendor/). You should run composer install and it will pull in all the dependencies. In your composer.json file you can also choose which dependencies are for dev environments only similar to the require-dev section found here: https://gist.github.com/philsturgeon/5976359

PHPUnit testing project generated with Composer

I have a project generated with Composer which has a dependency on PHPUnit. Now I have
dir1 -> PHPUnit stuff
dir2 -> project stuff
If I go into the project directory and run PHPUnit, it complains about loading ClassLoader.php twice:
PHPUnit requires it in vendors/autoload.php
The application code (bootstrap) requires it
How can I resolve this double-inclusion?
Make sure you're running the copy of PHPUnit being installed using Composer, which should be vendor/bin/phpunit. See composer.json, tests/composer.json, and the "Tests" section of README.md in this github repo for examples: https://github.com/phergie/phergie-irc-parser
From a certain composer version [citation needed], class loader got "namespaced" with a long name, thus enabling more composer based projects to work together, this is a non issue from that point on.

Categories