|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | +* |
| 6 | +* (c) Fabien Potencier <fabien@symfony.com> |
| 7 | +* |
| 8 | +* For the full copyright and license information, please view the LICENSE |
| 9 | +* file that was distributed with this source code. |
| 10 | +*/ |
| 11 | + |
| 12 | +namespaceSymfony\Bridge\Doctrine\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +useSymfony\Component\DependencyInjection\Definition; |
| 15 | +useSymfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +useSymfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Fabio B. Silva <fabio.bat.silva@gmail.com> |
| 20 | + */ |
| 21 | +class DoctrineExtensionTestextends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | +/** |
| 24 | + * @var \Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension |
| 25 | + */ |
| 26 | +private$extension; |
| 27 | + |
| 28 | +protectedfunctionsetUp() |
| 29 | + { |
| 30 | +parent::setUp(); |
| 31 | + |
| 32 | +$this->extension =$this |
| 33 | + ->getMockBuilder('Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension') |
| 34 | + ->setMethods(array( |
| 35 | +'getMappingResourceConfigDirectory', |
| 36 | +'getObjectManagerElementName', |
| 37 | +'getMappingObjectDefaultName', |
| 38 | +'getMappingResourceExtension', |
| 39 | +'load', |
| 40 | + )) |
| 41 | + ->getMock() |
| 42 | + ; |
| 43 | + |
| 44 | +$this->extension->expects($this->any()) |
| 45 | + ->method('getObjectManagerElementName') |
| 46 | + ->will($this->returnCallback(function ($name) { |
| 47 | +return'doctrine.orm.'.$name; |
| 48 | + })); |
| 49 | + } |
| 50 | + |
| 51 | +publicfunctionproviderBasicDrivers() |
| 52 | + { |
| 53 | +returnarray( |
| 54 | +array('doctrine.orm.cache.apc.class',array('type' =>'apc')), |
| 55 | +array('doctrine.orm.cache.array.class',array('type' =>'array')), |
| 56 | +array('doctrine.orm.cache.xcache.class',array('type' =>'xcache')), |
| 57 | +array('doctrine.orm.cache.wincache.class',array('type' =>'wincache')), |
| 58 | +array('doctrine.orm.cache.zenddata.class',array('type' =>'zenddata')), |
| 59 | +array('doctrine.orm.cache.redis.class',array('type' =>'redis'),array('setRedis')), |
| 60 | +array('doctrine.orm.cache.memcache.class',array('type' =>'memcache'),array('setMemcache')), |
| 61 | +array('doctrine.orm.cache.memcached.class',array('type' =>'memcached'),array('setMemcached')), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | +/** |
| 66 | + * @param string $class |
| 67 | + * @param array $config |
| 68 | + * |
| 69 | + * @dataProvider providerBasicDrivers |
| 70 | + */ |
| 71 | +publicfunctiontestLoadBasicCacheDriver($class,array$config,array$expectedCalls =array()) |
| 72 | + { |
| 73 | +$container =$this->createContainer(); |
| 74 | +$cacheName ='metadata_cache'; |
| 75 | +$objectManager =array( |
| 76 | +'name' =>'default', |
| 77 | +'metadata_cache_driver' =>$config |
| 78 | + ); |
| 79 | + |
| 80 | +$this->invokeLoadCacheDriver($objectManager,$container,$cacheName); |
| 81 | + |
| 82 | +$this->assertTrue($container->hasDefinition('doctrine.orm.default_metadata_cache')); |
| 83 | + |
| 84 | +$definition =$container->getDefinition('doctrine.orm.default_metadata_cache'); |
| 85 | +$defCalls =$definition->getMethodCalls(); |
| 86 | +$expectedCalls[] ='setNamespace'; |
| 87 | +$actualCalls =array_map(function ($call) { |
| 88 | +return$call[0]; |
| 89 | + },$defCalls); |
| 90 | + |
| 91 | +$this->assertFalse($definition->isPublic()); |
| 92 | +$this->assertEquals("%$class%",$definition->getClass()); |
| 93 | + |
| 94 | +foreach (array_unique($expectedCalls)as$call) { |
| 95 | +$this->assertContains($call,$actualCalls); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +publicfunctiontestServiceCacheDriver() |
| 100 | + { |
| 101 | +$cacheName ='metadata_cache'; |
| 102 | +$container =$this->createContainer(); |
| 103 | +$definition =newDefinition('%doctrine.orm.cache.apc.class%'); |
| 104 | +$objectManager =array( |
| 105 | +'name' =>'default', |
| 106 | +'metadata_cache_driver' =>array( |
| 107 | +'type' =>'service', |
| 108 | +'id' =>'service_driver' |
| 109 | + ) |
| 110 | + ); |
| 111 | + |
| 112 | +$container->setDefinition('service_driver',$definition); |
| 113 | + |
| 114 | +$this->invokeLoadCacheDriver($objectManager,$container,$cacheName); |
| 115 | + |
| 116 | +$this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache')); |
| 117 | + } |
| 118 | + |
| 119 | +/** |
| 120 | + * @expectedException InvalidArgumentException |
| 121 | + * @expectedExceptionMessage "unrecognized_type" is an unrecognized Doctrine cache driver. |
| 122 | + */ |
| 123 | +publicfunctiontestUnrecognizedCacheDriverException() |
| 124 | + { |
| 125 | +$cacheName ='metadata_cache'; |
| 126 | +$container =$this->createContainer(); |
| 127 | +$objectManager =array( |
| 128 | +'name' =>'default', |
| 129 | +'metadata_cache_driver' =>array( |
| 130 | +'type' =>'unrecognized_type' |
| 131 | + ) |
| 132 | + ); |
| 133 | + |
| 134 | +$this->invokeLoadCacheDriver($objectManager,$container,$cacheName); |
| 135 | + } |
| 136 | + |
| 137 | +protectedfunctioninvokeLoadCacheDriver(array$objectManager,ContainerBuilder$container,$cacheName) |
| 138 | + { |
| 139 | +$method =new \ReflectionMethod($this->extension,'loadObjectManagerCacheDriver'); |
| 140 | + |
| 141 | +$method->setAccessible(true); |
| 142 | + |
| 143 | +$method->invokeArgs($this->extension,array($objectManager,$container,$cacheName)); |
| 144 | + } |
| 145 | + |
| 146 | +/** |
| 147 | + * @param array $data |
| 148 | + * |
| 149 | + * @return \Symfony\Component\DependencyInjection\ContainerBuilder |
| 150 | + */ |
| 151 | +protectedfunctioncreateContainer(array$data =array()) |
| 152 | + { |
| 153 | +returnnewContainerBuilder(newParameterBag(array_merge(array( |
| 154 | +'kernel.bundles' =>array('FrameworkBundle' =>'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'), |
| 155 | +'kernel.cache_dir' =>__DIR__, |
| 156 | +'kernel.debug' =>false, |
| 157 | +'kernel.environment' =>'test', |
| 158 | +'kernel.name' =>'kernel', |
| 159 | +'kernel.root_dir' =>__DIR__, |
| 160 | + ),$data))); |
| 161 | + } |
| 162 | +} |