This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Cache/Tests/CacheItemTest.php

99 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Tests;
2017-02-20 13:34:33 +00:00
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\CacheItem;
2017-02-20 13:34:33 +00:00
class CacheItemTest extends TestCase
{
public function testValidKey()
{
$this->assertSame('foo', CacheItem::validateKey('foo'));
}
/**
* @dataProvider provideInvalidKey
2016-09-25 09:27:07 +01:00
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
* @expectedExceptionMessage Cache key
*/
public function testInvalidKey($key)
{
CacheItem::validateKey($key);
}
public function provideInvalidKey()
{
return array(
array(''),
array('{'),
array('}'),
array('('),
array(')'),
array('/'),
array('\\'),
array('@'),
array(':'),
array(true),
array(null),
array(1),
array(1.1),
array(array(array())),
array(new \Exception('foo')),
);
}
public function testTag()
{
$item = new CacheItem();
$r = new \ReflectionProperty($item, 'isTaggable');
$r->setAccessible(true);
$r->setValue($item, true);
$this->assertSame($item, $item->tag('foo'));
$this->assertSame($item, $item->tag(array('bar', 'baz')));
call_user_func(\Closure::bind(function () use ($item) {
$this->assertSame(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), $item->newMetadata[CacheItem::METADATA_TAGS]);
}, $this, CacheItem::class));
}
/**
* @dataProvider provideInvalidKey
2016-12-11 14:34:22 +00:00
* @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
* @expectedExceptionMessage Cache tag
*/
public function testInvalidTag($tag)
{
$item = new CacheItem();
$r = new \ReflectionProperty($item, 'isTaggable');
$r->setAccessible(true);
$r->setValue($item, true);
$item->tag($tag);
}
/**
* @expectedException \Symfony\Component\Cache\Exception\LogicException
* @expectedExceptionMessage Cache item "foo" comes from a non tag-aware pool: you cannot tag it.
*/
public function testNonTaggableItem()
{
$item = new CacheItem();
$r = new \ReflectionProperty($item, 'key');
$r->setAccessible(true);
$r->setValue($item, 'foo');
$item->tag(array());
}
}