Testing composer package downloads

I wrote a package, and as part of the development process, I want to run unit tests on it. This basically means that I need a boot file to register the autoloader for my package.

Any package that I look at does not have a specific boot file, so I don’t quite understand how developers test their own packages.

This is my directory structure.

src .CompanyName ..PackageName ...Class 1 ...Class 2 tests .Class1Test .Class2Test composer.json phpunit.xml.dist 

Now, if I run phpunit inside the root directory, all my tests say that \\CompanyName\\PackageName\\Class1 not found. This is legal, since no one turned them on. So the question is: How and when do I include my classes .


Looking at random packages I can see that they rely on vendor/autoload.php , but I don't have this provider. Do I have to run composer install in order for it to be created?

+6
source share
2 answers

Ok, I understood the answer.
Composer provides its own autoloader that I could use.

  • Run composer install or composer update in the root of the project. This will create a vendor directory with autoload.php composites.
  • Add vendor dir to .gitignore along with composer.lock
  • In phpunit.xml.dist specify the composer autoloader as the bootstrap file

phpunit.xml.dist file example

 <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" > <testsuites> <testsuite name="Your package test suit"> <directory>./tests/</directory> </testsuite> </testsuites> </phpunit> 

Pay attention to the entry for the boot.

+5
source

Yes, you should run composer update . This is not dangerous, since everything is placed in / vendor /, which you can subsequently delete.

0
source

All Articles