MaxIdLengthAdapterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Tests\Adapter;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Cache\Adapter\AbstractAdapter;
  13. class MaxIdLengthAdapterTest extends TestCase
  14. {
  15. public function testLongKey()
  16. {
  17. $cache = $this->getMockBuilder(MaxIdLengthAdapter::class)
  18. ->setConstructorArgs([str_repeat('-', 10)])
  19. ->setMethods(['doHave', 'doFetch', 'doDelete', 'doSave', 'doClear'])
  20. ->getMock();
  21. $cache->expects($this->exactly(2))
  22. ->method('doHave')
  23. ->withConsecutive(
  24. [$this->equalTo('----------:0GTYWa9n4ed8vqNlOT2iEr:')],
  25. [$this->equalTo('----------:---------------------------------------')]
  26. );
  27. $cache->hasItem(str_repeat('-', 40));
  28. $cache->hasItem(str_repeat('-', 39));
  29. }
  30. public function testLongKeyVersioning()
  31. {
  32. $cache = $this->getMockBuilder(MaxIdLengthAdapter::class)
  33. ->setConstructorArgs([str_repeat('-', 26)])
  34. ->getMock();
  35. $cache
  36. ->method('doFetch')
  37. ->willReturn(['2:']);
  38. $reflectionClass = new \ReflectionClass(AbstractAdapter::class);
  39. $reflectionMethod = $reflectionClass->getMethod('getId');
  40. $reflectionMethod->setAccessible(true);
  41. // No versioning enabled
  42. $this->assertEquals('--------------------------:------------', $reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)]));
  43. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)])));
  44. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 23)])));
  45. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 40)])));
  46. $reflectionProperty = $reflectionClass->getProperty('versioningIsEnabled');
  47. $reflectionProperty->setAccessible(true);
  48. $reflectionProperty->setValue($cache, true);
  49. // Versioning enabled
  50. $this->assertEquals('--------------------------:2:------------', $reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)]));
  51. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)])));
  52. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 23)])));
  53. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 40)])));
  54. }
  55. public function testTooLongNamespace()
  56. {
  57. $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
  58. $this->expectExceptionMessage('Namespace must be 26 chars max, 40 given ("----------------------------------------")');
  59. $this->getMockBuilder(MaxIdLengthAdapter::class)
  60. ->setConstructorArgs([str_repeat('-', 40)])
  61. ->getMock();
  62. }
  63. }
  64. abstract class MaxIdLengthAdapter extends AbstractAdapter
  65. {
  66. protected $maxIdLength = 50;
  67. public function __construct($ns)
  68. {
  69. parent::__construct($ns);
  70. }
  71. }