Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

composer package testing bootstrap

I've written a package and as part of the development proccess I want to run unit tests on it. This basically means I need a bootstrap file to register the autoloader for my package.

Any package I look at doesn't have a specific bootstrap file, thus I don't quite understand how the developers are testing 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 \\CompanyName\\PackageName\\Class1 wasn't found. Which is legite, since nobody had them included. So the question is - How and when do I include my classes.


Looking at a random packages I can see they rely on vendor/autoload.php, but I don't have this vendor dir. Should I run composer install to have it created ?

like image 573
Alex Avatar asked Dec 19 '12 11:12

Alex


1 Answers

Well, I've figured the answer.
Composer provides it's own autoloader I could use.

  1. Run composer install or composer update in the project root. This will create the vendor dir with composers autoload.php file.
  2. Add vendor dir to .gitignore along with composer.lock
  3. In phpunit.xml.dist specify composer's autoloader as the bootstrap file

Example phpunit.xml.dist file

<?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's test suit">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>

</phpunit>

Notice the bootstrap entry there.

like image 118
Alex Avatar answered Oct 21 '22 03:10

Alex