CacheItemTest.php 1.9 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 array(
  31. array(''),
  32. array('{'),
  33. array('}'),
  34. array('('),
  35. array(')'),
  36. array('/'),
  37. array('\\'),
  38. array('@'),
  39. array(':'),
  40. array(true),
  41. array(null),
  42. array(1),
  43. array(1.1),
  44. array(array(array())),
  45. array(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(array('bar', 'baz')));
  53. \call_user_func(\Closure::bind(function () use ($item) {
  54. $this->assertSame(array('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. }