public function testNewContainerWithSameContextClose() { $alternativeContainer = new XmlContainer(__DIR__ . self::A); $singletonB = $alternativeContainer->getBean('singletonRandom'); $singletonA = $this->container->getBean('singletonRandom'); $this->assertSame($singletonA, $singletonB, 'Different container, same instance'); $this->assertEquals($singletonA->getRandomId(), $singletonB->getRandomId(), 'Id are equals!'); $alternativeContainer->close(); try { $this->container->close(); $this->fail('Exception not throw'); } catch (OvoContainerException $e) { $this->assertTrue(true); $this->container = null; } }
<?php error_reporting(E_ALL); ini_set('display_errors', 1); use Ovo\Container\XmlContainer; use Ovo\Test\Shop\Repository\ProductRepository; use Ovo\Common\Properties\PropertiesLoader; //OVO-CONTAINER Framework require_once '../../src/Ovo/Container/OvoLoader.php'; //Application file to classpath XmlContainer::addNamespaceToClassLoader('Ovo\\Test', '../src/'); //DI Container $container = new XmlContainer('../config/shopContext.xml', '../config/property.ini'); //empty the basket if (isset($_GET['void'])) { $container->destroyBean('basket'); } //get basket bean, defined as session and init only first time $basket = $container->getBean('basket'); //Business logic if (isset($_GET['add']) and is_int((int) $_GET['add'])) { $basket->addToBasket((int) $_GET['add']); } if (isset($_GET['remove']) and is_int((int) $_GET['remove'])) { $basket->removeFromBasket((int) $_GET['remove']); } //View - Print all product shop $shop = new ProductRepository(); echo PropertiesLoader::getProperty('shop_name') . '<hr>'; foreach ($shop->getShopProducts() as $k => $v) { echo 'ID: ' . $v->getId() . ' Description: ' . $v->getDescription() . '
<?php ini_set('display_errors', 1); error_reporting(E_ALL); use Ovo\Container\XmlContainer; //OVO-CONTAINER Framework require_once '../../src/Ovo/Container/OvoLoader.php'; //add source to class loader XmlContainer::addNamespaceToClassLoader('Ovo\\Test', '../src/'); $time = new Ovo\Test\Utility\Timer(); $time->start(); //create the DI container $containerA = new XmlContainer("../config/randomContext.xml"); $containerB = new XmlContainer("../config/randomContextAlternative.xml"); $containerC = new XmlContainer("../config/randomContext.xml"); $sessionRandomA = $containerA->getBean("sessionSimpleRandom"); $sessionRandomB = $containerB->getBean("sessionSimpleRandom"); $sessionRandomC = $containerC->getBean("sessionSimpleRandom"); echo "(SESSION) - ID FIRST INSTANCE: " . $sessionRandomA->getRandomId() . " <br>(SESSION) - ID SECOND INSTANCE: " . $sessionRandomB->getRandomId() . " <br>(SESSION) - ID THIRD INSTANCE: " . $sessionRandomC->getRandomId() . "<hr>"; echo "Elapsed time: " . $time->stop();
<?php ini_set('display_errors', 1); error_reporting(E_ALL); use Ovo\Container\XmlContainer; //OVO-CONTAINER Framework require_once '../../src/Ovo/Container/OvoLoader.php'; //add source to class loader XmlContainer::addNamespaceToClassLoader('Ovo\\Test', '../src/'); $time = new Ovo\Test\Utility\Timer(); $time->start(); //create the DI container $container = new XmlContainer("../config/circular.xml"); $container->getBean('a'); echo "Elapsed time: " . $time->stop();
<?php ini_set('display_errors', 1); error_reporting(E_ALL); use Ovo\Container\XmlContainer; //OVO-CONTAINER Framework require_once '../../src/Ovo/Container/OvoLoader.php'; //add source to class loader XmlContainer::addNamespaceToClassLoader('Ovo\\Test', '../src/'); $time = new Ovo\Test\Utility\Timer(); $time->start(); //create the DI container $container = new XmlContainer("../config/randomContext.xml"); //get SimpleRandom as prototype $prototype = $container->getBean("prototypeSimpleRandom"); $prototypeSecond = $container->getBean("prototypeSimpleRandom"); echo "(PROTOTYPE) ID INSTANCE: " . $prototype->getRandomId() . " - ID SECOND INSTANCE: " . $prototypeSecond->getRandomId() . "<hr>"; //get SimpleRandom as Singleton $randomSingletonOne = $container->getBean("singletonSimpleRandom"); if (isset($_GET["delete"])) { $container->destroyBean("singletonSimpleRandom"); } $randomSingletonTwo = $container->getBean("singletonSimpleRandom"); echo "\n(SINGLETON)ID INSTANCE: " . $randomSingletonOne->getRandomId() . " - ID SECOND INSTANCE: " . $randomSingletonTwo->getRandomId() . "<hr>"; //get SimpleRandom as session $sessionRandomOne = $container->getBean("sessionSimpleRandom"); if (isset($_GET["delete"])) { $container->destroyBean("sessionSimpleRandom"); } $sessionRandomTwo = $container->getBean("sessionSimpleRandom"); echo "\n(SESSION) ID INSTANCE: " . $sessionRandomOne->getRandomId() . " - ID SECOND INSTANCE: " . $sessionRandomTwo->getRandomId() . "<hr>";
/** * @expectedException Ovo\Container\Exception\OvoLoaderException */ public function testCircularDependencyOnInit() { XmlContainer::addNamespaceToClassLoader('Circular', 'test/src'); $this->container = new XmlContainer(__DIR__ . self::CONFIG_FILE); }
public function testInitSimpleBean() { $simple = $this->container->getBean('simple'); $this->assertInstanceOf('Test\\Simple', $simple, 'Bean simple not instance of Test\\Simple'); $this->assertEquals('Test', $simple->getNameSpace(), 'Namespace different from Test'); }