CacheItemTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Cache\CacheItem;
  13. class CacheItemTest extends TestCase
  14. {
  15. public function testValidKey()
  16. {
  17. $this->assertSame('foo', CacheItem::validateKey('foo'));
  18. }
  19. /**
  20. * @dataProvider provideInvalidKey
  21. * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
  22. * @expectedExceptionMessage Cache key
  23. */
  24. public function testInvalidKey($key)
  25. {
  26. CacheItem::validateKey($key);
  27. }
  28. public function provideInvalidKey()
  29. {
  30. return [
  31. [''],
  32. ['{'],
  33. ['}'],
  34. ['('],
  35. [')'],
  36. ['/'],
  37. ['\\'],
  38. ['@'],
  39. [':'],
  40. [true],
  41. [null],
  42. [1],
  43. [1.1],
  44. [[[]]],
  45. [new \Exception('foo')],
  46. ];
  47. }
  48. public function testTag()
  49. {
  50. $item = new CacheItem();
  51. $this->assertSame($item, $item->tag('foo'));
  52. $this->assertSame($item, $item->tag(['bar', 'baz']));
  53. (\Closure::bind(function () use ($item) {
  54. $this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], $item->tags);
  55. }, $this, CacheItem::class))();
  56. }
  57. /**
  58. * @dataProvider provideInvalidKey
  59. * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
  60. * @expectedExceptionMessage Cache tag
  61. */
  62. public function testInvalidTag($tag)
  63. {
  64. $item = new CacheItem();
  65. $item->tag($tag);
  66. }
  67. }