MaxIdLengthAdapterTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $reflectionClass = new \ReflectionClass(AbstractAdapter::class);
  36. $reflectionMethod = $reflectionClass->getMethod('getId');
  37. $reflectionMethod->setAccessible(true);
  38. // No versioning enabled
  39. $this->assertEquals('--------------------------:------------', $reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)]));
  40. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)])));
  41. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 23)])));
  42. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 40)])));
  43. $reflectionProperty = $reflectionClass->getProperty('versioningIsEnabled');
  44. $reflectionProperty->setAccessible(true);
  45. $reflectionProperty->setValue($cache, true);
  46. // Versioning enabled
  47. $this->assertEquals('--------------------------:1:------------', $reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)]));
  48. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)])));
  49. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 23)])));
  50. $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 40)])));
  51. }
  52. /**
  53. * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
  54. * @expectedExceptionMessage Namespace must be 26 chars max, 40 given ("----------------------------------------")
  55. */
  56. public function testTooLongNamespace()
  57. {
  58. $cache = $this->getMockBuilder(MaxIdLengthAdapter::class)
  59. ->setConstructorArgs([str_repeat('-', 40)])
  60. ->getMock();
  61. }
  62. }
  63. abstract class MaxIdLengthAdapter extends AbstractAdapter
  64. {
  65. protected $maxIdLength = 50;
  66. public function __construct($ns)
  67. {
  68. parent::__construct($ns);
  69. }
  70. }