public function testBuilder() { $definitionApp = $this->getMockBuilder('G\\Yaml2Pimple\\Definition')->disableOriginalConstructor()->getMock(); $definitionApp->expects($this->any())->method('getClass')->will($this->returnValue('App')); $definitionApp->expects($this->any())->method('getArguments')->will($this->returnValue(['@Proxy', '%name%'])); $definitionProxy = $this->getMockBuilder('G\\Yaml2Pimple\\Definition')->disableOriginalConstructor()->getMock(); $definitionProxy->expects($this->any())->method('getClass')->will($this->returnValue('Proxy')); $definitionProxy->expects($this->any())->method('getArguments')->will($this->returnValue(['@Curl'])); $definitionCurl = $this->getMockBuilder('G\\Yaml2Pimple\\Definition')->disableOriginalConstructor()->getMock(); $definitionCurl->expects($this->any())->method('getClass')->will($this->returnValue('Curl')); $definitionCurl->expects($this->any())->method('getArguments')->will($this->returnValue(null)); $conf = ['parameters' => ['name' => 'Gonzalo'], 'services' => ['App' => $definitionApp, 'Proxy' => $definitionProxy, 'Curl' => $definitionCurl]]; $container = new Container(); $builder = new ContainerBuilder($container); $builder->buildFromArray($conf); $this->assertInstanceOf('App', $container['App']); $this->assertInstanceOf('Proxy', $container['Proxy']); $this->assertInstanceOf('Curl', $container['Curl']); $this->assertEquals('Hello Gonzalo', $container['App']->hello()); }
include __DIR__ . '/src/Proxy.php'; include __DIR__ . '/src/Test.php'; include __DIR__ . '/src/Factory.php'; use G\Yaml2Pimple\ContainerBuilder; use G\Yaml2Pimple\Loader\YamlFileLoader; use G\Yaml2Pimple\Loader\CacheLoader; use G\Yaml2Pimple\Normalizer\ChainNormalizer; use G\Yaml2Pimple\Normalizer\PimpleNormalizer; use G\Yaml2Pimple\Normalizer\ExpressionNormalizer; use Symfony\Component\Config\FileLocator; use G\Yaml2Pimple\Proxy\ServiceProxyAdapter; use G\Yaml2Pimple\Proxy\AspectProxyAdapter; use G\Yaml2Pimple\Factory\ServiceFactory; use G\Yaml2Pimple\Factory\ProxyParameterFactory; $container = new \Pimple(); $builder = new ContainerBuilder($container); $ymlLoader = new YamlFileLoader(new FileLocator(__DIR__)); $cacheLoader = new CacheLoader($ymlLoader, __DIR__ . '/cache/'); // set the normalizers $builder->setNormalizer(new ChainNormalizer(array(new PimpleNormalizer(), new ExpressionNormalizer()))); $parameterFactory = new ProxyParameterFactory(); $serviceFactory = new ServiceFactory(new ServiceProxyAdapter(__DIR__ . '/cache/')); $serviceFactory->setAspectFactory(new AspectProxyAdapter(__DIR__ . '/cache/')); // set our loader helper $builder->setLoader($cacheLoader); // lazy service proxy factory $builder->setServiceFactory($serviceFactory); // lazy parameter proxy factory $builder->setParameterFactory($parameterFactory); $builder->load('test.yml'); $app = $container['App'];