title: Autoloading classes in PHPUnit using Composer and autoload.php
body: <p>I have just installed PHPUnit version 3.7.19 by Sebastian Bergmann via Composer and have written a class I would like to unit test.</p>
  <p>I would like to have all my classes autoloaded into each unit test <strong>without</strong> having to use <code>include</code> or <code>require</code> at the top of my test but this is proving to be difficult!</p>
  <p>This is what my directory structure looks like (a trailing / slash indicates a directory, not a file):</p>
  <pre><code>* composer.json
  * composer.lock
  * composer.phar
  * lib/
      * returning.php
  * tests/
      * returningTest.php
  * vendor/
      * bin/
          * phpunit
      * composer/
      * phpunit/
      * symfony/
      * autoload.php
  </code></pre>
  <p>My <strong>composer.json</strong> file includes the following:</p>
  <pre><code>&quot;require&quot;: {
      &quot;phpunit/phpunit&quot;: &quot;3.7.*&quot;,
      &quot;phpunit/phpunit-selenium&quot;: &quot;&gt;=1.2&quot;
  }
  </code></pre>
  <p>My <strong>returning.php</strong> class file includes the following:</p>
  <pre><code>&lt;?php
  class Returning {
      public $var;
      function __construct(){
          $this-&gt;var = 1;
      }
  }
  ?&gt;
  </code></pre>
  <p>My <strong>returningTest.php</strong> test file includes the following:</p>
  <pre><code>&lt;?php
  class ReturningTest extends PHPUnit_Framework_TestCase
  {
      protected $obj = null;
      
      protected function setUp()
      {
          $this-&gt;obj = new Returning;
      }
      
      public function testExample()
      {   
          $this-&gt;assertEquals(1, $this-&gt;obj-&gt;var);
      }
  
      protected function tearDown()
      {
          
      }
  }
  ?&gt;
  </code></pre>
  <p>However, when I run <code>./vendor/bin/phpunit tests</code> from the command-line, I get the following error:</p>
  <blockquote>
  <p>PHP Fatal error:  Class 'Returning' not found in
  /files/code/php/db/tests/returningTest.php on line 8</p>
  </blockquote>
  <p>I noticed that <code>composer</code> produced an <code>autoload.php</code> file in <code>vendor/autoload.php</code> but not sure if this is relevant for my problem.</p>
  <p>Also, in some other answers on Stack Overflow people have mentioned something about using <a href="https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md" rel="nofollow noreferrer">PSR-0</a> in composer and the <code>namespace</code> command in PHP, but I have not been successful in using either one.</p>
  <p>Please help! I just want to autoload my classes in PHPUnit so I can just use them to create objects without worrying about <code>include</code> or <code>require</code>.</p>
  <hr />
  <p><strong>Update: 14th of August 2013</strong></p>
  <p>I have now created an Open Source project called <a href="https://github.com/jasdeepkhalsa/phpunit-skeleton" rel="nofollow noreferrer">PHPUnit Skeleton</a> to help you get up and running with PHPUnit testing easily for your project.</p>