Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 33 | class DefaultCacheFactoryTest extends OrmTestCase |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var \Doctrine\ORM\Cache\CacheFactory |
||
| 37 | */ |
||
| 38 | private $factory; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \Doctrine\ORM\EntityManager |
||
| 42 | */ |
||
| 43 | private $em; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Doctrine\ORM\Cache\RegionsConfiguration |
||
| 47 | */ |
||
| 48 | private $regionsConfig; |
||
| 49 | |||
| 50 | protected function setUp() |
||
| 63 | |||
| 64 | public function testImplementsCacheFactory() |
||
| 68 | |||
| 69 | View Code Duplication | public function testBuildCachedEntityPersisterReadOnly() |
|
| 70 | { |
||
| 71 | $em = $this->em; |
||
| 72 | $metadata = clone $em->getClassMetadata(State::class); |
||
| 73 | $persister = new BasicEntityPersister($em, $metadata); |
||
| 74 | $region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl())); |
||
| 75 | |||
| 76 | $metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_READ_ONLY; |
||
| 77 | |||
| 78 | $this->factory->expects($this->once()) |
||
|
|
|||
| 79 | ->method('getRegion') |
||
| 80 | ->with($this->equalTo($metadata->cache)) |
||
| 81 | ->will($this->returnValue($region)); |
||
| 82 | |||
| 83 | $cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
||
| 84 | |||
| 85 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister); |
||
| 86 | $this->assertInstanceOf(ReadOnlyCachedEntityPersister::class, $cachedPersister); |
||
| 87 | } |
||
| 88 | |||
| 89 | View Code Duplication | public function testBuildCachedEntityPersisterReadWrite() |
|
| 90 | { |
||
| 91 | $em = $this->em; |
||
| 92 | $metadata = clone $em->getClassMetadata(State::class); |
||
| 93 | $persister = new BasicEntityPersister($em, $metadata); |
||
| 94 | $region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl())); |
||
| 95 | |||
| 96 | $metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_READ_WRITE; |
||
| 97 | |||
| 98 | $this->factory->expects($this->once()) |
||
| 99 | ->method('getRegion') |
||
| 100 | ->with($this->equalTo($metadata->cache)) |
||
| 101 | ->will($this->returnValue($region)); |
||
| 102 | |||
| 103 | $cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
||
| 104 | |||
| 105 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister); |
||
| 106 | $this->assertInstanceOf(ReadWriteCachedEntityPersister::class, $cachedPersister); |
||
| 107 | } |
||
| 108 | |||
| 109 | View Code Duplication | public function testBuildCachedEntityPersisterNonStrictReadWrite() |
|
| 110 | { |
||
| 111 | $em = $this->em; |
||
| 112 | $metadata = clone $em->getClassMetadata(State::class); |
||
| 113 | $persister = new BasicEntityPersister($em, $metadata); |
||
| 114 | $region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl())); |
||
| 115 | |||
| 116 | $metadata->cache['usage'] = ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE; |
||
| 117 | |||
| 118 | $this->factory->expects($this->once()) |
||
| 119 | ->method('getRegion') |
||
| 120 | ->with($this->equalTo($metadata->cache)) |
||
| 121 | ->will($this->returnValue($region)); |
||
| 122 | |||
| 123 | $cachedPersister = $this->factory->buildCachedEntityPersister($em, $persister, $metadata); |
||
| 124 | |||
| 125 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister); |
||
| 126 | $this->assertInstanceOf(NonStrictReadWriteCachedEntityPersister::class, $cachedPersister); |
||
| 127 | } |
||
| 128 | |||
| 129 | View Code Duplication | public function testBuildCachedCollectionPersisterReadOnly() |
|
| 130 | { |
||
| 131 | $em = $this->em; |
||
| 132 | $metadata = $em->getClassMetadata(State::class); |
||
| 133 | $mapping = $metadata->associationMappings['cities']; |
||
| 134 | $persister = new OneToManyPersister($em); |
||
| 135 | $region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl())); |
||
| 136 | |||
| 137 | $mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_READ_ONLY; |
||
| 138 | |||
| 139 | $this->factory->expects($this->once()) |
||
| 140 | ->method('getRegion') |
||
| 141 | ->with($this->equalTo($mapping['cache'])) |
||
| 142 | ->will($this->returnValue($region)); |
||
| 143 | |||
| 144 | |||
| 145 | $cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $mapping); |
||
| 146 | |||
| 147 | $this->assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
||
| 148 | $this->assertInstanceOf(ReadOnlyCachedCollectionPersister::class, $cachedPersister); |
||
| 149 | } |
||
| 150 | |||
| 151 | View Code Duplication | public function testBuildCachedCollectionPersisterReadWrite() |
|
| 152 | { |
||
| 153 | $em = $this->em; |
||
| 154 | $metadata = $em->getClassMetadata(State::class); |
||
| 155 | $mapping = $metadata->associationMappings['cities']; |
||
| 156 | $persister = new OneToManyPersister($em); |
||
| 157 | $region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl())); |
||
| 158 | |||
| 159 | $mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_READ_WRITE; |
||
| 160 | |||
| 161 | $this->factory->expects($this->once()) |
||
| 162 | ->method('getRegion') |
||
| 163 | ->with($this->equalTo($mapping['cache'])) |
||
| 164 | ->will($this->returnValue($region)); |
||
| 165 | |||
| 166 | $cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $mapping); |
||
| 167 | |||
| 168 | $this->assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
||
| 169 | $this->assertInstanceOf(ReadWriteCachedCollectionPersister::class, $cachedPersister); |
||
| 170 | } |
||
| 171 | |||
| 172 | View Code Duplication | public function testBuildCachedCollectionPersisterNonStrictReadWrite() |
|
| 173 | { |
||
| 174 | $em = $this->em; |
||
| 175 | $metadata = $em->getClassMetadata(State::class); |
||
| 176 | $mapping = $metadata->associationMappings['cities']; |
||
| 177 | $persister = new OneToManyPersister($em); |
||
| 178 | $region = new ConcurrentRegionMock(new DefaultRegion('regionName', $this->getSharedSecondLevelCacheDriverImpl())); |
||
| 179 | |||
| 180 | $mapping['cache']['usage'] = ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE; |
||
| 181 | |||
| 182 | $this->factory->expects($this->once()) |
||
| 183 | ->method('getRegion') |
||
| 184 | ->with($this->equalTo($mapping['cache'])) |
||
| 185 | ->will($this->returnValue($region)); |
||
| 186 | |||
| 187 | $cachedPersister = $this->factory->buildCachedCollectionPersister($em, $persister, $mapping); |
||
| 188 | |||
| 189 | $this->assertInstanceOf(CachedCollectionPersister::class, $cachedPersister); |
||
| 190 | $this->assertInstanceOf(NonStrictReadWriteCachedCollectionPersister::class, $cachedPersister); |
||
| 191 | } |
||
| 192 | |||
| 193 | View Code Duplication | public function testInheritedEntityCacheRegion() |
|
| 194 | { |
||
| 195 | $em = $this->em; |
||
| 196 | $metadata1 = clone $em->getClassMetadata(AttractionContactInfo::class); |
||
| 197 | $metadata2 = clone $em->getClassMetadata(AttractionLocationInfo::class); |
||
| 198 | $persister1 = new BasicEntityPersister($em, $metadata1); |
||
| 199 | $persister2 = new BasicEntityPersister($em, $metadata2); |
||
| 200 | $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
||
| 201 | |||
| 202 | $cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1); |
||
| 203 | $cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2); |
||
| 204 | |||
| 205 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister1); |
||
| 206 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister2); |
||
| 207 | |||
| 208 | $this->assertNotSame($cachedPersister1, $cachedPersister2); |
||
| 209 | $this->assertSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion()); |
||
| 210 | } |
||
| 211 | |||
| 212 | View Code Duplication | public function testCreateNewCacheDriver() |
|
| 213 | { |
||
| 214 | $em = $this->em; |
||
| 215 | $metadata1 = clone $em->getClassMetadata(State::class); |
||
| 216 | $metadata2 = clone $em->getClassMetadata(City::class); |
||
| 217 | $persister1 = new BasicEntityPersister($em, $metadata1); |
||
| 218 | $persister2 = new BasicEntityPersister($em, $metadata2); |
||
| 219 | $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
||
| 220 | |||
| 221 | $cachedPersister1 = $factory->buildCachedEntityPersister($em, $persister1, $metadata1); |
||
| 222 | $cachedPersister2 = $factory->buildCachedEntityPersister($em, $persister2, $metadata2); |
||
| 223 | |||
| 224 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister1); |
||
| 225 | $this->assertInstanceOf(CachedEntityPersister::class, $cachedPersister2); |
||
| 226 | |||
| 227 | $this->assertNotSame($cachedPersister1, $cachedPersister2); |
||
| 228 | $this->assertNotSame($cachedPersister1->getCacheRegion(), $cachedPersister2->getCacheRegion()); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @expectedException InvalidArgumentException |
||
| 233 | * @expectedExceptionMessage Unrecognized access strategy type [-1] |
||
| 234 | */ |
||
| 235 | public function testBuildCachedEntityPersisterNonStrictException() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @expectedException InvalidArgumentException |
||
| 248 | * @expectedExceptionMessage Unrecognized access strategy type [-1] |
||
| 249 | */ |
||
| 250 | public function testBuildCachedCollectionPersisterException() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @expectedException LogicException |
||
| 264 | * @expectedExceptionMessage If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function testInvalidFileLockRegionDirectoryException() |
|
| 267 | { |
||
| 268 | $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
||
| 269 | |||
| 270 | $factory->getRegion( |
||
| 271 | [ |
||
| 272 | 'usage' => ClassMetadata::CACHE_USAGE_READ_WRITE, |
||
| 273 | 'region' => 'foo' |
||
| 274 | ] |
||
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @expectedException LogicException |
||
| 280 | * @expectedExceptionMessage If you want to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you want to use it please provide a valid directory |
||
| 281 | */ |
||
| 282 | View Code Duplication | public function testInvalidFileLockRegionDirectoryExceptionWithEmptyString() |
|
| 283 | { |
||
| 284 | $factory = new DefaultCacheFactory($this->regionsConfig, $this->getSharedSecondLevelCacheDriverImpl()); |
||
| 285 | |||
| 286 | $factory->setFileLockRegionDirectory(''); |
||
| 287 | |||
| 288 | $factory->getRegion( |
||
| 289 | [ |
||
| 290 | 'usage' => ClassMetadata::CACHE_USAGE_READ_WRITE, |
||
| 291 | 'region' => 'foo' |
||
| 292 | ] |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | |||
| 296 | public function testBuildsNewNamespacedCacheInstancePerRegionInstance() |
||
| 316 | |||
| 317 | public function testAppendsNamespacedCacheInstancePerRegionInstanceWhenItsAlreadySet() |
||
| 340 | |||
| 341 | View Code Duplication | public function testBuildsDefaultCacheRegionFromGenericCacheRegion() |
|
| 342 | { |
||
| 343 | /* @var $cache \Doctrine\Common\Cache\Cache */ |
||
| 344 | $cache = $this->createMock(Cache::class); |
||
| 345 | |||
| 346 | $factory = new DefaultCacheFactory($this->regionsConfig, $cache); |
||
| 347 | |||
| 348 | $this->assertInstanceOf( |
||
| 349 | DefaultRegion::class, |
||
| 350 | $factory->getRegion( |
||
| 358 | |||
| 359 | View Code Duplication | public function testBuildsMultiGetCacheRegionFromGenericCacheRegion() |
|
| 376 | |||
| 377 | } |
||
| 378 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.