Completed
Push — master ( 97e40f...89ec5c )
by André
40:26 queued 12:35
created

AbstractBaseHandlerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 9 1
A getCacheItem() 0 6 1
B setUp() 0 39 1
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Cache\Tests;
8
9
use eZ\Publish\Core\Persistence\Cache\Handler as CacheHandler;
10
use eZ\Publish\Core\Persistence\Cache\SectionHandler as CacheSectionHandler;
11
use eZ\Publish\Core\Persistence\Cache\LocationHandler as CacheLocationHandler;
12
use eZ\Publish\Core\Persistence\Cache\ContentHandler as CacheContentHandler;
13
use eZ\Publish\Core\Persistence\Cache\ContentLanguageHandler as CacheContentLanguageHandler;
14
use eZ\Publish\Core\Persistence\Cache\ContentTypeHandler as CacheContentTypeHandler;
15
use eZ\Publish\Core\Persistence\Cache\UserHandler as CacheUserHandler;
16
use eZ\Publish\Core\Persistence\Cache\TransactionHandler as CacheTransactionHandler;
17
use eZ\Publish\Core\Persistence\Cache\TrashHandler as CacheTrashHandler;
18
use eZ\Publish\Core\Persistence\Cache\UrlAliasHandler as CacheUrlAliasHandler;
19
use eZ\Publish\Core\Persistence\Cache\ObjectStateHandler as CacheObjectStateHandler;
20
use eZ\Publish\Core\Persistence\Cache\URLHandler as CacheUrlHandler;
21
use eZ\Publish\SPI\Persistence\Handler;
22
use eZ\Publish\Core\Persistence\Cache\PersistenceLogger;
23
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
24
use Symfony\Component\Cache\CacheItem;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * Abstract test case for spi cache impl.
29
 */
30
abstract class AbstractBaseHandlerTest extends TestCase
31
{
32
    /**
33
     * @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface|\PHPUnit_Framework_MockObject_MockObject
34
     */
35
    protected $cacheMock;
36
37
    /**
38
     * @var \eZ\Publish\SPI\Persistence\Handler|\PHPUnit_Framework_MockObject_MockObject
39
     */
40
    protected $persistenceHandlerMock;
41
42
    /**
43
     * @var \eZ\Publish\Core\Persistence\Cache\Handler
44
     */
45
    protected $persistenceCacheHandler;
46
47
    /**
48
     * @var \eZ\Publish\Core\Persistence\Cache\PersistenceLogger|\PHPUnit_Framework_MockObject_MockObject
49
     */
50
    protected $loggerMock;
51
52
    /**
53
     * @var \Closure
54
     */
55
    protected $cacheItemsClosure;
56
57
    /**
58
     * Setup the HandlerTest.
59
     */
60
    final protected function setUp()
61
    {
62
        parent::setUp();
63
64
        $this->persistenceHandlerMock = $this->createMock(Handler::class);
65
        $this->cacheMock = $this->createMock(TagAwareAdapterInterface::class);
66
        $this->loggerMock = $this->createMock(PersistenceLogger::class);
67
68
        $this->persistenceCacheHandler = new CacheHandler(
69
            $this->persistenceHandlerMock,
70
            new CacheSectionHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
71
            new CacheLocationHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
72
            new CacheContentHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
73
            new CacheContentLanguageHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
74
            new CacheContentTypeHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
75
            new CacheUserHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
76
            new CacheTransactionHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
77
            new CacheTrashHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
78
            new CacheUrlAliasHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
79
            new CacheObjectStateHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
80
            new CacheUrlHandler($this->cacheMock, $this->persistenceHandlerMock, $this->loggerMock),
81
            $this->loggerMock,
82
            $this->cacheMock
83
        );
84
85
        $this->cacheItemsClosure = \Closure::bind(
86
            function ($key, $value, $isHit, $defaultLifetime = 0) {
87
                $item = new CacheItem();
88
                $item->key = $key;
0 ignored issues
show
Bug introduced by
The property key cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
89
                $item->value = $value;
0 ignored issues
show
Bug introduced by
The property value cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
90
                $item->isHit = $isHit;
0 ignored issues
show
Bug introduced by
The property isHit cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
91
                $item->defaultLifetime = $defaultLifetime;
0 ignored issues
show
Bug introduced by
The property defaultLifetime cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
92
93
                return $item;
94
            },
95
            null,
96
            CacheItem::class
97
        );
98
    }
99
100
    /**
101
     * Tear down test (properties).
102
     */
103
    final protected function tearDown()
104
    {
105
        unset($this->cacheMock);
106
        unset($this->persistenceHandlerMock);
107
        unset($this->persistenceCacheHandler);
108
        unset($this->loggerMock);
109
        unset($this->cacheItemsClosure);
110
        parent::tearDown();
111
    }
112
113
    /**
114
     * @param $key
115
     * @param null $value If null the cache item will be assumed to be a cache miss here.
116
     * @param int $defaultLifetime
117
     *
118
     * @return CacheItem
119
     */
120
    final protected function getCacheItem($key, $value = null, $defaultLifetime = 0)
121
    {
122
        $cacheItemsClosure = $this->cacheItemsClosure;
123
124
        return $cacheItemsClosure($key, $value, (bool)$value, $defaultLifetime);
125
    }
126
}
127