Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 90
CacheTraitTest
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 72
 testSave
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 22
 testNoCallbackCallOnHit
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 19
 testRecomputeOnBetaInf
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 22
 testExceptionOnNegativeBeta
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 9
TestPool
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 9
90
0.00% covered (danger)
0.00%
0 / 18
 hasItem
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 deleteItem
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 deleteItems
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getItem
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getItems
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 saveDeferred
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 save
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 commit
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 clear
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
<?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\Contracts\Tests\Cache;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Contracts\Cache\CacheTrait;
/**
 * @author Tobias Nyholm <tobias.nyholm@gmail.com>
 */
class CacheTraitTest extends TestCase
{
    public function testSave()
    {
        $item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
        $item->method('set')
            ->willReturn($item);
        $item->method('isHit')
            ->willReturn(false);
        $item->expects($this->once())
            ->method('set')
            ->with('computed data');
        $cache = $this->getMockBuilder(TestPool::class)
            ->setMethods(['getItem', 'save'])
            ->getMock();
        $cache->expects($this->once())
            ->method('getItem')
            ->with('key')
            ->willReturn($item);
        $cache->expects($this->once())
            ->method('save');
        $callback = function (CacheItemInterface $item) {
            return 'computed data';
        };
        $cache->get('key', $callback);
    }
    public function testNoCallbackCallOnHit()
    {
        $item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
        $item->method('isHit')
            ->willReturn(true);
        $item->expects($this->never())
            ->method('set');
        $cache = $this->getMockBuilder(TestPool::class)
            ->setMethods(['getItem', 'save'])
            ->getMock();
        $cache->expects($this->once())
            ->method('getItem')
            ->with('key')
            ->willReturn($item);
        $cache->expects($this->never())
            ->method('save');
        $callback = function (CacheItemInterface $item) {
            $this->assertTrue(false, 'This code should never be reached');
        };
        $cache->get('key', $callback);
    }
    public function testRecomputeOnBetaInf()
    {
        $item = $this->getMockBuilder(CacheItemInterface::class)->getMock();
        $item->method('set')
            ->willReturn($item);
        $item->method('isHit')
            // We want to recompute even if it is a hit
            ->willReturn(true);
        $item->expects($this->once())
            ->method('set')
            ->with('computed data');
        $cache = $this->getMockBuilder(TestPool::class)
            ->setMethods(['getItem', 'save'])
            ->getMock();
        $cache->expects($this->once())
            ->method('getItem')
            ->with('key')
            ->willReturn($item);
        $cache->expects($this->once())
            ->method('save');
        $callback = function (CacheItemInterface $item) {
            return 'computed data';
        };
        $cache->get('key', $callback, INF);
    }
    public function testExceptionOnNegativeBeta()
    {
        $cache = $this->getMockBuilder(TestPool::class)
            ->setMethods(['getItem', 'save'])
            ->getMock();
        $callback = function (CacheItemInterface $item) {
            return 'computed data';
        };
        $this->expectException(\InvalidArgumentException::class);
        $cache->get('key', $callback, -2);
    }
}
class TestPool implements CacheItemPoolInterface
{
    use CacheTrait;
    public function hasItem($key): bool
    {
    }
    public function deleteItem($key): bool
    {
    }
    public function deleteItems(array $keys = []): bool
    {
    }
    public function getItem($key): CacheItemInterface
    {
    }
    public function getItems(array $key = []): iterable
    {
    }
    public function saveDeferred(CacheItemInterface $item): bool
    {
    }
    public function save(CacheItemInterface $item): bool
    {
    }
    public function commit(): bool
    {
    }
    public function clear(): bool
    {
    }
}