Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Normally in our apps, we do constructor injection and define an interface for all components in our system:</p> <pre><code>class Book { /** * @var Db_AdapterInterface */ private $_database; public function __construct(Db_AdapterInterface $database) { $this-&gt;_database = $database; } } </code></pre> <p>We have then of course a standard Db_Adapter and then another Db_TestAdapter. In Db_TestAdapter we can define results of SQL queries in our tests.</p> <p>For our normal app, we have something like this for our container:</p> <pre><code>$container-&gt;add('Db_AdapterInterface', new Db_Adapter()); </code></pre> <p>And then in our tests, we have this line instead:</p> <pre><code>$container-&gt;add('Db_AdapterInterface', new Db_TestAdapter()); </code></pre> <p>To get an instance of a Book, we simply ask the container for it:</p> <pre><code>$book = $container-&gt;construct('Book'); </code></pre> <p>And the container injects all the needed dependencies into the object.</p> <p>If you keep all your objects loosely coupled, ie object A only needs an interface B then you can always provide object A with a test implementation. At that point what container you use doesn't matter.</p> <p>We have a very simple IoC container which can do basic constructor injection. Our tests inherit from a base class which fills the container with standard test objects. That way we don't have a lot of code just to construct an object we want to test.</p> <p><strong>Update:</strong> I added an example of wiring things up in the container.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload