Completed
Push — ezp28048_trash_move_when_cant_... ( 870d3a...c9bbdc )
by
unknown
15:54
created

InstantCachePurgerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
A testPurge() 0 12 1
A testPurgeAll() 0 9 1
B testPurgeForContent() 0 36 2
B getPermissionResolverMock() 0 24 1
1
<?php
2
3
/**
4
 * File containing the InstantCachePurgerTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\Cache\Tests\Http;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\User\UserReference;
14
use eZ\Publish\Core\MVC\Symfony\Cache\Http\InstantCachePurger;
15
use eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface;
16
use eZ\Publish\Core\MVC\Symfony\Event\ContentCacheClearEvent;
17
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
18
use eZ\Publish\Core\Repository\Helper\LimitationService;
19
use eZ\Publish\Core\Repository\Helper\RoleDomainMapper;
20
use eZ\Publish\Core\Repository\Permission\PermissionResolver;
21
use eZ\Publish\Core\Repository\Repository;
22
use eZ\Publish\Core\Repository\Values\Content\Location;
23
use eZ\Publish\SPI\Persistence\User\Handler as UserHandler;
24
use PHPUnit\Framework\TestCase;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
use Symfony\Component\EventDispatcher\EventDispatcher;
27
28
class InstantCachePurgerTest extends TestCase
29
{
30
    /**
31
     * @var \eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    protected $purgeClient;
34
35
    /**
36
     * @var \eZ\Publish\API\Repository\ContentService|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    protected $contentService;
39
40
    /**
41
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    protected $eventDispatcher;
44
45
    /**
46
     * @var \eZ\Publish\Core\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    protected $repository;
49
50
    protected function setUp()
51
    {
52
        parent::setUp();
53
        $this->purgeClient = $this->getMock(PurgeClientInterface::class);
54
        $this->contentService = $this->getMock(ContentService::class);
55
        $this->eventDispatcher = $this->getMock(EventDispatcherInterface::class);
56
57
        $this->repository = $this
58
            ->getMockBuilder(Repository::class)
59
            ->disableOriginalConstructor()
60
            ->setMethods(
61
                array_diff(
62
                    get_class_methods(Repository::class),
63
                    array('sudo')
64
                )
65
            )
66
            ->getMock();
67
68
        $this->repository
69
            ->expects($this->any())
70
            ->method('getPermissionResolver')
71
            ->will($this->returnValue($this->getPermissionResolverMock()));
72
    }
73
74
    public function testPurge()
75
    {
76
        $locationIds = array(123, 456, 789);
77
        $this->purgeClient
78
            ->expects($this->once())
79
            ->method('purge')
80
            ->with($locationIds)
81
            ->will($this->returnArgument(0));
82
83
        $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $this->eventDispatcher, $this->repository);
84
        $this->assertSame($locationIds, $purger->purge($locationIds));
85
    }
86
87
    public function testPurgeAll()
88
    {
89
        $this->purgeClient
90
            ->expects($this->once())
91
            ->method('purgeAll');
92
93
        $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $this->eventDispatcher, $this->repository);
94
        $purger->purgeAll();
95
    }
96
97
    public function testPurgeForContent()
98
    {
99
        $contentId = 123;
100
        $contentInfo = new ContentInfo(['id' => $contentId, 'published' => true]);
101
        // Assume listeners have added locations.
102
        // Adding duplicates on purpose.
103
        $locationIds = [123, 456, 789, 234, 567];
104
105
        $this->contentService
106
            ->expects($this->once())
107
            ->method('loadContentInfo')
108
            ->with($contentId)
109
            ->will($this->returnValue($contentInfo));
110
111
        $eventDispatcher = new EventDispatcher();
112
        $eventDispatcher->addListener(
113
            MVCEvents::CACHE_CLEAR_CONTENT,
114
            function (ContentCacheClearEvent $event) use ($locationIds) {
115
                foreach ($locationIds as $id) {
116
                    $event->addLocationToClear(new Location(['id' => $id]));
117
                }
118
119
                // Adding a few duplicates on purpose.
120
                $event->addLocationToClear(new Location(['id' => 123]));
121
                $event->addLocationToClear(new Location(['id' => 567]));
122
            }
123
        );
124
125
        $this->purgeClient
126
            ->expects($this->once())
127
            ->method('purge')
128
            ->with($locationIds);
129
130
        $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $eventDispatcher, $this->repository);
131
        $purger->purgeForContent($contentId);
132
    }
133
134
    /**
135
     * @return \eZ\Publish\Core\Repository\Permission\PermissionResolver|\PHPUnit_Framework_MockObject_MockObject
136
     */
137
    private function getPermissionResolverMock()
138
    {
139
        return $this
140
            ->getMockBuilder(PermissionResolver::class)
141
            ->setMethods(null)
142
            ->setConstructorArgs(
143
                [
144
                    $this
145
                        ->getMockBuilder(RoleDomainMapper::class)
146
                        ->disableOriginalConstructor()
147
                        ->getMock(),
148
                    $this
149
                        ->getMockBuilder(LimitationService::class)
150
                        ->getMock(),
151
                    $this
152
                        ->getMockBuilder(UserHandler::class)
153
                        ->getMock(),
154
                    $this
155
                        ->getMockBuilder(UserReference::class)
156
                        ->getMock(),
157
                ]
158
            )
159
            ->getMock();
160
    }
161
}
162