Skip to content

Instantly share code, notes, and snippets.

@rogeriopradoj
Forked from anonymous/ProductManager.php
Created November 1, 2013 03:19
Show Gist options
  • Save rogeriopradoj/7260552 to your computer and use it in GitHub Desktop.
Save rogeriopradoj/7260552 to your computer and use it in GitHub Desktop.
<?php
require_once "../vendor/autoload.php";
//Dentro de um arquivo qualquer que você for carregar no bootstrap do CI, como o config.php e routes.php
//Exemplo: require_once "dependency_injection_CI.php";
//O composer irá se encarregar de carregar
use Projeto\Empresa\Services\ProductManager;
//Singleton do CI
$CI =& get_instance();
$container = new Pimple();
$container['product_manager'] = function ($c) use ($CI) {
$CI->load->model("product_model");
return new ProductManager($CI->product_model);
}
//Agora o container do Pimple é acessível em qualquer lugar
$CI->set_item('container', $container);
<?php
namespace Projeto\Empresa\Services;
class ProductManager
{
private $productDAO;
public function __construct(\Product_model $productDAO)
{
$this->productDAO = $productDAO;
}
public function doSomething()
{
return true;
}
}
<?php
class ProductManagerTest extends PHPUnit_Framework_TestCase
{
private $container;
public function setUp()
{
$CI =& get_instance();
$this->container = $CI->config->item("container");
}
public function testDoSomethingShouldWork()
{
//O pimple carrega por lazy load as dependencias do ProductManager (no caso o model)
$productManager = $this->container['product_manager'];
$this->assertTrue($productManager->doSomething());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment