Created
October 30, 2013 22:54
-
-
Save anonymous/7241730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Projeto\Empresa\Services; | |
class ProductManager | |
{ | |
private $productDAO; | |
public function __construct(\Product_model $productDAO) | |
{ | |
$this->productDAO = $productDAO; | |
} | |
public function doSomething() | |
{ | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
dependency_injection_CI.php line 21:
$CI->config->set_item('container', $container);