title: How can I bootstrap my dependency injector services in Phalcon unit tests?
body: <p>I went through the Phalcon docs and got PHPUnit set up and working. However, I'm having trouble bootstrapping my existing services into the unit testing framework.</p>
  
  <p>Currently, I load my config, routes, and services in the <code>/public/index.php</code>. For example, I'm loading the session library:</p>
  
  <pre><code>$di-&gt;set(
      'session',
      function () use ( $config ) {
          $session = new Phalcon\Session\Adapter\Redis(
              ... config ...
          );
      });
  </code></pre>
  
  <p>So now, in my application I'd have code that calls <code>$this-&gt;session-&gt;get( 'user_id' )</code> to use the session library. I have many of these services -- one for SQL, MongoDB, cookies, etc.</p>
  
  <p>The problem I'm having is correctly loading these services into the unit testing class. The Phalcon docs recommend loading my DI services in the UnitTestCase class (<a href="http://docs.phalconphp.com/en/latest/reference/unit-testing.html#sample-unit-test" rel="nofollow">explained here</a>) but I really do not want re-define the same services in this class loader. Additionally, I want to use the components in my application in the same ways; those components rely on the services being lazy-loaded.</p>
  
  <p>So, to attempt this, I include the same <code>/app/config/services.php</code> file in my unit testing <code>TestHelper.php</code> init script thinking that I can just use the same $di object. This works in that my test cases can call <code>$this-&gt;di-&gt;getSession()-&gt;get( 'var' )</code> but as soon as a component in my app tries to call <code>$this-&gt;session-&gt;get( 'var' )</code> it throws an error:</p>
  
  <pre><code>1) Libraries\SessionTest::testGetSet
  Undefined property: Libraries\SessionTest::$session
  
  /home/mike/PhalconProject/app/library/Session.php:25
  /home/mike/PhalconProject/tests/libraries/SessionTest.php:13
  </code></pre>
  
  <p>This error is telling me that my application session-management library <code>Session.php</code> is failing when accessing <code>$this-&gt;session-&gt;</code> via the dependency injector.</p>
  
  <p>Is there something I'm fundamentally doing wrong here? Do I need to redefine the services in my unit testing class? And if I do need to redefine them, will my application be able to load its services?</p>